blob: 2daf45411ec6acf28c4e2e29ef242cfd1d6f8fa6 [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 Song407659a2018-11-30 23:41:18 +00005051 Expr::EvalResult Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005052 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Fangrui Song407659a2018-11-30 23:41:18 +00005053 NestedLoopCount = Result.Val.getInt().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 Song407659a2018-11-30 23:41:18 +00005058 Expr::EvalResult EVResult;
5059 if (OrderedLoopCountExpr->EvaluateAsInt(EVResult, SemaRef.getASTContext())) {
5060 llvm::APSInt Result = EVResult.Val.getInt();
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005061 if (Result.getLimitedValue() < NestedLoopCount) {
5062 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
5063 diag::err_omp_wrong_ordered_loop_count)
5064 << OrderedLoopCountExpr->getSourceRange();
5065 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
5066 diag::note_collapse_loop_count)
5067 << CollapseLoopCountExpr->getSourceRange();
5068 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00005069 OrderedLoopCount = Result.getLimitedValue();
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005070 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005071 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005072 // This is helper routine for loop directives (e.g., 'for', 'simd',
5073 // 'for simd', etc.).
Alexey Bataeve3727102018-04-18 15:57:46 +00005074 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005075 SmallVector<LoopIterationSpace, 4> IterSpaces;
Alexey Bataevf138fda2018-08-13 19:04:24 +00005076 IterSpaces.resize(std::max(OrderedLoopCount, NestedLoopCount));
Alexander Musmana5f070a2014-10-01 06:03:56 +00005077 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005078 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataevf138fda2018-08-13 19:04:24 +00005079 if (checkOpenMPIterationSpace(
5080 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
5081 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
5082 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt],
5083 Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00005084 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005085 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005086 // OpenMP [2.8.1, simd construct, Restrictions]
5087 // All loops associated with the construct must be perfectly nested; that
5088 // is, there must be no intervening code nor any OpenMP directive between
5089 // any two loops.
5090 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005091 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00005092 for (unsigned Cnt = NestedLoopCount; Cnt < OrderedLoopCount; ++Cnt) {
5093 if (checkOpenMPIterationSpace(
5094 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
5095 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
5096 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt],
5097 Captures))
5098 return 0;
5099 if (Cnt > 0 && IterSpaces[Cnt].CounterVar) {
5100 // Handle initialization of captured loop iterator variables.
5101 auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar);
5102 if (isa<OMPCapturedExprDecl>(DRE->getDecl())) {
5103 Captures[DRE] = DRE;
5104 }
5105 }
5106 // Move on to the next nested for loop, or to the loop body.
5107 // OpenMP [2.8.1, simd construct, Restrictions]
5108 // All loops associated with the construct must be perfectly nested; that
5109 // is, there must be no intervening code nor any OpenMP directive between
5110 // any two loops.
5111 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
5112 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005113
Alexander Musmana5f070a2014-10-01 06:03:56 +00005114 Built.clear(/* size */ NestedLoopCount);
5115
5116 if (SemaRef.CurContext->isDependentContext())
5117 return NestedLoopCount;
5118
5119 // An example of what is generated for the following code:
5120 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00005121 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00005122 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00005123 // for (k = 0; k < NK; ++k)
5124 // for (j = J0; j < NJ; j+=2) {
5125 // <loop body>
5126 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005127 //
5128 // We generate the code below.
5129 // Note: the loop body may be outlined in CodeGen.
5130 // Note: some counters may be C++ classes, operator- is used to find number of
5131 // iterations and operator+= to calculate counter value.
5132 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
5133 // or i64 is currently supported).
5134 //
5135 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
5136 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
5137 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
5138 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
5139 // // similar updates for vars in clauses (e.g. 'linear')
5140 // <loop body (using local i and j)>
5141 // }
5142 // i = NI; // assign final values of counters
5143 // j = NJ;
5144 //
5145
5146 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
5147 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00005148 // Precondition tests if there is at least one iteration (all conditions are
5149 // true).
5150 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexey Bataeve3727102018-04-18 15:57:46 +00005151 Expr *N0 = IterSpaces[0].NumIterations;
5152 ExprResult LastIteration32 =
5153 widenIterationCount(/*Bits=*/32,
5154 SemaRef
5155 .PerformImplicitConversion(
5156 N0->IgnoreImpCasts(), N0->getType(),
5157 Sema::AA_Converting, /*AllowExplicit=*/true)
5158 .get(),
5159 SemaRef);
5160 ExprResult LastIteration64 = widenIterationCount(
5161 /*Bits=*/64,
5162 SemaRef
5163 .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(),
5164 Sema::AA_Converting,
5165 /*AllowExplicit=*/true)
5166 .get(),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005167 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005168
5169 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
5170 return NestedLoopCount;
5171
Alexey Bataeve3727102018-04-18 15:57:46 +00005172 ASTContext &C = SemaRef.Context;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005173 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
5174
5175 Scope *CurScope = DSA.getCurScope();
5176 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00005177 if (PreCond.isUsable()) {
Alexey Bataeva7206b92016-12-20 16:51:02 +00005178 PreCond =
5179 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd,
5180 PreCond.get(), IterSpaces[Cnt].PreCond);
Alexey Bataev62dbb972015-04-22 11:59:37 +00005181 }
Alexey Bataeve3727102018-04-18 15:57:46 +00005182 Expr *N = IterSpaces[Cnt].NumIterations;
Alexey Bataeva7206b92016-12-20 16:51:02 +00005183 SourceLocation Loc = N->getExprLoc();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005184 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
5185 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005186 LastIteration32 = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005187 CurScope, Loc, BO_Mul, LastIteration32.get(),
David Majnemer9d168222016-08-05 17:44:54 +00005188 SemaRef
5189 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5190 Sema::AA_Converting,
5191 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005192 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005193 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005194 LastIteration64 = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005195 CurScope, Loc, BO_Mul, LastIteration64.get(),
David Majnemer9d168222016-08-05 17:44:54 +00005196 SemaRef
5197 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5198 Sema::AA_Converting,
5199 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005200 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005201 }
5202
5203 // Choose either the 32-bit or 64-bit version.
5204 ExprResult LastIteration = LastIteration64;
5205 if (LastIteration32.isUsable() &&
5206 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
5207 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
Alexey Bataeve3727102018-04-18 15:57:46 +00005208 fitsInto(
5209 /*Bits=*/32,
Alexander Musmana5f070a2014-10-01 06:03:56 +00005210 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
5211 LastIteration64.get(), SemaRef)))
5212 LastIteration = LastIteration32;
Alexey Bataev7292c292016-04-25 12:22:29 +00005213 QualType VType = LastIteration.get()->getType();
5214 QualType RealVType = VType;
5215 QualType StrideVType = VType;
5216 if (isOpenMPTaskLoopDirective(DKind)) {
5217 VType =
5218 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
5219 StrideVType =
5220 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
5221 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005222
5223 if (!LastIteration.isUsable())
5224 return 0;
5225
5226 // Save the number of iterations.
5227 ExprResult NumIterations = LastIteration;
5228 {
5229 LastIteration = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005230 CurScope, LastIteration.get()->getExprLoc(), BO_Sub,
5231 LastIteration.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00005232 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5233 if (!LastIteration.isUsable())
5234 return 0;
5235 }
5236
5237 // Calculate the last iteration number beforehand instead of doing this on
5238 // each iteration. Do not do this if the number of iterations may be kfold-ed.
5239 llvm::APSInt Result;
5240 bool IsConstant =
5241 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
5242 ExprResult CalcLastIteration;
5243 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005244 ExprResult SaveRef =
5245 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005246 LastIteration = SaveRef;
5247
5248 // Prepare SaveRef + 1.
5249 NumIterations = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005250 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00005251 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5252 if (!NumIterations.isUsable())
5253 return 0;
5254 }
5255
5256 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
5257
David Majnemer9d168222016-08-05 17:44:54 +00005258 // Build variables passed into runtime, necessary for worksharing directives.
Carlo Bertolliffafe102017-04-20 00:39:39 +00005259 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005260 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5261 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005262 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005263 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
5264 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005265 SemaRef.AddInitializerToDecl(LBDecl,
5266 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5267 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005268
5269 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005270 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
5271 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005272 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
Richard Smith3beb7c62017-01-12 02:27:38 +00005273 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005274
5275 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
5276 // This will be used to implement clause 'lastprivate'.
5277 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005278 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
5279 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005280 SemaRef.AddInitializerToDecl(ILDecl,
5281 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5282 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005283
5284 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev7292c292016-04-25 12:22:29 +00005285 VarDecl *STDecl =
5286 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
5287 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005288 SemaRef.AddInitializerToDecl(STDecl,
5289 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
5290 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005291
5292 // Build expression: UB = min(UB, LastIteration)
David Majnemer9d168222016-08-05 17:44:54 +00005293 // It is necessary for CodeGen of directives with static scheduling.
Alexander Musmanc6388682014-12-15 07:07:06 +00005294 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
5295 UB.get(), LastIteration.get());
5296 ExprResult CondOp = SemaRef.ActOnConditionalOp(
Alexey Bataev86ec3fe2018-07-25 14:40:26 +00005297 LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(),
5298 LastIteration.get(), UB.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00005299 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
5300 CondOp.get());
5301 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
Carlo Bertolli9925f152016-06-27 14:55:37 +00005302
5303 // If we have a combined directive that combines 'distribute', 'for' or
5304 // 'simd' we need to be able to access the bounds of the schedule of the
5305 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
5306 // by scheduling 'distribute' have to be passed to the schedule of 'for'.
5307 if (isOpenMPLoopBoundSharingDirective(DKind)) {
Carlo Bertolliffafe102017-04-20 00:39:39 +00005308 // Lower bound variable, initialized with zero.
5309 VarDecl *CombLBDecl =
5310 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb");
5311 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc);
5312 SemaRef.AddInitializerToDecl(
5313 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5314 /*DirectInit*/ false);
5315
5316 // Upper bound variable, initialized with last iteration number.
5317 VarDecl *CombUBDecl =
5318 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub");
5319 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc);
5320 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(),
5321 /*DirectInit*/ false);
5322
5323 ExprResult CombIsUBGreater = SemaRef.BuildBinOp(
5324 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get());
5325 ExprResult CombCondOp =
5326 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(),
5327 LastIteration.get(), CombUB.get());
5328 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(),
5329 CombCondOp.get());
5330 CombEUB = SemaRef.ActOnFinishFullExpr(CombEUB.get());
5331
Alexey Bataeve3727102018-04-18 15:57:46 +00005332 const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005333 // We expect to have at least 2 more parameters than the 'parallel'
5334 // directive does - the lower and upper bounds of the previous schedule.
5335 assert(CD->getNumParams() >= 4 &&
5336 "Unexpected number of parameters in loop combined directive");
5337
5338 // Set the proper type for the bounds given what we learned from the
5339 // enclosed loops.
Alexey Bataeve3727102018-04-18 15:57:46 +00005340 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
5341 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
Carlo Bertolli9925f152016-06-27 14:55:37 +00005342
5343 // Previous lower and upper bounds are obtained from the region
5344 // parameters.
5345 PrevLB =
5346 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
5347 PrevUB =
5348 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
5349 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005350 }
5351
5352 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005353 ExprResult IV;
Carlo Bertolliffafe102017-04-20 00:39:39 +00005354 ExprResult Init, CombInit;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005355 {
Alexey Bataev7292c292016-04-25 12:22:29 +00005356 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
5357 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
David Majnemer9d168222016-08-05 17:44:54 +00005358 Expr *RHS =
5359 (isOpenMPWorksharingDirective(DKind) ||
5360 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
5361 ? LB.get()
5362 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005363 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
5364 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Carlo Bertolliffafe102017-04-20 00:39:39 +00005365
5366 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5367 Expr *CombRHS =
5368 (isOpenMPWorksharingDirective(DKind) ||
5369 isOpenMPTaskLoopDirective(DKind) ||
5370 isOpenMPDistributeDirective(DKind))
5371 ? CombLB.get()
5372 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
5373 CombInit =
5374 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS);
5375 CombInit = SemaRef.ActOnFinishFullExpr(CombInit.get());
5376 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005377 }
5378
Alexander Musmanc6388682014-12-15 07:07:06 +00005379 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005380 SourceLocation CondLoc = AStmt->getBeginLoc();
Alexander Musmanc6388682014-12-15 07:07:06 +00005381 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005382 (isOpenMPWorksharingDirective(DKind) ||
5383 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00005384 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
5385 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
5386 NumIterations.get());
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005387 ExprResult CombDistCond;
5388 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5389 CombDistCond =
Gheorghe-Teodor Bercea9d6341f2018-10-29 19:44:25 +00005390 SemaRef.BuildBinOp(
5391 CurScope, CondLoc, BO_LT, IV.get(), NumIterations.get());
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005392 }
5393
Carlo Bertolliffafe102017-04-20 00:39:39 +00005394 ExprResult CombCond;
5395 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5396 CombCond =
5397 SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), CombUB.get());
5398 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005399 // Loop increment (IV = IV + 1)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005400 SourceLocation IncLoc = AStmt->getBeginLoc();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005401 ExprResult Inc =
5402 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
5403 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
5404 if (!Inc.isUsable())
5405 return 0;
5406 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00005407 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
5408 if (!Inc.isUsable())
5409 return 0;
5410
5411 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
5412 // Used for directives with static scheduling.
Carlo Bertolliffafe102017-04-20 00:39:39 +00005413 // In combined construct, add combined version that use CombLB and CombUB
5414 // base variables for the update
5415 ExprResult NextLB, NextUB, CombNextLB, CombNextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005416 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5417 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005418 // LB + ST
5419 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
5420 if (!NextLB.isUsable())
5421 return 0;
5422 // LB = LB + ST
5423 NextLB =
5424 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
5425 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
5426 if (!NextLB.isUsable())
5427 return 0;
5428 // UB + ST
5429 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
5430 if (!NextUB.isUsable())
5431 return 0;
5432 // UB = UB + ST
5433 NextUB =
5434 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
5435 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
5436 if (!NextUB.isUsable())
5437 return 0;
Carlo Bertolliffafe102017-04-20 00:39:39 +00005438 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5439 CombNextLB =
5440 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get());
5441 if (!NextLB.isUsable())
5442 return 0;
5443 // LB = LB + ST
5444 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(),
5445 CombNextLB.get());
5446 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get());
5447 if (!CombNextLB.isUsable())
5448 return 0;
5449 // UB + ST
5450 CombNextUB =
5451 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get());
5452 if (!CombNextUB.isUsable())
5453 return 0;
5454 // UB = UB + ST
5455 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(),
5456 CombNextUB.get());
5457 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get());
5458 if (!CombNextUB.isUsable())
5459 return 0;
5460 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005461 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005462
Carlo Bertolliffafe102017-04-20 00:39:39 +00005463 // Create increment expression for distribute loop when combined in a same
Carlo Bertolli8429d812017-02-17 21:29:13 +00005464 // directive with for as IV = IV + ST; ensure upper bound expression based
5465 // on PrevUB instead of NumIterations - used to implement 'for' when found
5466 // in combination with 'distribute', like in 'distribute parallel for'
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005467 SourceLocation DistIncLoc = AStmt->getBeginLoc();
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005468 ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond;
Carlo Bertolli8429d812017-02-17 21:29:13 +00005469 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5470 DistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get());
5471 assert(DistCond.isUsable() && "distribute cond expr was not built");
5472
5473 DistInc =
5474 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get());
5475 assert(DistInc.isUsable() && "distribute inc expr was not built");
5476 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(),
5477 DistInc.get());
5478 DistInc = SemaRef.ActOnFinishFullExpr(DistInc.get());
5479 assert(DistInc.isUsable() && "distribute inc expr was not built");
5480
5481 // Build expression: UB = min(UB, prevUB) for #for in composite or combined
5482 // construct
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005483 SourceLocation DistEUBLoc = AStmt->getBeginLoc();
Carlo Bertolli8429d812017-02-17 21:29:13 +00005484 ExprResult IsUBGreater =
5485 SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get());
5486 ExprResult CondOp = SemaRef.ActOnConditionalOp(
5487 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get());
5488 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(),
5489 CondOp.get());
5490 PrevEUB = SemaRef.ActOnFinishFullExpr(PrevEUB.get());
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005491
5492 // Build IV <= PrevUB to be used in parallel for is in combination with
5493 // a distribute directive with schedule(static, 1)
5494 ParForInDistCond =
5495 SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), PrevUB.get());
Carlo Bertolli8429d812017-02-17 21:29:13 +00005496 }
5497
Alexander Musmana5f070a2014-10-01 06:03:56 +00005498 // Build updates and final values of the loop counters.
5499 bool HasErrors = false;
5500 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005501 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005502 Built.Updates.resize(NestedLoopCount);
5503 Built.Finals.resize(NestedLoopCount);
5504 {
5505 ExprResult Div;
5506 // Go from inner nested loop to outer.
5507 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5508 LoopIterationSpace &IS = IterSpaces[Cnt];
5509 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
5510 // Build: Iter = (IV / Div) % IS.NumIters
5511 // where Div is product of previous iterations' IS.NumIters.
5512 ExprResult Iter;
5513 if (Div.isUsable()) {
5514 Iter =
5515 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
5516 } else {
5517 Iter = IV;
5518 assert((Cnt == (int)NestedLoopCount - 1) &&
5519 "unusable div expected on first iteration only");
5520 }
5521
5522 if (Cnt != 0 && Iter.isUsable())
5523 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
5524 IS.NumIterations);
5525 if (!Iter.isUsable()) {
5526 HasErrors = true;
5527 break;
5528 }
5529
Alexey Bataev39f915b82015-05-08 10:41:21 +00005530 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005531 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00005532 DeclRefExpr *CounterVar = buildDeclRefExpr(
5533 SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
5534 /*RefersToCapture=*/true);
5535 ExprResult Init = buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005536 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005537 if (!Init.isUsable()) {
5538 HasErrors = true;
5539 break;
5540 }
Alexey Bataeve3727102018-04-18 15:57:46 +00005541 ExprResult Update = buildCounterUpdate(
Alexey Bataev5a3af132016-03-29 08:58:54 +00005542 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
5543 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005544 if (!Update.isUsable()) {
5545 HasErrors = true;
5546 break;
5547 }
5548
5549 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
Alexey Bataeve3727102018-04-18 15:57:46 +00005550 ExprResult Final = buildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00005551 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005552 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005553 if (!Final.isUsable()) {
5554 HasErrors = true;
5555 break;
5556 }
5557
5558 // Build Div for the next iteration: Div <- Div * IS.NumIters
5559 if (Cnt != 0) {
5560 if (Div.isUnset())
5561 Div = IS.NumIterations;
5562 else
5563 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
5564 IS.NumIterations);
5565
5566 // Add parentheses (for debugging purposes only).
5567 if (Div.isUsable())
Alexey Bataev8b427062016-05-25 12:36:08 +00005568 Div = tryBuildCapture(SemaRef, Div.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005569 if (!Div.isUsable()) {
5570 HasErrors = true;
5571 break;
5572 }
5573 }
5574 if (!Update.isUsable() || !Final.isUsable()) {
5575 HasErrors = true;
5576 break;
5577 }
5578 // Save results
5579 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00005580 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005581 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005582 Built.Updates[Cnt] = Update.get();
5583 Built.Finals[Cnt] = Final.get();
5584 }
5585 }
5586
5587 if (HasErrors)
5588 return 0;
5589
5590 // Save results
5591 Built.IterationVarRef = IV.get();
5592 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00005593 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00005594 Built.CalcLastIteration =
5595 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005596 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00005597 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005598 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005599 Built.Init = Init.get();
5600 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005601 Built.LB = LB.get();
5602 Built.UB = UB.get();
5603 Built.IL = IL.get();
5604 Built.ST = ST.get();
5605 Built.EUB = EUB.get();
5606 Built.NLB = NextLB.get();
5607 Built.NUB = NextUB.get();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005608 Built.PrevLB = PrevLB.get();
5609 Built.PrevUB = PrevUB.get();
Carlo Bertolli8429d812017-02-17 21:29:13 +00005610 Built.DistInc = DistInc.get();
5611 Built.PrevEUB = PrevEUB.get();
Carlo Bertolliffafe102017-04-20 00:39:39 +00005612 Built.DistCombinedFields.LB = CombLB.get();
5613 Built.DistCombinedFields.UB = CombUB.get();
5614 Built.DistCombinedFields.EUB = CombEUB.get();
5615 Built.DistCombinedFields.Init = CombInit.get();
5616 Built.DistCombinedFields.Cond = CombCond.get();
5617 Built.DistCombinedFields.NLB = CombNextLB.get();
5618 Built.DistCombinedFields.NUB = CombNextUB.get();
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005619 Built.DistCombinedFields.DistCond = CombDistCond.get();
5620 Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005621
Alexey Bataevabfc0692014-06-25 06:52:00 +00005622 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005623}
5624
Alexey Bataev10e775f2015-07-30 11:36:16 +00005625static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005626 auto CollapseClauses =
5627 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
5628 if (CollapseClauses.begin() != CollapseClauses.end())
5629 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005630 return nullptr;
5631}
5632
Alexey Bataev10e775f2015-07-30 11:36:16 +00005633static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005634 auto OrderedClauses =
5635 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
5636 if (OrderedClauses.begin() != OrderedClauses.end())
5637 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005638 return nullptr;
5639}
5640
Kelvin Lic5609492016-07-15 04:39:07 +00005641static bool checkSimdlenSafelenSpecified(Sema &S,
5642 const ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00005643 const OMPSafelenClause *Safelen = nullptr;
5644 const OMPSimdlenClause *Simdlen = nullptr;
Kelvin Lic5609492016-07-15 04:39:07 +00005645
Alexey Bataeve3727102018-04-18 15:57:46 +00005646 for (const OMPClause *Clause : Clauses) {
Kelvin Lic5609492016-07-15 04:39:07 +00005647 if (Clause->getClauseKind() == OMPC_safelen)
5648 Safelen = cast<OMPSafelenClause>(Clause);
5649 else if (Clause->getClauseKind() == OMPC_simdlen)
5650 Simdlen = cast<OMPSimdlenClause>(Clause);
5651 if (Safelen && Simdlen)
5652 break;
5653 }
5654
5655 if (Simdlen && Safelen) {
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 Song407659a2018-11-30 23:41:18 +00005666 Expr::EvalResult SimdlenResult, SafelenResult;
5667 SimdlenLength->EvaluateAsInt(SimdlenResult, S.Context);
5668 SafelenLength->EvaluateAsInt(SafelenResult, S.Context);
5669 llvm::APSInt SimdlenRes = SimdlenResult.Val.getInt();
5670 llvm::APSInt SafelenRes = SafelenResult.Val.getInt();
Kelvin Lic5609492016-07-15 04:39:07 +00005671 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
5672 // If both simdlen and safelen clauses are specified, the value of the
5673 // simdlen parameter must be less than or equal to the value of the safelen
5674 // parameter.
5675 if (SimdlenRes > SafelenRes) {
5676 S.Diag(SimdlenLength->getExprLoc(),
5677 diag::err_omp_wrong_simdlen_safelen_values)
5678 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
5679 return true;
5680 }
Alexey Bataev66b15b52015-08-21 11:14:16 +00005681 }
5682 return false;
5683}
5684
Alexey Bataeve3727102018-04-18 15:57:46 +00005685StmtResult
5686Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
5687 SourceLocation StartLoc, SourceLocation EndLoc,
5688 VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005689 if (!AStmt)
5690 return StmtError();
5691
5692 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005693 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005694 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5695 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00005696 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataev10e775f2015-07-30 11:36:16 +00005697 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5698 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005699 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005700 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005701
Alexander Musmana5f070a2014-10-01 06:03:56 +00005702 assert((CurContext->isDependentContext() || B.builtAll()) &&
5703 "omp simd loop exprs were not built");
5704
Alexander Musman3276a272015-03-21 10:12:56 +00005705 if (!CurContext->isDependentContext()) {
5706 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005707 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005708 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexander Musman3276a272015-03-21 10:12:56 +00005709 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005710 B.NumIterations, *this, CurScope,
5711 DSAStack))
Alexander Musman3276a272015-03-21 10:12:56 +00005712 return StmtError();
5713 }
5714 }
5715
Kelvin Lic5609492016-07-15 04:39:07 +00005716 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005717 return StmtError();
5718
Reid Kleckner87a31802018-03-12 21:43:02 +00005719 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005720 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5721 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005722}
5723
Alexey Bataeve3727102018-04-18 15:57:46 +00005724StmtResult
5725Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
5726 SourceLocation StartLoc, SourceLocation EndLoc,
5727 VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005728 if (!AStmt)
5729 return StmtError();
5730
5731 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005732 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005733 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5734 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00005735 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataev10e775f2015-07-30 11:36:16 +00005736 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5737 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005738 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005739 return StmtError();
5740
Alexander Musmana5f070a2014-10-01 06:03:56 +00005741 assert((CurContext->isDependentContext() || B.builtAll()) &&
5742 "omp for loop exprs were not built");
5743
Alexey Bataev54acd402015-08-04 11:18:19 +00005744 if (!CurContext->isDependentContext()) {
5745 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005746 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005747 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00005748 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005749 B.NumIterations, *this, CurScope,
5750 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005751 return StmtError();
5752 }
5753 }
5754
Reid Kleckner87a31802018-03-12 21:43:02 +00005755 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005756 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005757 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005758}
5759
Alexander Musmanf82886e2014-09-18 05:12:34 +00005760StmtResult Sema::ActOnOpenMPForSimdDirective(
5761 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00005762 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005763 if (!AStmt)
5764 return StmtError();
5765
5766 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005767 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005768 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5769 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00005770 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00005771 checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00005772 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5773 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005774 if (NestedLoopCount == 0)
5775 return StmtError();
5776
Alexander Musmanc6388682014-12-15 07:07:06 +00005777 assert((CurContext->isDependentContext() || B.builtAll()) &&
5778 "omp for simd loop exprs were not built");
5779
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005780 if (!CurContext->isDependentContext()) {
5781 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005782 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005783 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005784 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005785 B.NumIterations, *this, CurScope,
5786 DSAStack))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005787 return StmtError();
5788 }
5789 }
5790
Kelvin Lic5609492016-07-15 04:39:07 +00005791 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005792 return StmtError();
5793
Reid Kleckner87a31802018-03-12 21:43:02 +00005794 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005795 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5796 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005797}
5798
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005799StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5800 Stmt *AStmt,
5801 SourceLocation StartLoc,
5802 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005803 if (!AStmt)
5804 return StmtError();
5805
5806 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005807 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00005808 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005809 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00005810 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005811 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005812 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005813 return StmtError();
5814 // All associated statements must be '#pragma omp section' except for
5815 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005816 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005817 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5818 if (SectionStmt)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005819 Diag(SectionStmt->getBeginLoc(),
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005820 diag::err_omp_sections_substmt_not_section);
5821 return StmtError();
5822 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005823 cast<OMPSectionDirective>(SectionStmt)
5824 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005825 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005826 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005827 Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005828 return StmtError();
5829 }
5830
Reid Kleckner87a31802018-03-12 21:43:02 +00005831 setFunctionHasBranchProtectedScope();
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005832
Alexey Bataev25e5b442015-09-15 12:52:43 +00005833 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5834 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005835}
5836
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005837StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5838 SourceLocation StartLoc,
5839 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005840 if (!AStmt)
5841 return StmtError();
5842
5843 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005844
Reid Kleckner87a31802018-03-12 21:43:02 +00005845 setFunctionHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00005846 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005847
Alexey Bataev25e5b442015-09-15 12:52:43 +00005848 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5849 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005850}
5851
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005852StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5853 Stmt *AStmt,
5854 SourceLocation StartLoc,
5855 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005856 if (!AStmt)
5857 return StmtError();
5858
5859 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00005860
Reid Kleckner87a31802018-03-12 21:43:02 +00005861 setFunctionHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00005862
Alexey Bataev3255bf32015-01-19 05:20:46 +00005863 // OpenMP [2.7.3, single Construct, Restrictions]
5864 // The copyprivate clause must not be used with the nowait clause.
Alexey Bataeve3727102018-04-18 15:57:46 +00005865 const OMPClause *Nowait = nullptr;
5866 const OMPClause *Copyprivate = nullptr;
5867 for (const OMPClause *Clause : Clauses) {
Alexey Bataev3255bf32015-01-19 05:20:46 +00005868 if (Clause->getClauseKind() == OMPC_nowait)
5869 Nowait = Clause;
5870 else if (Clause->getClauseKind() == OMPC_copyprivate)
5871 Copyprivate = Clause;
5872 if (Copyprivate && Nowait) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005873 Diag(Copyprivate->getBeginLoc(),
Alexey Bataev3255bf32015-01-19 05:20:46 +00005874 diag::err_omp_single_copyprivate_with_nowait);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005875 Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here);
Alexey Bataev3255bf32015-01-19 05:20:46 +00005876 return StmtError();
5877 }
5878 }
5879
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005880 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5881}
5882
Alexander Musman80c22892014-07-17 08:54:58 +00005883StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5884 SourceLocation StartLoc,
5885 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005886 if (!AStmt)
5887 return StmtError();
5888
5889 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00005890
Reid Kleckner87a31802018-03-12 21:43:02 +00005891 setFunctionHasBranchProtectedScope();
Alexander Musman80c22892014-07-17 08:54:58 +00005892
5893 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5894}
5895
Alexey Bataev28c75412015-12-15 08:19:24 +00005896StmtResult Sema::ActOnOpenMPCriticalDirective(
5897 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5898 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005899 if (!AStmt)
5900 return StmtError();
5901
5902 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005903
Alexey Bataev28c75412015-12-15 08:19:24 +00005904 bool ErrorFound = false;
5905 llvm::APSInt Hint;
5906 SourceLocation HintLoc;
5907 bool DependentHint = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00005908 for (const OMPClause *C : Clauses) {
Alexey Bataev28c75412015-12-15 08:19:24 +00005909 if (C->getClauseKind() == OMPC_hint) {
5910 if (!DirName.getName()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005911 Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name);
Alexey Bataev28c75412015-12-15 08:19:24 +00005912 ErrorFound = true;
5913 }
5914 Expr *E = cast<OMPHintClause>(C)->getHint();
5915 if (E->isTypeDependent() || E->isValueDependent() ||
Alexey Bataeve3727102018-04-18 15:57:46 +00005916 E->isInstantiationDependent()) {
Alexey Bataev28c75412015-12-15 08:19:24 +00005917 DependentHint = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00005918 } else {
Alexey Bataev28c75412015-12-15 08:19:24 +00005919 Hint = E->EvaluateKnownConstInt(Context);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005920 HintLoc = C->getBeginLoc();
Alexey Bataev28c75412015-12-15 08:19:24 +00005921 }
5922 }
5923 }
5924 if (ErrorFound)
5925 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00005926 const auto Pair = DSAStack->getCriticalWithHint(DirName);
Alexey Bataev28c75412015-12-15 08:19:24 +00005927 if (Pair.first && DirName.getName() && !DependentHint) {
5928 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5929 Diag(StartLoc, diag::err_omp_critical_with_hint);
Alexey Bataeve3727102018-04-18 15:57:46 +00005930 if (HintLoc.isValid())
Alexey Bataev28c75412015-12-15 08:19:24 +00005931 Diag(HintLoc, diag::note_omp_critical_hint_here)
5932 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +00005933 else
Alexey Bataev28c75412015-12-15 08:19:24 +00005934 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
Alexey Bataeve3727102018-04-18 15:57:46 +00005935 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005936 Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here)
Alexey Bataev28c75412015-12-15 08:19:24 +00005937 << 1
5938 << C->getHint()->EvaluateKnownConstInt(Context).toString(
5939 /*Radix=*/10, /*Signed=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +00005940 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005941 Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1;
Alexey Bataeve3727102018-04-18 15:57:46 +00005942 }
Alexey Bataev28c75412015-12-15 08:19:24 +00005943 }
5944 }
5945
Reid Kleckner87a31802018-03-12 21:43:02 +00005946 setFunctionHasBranchProtectedScope();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005947
Alexey Bataev28c75412015-12-15 08:19:24 +00005948 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5949 Clauses, AStmt);
5950 if (!Pair.first && DirName.getName() && !DependentHint)
5951 DSAStack->addCriticalWithHint(Dir, Hint);
5952 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005953}
5954
Alexey Bataev4acb8592014-07-07 13:01:15 +00005955StmtResult Sema::ActOnOpenMPParallelForDirective(
5956 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00005957 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005958 if (!AStmt)
5959 return StmtError();
5960
Alexey Bataeve3727102018-04-18 15:57:46 +00005961 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005962 // 1.2.2 OpenMP Language Terminology
5963 // Structured block - An executable statement with a single entry at the
5964 // top and a single exit at the bottom.
5965 // The point of exit cannot be a branch out of the structured block.
5966 // longjmp() and throw() must not violate the entry/exit criteria.
5967 CS->getCapturedDecl()->setNothrow();
5968
Alexander Musmanc6388682014-12-15 07:07:06 +00005969 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005970 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5971 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005972 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00005973 checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00005974 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5975 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005976 if (NestedLoopCount == 0)
5977 return StmtError();
5978
Alexander Musmana5f070a2014-10-01 06:03:56 +00005979 assert((CurContext->isDependentContext() || B.builtAll()) &&
5980 "omp parallel for loop exprs were not built");
5981
Alexey Bataev54acd402015-08-04 11:18:19 +00005982 if (!CurContext->isDependentContext()) {
5983 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005984 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005985 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00005986 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005987 B.NumIterations, *this, CurScope,
5988 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005989 return StmtError();
5990 }
5991 }
5992
Reid Kleckner87a31802018-03-12 21:43:02 +00005993 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005994 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005995 NestedLoopCount, Clauses, AStmt, B,
5996 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00005997}
5998
Alexander Musmane4e893b2014-09-23 09:33:00 +00005999StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
6000 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00006001 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006002 if (!AStmt)
6003 return StmtError();
6004
Alexey Bataeve3727102018-04-18 15:57:46 +00006005 auto *CS = cast<CapturedStmt>(AStmt);
Alexander Musmane4e893b2014-09-23 09:33:00 +00006006 // 1.2.2 OpenMP Language Terminology
6007 // Structured block - An executable statement with a single entry at the
6008 // top and a single exit at the bottom.
6009 // The point of exit cannot be a branch out of the structured block.
6010 // longjmp() and throw() must not violate the entry/exit criteria.
6011 CS->getCapturedDecl()->setNothrow();
6012
Alexander Musmanc6388682014-12-15 07:07:06 +00006013 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006014 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6015 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00006016 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00006017 checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00006018 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6019 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00006020 if (NestedLoopCount == 0)
6021 return StmtError();
6022
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00006023 if (!CurContext->isDependentContext()) {
6024 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00006025 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00006026 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00006027 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006028 B.NumIterations, *this, CurScope,
6029 DSAStack))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00006030 return StmtError();
6031 }
6032 }
6033
Kelvin Lic5609492016-07-15 04:39:07 +00006034 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00006035 return StmtError();
6036
Reid Kleckner87a31802018-03-12 21:43:02 +00006037 setFunctionHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00006038 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00006039 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00006040}
6041
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006042StmtResult
6043Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
6044 Stmt *AStmt, SourceLocation StartLoc,
6045 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006046 if (!AStmt)
6047 return StmtError();
6048
6049 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006050 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00006051 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006052 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00006053 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006054 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00006055 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006056 return StmtError();
6057 // All associated statements must be '#pragma omp section' except for
6058 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00006059 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006060 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
6061 if (SectionStmt)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006062 Diag(SectionStmt->getBeginLoc(),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006063 diag::err_omp_parallel_sections_substmt_not_section);
6064 return StmtError();
6065 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006066 cast<OMPSectionDirective>(SectionStmt)
6067 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006068 }
6069 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006070 Diag(AStmt->getBeginLoc(),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006071 diag::err_omp_parallel_sections_not_compound_stmt);
6072 return StmtError();
6073 }
6074
Reid Kleckner87a31802018-03-12 21:43:02 +00006075 setFunctionHasBranchProtectedScope();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006076
Alexey Bataev25e5b442015-09-15 12:52:43 +00006077 return OMPParallelSectionsDirective::Create(
6078 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006079}
6080
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006081StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
6082 Stmt *AStmt, SourceLocation StartLoc,
6083 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006084 if (!AStmt)
6085 return StmtError();
6086
David Majnemer9d168222016-08-05 17:44:54 +00006087 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006088 // 1.2.2 OpenMP Language Terminology
6089 // Structured block - An executable statement with a single entry at the
6090 // top and a single exit at the bottom.
6091 // The point of exit cannot be a branch out of the structured block.
6092 // longjmp() and throw() must not violate the entry/exit criteria.
6093 CS->getCapturedDecl()->setNothrow();
6094
Reid Kleckner87a31802018-03-12 21:43:02 +00006095 setFunctionHasBranchProtectedScope();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006096
Alexey Bataev25e5b442015-09-15 12:52:43 +00006097 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
6098 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006099}
6100
Alexey Bataev68446b72014-07-18 07:47:19 +00006101StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
6102 SourceLocation EndLoc) {
6103 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
6104}
6105
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00006106StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
6107 SourceLocation EndLoc) {
6108 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
6109}
6110
Alexey Bataev2df347a2014-07-18 10:17:07 +00006111StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
6112 SourceLocation EndLoc) {
6113 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
6114}
6115
Alexey Bataev169d96a2017-07-18 20:17:46 +00006116StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses,
6117 Stmt *AStmt,
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006118 SourceLocation StartLoc,
6119 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006120 if (!AStmt)
6121 return StmtError();
6122
6123 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006124
Reid Kleckner87a31802018-03-12 21:43:02 +00006125 setFunctionHasBranchProtectedScope();
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006126
Alexey Bataev169d96a2017-07-18 20:17:46 +00006127 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses,
Alexey Bataev3b1b8952017-07-25 15:53:26 +00006128 AStmt,
6129 DSAStack->getTaskgroupReductionRef());
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006130}
6131
Alexey Bataev6125da92014-07-21 11:26:11 +00006132StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
6133 SourceLocation StartLoc,
6134 SourceLocation EndLoc) {
6135 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
6136 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
6137}
6138
Alexey Bataev346265e2015-09-25 10:37:12 +00006139StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
6140 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00006141 SourceLocation StartLoc,
6142 SourceLocation EndLoc) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006143 const OMPClause *DependFound = nullptr;
6144 const OMPClause *DependSourceClause = nullptr;
6145 const OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00006146 bool ErrorFound = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00006147 const OMPThreadsClause *TC = nullptr;
6148 const OMPSIMDClause *SC = nullptr;
6149 for (const OMPClause *C : Clauses) {
Alexey Bataeveb482352015-12-18 05:05:56 +00006150 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
6151 DependFound = C;
6152 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
6153 if (DependSourceClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006154 Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
Alexey Bataeveb482352015-12-18 05:05:56 +00006155 << getOpenMPDirectiveName(OMPD_ordered)
6156 << getOpenMPClauseName(OMPC_depend) << 2;
6157 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006158 } else {
Alexey Bataeveb482352015-12-18 05:05:56 +00006159 DependSourceClause = C;
Alexey Bataeve3727102018-04-18 15:57:46 +00006160 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006161 if (DependSinkClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006162 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006163 << 0;
6164 ErrorFound = true;
6165 }
6166 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
6167 if (DependSourceClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006168 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006169 << 1;
6170 ErrorFound = true;
6171 }
6172 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00006173 }
Alexey Bataeve3727102018-04-18 15:57:46 +00006174 } else if (C->getClauseKind() == OMPC_threads) {
Alexey Bataev346265e2015-09-25 10:37:12 +00006175 TC = cast<OMPThreadsClause>(C);
Alexey Bataeve3727102018-04-18 15:57:46 +00006176 } else if (C->getClauseKind() == OMPC_simd) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006177 SC = cast<OMPSIMDClause>(C);
Alexey Bataeve3727102018-04-18 15:57:46 +00006178 }
Alexey Bataev346265e2015-09-25 10:37:12 +00006179 }
Alexey Bataeveb482352015-12-18 05:05:56 +00006180 if (!ErrorFound && !SC &&
6181 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006182 // OpenMP [2.8.1,simd Construct, Restrictions]
6183 // An ordered construct with the simd clause is the only OpenMP construct
6184 // that can appear in the simd region.
6185 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00006186 ErrorFound = true;
6187 } else if (DependFound && (TC || SC)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006188 Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd)
Alexey Bataeveb482352015-12-18 05:05:56 +00006189 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
6190 ErrorFound = true;
Alexey Bataevf138fda2018-08-13 19:04:24 +00006191 } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006192 Diag(DependFound->getBeginLoc(),
Alexey Bataeveb482352015-12-18 05:05:56 +00006193 diag::err_omp_ordered_directive_without_param);
6194 ErrorFound = true;
6195 } else if (TC || Clauses.empty()) {
Alexey Bataevf138fda2018-08-13 19:04:24 +00006196 if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006197 SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc;
Alexey Bataeveb482352015-12-18 05:05:56 +00006198 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
6199 << (TC != nullptr);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006200 Diag(Param->getBeginLoc(), diag::note_omp_ordered_param);
Alexey Bataeveb482352015-12-18 05:05:56 +00006201 ErrorFound = true;
6202 }
6203 }
6204 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006205 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00006206
6207 if (AStmt) {
6208 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6209
Reid Kleckner87a31802018-03-12 21:43:02 +00006210 setFunctionHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006211 }
Alexey Bataev346265e2015-09-25 10:37:12 +00006212
6213 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00006214}
6215
Alexey Bataev1d160b12015-03-13 12:27:31 +00006216namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006217/// Helper class for checking expression in 'omp atomic [update]'
Alexey Bataev1d160b12015-03-13 12:27:31 +00006218/// construct.
6219class OpenMPAtomicUpdateChecker {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006220 /// Error results for atomic update expressions.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006221 enum ExprAnalysisErrorCode {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006222 /// A statement is not an expression statement.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006223 NotAnExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006224 /// Expression is not builtin binary or unary operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006225 NotABinaryOrUnaryExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006226 /// Unary operation is not post-/pre- increment/decrement operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006227 NotAnUnaryIncDecExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006228 /// An expression is not of scalar type.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006229 NotAScalarType,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006230 /// A binary operation is not an assignment operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006231 NotAnAssignmentOp,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006232 /// RHS part of the binary operation is not a binary expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006233 NotABinaryExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006234 /// RHS part is not additive/multiplicative/shift/biwise binary
Alexey Bataev1d160b12015-03-13 12:27:31 +00006235 /// expression.
6236 NotABinaryOperator,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006237 /// RHS binary operation does not have reference to the updated LHS
Alexey Bataev1d160b12015-03-13 12:27:31 +00006238 /// part.
6239 NotAnUpdateExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006240 /// No errors is found.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006241 NoError
6242 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006243 /// Reference to Sema.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006244 Sema &SemaRef;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006245 /// A location for note diagnostics (when error is found).
Alexey Bataev1d160b12015-03-13 12:27:31 +00006246 SourceLocation NoteLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006247 /// 'x' lvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006248 Expr *X;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006249 /// 'expr' rvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006250 Expr *E;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006251 /// Helper expression of the form
Alexey Bataevb4505a72015-03-30 05:20:59 +00006252 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6253 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6254 Expr *UpdateExpr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006255 /// Is 'x' a LHS in a RHS part of full update expression. It is
Alexey Bataevb4505a72015-03-30 05:20:59 +00006256 /// important for non-associative operations.
6257 bool IsXLHSInRHSPart;
6258 BinaryOperatorKind Op;
6259 SourceLocation OpLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006260 /// true if the source expression is a postfix unary operation, false
Alexey Bataevb78ca832015-04-01 03:33:17 +00006261 /// if it is a prefix unary operation.
6262 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006263
6264public:
6265 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00006266 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00006267 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006268 /// Check specified statement that it is suitable for 'atomic update'
Alexey Bataev1d160b12015-03-13 12:27:31 +00006269 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00006270 /// expression. If DiagId and NoteId == 0, then only check is performed
6271 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006272 /// \param DiagId Diagnostic which should be emitted if error is found.
6273 /// \param NoteId Diagnostic note for the main error message.
6274 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00006275 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006276 /// Return the 'x' lvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006277 Expr *getX() const { return X; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006278 /// Return the 'expr' rvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006279 Expr *getExpr() const { return E; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006280 /// Return the update expression used in calculation of the updated
Alexey Bataevb4505a72015-03-30 05:20:59 +00006281 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6282 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6283 Expr *getUpdateExpr() const { return UpdateExpr; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006284 /// Return true if 'x' is LHS in RHS part of full update expression,
Alexey Bataevb4505a72015-03-30 05:20:59 +00006285 /// false otherwise.
6286 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
6287
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006288 /// true if the source expression is a postfix unary operation, false
Alexey Bataevb78ca832015-04-01 03:33:17 +00006289 /// if it is a prefix unary operation.
6290 bool isPostfixUpdate() const { return IsPostfixUpdate; }
6291
Alexey Bataev1d160b12015-03-13 12:27:31 +00006292private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00006293 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
6294 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00006295};
6296} // namespace
6297
6298bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
6299 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
6300 ExprAnalysisErrorCode ErrorFound = NoError;
6301 SourceLocation ErrorLoc, NoteLoc;
6302 SourceRange ErrorRange, NoteRange;
6303 // Allowed constructs are:
6304 // x = x binop expr;
6305 // x = expr binop x;
6306 if (AtomicBinOp->getOpcode() == BO_Assign) {
6307 X = AtomicBinOp->getLHS();
Alexey Bataeve3727102018-04-18 15:57:46 +00006308 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006309 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
6310 if (AtomicInnerBinOp->isMultiplicativeOp() ||
6311 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
6312 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006313 Op = AtomicInnerBinOp->getOpcode();
6314 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataeve3727102018-04-18 15:57:46 +00006315 Expr *LHS = AtomicInnerBinOp->getLHS();
6316 Expr *RHS = AtomicInnerBinOp->getRHS();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006317 llvm::FoldingSetNodeID XId, LHSId, RHSId;
6318 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
6319 /*Canonical=*/true);
6320 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
6321 /*Canonical=*/true);
6322 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
6323 /*Canonical=*/true);
6324 if (XId == LHSId) {
6325 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006326 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006327 } else if (XId == RHSId) {
6328 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006329 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006330 } else {
6331 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6332 ErrorRange = AtomicInnerBinOp->getSourceRange();
6333 NoteLoc = X->getExprLoc();
6334 NoteRange = X->getSourceRange();
6335 ErrorFound = NotAnUpdateExpression;
6336 }
6337 } else {
6338 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6339 ErrorRange = AtomicInnerBinOp->getSourceRange();
6340 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
6341 NoteRange = SourceRange(NoteLoc, NoteLoc);
6342 ErrorFound = NotABinaryOperator;
6343 }
6344 } else {
6345 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
6346 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
6347 ErrorFound = NotABinaryExpression;
6348 }
6349 } else {
6350 ErrorLoc = AtomicBinOp->getExprLoc();
6351 ErrorRange = AtomicBinOp->getSourceRange();
6352 NoteLoc = AtomicBinOp->getOperatorLoc();
6353 NoteRange = SourceRange(NoteLoc, NoteLoc);
6354 ErrorFound = NotAnAssignmentOp;
6355 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006356 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006357 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6358 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6359 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006360 }
6361 if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006362 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006363 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006364}
6365
6366bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
6367 unsigned NoteId) {
6368 ExprAnalysisErrorCode ErrorFound = NoError;
6369 SourceLocation ErrorLoc, NoteLoc;
6370 SourceRange ErrorRange, NoteRange;
6371 // Allowed constructs are:
6372 // x++;
6373 // x--;
6374 // ++x;
6375 // --x;
6376 // x binop= expr;
6377 // x = x binop expr;
6378 // x = expr binop x;
6379 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
6380 AtomicBody = AtomicBody->IgnoreParenImpCasts();
6381 if (AtomicBody->getType()->isScalarType() ||
6382 AtomicBody->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006383 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006384 AtomicBody->IgnoreParenImpCasts())) {
6385 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00006386 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006387 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00006388 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006389 E = AtomicCompAssignOp->getRHS();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006390 X = AtomicCompAssignOp->getLHS()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006391 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006392 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
6393 AtomicBody->IgnoreParenImpCasts())) {
6394 // Check for Binary Operation
David Majnemer9d168222016-08-05 17:44:54 +00006395 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
Alexey Bataevb4505a72015-03-30 05:20:59 +00006396 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006397 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
David Majnemer9d168222016-08-05 17:44:54 +00006398 AtomicBody->IgnoreParenImpCasts())) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006399 // Check for Unary Operation
6400 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006401 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006402 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
6403 OpLoc = AtomicUnaryOp->getOperatorLoc();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006404 X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006405 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
6406 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006407 } else {
6408 ErrorFound = NotAnUnaryIncDecExpression;
6409 ErrorLoc = AtomicUnaryOp->getExprLoc();
6410 ErrorRange = AtomicUnaryOp->getSourceRange();
6411 NoteLoc = AtomicUnaryOp->getOperatorLoc();
6412 NoteRange = SourceRange(NoteLoc, NoteLoc);
6413 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006414 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006415 ErrorFound = NotABinaryOrUnaryExpression;
6416 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
6417 NoteRange = ErrorRange = AtomicBody->getSourceRange();
6418 }
6419 } else {
6420 ErrorFound = NotAScalarType;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006421 NoteLoc = ErrorLoc = AtomicBody->getBeginLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006422 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6423 }
6424 } else {
6425 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006426 NoteLoc = ErrorLoc = S->getBeginLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006427 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6428 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006429 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006430 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6431 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6432 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006433 }
6434 if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006435 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006436 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006437 // Build an update expression of form 'OpaqueValueExpr(x) binop
6438 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
6439 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
6440 auto *OVEX = new (SemaRef.getASTContext())
6441 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
6442 auto *OVEExpr = new (SemaRef.getASTContext())
6443 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
Alexey Bataeve3727102018-04-18 15:57:46 +00006444 ExprResult Update =
Alexey Bataevb4505a72015-03-30 05:20:59 +00006445 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
6446 IsXLHSInRHSPart ? OVEExpr : OVEX);
6447 if (Update.isInvalid())
6448 return true;
6449 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
6450 Sema::AA_Casting);
6451 if (Update.isInvalid())
6452 return true;
6453 UpdateExpr = Update.get();
6454 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00006455 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006456}
6457
Alexey Bataev0162e452014-07-22 10:10:35 +00006458StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
6459 Stmt *AStmt,
6460 SourceLocation StartLoc,
6461 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006462 if (!AStmt)
6463 return StmtError();
6464
David Majnemer9d168222016-08-05 17:44:54 +00006465 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00006466 // 1.2.2 OpenMP Language Terminology
6467 // Structured block - An executable statement with a single entry at the
6468 // top and a single exit at the bottom.
6469 // The point of exit cannot be a branch out of the structured block.
6470 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00006471 OpenMPClauseKind AtomicKind = OMPC_unknown;
6472 SourceLocation AtomicKindLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00006473 for (const OMPClause *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00006474 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00006475 C->getClauseKind() == OMPC_update ||
6476 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00006477 if (AtomicKind != OMPC_unknown) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006478 Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00006479 << SourceRange(C->getBeginLoc(), C->getEndLoc());
Alexey Bataevdea47612014-07-23 07:46:59 +00006480 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
6481 << getOpenMPClauseName(AtomicKind);
6482 } else {
6483 AtomicKind = C->getClauseKind();
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006484 AtomicKindLoc = C->getBeginLoc();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006485 }
6486 }
6487 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006488
Alexey Bataeve3727102018-04-18 15:57:46 +00006489 Stmt *Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00006490 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
6491 Body = EWC->getSubExpr();
6492
Alexey Bataev62cec442014-11-18 10:14:22 +00006493 Expr *X = nullptr;
6494 Expr *V = nullptr;
6495 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006496 Expr *UE = nullptr;
6497 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006498 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00006499 // OpenMP [2.12.6, atomic Construct]
6500 // In the next expressions:
6501 // * x and v (as applicable) are both l-value expressions with scalar type.
6502 // * During the execution of an atomic region, multiple syntactic
6503 // occurrences of x must designate the same storage location.
6504 // * Neither of v and expr (as applicable) may access the storage location
6505 // designated by x.
6506 // * Neither of x and expr (as applicable) may access the storage location
6507 // designated by v.
6508 // * expr is an expression with scalar type.
6509 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
6510 // * binop, binop=, ++, and -- are not overloaded operators.
6511 // * The expression x binop expr must be numerically equivalent to x binop
6512 // (expr). This requirement is satisfied if the operators in expr have
6513 // precedence greater than binop, or by using parentheses around expr or
6514 // subexpressions of expr.
6515 // * The expression expr binop x must be numerically equivalent to (expr)
6516 // binop x. This requirement is satisfied if the operators in expr have
6517 // precedence equal to or greater than binop, or by using parentheses around
6518 // expr or subexpressions of expr.
6519 // * For forms that allow multiple occurrences of x, the number of times
6520 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00006521 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006522 enum {
6523 NotAnExpression,
6524 NotAnAssignmentOp,
6525 NotAScalarType,
6526 NotAnLValue,
6527 NoError
6528 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00006529 SourceLocation ErrorLoc, NoteLoc;
6530 SourceRange ErrorRange, NoteRange;
6531 // If clause is read:
6532 // v = x;
Alexey Bataeve3727102018-04-18 15:57:46 +00006533 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
6534 const auto *AtomicBinOp =
Alexey Bataev62cec442014-11-18 10:14:22 +00006535 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6536 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6537 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6538 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
6539 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6540 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
6541 if (!X->isLValue() || !V->isLValue()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006542 const Expr *NotLValueExpr = X->isLValue() ? V : X;
Alexey Bataev62cec442014-11-18 10:14:22 +00006543 ErrorFound = NotAnLValue;
6544 ErrorLoc = AtomicBinOp->getExprLoc();
6545 ErrorRange = AtomicBinOp->getSourceRange();
6546 NoteLoc = NotLValueExpr->getExprLoc();
6547 NoteRange = NotLValueExpr->getSourceRange();
6548 }
6549 } else if (!X->isInstantiationDependent() ||
6550 !V->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006551 const Expr *NotScalarExpr =
Alexey Bataev62cec442014-11-18 10:14:22 +00006552 (X->isInstantiationDependent() || X->getType()->isScalarType())
6553 ? V
6554 : X;
6555 ErrorFound = NotAScalarType;
6556 ErrorLoc = AtomicBinOp->getExprLoc();
6557 ErrorRange = AtomicBinOp->getSourceRange();
6558 NoteLoc = NotScalarExpr->getExprLoc();
6559 NoteRange = NotScalarExpr->getSourceRange();
6560 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006561 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00006562 ErrorFound = NotAnAssignmentOp;
6563 ErrorLoc = AtomicBody->getExprLoc();
6564 ErrorRange = AtomicBody->getSourceRange();
6565 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6566 : AtomicBody->getExprLoc();
6567 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6568 : AtomicBody->getSourceRange();
6569 }
6570 } else {
6571 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006572 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataev62cec442014-11-18 10:14:22 +00006573 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006574 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006575 if (ErrorFound != NoError) {
6576 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
6577 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006578 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6579 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00006580 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00006581 }
6582 if (CurContext->isDependentContext())
Alexey Bataev62cec442014-11-18 10:14:22 +00006583 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00006584 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006585 enum {
6586 NotAnExpression,
6587 NotAnAssignmentOp,
6588 NotAScalarType,
6589 NotAnLValue,
6590 NoError
6591 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006592 SourceLocation ErrorLoc, NoteLoc;
6593 SourceRange ErrorRange, NoteRange;
6594 // If clause is write:
6595 // x = expr;
Alexey Bataeve3727102018-04-18 15:57:46 +00006596 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
6597 const auto *AtomicBinOp =
Alexey Bataevf33eba62014-11-28 07:21:40 +00006598 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6599 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00006600 X = AtomicBinOp->getLHS();
6601 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006602 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6603 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
6604 if (!X->isLValue()) {
6605 ErrorFound = NotAnLValue;
6606 ErrorLoc = AtomicBinOp->getExprLoc();
6607 ErrorRange = AtomicBinOp->getSourceRange();
6608 NoteLoc = X->getExprLoc();
6609 NoteRange = X->getSourceRange();
6610 }
6611 } else if (!X->isInstantiationDependent() ||
6612 !E->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006613 const Expr *NotScalarExpr =
Alexey Bataevf33eba62014-11-28 07:21:40 +00006614 (X->isInstantiationDependent() || X->getType()->isScalarType())
6615 ? E
6616 : X;
6617 ErrorFound = NotAScalarType;
6618 ErrorLoc = AtomicBinOp->getExprLoc();
6619 ErrorRange = AtomicBinOp->getSourceRange();
6620 NoteLoc = NotScalarExpr->getExprLoc();
6621 NoteRange = NotScalarExpr->getSourceRange();
6622 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006623 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00006624 ErrorFound = NotAnAssignmentOp;
6625 ErrorLoc = AtomicBody->getExprLoc();
6626 ErrorRange = AtomicBody->getSourceRange();
6627 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6628 : AtomicBody->getExprLoc();
6629 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6630 : AtomicBody->getSourceRange();
6631 }
6632 } else {
6633 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006634 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006635 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006636 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00006637 if (ErrorFound != NoError) {
6638 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
6639 << ErrorRange;
6640 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6641 << NoteRange;
6642 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00006643 }
6644 if (CurContext->isDependentContext())
Alexey Bataevf33eba62014-11-28 07:21:40 +00006645 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006646 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006647 // If clause is update:
6648 // x++;
6649 // x--;
6650 // ++x;
6651 // --x;
6652 // x binop= expr;
6653 // x = x binop expr;
6654 // x = expr binop x;
6655 OpenMPAtomicUpdateChecker Checker(*this);
6656 if (Checker.checkStatement(
6657 Body, (AtomicKind == OMPC_update)
6658 ? diag::err_omp_atomic_update_not_expression_statement
6659 : diag::err_omp_atomic_not_expression_statement,
6660 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00006661 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006662 if (!CurContext->isDependentContext()) {
6663 E = Checker.getExpr();
6664 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006665 UE = Checker.getUpdateExpr();
6666 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00006667 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006668 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006669 enum {
6670 NotAnAssignmentOp,
6671 NotACompoundStatement,
6672 NotTwoSubstatements,
6673 NotASpecificExpression,
6674 NoError
6675 } ErrorFound = NoError;
6676 SourceLocation ErrorLoc, NoteLoc;
6677 SourceRange ErrorRange, NoteRange;
Alexey Bataeve3727102018-04-18 15:57:46 +00006678 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006679 // If clause is a capture:
6680 // v = x++;
6681 // v = x--;
6682 // v = ++x;
6683 // v = --x;
6684 // v = x binop= expr;
6685 // v = x = x binop expr;
6686 // v = x = expr binop x;
Alexey Bataeve3727102018-04-18 15:57:46 +00006687 const auto *AtomicBinOp =
Alexey Bataevb78ca832015-04-01 03:33:17 +00006688 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6689 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6690 V = AtomicBinOp->getLHS();
6691 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6692 OpenMPAtomicUpdateChecker Checker(*this);
6693 if (Checker.checkStatement(
6694 Body, diag::err_omp_atomic_capture_not_expression_statement,
6695 diag::note_omp_atomic_update))
6696 return StmtError();
6697 E = Checker.getExpr();
6698 X = Checker.getX();
6699 UE = Checker.getUpdateExpr();
6700 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6701 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00006702 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006703 ErrorLoc = AtomicBody->getExprLoc();
6704 ErrorRange = AtomicBody->getSourceRange();
6705 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6706 : AtomicBody->getExprLoc();
6707 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6708 : AtomicBody->getSourceRange();
6709 ErrorFound = NotAnAssignmentOp;
6710 }
6711 if (ErrorFound != NoError) {
6712 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6713 << ErrorRange;
6714 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6715 return StmtError();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006716 }
Alexey Bataeve3727102018-04-18 15:57:46 +00006717 if (CurContext->isDependentContext())
6718 UE = V = E = X = nullptr;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006719 } else {
6720 // If clause is a capture:
6721 // { v = x; x = expr; }
6722 // { v = x; x++; }
6723 // { v = x; x--; }
6724 // { v = x; ++x; }
6725 // { v = x; --x; }
6726 // { v = x; x binop= expr; }
6727 // { v = x; x = x binop expr; }
6728 // { v = x; x = expr binop x; }
6729 // { x++; v = x; }
6730 // { x--; v = x; }
6731 // { ++x; v = x; }
6732 // { --x; v = x; }
6733 // { x binop= expr; v = x; }
6734 // { x = x binop expr; v = x; }
6735 // { x = expr binop x; v = x; }
6736 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6737 // Check that this is { expr1; expr2; }
6738 if (CS->size() == 2) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006739 Stmt *First = CS->body_front();
6740 Stmt *Second = CS->body_back();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006741 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6742 First = EWC->getSubExpr()->IgnoreParenImpCasts();
6743 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6744 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6745 // Need to find what subexpression is 'v' and what is 'x'.
6746 OpenMPAtomicUpdateChecker Checker(*this);
6747 bool IsUpdateExprFound = !Checker.checkStatement(Second);
6748 BinaryOperator *BinOp = nullptr;
6749 if (IsUpdateExprFound) {
6750 BinOp = dyn_cast<BinaryOperator>(First);
6751 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6752 }
6753 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6754 // { v = x; x++; }
6755 // { v = x; x--; }
6756 // { v = x; ++x; }
6757 // { v = x; --x; }
6758 // { v = x; x binop= expr; }
6759 // { v = x; x = x binop expr; }
6760 // { v = x; x = expr binop x; }
6761 // Check that the first expression has form v = x.
Alexey Bataeve3727102018-04-18 15:57:46 +00006762 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006763 llvm::FoldingSetNodeID XId, PossibleXId;
6764 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6765 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6766 IsUpdateExprFound = XId == PossibleXId;
6767 if (IsUpdateExprFound) {
6768 V = BinOp->getLHS();
6769 X = Checker.getX();
6770 E = Checker.getExpr();
6771 UE = Checker.getUpdateExpr();
6772 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006773 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006774 }
6775 }
6776 if (!IsUpdateExprFound) {
6777 IsUpdateExprFound = !Checker.checkStatement(First);
6778 BinOp = nullptr;
6779 if (IsUpdateExprFound) {
6780 BinOp = dyn_cast<BinaryOperator>(Second);
6781 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6782 }
6783 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6784 // { x++; v = x; }
6785 // { x--; v = x; }
6786 // { ++x; v = x; }
6787 // { --x; v = x; }
6788 // { x binop= expr; v = x; }
6789 // { x = x binop expr; v = x; }
6790 // { x = expr binop x; v = x; }
6791 // Check that the second expression has form v = x.
Alexey Bataeve3727102018-04-18 15:57:46 +00006792 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006793 llvm::FoldingSetNodeID XId, PossibleXId;
6794 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6795 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6796 IsUpdateExprFound = XId == PossibleXId;
6797 if (IsUpdateExprFound) {
6798 V = BinOp->getLHS();
6799 X = Checker.getX();
6800 E = Checker.getExpr();
6801 UE = Checker.getUpdateExpr();
6802 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006803 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006804 }
6805 }
6806 }
6807 if (!IsUpdateExprFound) {
6808 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00006809 auto *FirstExpr = dyn_cast<Expr>(First);
6810 auto *SecondExpr = dyn_cast<Expr>(Second);
6811 if (!FirstExpr || !SecondExpr ||
6812 !(FirstExpr->isInstantiationDependent() ||
6813 SecondExpr->isInstantiationDependent())) {
6814 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6815 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006816 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00006817 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006818 : First->getBeginLoc();
Alexey Bataev5a195472015-09-04 12:55:50 +00006819 NoteRange = ErrorRange = FirstBinOp
6820 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00006821 : SourceRange(ErrorLoc, ErrorLoc);
6822 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006823 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6824 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6825 ErrorFound = NotAnAssignmentOp;
6826 NoteLoc = ErrorLoc = SecondBinOp
6827 ? SecondBinOp->getOperatorLoc()
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006828 : Second->getBeginLoc();
Alexey Bataev5a195472015-09-04 12:55:50 +00006829 NoteRange = ErrorRange =
6830 SecondBinOp ? SecondBinOp->getSourceRange()
6831 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00006832 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +00006833 Expr *PossibleXRHSInFirst =
Alexey Bataev5a195472015-09-04 12:55:50 +00006834 FirstBinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00006835 Expr *PossibleXLHSInSecond =
Alexey Bataev5a195472015-09-04 12:55:50 +00006836 SecondBinOp->getLHS()->IgnoreParenImpCasts();
6837 llvm::FoldingSetNodeID X1Id, X2Id;
6838 PossibleXRHSInFirst->Profile(X1Id, Context,
6839 /*Canonical=*/true);
6840 PossibleXLHSInSecond->Profile(X2Id, Context,
6841 /*Canonical=*/true);
6842 IsUpdateExprFound = X1Id == X2Id;
6843 if (IsUpdateExprFound) {
6844 V = FirstBinOp->getLHS();
6845 X = SecondBinOp->getLHS();
6846 E = SecondBinOp->getRHS();
6847 UE = nullptr;
6848 IsXLHSInRHSPart = false;
6849 IsPostfixUpdate = true;
6850 } else {
6851 ErrorFound = NotASpecificExpression;
6852 ErrorLoc = FirstBinOp->getExprLoc();
6853 ErrorRange = FirstBinOp->getSourceRange();
6854 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6855 NoteRange = SecondBinOp->getRHS()->getSourceRange();
6856 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006857 }
6858 }
6859 }
6860 }
6861 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006862 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006863 NoteRange = ErrorRange =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006864 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
Alexey Bataevb78ca832015-04-01 03:33:17 +00006865 ErrorFound = NotTwoSubstatements;
6866 }
6867 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006868 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006869 NoteRange = ErrorRange =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006870 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
Alexey Bataevb78ca832015-04-01 03:33:17 +00006871 ErrorFound = NotACompoundStatement;
6872 }
6873 if (ErrorFound != NoError) {
6874 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6875 << ErrorRange;
6876 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6877 return StmtError();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006878 }
Alexey Bataeve3727102018-04-18 15:57:46 +00006879 if (CurContext->isDependentContext())
6880 UE = V = E = X = nullptr;
Alexey Bataev459dec02014-07-24 06:46:57 +00006881 }
Alexey Bataevdea47612014-07-23 07:46:59 +00006882 }
Alexey Bataev0162e452014-07-22 10:10:35 +00006883
Reid Kleckner87a31802018-03-12 21:43:02 +00006884 setFunctionHasBranchProtectedScope();
Alexey Bataev0162e452014-07-22 10:10:35 +00006885
Alexey Bataev62cec442014-11-18 10:14:22 +00006886 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00006887 X, V, E, UE, IsXLHSInRHSPart,
6888 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00006889}
6890
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006891StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6892 Stmt *AStmt,
6893 SourceLocation StartLoc,
6894 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006895 if (!AStmt)
6896 return StmtError();
6897
Alexey Bataeve3727102018-04-18 15:57:46 +00006898 auto *CS = cast<CapturedStmt>(AStmt);
Samuel Antao4af1b7b2015-12-02 17:44:43 +00006899 // 1.2.2 OpenMP Language Terminology
6900 // Structured block - An executable statement with a single entry at the
6901 // top and a single exit at the bottom.
6902 // The point of exit cannot be a branch out of the structured block.
6903 // longjmp() and throw() must not violate the entry/exit criteria.
6904 CS->getCapturedDecl()->setNothrow();
Alexey Bataev8451efa2018-01-15 19:06:12 +00006905 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target);
6906 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6907 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6908 // 1.2.2 OpenMP Language Terminology
6909 // Structured block - An executable statement with a single entry at the
6910 // top and a single exit at the bottom.
6911 // The point of exit cannot be a branch out of the structured block.
6912 // longjmp() and throw() must not violate the entry/exit criteria.
6913 CS->getCapturedDecl()->setNothrow();
6914 }
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006915
Alexey Bataev13314bf2014-10-09 04:18:56 +00006916 // OpenMP [2.16, Nesting of Regions]
6917 // If specified, a teams construct must be contained within a target
6918 // construct. That target construct must contain no statements or directives
6919 // outside of the teams construct.
6920 if (DSAStack->hasInnerTeamsRegion()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006921 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true);
Alexey Bataev13314bf2014-10-09 04:18:56 +00006922 bool OMPTeamsFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006923 if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00006924 auto I = CS->body_begin();
6925 while (I != CS->body_end()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006926 const auto *OED = dyn_cast<OMPExecutableDirective>(*I);
Alexey Bataev13314bf2014-10-09 04:18:56 +00006927 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6928 OMPTeamsFound = false;
6929 break;
6930 }
6931 ++I;
6932 }
6933 assert(I != CS->body_end() && "Not found statement");
6934 S = *I;
Kelvin Li3834dce2016-06-27 19:15:43 +00006935 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +00006936 const auto *OED = dyn_cast<OMPExecutableDirective>(S);
Kelvin Li3834dce2016-06-27 19:15:43 +00006937 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
Alexey Bataev13314bf2014-10-09 04:18:56 +00006938 }
6939 if (!OMPTeamsFound) {
6940 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
6941 Diag(DSAStack->getInnerTeamsRegionLoc(),
6942 diag::note_omp_nested_teams_construct_here);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006943 Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here)
Alexey Bataev13314bf2014-10-09 04:18:56 +00006944 << isa<OMPExecutableDirective>(S);
6945 return StmtError();
6946 }
6947 }
6948
Reid Kleckner87a31802018-03-12 21:43:02 +00006949 setFunctionHasBranchProtectedScope();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006950
6951 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6952}
6953
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006954StmtResult
6955Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
6956 Stmt *AStmt, SourceLocation StartLoc,
6957 SourceLocation EndLoc) {
6958 if (!AStmt)
6959 return StmtError();
6960
Alexey Bataeve3727102018-04-18 15:57:46 +00006961 auto *CS = cast<CapturedStmt>(AStmt);
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006962 // 1.2.2 OpenMP Language Terminology
6963 // Structured block - An executable statement with a single entry at the
6964 // top and a single exit at the bottom.
6965 // The point of exit cannot be a branch out of the structured block.
6966 // longjmp() and throw() must not violate the entry/exit criteria.
6967 CS->getCapturedDecl()->setNothrow();
Alexey Bataev8451efa2018-01-15 19:06:12 +00006968 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel);
6969 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6970 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6971 // 1.2.2 OpenMP Language Terminology
6972 // Structured block - An executable statement with a single entry at the
6973 // top and a single exit at the bottom.
6974 // The point of exit cannot be a branch out of the structured block.
6975 // longjmp() and throw() must not violate the entry/exit criteria.
6976 CS->getCapturedDecl()->setNothrow();
6977 }
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006978
Reid Kleckner87a31802018-03-12 21:43:02 +00006979 setFunctionHasBranchProtectedScope();
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006980
6981 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6982 AStmt);
6983}
6984
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006985StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
6986 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00006987 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006988 if (!AStmt)
6989 return StmtError();
6990
Alexey Bataeve3727102018-04-18 15:57:46 +00006991 auto *CS = cast<CapturedStmt>(AStmt);
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006992 // 1.2.2 OpenMP Language Terminology
6993 // Structured block - An executable statement with a single entry at the
6994 // top and a single exit at the bottom.
6995 // The point of exit cannot be a branch out of the structured block.
6996 // longjmp() and throw() must not violate the entry/exit criteria.
6997 CS->getCapturedDecl()->setNothrow();
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00006998 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
6999 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7000 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7001 // 1.2.2 OpenMP Language Terminology
7002 // Structured block - An executable statement with a single entry at the
7003 // top and a single exit at the bottom.
7004 // The point of exit cannot be a branch out of the structured block.
7005 // longjmp() and throw() must not violate the entry/exit criteria.
7006 CS->getCapturedDecl()->setNothrow();
7007 }
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007008
7009 OMPLoopDirective::HelperExprs B;
7010 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7011 // define the nested loops number.
7012 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007013 checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00007014 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007015 VarsWithImplicitDSA, B);
7016 if (NestedLoopCount == 0)
7017 return StmtError();
7018
7019 assert((CurContext->isDependentContext() || B.builtAll()) &&
7020 "omp target parallel for loop exprs were not built");
7021
7022 if (!CurContext->isDependentContext()) {
7023 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007024 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007025 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007026 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007027 B.NumIterations, *this, CurScope,
7028 DSAStack))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007029 return StmtError();
7030 }
7031 }
7032
Reid Kleckner87a31802018-03-12 21:43:02 +00007033 setFunctionHasBranchProtectedScope();
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007034 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
7035 NestedLoopCount, Clauses, AStmt,
7036 B, DSAStack->isCancelRegion());
7037}
7038
Alexey Bataev95b64a92017-05-30 16:00:04 +00007039/// Check for existence of a map clause in the list of clauses.
7040static bool hasClauses(ArrayRef<OMPClause *> Clauses,
7041 const OpenMPClauseKind K) {
7042 return llvm::any_of(
7043 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; });
7044}
Samuel Antaodf67fc42016-01-19 19:15:56 +00007045
Alexey Bataev95b64a92017-05-30 16:00:04 +00007046template <typename... Params>
7047static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K,
7048 const Params... ClauseTypes) {
7049 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...);
Samuel Antaodf67fc42016-01-19 19:15:56 +00007050}
7051
Michael Wong65f367f2015-07-21 13:44:28 +00007052StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
7053 Stmt *AStmt,
7054 SourceLocation StartLoc,
7055 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007056 if (!AStmt)
7057 return StmtError();
7058
7059 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7060
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00007061 // OpenMP [2.10.1, Restrictions, p. 97]
7062 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00007063 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr)) {
7064 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
7065 << "'map' or 'use_device_ptr'"
David Majnemer9d168222016-08-05 17:44:54 +00007066 << getOpenMPDirectiveName(OMPD_target_data);
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00007067 return StmtError();
7068 }
7069
Reid Kleckner87a31802018-03-12 21:43:02 +00007070 setFunctionHasBranchProtectedScope();
Michael Wong65f367f2015-07-21 13:44:28 +00007071
7072 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
7073 AStmt);
7074}
7075
Samuel Antaodf67fc42016-01-19 19:15:56 +00007076StmtResult
7077Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
7078 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00007079 SourceLocation EndLoc, Stmt *AStmt) {
7080 if (!AStmt)
7081 return StmtError();
7082
Alexey Bataeve3727102018-04-18 15:57:46 +00007083 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00007084 // 1.2.2 OpenMP Language Terminology
7085 // Structured block - An executable statement with a single entry at the
7086 // top and a single exit at the bottom.
7087 // The point of exit cannot be a branch out of the structured block.
7088 // longjmp() and throw() must not violate the entry/exit criteria.
7089 CS->getCapturedDecl()->setNothrow();
7090 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data);
7091 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7092 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7093 // 1.2.2 OpenMP Language Terminology
7094 // Structured block - An executable statement with a single entry at the
7095 // top and a single exit at the bottom.
7096 // The point of exit cannot be a branch out of the structured block.
7097 // longjmp() and throw() must not violate the entry/exit criteria.
7098 CS->getCapturedDecl()->setNothrow();
7099 }
7100
Samuel Antaodf67fc42016-01-19 19:15:56 +00007101 // OpenMP [2.10.2, Restrictions, p. 99]
7102 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00007103 if (!hasClauses(Clauses, OMPC_map)) {
7104 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
7105 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data);
Samuel Antaodf67fc42016-01-19 19:15:56 +00007106 return StmtError();
7107 }
7108
Alexey Bataev7828b252017-11-21 17:08:48 +00007109 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
7110 AStmt);
Samuel Antaodf67fc42016-01-19 19:15:56 +00007111}
7112
Samuel Antao72590762016-01-19 20:04:50 +00007113StmtResult
7114Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
7115 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00007116 SourceLocation EndLoc, Stmt *AStmt) {
7117 if (!AStmt)
7118 return StmtError();
7119
Alexey Bataeve3727102018-04-18 15:57:46 +00007120 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00007121 // 1.2.2 OpenMP Language Terminology
7122 // Structured block - An executable statement with a single entry at the
7123 // top and a single exit at the bottom.
7124 // The point of exit cannot be a branch out of the structured block.
7125 // longjmp() and throw() must not violate the entry/exit criteria.
7126 CS->getCapturedDecl()->setNothrow();
7127 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data);
7128 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7129 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7130 // 1.2.2 OpenMP Language Terminology
7131 // Structured block - An executable statement with a single entry at the
7132 // top and a single exit at the bottom.
7133 // The point of exit cannot be a branch out of the structured block.
7134 // longjmp() and throw() must not violate the entry/exit criteria.
7135 CS->getCapturedDecl()->setNothrow();
7136 }
7137
Samuel Antao72590762016-01-19 20:04:50 +00007138 // OpenMP [2.10.3, Restrictions, p. 102]
7139 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00007140 if (!hasClauses(Clauses, OMPC_map)) {
7141 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
7142 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data);
Samuel Antao72590762016-01-19 20:04:50 +00007143 return StmtError();
7144 }
7145
Alexey Bataev7828b252017-11-21 17:08:48 +00007146 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
7147 AStmt);
Samuel Antao72590762016-01-19 20:04:50 +00007148}
7149
Samuel Antao686c70c2016-05-26 17:30:50 +00007150StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
7151 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00007152 SourceLocation EndLoc,
7153 Stmt *AStmt) {
7154 if (!AStmt)
7155 return StmtError();
7156
Alexey Bataeve3727102018-04-18 15:57:46 +00007157 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00007158 // 1.2.2 OpenMP Language Terminology
7159 // Structured block - An executable statement with a single entry at the
7160 // top and a single exit at the bottom.
7161 // The point of exit cannot be a branch out of the structured block.
7162 // longjmp() and throw() must not violate the entry/exit criteria.
7163 CS->getCapturedDecl()->setNothrow();
7164 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update);
7165 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7166 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7167 // 1.2.2 OpenMP Language Terminology
7168 // Structured block - An executable statement with a single entry at the
7169 // top and a single exit at the bottom.
7170 // The point of exit cannot be a branch out of the structured block.
7171 // longjmp() and throw() must not violate the entry/exit criteria.
7172 CS->getCapturedDecl()->setNothrow();
7173 }
7174
Alexey Bataev95b64a92017-05-30 16:00:04 +00007175 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) {
Samuel Antao686c70c2016-05-26 17:30:50 +00007176 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
7177 return StmtError();
7178 }
Alexey Bataev7828b252017-11-21 17:08:48 +00007179 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses,
7180 AStmt);
Samuel Antao686c70c2016-05-26 17:30:50 +00007181}
7182
Alexey Bataev13314bf2014-10-09 04:18:56 +00007183StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
7184 Stmt *AStmt, SourceLocation StartLoc,
7185 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007186 if (!AStmt)
7187 return StmtError();
7188
Alexey Bataeve3727102018-04-18 15:57:46 +00007189 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev13314bf2014-10-09 04:18:56 +00007190 // 1.2.2 OpenMP Language Terminology
7191 // Structured block - An executable statement with a single entry at the
7192 // top and a single exit at the bottom.
7193 // The point of exit cannot be a branch out of the structured block.
7194 // longjmp() and throw() must not violate the entry/exit criteria.
7195 CS->getCapturedDecl()->setNothrow();
7196
Reid Kleckner87a31802018-03-12 21:43:02 +00007197 setFunctionHasBranchProtectedScope();
Alexey Bataev13314bf2014-10-09 04:18:56 +00007198
Alexey Bataevceabd412017-11-30 18:01:54 +00007199 DSAStack->setParentTeamsRegionLoc(StartLoc);
7200
Alexey Bataev13314bf2014-10-09 04:18:56 +00007201 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
7202}
7203
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007204StmtResult
7205Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
7206 SourceLocation EndLoc,
7207 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007208 if (DSAStack->isParentNowaitRegion()) {
7209 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
7210 return StmtError();
7211 }
7212 if (DSAStack->isParentOrderedRegion()) {
7213 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
7214 return StmtError();
7215 }
7216 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
7217 CancelRegion);
7218}
7219
Alexey Bataev87933c72015-09-18 08:07:34 +00007220StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
7221 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00007222 SourceLocation EndLoc,
7223 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev80909872015-07-02 11:25:17 +00007224 if (DSAStack->isParentNowaitRegion()) {
7225 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
7226 return StmtError();
7227 }
7228 if (DSAStack->isParentOrderedRegion()) {
7229 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
7230 return StmtError();
7231 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00007232 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00007233 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
7234 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00007235}
7236
Alexey Bataev382967a2015-12-08 12:06:20 +00007237static bool checkGrainsizeNumTasksClauses(Sema &S,
7238 ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007239 const OMPClause *PrevClause = nullptr;
Alexey Bataev382967a2015-12-08 12:06:20 +00007240 bool ErrorFound = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00007241 for (const OMPClause *C : Clauses) {
Alexey Bataev382967a2015-12-08 12:06:20 +00007242 if (C->getClauseKind() == OMPC_grainsize ||
7243 C->getClauseKind() == OMPC_num_tasks) {
7244 if (!PrevClause)
7245 PrevClause = C;
7246 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007247 S.Diag(C->getBeginLoc(),
Alexey Bataev382967a2015-12-08 12:06:20 +00007248 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
7249 << getOpenMPClauseName(C->getClauseKind())
7250 << getOpenMPClauseName(PrevClause->getClauseKind());
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007251 S.Diag(PrevClause->getBeginLoc(),
Alexey Bataev382967a2015-12-08 12:06:20 +00007252 diag::note_omp_previous_grainsize_num_tasks)
7253 << getOpenMPClauseName(PrevClause->getClauseKind());
7254 ErrorFound = true;
7255 }
7256 }
7257 }
7258 return ErrorFound;
7259}
7260
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007261static bool checkReductionClauseWithNogroup(Sema &S,
7262 ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007263 const OMPClause *ReductionClause = nullptr;
7264 const OMPClause *NogroupClause = nullptr;
7265 for (const OMPClause *C : Clauses) {
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007266 if (C->getClauseKind() == OMPC_reduction) {
7267 ReductionClause = C;
7268 if (NogroupClause)
7269 break;
7270 continue;
7271 }
7272 if (C->getClauseKind() == OMPC_nogroup) {
7273 NogroupClause = C;
7274 if (ReductionClause)
7275 break;
7276 continue;
7277 }
7278 }
7279 if (ReductionClause && NogroupClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007280 S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup)
7281 << SourceRange(NogroupClause->getBeginLoc(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007282 NogroupClause->getEndLoc());
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007283 return true;
7284 }
7285 return false;
7286}
7287
Alexey Bataev49f6e782015-12-01 04:18:41 +00007288StmtResult Sema::ActOnOpenMPTaskLoopDirective(
7289 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007290 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00007291 if (!AStmt)
7292 return StmtError();
7293
7294 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7295 OMPLoopDirective::HelperExprs B;
7296 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7297 // define the nested loops number.
7298 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007299 checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007300 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00007301 VarsWithImplicitDSA, B);
7302 if (NestedLoopCount == 0)
7303 return StmtError();
7304
7305 assert((CurContext->isDependentContext() || B.builtAll()) &&
7306 "omp for loop exprs were not built");
7307
Alexey Bataev382967a2015-12-08 12:06:20 +00007308 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7309 // The grainsize clause and num_tasks clause are mutually exclusive and may
7310 // not appear on the same taskloop directive.
7311 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7312 return StmtError();
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007313 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7314 // If a reduction clause is present on the taskloop directive, the nogroup
7315 // clause must not be specified.
7316 if (checkReductionClauseWithNogroup(*this, Clauses))
7317 return StmtError();
Alexey Bataev382967a2015-12-08 12:06:20 +00007318
Reid Kleckner87a31802018-03-12 21:43:02 +00007319 setFunctionHasBranchProtectedScope();
Alexey Bataev49f6e782015-12-01 04:18:41 +00007320 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
7321 NestedLoopCount, Clauses, AStmt, B);
7322}
7323
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007324StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
7325 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007326 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007327 if (!AStmt)
7328 return StmtError();
7329
7330 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7331 OMPLoopDirective::HelperExprs B;
7332 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7333 // define the nested loops number.
7334 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007335 checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007336 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
7337 VarsWithImplicitDSA, B);
7338 if (NestedLoopCount == 0)
7339 return StmtError();
7340
7341 assert((CurContext->isDependentContext() || B.builtAll()) &&
7342 "omp for loop exprs were not built");
7343
Alexey Bataev5a3af132016-03-29 08:58:54 +00007344 if (!CurContext->isDependentContext()) {
7345 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007346 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007347 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007348 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007349 B.NumIterations, *this, CurScope,
7350 DSAStack))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007351 return StmtError();
7352 }
7353 }
7354
Alexey Bataev382967a2015-12-08 12:06:20 +00007355 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7356 // The grainsize clause and num_tasks clause are mutually exclusive and may
7357 // not appear on the same taskloop directive.
7358 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7359 return StmtError();
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007360 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7361 // If a reduction clause is present on the taskloop directive, the nogroup
7362 // clause must not be specified.
7363 if (checkReductionClauseWithNogroup(*this, Clauses))
7364 return StmtError();
Alexey Bataev438388c2017-11-22 18:34:02 +00007365 if (checkSimdlenSafelenSpecified(*this, Clauses))
7366 return StmtError();
Alexey Bataev382967a2015-12-08 12:06:20 +00007367
Reid Kleckner87a31802018-03-12 21:43:02 +00007368 setFunctionHasBranchProtectedScope();
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007369 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
7370 NestedLoopCount, Clauses, AStmt, B);
7371}
7372
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007373StmtResult Sema::ActOnOpenMPDistributeDirective(
7374 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007375 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007376 if (!AStmt)
7377 return StmtError();
7378
7379 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7380 OMPLoopDirective::HelperExprs B;
7381 // In presence of clause 'collapse' with number of loops, it will
7382 // define the nested loops number.
7383 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007384 checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007385 nullptr /*ordered not a clause on distribute*/, AStmt,
7386 *this, *DSAStack, VarsWithImplicitDSA, B);
7387 if (NestedLoopCount == 0)
7388 return StmtError();
7389
7390 assert((CurContext->isDependentContext() || B.builtAll()) &&
7391 "omp for loop exprs were not built");
7392
Reid Kleckner87a31802018-03-12 21:43:02 +00007393 setFunctionHasBranchProtectedScope();
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007394 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
7395 NestedLoopCount, Clauses, AStmt, B);
7396}
7397
Carlo Bertolli9925f152016-06-27 14:55:37 +00007398StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
7399 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007400 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Carlo Bertolli9925f152016-06-27 14:55:37 +00007401 if (!AStmt)
7402 return StmtError();
7403
Alexey Bataeve3727102018-04-18 15:57:46 +00007404 auto *CS = cast<CapturedStmt>(AStmt);
Carlo Bertolli9925f152016-06-27 14:55:37 +00007405 // 1.2.2 OpenMP Language Terminology
7406 // Structured block - An executable statement with a single entry at the
7407 // top and a single exit at the bottom.
7408 // The point of exit cannot be a branch out of the structured block.
7409 // longjmp() and throw() must not violate the entry/exit criteria.
7410 CS->getCapturedDecl()->setNothrow();
Alexey Bataev7f96c372017-11-22 17:19:31 +00007411 for (int ThisCaptureLevel =
7412 getOpenMPCaptureLevels(OMPD_distribute_parallel_for);
7413 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7414 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7415 // 1.2.2 OpenMP Language Terminology
7416 // Structured block - An executable statement with a single entry at the
7417 // top and a single exit at the bottom.
7418 // The point of exit cannot be a branch out of the structured block.
7419 // longjmp() and throw() must not violate the entry/exit criteria.
7420 CS->getCapturedDecl()->setNothrow();
7421 }
Carlo Bertolli9925f152016-06-27 14:55:37 +00007422
7423 OMPLoopDirective::HelperExprs B;
7424 // In presence of clause 'collapse' with number of loops, it will
7425 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007426 unsigned NestedLoopCount = checkOpenMPLoop(
Carlo Bertolli9925f152016-06-27 14:55:37 +00007427 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataev7f96c372017-11-22 17:19:31 +00007428 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Carlo Bertolli9925f152016-06-27 14:55:37 +00007429 VarsWithImplicitDSA, B);
7430 if (NestedLoopCount == 0)
7431 return StmtError();
7432
7433 assert((CurContext->isDependentContext() || B.builtAll()) &&
7434 "omp for loop exprs were not built");
7435
Reid Kleckner87a31802018-03-12 21:43:02 +00007436 setFunctionHasBranchProtectedScope();
Carlo Bertolli9925f152016-06-27 14:55:37 +00007437 return OMPDistributeParallelForDirective::Create(
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00007438 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7439 DSAStack->isCancelRegion());
Carlo Bertolli9925f152016-06-27 14:55:37 +00007440}
7441
Kelvin Li4a39add2016-07-05 05:00:15 +00007442StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
7443 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007444 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li4a39add2016-07-05 05:00:15 +00007445 if (!AStmt)
7446 return StmtError();
7447
Alexey Bataeve3727102018-04-18 15:57:46 +00007448 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li4a39add2016-07-05 05:00:15 +00007449 // 1.2.2 OpenMP Language Terminology
7450 // Structured block - An executable statement with a single entry at the
7451 // top and a single exit at the bottom.
7452 // The point of exit cannot be a branch out of the structured block.
7453 // longjmp() and throw() must not violate the entry/exit criteria.
7454 CS->getCapturedDecl()->setNothrow();
Alexey Bataev974acd62017-11-27 19:38:52 +00007455 for (int ThisCaptureLevel =
7456 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd);
7457 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7458 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7459 // 1.2.2 OpenMP Language Terminology
7460 // Structured block - An executable statement with a single entry at the
7461 // top and a single exit at the bottom.
7462 // The point of exit cannot be a branch out of the structured block.
7463 // longjmp() and throw() must not violate the entry/exit criteria.
7464 CS->getCapturedDecl()->setNothrow();
7465 }
Kelvin Li4a39add2016-07-05 05:00:15 +00007466
7467 OMPLoopDirective::HelperExprs B;
7468 // In presence of clause 'collapse' with number of loops, it will
7469 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007470 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li4a39add2016-07-05 05:00:15 +00007471 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev974acd62017-11-27 19:38:52 +00007472 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li4a39add2016-07-05 05:00:15 +00007473 VarsWithImplicitDSA, B);
7474 if (NestedLoopCount == 0)
7475 return StmtError();
7476
7477 assert((CurContext->isDependentContext() || B.builtAll()) &&
7478 "omp for loop exprs were not built");
7479
Alexey Bataev438388c2017-11-22 18:34:02 +00007480 if (!CurContext->isDependentContext()) {
7481 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007482 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00007483 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7484 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7485 B.NumIterations, *this, CurScope,
7486 DSAStack))
7487 return StmtError();
7488 }
7489 }
7490
Kelvin Lic5609492016-07-15 04:39:07 +00007491 if (checkSimdlenSafelenSpecified(*this, Clauses))
7492 return StmtError();
7493
Reid Kleckner87a31802018-03-12 21:43:02 +00007494 setFunctionHasBranchProtectedScope();
Kelvin Li4a39add2016-07-05 05:00:15 +00007495 return OMPDistributeParallelForSimdDirective::Create(
7496 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7497}
7498
Kelvin Li787f3fc2016-07-06 04:45:38 +00007499StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
7500 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007501 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li787f3fc2016-07-06 04:45:38 +00007502 if (!AStmt)
7503 return StmtError();
7504
Alexey Bataeve3727102018-04-18 15:57:46 +00007505 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li787f3fc2016-07-06 04:45:38 +00007506 // 1.2.2 OpenMP Language Terminology
7507 // Structured block - An executable statement with a single entry at the
7508 // top and a single exit at the bottom.
7509 // The point of exit cannot be a branch out of the structured block.
7510 // longjmp() and throw() must not violate the entry/exit criteria.
7511 CS->getCapturedDecl()->setNothrow();
Alexey Bataev617db5f2017-12-04 15:38:33 +00007512 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd);
7513 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7514 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7515 // 1.2.2 OpenMP Language Terminology
7516 // Structured block - An executable statement with a single entry at the
7517 // top and a single exit at the bottom.
7518 // The point of exit cannot be a branch out of the structured block.
7519 // longjmp() and throw() must not violate the entry/exit criteria.
7520 CS->getCapturedDecl()->setNothrow();
7521 }
Kelvin Li787f3fc2016-07-06 04:45:38 +00007522
7523 OMPLoopDirective::HelperExprs B;
7524 // In presence of clause 'collapse' with number of loops, it will
7525 // define the nested loops number.
7526 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007527 checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev617db5f2017-12-04 15:38:33 +00007528 nullptr /*ordered not a clause on distribute*/, CS, *this,
7529 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li787f3fc2016-07-06 04:45:38 +00007530 if (NestedLoopCount == 0)
7531 return StmtError();
7532
7533 assert((CurContext->isDependentContext() || B.builtAll()) &&
7534 "omp for loop exprs were not built");
7535
Alexey Bataev438388c2017-11-22 18:34:02 +00007536 if (!CurContext->isDependentContext()) {
7537 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007538 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00007539 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7540 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7541 B.NumIterations, *this, CurScope,
7542 DSAStack))
7543 return StmtError();
7544 }
7545 }
7546
Kelvin Lic5609492016-07-15 04:39:07 +00007547 if (checkSimdlenSafelenSpecified(*this, Clauses))
7548 return StmtError();
7549
Reid Kleckner87a31802018-03-12 21:43:02 +00007550 setFunctionHasBranchProtectedScope();
Kelvin Li787f3fc2016-07-06 04:45:38 +00007551 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
7552 NestedLoopCount, Clauses, AStmt, B);
7553}
7554
Kelvin Lia579b912016-07-14 02:54:56 +00007555StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
7556 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007557 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Lia579b912016-07-14 02:54:56 +00007558 if (!AStmt)
7559 return StmtError();
7560
Alexey Bataeve3727102018-04-18 15:57:46 +00007561 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Lia579b912016-07-14 02:54:56 +00007562 // 1.2.2 OpenMP Language Terminology
7563 // Structured block - An executable statement with a single entry at the
7564 // top and a single exit at the bottom.
7565 // The point of exit cannot be a branch out of the structured block.
7566 // longjmp() and throw() must not violate the entry/exit criteria.
7567 CS->getCapturedDecl()->setNothrow();
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007568 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
7569 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7570 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7571 // 1.2.2 OpenMP Language Terminology
7572 // Structured block - An executable statement with a single entry at the
7573 // top and a single exit at the bottom.
7574 // The point of exit cannot be a branch out of the structured block.
7575 // longjmp() and throw() must not violate the entry/exit criteria.
7576 CS->getCapturedDecl()->setNothrow();
7577 }
Kelvin Lia579b912016-07-14 02:54:56 +00007578
7579 OMPLoopDirective::HelperExprs B;
7580 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7581 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007582 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Lia579b912016-07-14 02:54:56 +00007583 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007584 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Kelvin Lia579b912016-07-14 02:54:56 +00007585 VarsWithImplicitDSA, B);
7586 if (NestedLoopCount == 0)
7587 return StmtError();
7588
7589 assert((CurContext->isDependentContext() || B.builtAll()) &&
7590 "omp target parallel for simd loop exprs were not built");
7591
7592 if (!CurContext->isDependentContext()) {
7593 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007594 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007595 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Lia579b912016-07-14 02:54:56 +00007596 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7597 B.NumIterations, *this, CurScope,
7598 DSAStack))
7599 return StmtError();
7600 }
7601 }
Kelvin Lic5609492016-07-15 04:39:07 +00007602 if (checkSimdlenSafelenSpecified(*this, Clauses))
Kelvin Lia579b912016-07-14 02:54:56 +00007603 return StmtError();
7604
Reid Kleckner87a31802018-03-12 21:43:02 +00007605 setFunctionHasBranchProtectedScope();
Kelvin Lia579b912016-07-14 02:54:56 +00007606 return OMPTargetParallelForSimdDirective::Create(
7607 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7608}
7609
Kelvin Li986330c2016-07-20 22:57:10 +00007610StmtResult Sema::ActOnOpenMPTargetSimdDirective(
7611 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007612 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li986330c2016-07-20 22:57:10 +00007613 if (!AStmt)
7614 return StmtError();
7615
Alexey Bataeve3727102018-04-18 15:57:46 +00007616 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li986330c2016-07-20 22:57:10 +00007617 // 1.2.2 OpenMP Language Terminology
7618 // Structured block - An executable statement with a single entry at the
7619 // top and a single exit at the bottom.
7620 // The point of exit cannot be a branch out of the structured block.
7621 // longjmp() and throw() must not violate the entry/exit criteria.
7622 CS->getCapturedDecl()->setNothrow();
Alexey Bataevf8365372017-11-17 17:57:25 +00007623 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd);
7624 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7625 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7626 // 1.2.2 OpenMP Language Terminology
7627 // Structured block - An executable statement with a single entry at the
7628 // top and a single exit at the bottom.
7629 // The point of exit cannot be a branch out of the structured block.
7630 // longjmp() and throw() must not violate the entry/exit criteria.
7631 CS->getCapturedDecl()->setNothrow();
7632 }
7633
Kelvin Li986330c2016-07-20 22:57:10 +00007634 OMPLoopDirective::HelperExprs B;
7635 // In presence of clause 'collapse' with number of loops, it will define the
7636 // nested loops number.
David Majnemer9d168222016-08-05 17:44:54 +00007637 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007638 checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
Alexey Bataevf8365372017-11-17 17:57:25 +00007639 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Kelvin Li986330c2016-07-20 22:57:10 +00007640 VarsWithImplicitDSA, B);
7641 if (NestedLoopCount == 0)
7642 return StmtError();
7643
7644 assert((CurContext->isDependentContext() || B.builtAll()) &&
7645 "omp target simd loop exprs were not built");
7646
7647 if (!CurContext->isDependentContext()) {
7648 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007649 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007650 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Li986330c2016-07-20 22:57:10 +00007651 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7652 B.NumIterations, *this, CurScope,
7653 DSAStack))
7654 return StmtError();
7655 }
7656 }
7657
7658 if (checkSimdlenSafelenSpecified(*this, Clauses))
7659 return StmtError();
7660
Reid Kleckner87a31802018-03-12 21:43:02 +00007661 setFunctionHasBranchProtectedScope();
Kelvin Li986330c2016-07-20 22:57:10 +00007662 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
7663 NestedLoopCount, Clauses, AStmt, B);
7664}
7665
Kelvin Li02532872016-08-05 14:37:37 +00007666StmtResult Sema::ActOnOpenMPTeamsDistributeDirective(
7667 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007668 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li02532872016-08-05 14:37:37 +00007669 if (!AStmt)
7670 return StmtError();
7671
Alexey Bataeve3727102018-04-18 15:57:46 +00007672 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li02532872016-08-05 14:37:37 +00007673 // 1.2.2 OpenMP Language Terminology
7674 // Structured block - An executable statement with a single entry at the
7675 // top and a single exit at the bottom.
7676 // The point of exit cannot be a branch out of the structured block.
7677 // longjmp() and throw() must not violate the entry/exit criteria.
7678 CS->getCapturedDecl()->setNothrow();
Alexey Bataev95c6dd42017-11-29 15:14:16 +00007679 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute);
7680 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7681 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7682 // 1.2.2 OpenMP Language Terminology
7683 // Structured block - An executable statement with a single entry at the
7684 // top and a single exit at the bottom.
7685 // The point of exit cannot be a branch out of the structured block.
7686 // longjmp() and throw() must not violate the entry/exit criteria.
7687 CS->getCapturedDecl()->setNothrow();
7688 }
Kelvin Li02532872016-08-05 14:37:37 +00007689
7690 OMPLoopDirective::HelperExprs B;
7691 // In presence of clause 'collapse' with number of loops, it will
7692 // define the nested loops number.
7693 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007694 checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses),
Alexey Bataev95c6dd42017-11-29 15:14:16 +00007695 nullptr /*ordered not a clause on distribute*/, CS, *this,
7696 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li02532872016-08-05 14:37:37 +00007697 if (NestedLoopCount == 0)
7698 return StmtError();
7699
7700 assert((CurContext->isDependentContext() || B.builtAll()) &&
7701 "omp teams distribute loop exprs were not built");
7702
Reid Kleckner87a31802018-03-12 21:43:02 +00007703 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007704
7705 DSAStack->setParentTeamsRegionLoc(StartLoc);
7706
David Majnemer9d168222016-08-05 17:44:54 +00007707 return OMPTeamsDistributeDirective::Create(
7708 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Kelvin Li02532872016-08-05 14:37:37 +00007709}
7710
Kelvin Li4e325f72016-10-25 12:50:55 +00007711StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective(
7712 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007713 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li4e325f72016-10-25 12:50:55 +00007714 if (!AStmt)
7715 return StmtError();
7716
Alexey Bataeve3727102018-04-18 15:57:46 +00007717 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li4e325f72016-10-25 12:50:55 +00007718 // 1.2.2 OpenMP Language Terminology
7719 // Structured block - An executable statement with a single entry at the
7720 // top and a single exit at the bottom.
7721 // The point of exit cannot be a branch out of the structured block.
7722 // longjmp() and throw() must not violate the entry/exit criteria.
7723 CS->getCapturedDecl()->setNothrow();
Alexey Bataev999277a2017-12-06 14:31:09 +00007724 for (int ThisCaptureLevel =
7725 getOpenMPCaptureLevels(OMPD_teams_distribute_simd);
7726 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7727 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7728 // 1.2.2 OpenMP Language Terminology
7729 // Structured block - An executable statement with a single entry at the
7730 // top and a single exit at the bottom.
7731 // The point of exit cannot be a branch out of the structured block.
7732 // longjmp() and throw() must not violate the entry/exit criteria.
7733 CS->getCapturedDecl()->setNothrow();
7734 }
7735
Kelvin Li4e325f72016-10-25 12:50:55 +00007736
7737 OMPLoopDirective::HelperExprs B;
7738 // In presence of clause 'collapse' with number of loops, it will
7739 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007740 unsigned NestedLoopCount = checkOpenMPLoop(
Samuel Antao4c8035b2016-12-12 18:00:20 +00007741 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev999277a2017-12-06 14:31:09 +00007742 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Samuel Antao4c8035b2016-12-12 18:00:20 +00007743 VarsWithImplicitDSA, B);
Kelvin Li4e325f72016-10-25 12:50:55 +00007744
7745 if (NestedLoopCount == 0)
7746 return StmtError();
7747
7748 assert((CurContext->isDependentContext() || B.builtAll()) &&
7749 "omp teams distribute simd loop exprs were not built");
7750
7751 if (!CurContext->isDependentContext()) {
7752 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007753 for (OMPClause *C : Clauses) {
Kelvin Li4e325f72016-10-25 12:50:55 +00007754 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7755 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7756 B.NumIterations, *this, CurScope,
7757 DSAStack))
7758 return StmtError();
7759 }
7760 }
7761
7762 if (checkSimdlenSafelenSpecified(*this, Clauses))
7763 return StmtError();
7764
Reid Kleckner87a31802018-03-12 21:43:02 +00007765 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007766
7767 DSAStack->setParentTeamsRegionLoc(StartLoc);
7768
Kelvin Li4e325f72016-10-25 12:50:55 +00007769 return OMPTeamsDistributeSimdDirective::Create(
7770 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7771}
7772
Kelvin Li579e41c2016-11-30 23:51:03 +00007773StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective(
7774 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007775 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li579e41c2016-11-30 23:51:03 +00007776 if (!AStmt)
7777 return StmtError();
7778
Alexey Bataeve3727102018-04-18 15:57:46 +00007779 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li579e41c2016-11-30 23:51:03 +00007780 // 1.2.2 OpenMP Language Terminology
7781 // Structured block - An executable statement with a single entry at the
7782 // top and a single exit at the bottom.
7783 // The point of exit cannot be a branch out of the structured block.
7784 // longjmp() and throw() must not violate the entry/exit criteria.
7785 CS->getCapturedDecl()->setNothrow();
7786
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00007787 for (int ThisCaptureLevel =
7788 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd);
7789 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7790 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7791 // 1.2.2 OpenMP Language Terminology
7792 // Structured block - An executable statement with a single entry at the
7793 // top and a single exit at the bottom.
7794 // The point of exit cannot be a branch out of the structured block.
7795 // longjmp() and throw() must not violate the entry/exit criteria.
7796 CS->getCapturedDecl()->setNothrow();
7797 }
7798
Kelvin Li579e41c2016-11-30 23:51:03 +00007799 OMPLoopDirective::HelperExprs B;
7800 // In presence of clause 'collapse' with number of loops, it will
7801 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007802 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li579e41c2016-11-30 23:51:03 +00007803 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00007804 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li579e41c2016-11-30 23:51:03 +00007805 VarsWithImplicitDSA, B);
7806
7807 if (NestedLoopCount == 0)
7808 return StmtError();
7809
7810 assert((CurContext->isDependentContext() || B.builtAll()) &&
7811 "omp for loop exprs were not built");
7812
7813 if (!CurContext->isDependentContext()) {
7814 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007815 for (OMPClause *C : Clauses) {
Kelvin Li579e41c2016-11-30 23:51:03 +00007816 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7817 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7818 B.NumIterations, *this, CurScope,
7819 DSAStack))
7820 return StmtError();
7821 }
7822 }
7823
7824 if (checkSimdlenSafelenSpecified(*this, Clauses))
7825 return StmtError();
7826
Reid Kleckner87a31802018-03-12 21:43:02 +00007827 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007828
7829 DSAStack->setParentTeamsRegionLoc(StartLoc);
7830
Kelvin Li579e41c2016-11-30 23:51:03 +00007831 return OMPTeamsDistributeParallelForSimdDirective::Create(
7832 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7833}
7834
Kelvin Li7ade93f2016-12-09 03:24:30 +00007835StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective(
7836 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007837 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li7ade93f2016-12-09 03:24:30 +00007838 if (!AStmt)
7839 return StmtError();
7840
Alexey Bataeve3727102018-04-18 15:57:46 +00007841 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li7ade93f2016-12-09 03:24:30 +00007842 // 1.2.2 OpenMP Language Terminology
7843 // Structured block - An executable statement with a single entry at the
7844 // top and a single exit at the bottom.
7845 // The point of exit cannot be a branch out of the structured block.
7846 // longjmp() and throw() must not violate the entry/exit criteria.
7847 CS->getCapturedDecl()->setNothrow();
7848
Carlo Bertolli62fae152017-11-20 20:46:39 +00007849 for (int ThisCaptureLevel =
7850 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for);
7851 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7852 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7853 // 1.2.2 OpenMP Language Terminology
7854 // Structured block - An executable statement with a single entry at the
7855 // top and a single exit at the bottom.
7856 // The point of exit cannot be a branch out of the structured block.
7857 // longjmp() and throw() must not violate the entry/exit criteria.
7858 CS->getCapturedDecl()->setNothrow();
7859 }
7860
Kelvin Li7ade93f2016-12-09 03:24:30 +00007861 OMPLoopDirective::HelperExprs B;
7862 // In presence of clause 'collapse' with number of loops, it will
7863 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007864 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li7ade93f2016-12-09 03:24:30 +00007865 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
Carlo Bertolli62fae152017-11-20 20:46:39 +00007866 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li7ade93f2016-12-09 03:24:30 +00007867 VarsWithImplicitDSA, B);
7868
7869 if (NestedLoopCount == 0)
7870 return StmtError();
7871
7872 assert((CurContext->isDependentContext() || B.builtAll()) &&
7873 "omp for loop exprs were not built");
7874
Reid Kleckner87a31802018-03-12 21:43:02 +00007875 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007876
7877 DSAStack->setParentTeamsRegionLoc(StartLoc);
7878
Kelvin Li7ade93f2016-12-09 03:24:30 +00007879 return OMPTeamsDistributeParallelForDirective::Create(
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00007880 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7881 DSAStack->isCancelRegion());
Kelvin Li7ade93f2016-12-09 03:24:30 +00007882}
7883
Kelvin Libf594a52016-12-17 05:48:59 +00007884StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses,
7885 Stmt *AStmt,
7886 SourceLocation StartLoc,
7887 SourceLocation EndLoc) {
7888 if (!AStmt)
7889 return StmtError();
7890
Alexey Bataeve3727102018-04-18 15:57:46 +00007891 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Libf594a52016-12-17 05:48:59 +00007892 // 1.2.2 OpenMP Language Terminology
7893 // Structured block - An executable statement with a single entry at the
7894 // top and a single exit at the bottom.
7895 // The point of exit cannot be a branch out of the structured block.
7896 // longjmp() and throw() must not violate the entry/exit criteria.
7897 CS->getCapturedDecl()->setNothrow();
7898
Alexey Bataevf9fc42e2017-11-22 14:25:55 +00007899 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams);
7900 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7901 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7902 // 1.2.2 OpenMP Language Terminology
7903 // Structured block - An executable statement with a single entry at the
7904 // top and a single exit at the bottom.
7905 // The point of exit cannot be a branch out of the structured block.
7906 // longjmp() and throw() must not violate the entry/exit criteria.
7907 CS->getCapturedDecl()->setNothrow();
7908 }
Reid Kleckner87a31802018-03-12 21:43:02 +00007909 setFunctionHasBranchProtectedScope();
Kelvin Libf594a52016-12-17 05:48:59 +00007910
7911 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses,
7912 AStmt);
7913}
7914
Kelvin Li83c451e2016-12-25 04:52:54 +00007915StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective(
7916 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007917 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li83c451e2016-12-25 04:52:54 +00007918 if (!AStmt)
7919 return StmtError();
7920
Alexey Bataeve3727102018-04-18 15:57:46 +00007921 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li83c451e2016-12-25 04:52:54 +00007922 // 1.2.2 OpenMP Language Terminology
7923 // Structured block - An executable statement with a single entry at the
7924 // top and a single exit at the bottom.
7925 // The point of exit cannot be a branch out of the structured block.
7926 // longjmp() and throw() must not violate the entry/exit criteria.
7927 CS->getCapturedDecl()->setNothrow();
Alexey Bataevdfa430f2017-12-08 15:03:50 +00007928 for (int ThisCaptureLevel =
7929 getOpenMPCaptureLevels(OMPD_target_teams_distribute);
7930 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7931 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7932 // 1.2.2 OpenMP Language Terminology
7933 // Structured block - An executable statement with a single entry at the
7934 // top and a single exit at the bottom.
7935 // The point of exit cannot be a branch out of the structured block.
7936 // longjmp() and throw() must not violate the entry/exit criteria.
7937 CS->getCapturedDecl()->setNothrow();
7938 }
Kelvin Li83c451e2016-12-25 04:52:54 +00007939
7940 OMPLoopDirective::HelperExprs B;
7941 // In presence of clause 'collapse' with number of loops, it will
7942 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007943 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataevdfa430f2017-12-08 15:03:50 +00007944 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses),
7945 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li83c451e2016-12-25 04:52:54 +00007946 VarsWithImplicitDSA, B);
7947 if (NestedLoopCount == 0)
7948 return StmtError();
7949
7950 assert((CurContext->isDependentContext() || B.builtAll()) &&
7951 "omp target teams distribute loop exprs were not built");
7952
Reid Kleckner87a31802018-03-12 21:43:02 +00007953 setFunctionHasBranchProtectedScope();
Kelvin Li83c451e2016-12-25 04:52:54 +00007954 return OMPTargetTeamsDistributeDirective::Create(
7955 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7956}
7957
Kelvin Li80e8f562016-12-29 22:16:30 +00007958StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective(
7959 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007960 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li80e8f562016-12-29 22:16:30 +00007961 if (!AStmt)
7962 return StmtError();
7963
Alexey Bataeve3727102018-04-18 15:57:46 +00007964 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li80e8f562016-12-29 22:16:30 +00007965 // 1.2.2 OpenMP Language Terminology
7966 // Structured block - An executable statement with a single entry at the
7967 // top and a single exit at the bottom.
7968 // The point of exit cannot be a branch out of the structured block.
7969 // longjmp() and throw() must not violate the entry/exit criteria.
7970 CS->getCapturedDecl()->setNothrow();
Carlo Bertolli52978c32018-01-03 21:12:44 +00007971 for (int ThisCaptureLevel =
7972 getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for);
7973 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7974 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7975 // 1.2.2 OpenMP Language Terminology
7976 // Structured block - An executable statement with a single entry at the
7977 // top and a single exit at the bottom.
7978 // The point of exit cannot be a branch out of the structured block.
7979 // longjmp() and throw() must not violate the entry/exit criteria.
7980 CS->getCapturedDecl()->setNothrow();
7981 }
7982
Kelvin Li80e8f562016-12-29 22:16:30 +00007983 OMPLoopDirective::HelperExprs B;
7984 // In presence of clause 'collapse' with number of loops, it will
7985 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007986 unsigned NestedLoopCount = checkOpenMPLoop(
Carlo Bertolli52978c32018-01-03 21:12:44 +00007987 OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
7988 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li80e8f562016-12-29 22:16:30 +00007989 VarsWithImplicitDSA, B);
7990 if (NestedLoopCount == 0)
7991 return StmtError();
7992
7993 assert((CurContext->isDependentContext() || B.builtAll()) &&
7994 "omp target teams distribute parallel for loop exprs were not built");
7995
Alexey Bataev647dd842018-01-15 20:59:40 +00007996 if (!CurContext->isDependentContext()) {
7997 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007998 for (OMPClause *C : Clauses) {
Alexey Bataev647dd842018-01-15 20:59:40 +00007999 if (auto *LC = dyn_cast<OMPLinearClause>(C))
8000 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8001 B.NumIterations, *this, CurScope,
8002 DSAStack))
8003 return StmtError();
8004 }
8005 }
8006
Reid Kleckner87a31802018-03-12 21:43:02 +00008007 setFunctionHasBranchProtectedScope();
Kelvin Li80e8f562016-12-29 22:16:30 +00008008 return OMPTargetTeamsDistributeParallelForDirective::Create(
Alexey Bataev16e79882017-11-22 21:12:03 +00008009 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
8010 DSAStack->isCancelRegion());
Kelvin Li80e8f562016-12-29 22:16:30 +00008011}
8012
Kelvin Li1851df52017-01-03 05:23:48 +00008013StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
8014 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00008015 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li1851df52017-01-03 05:23:48 +00008016 if (!AStmt)
8017 return StmtError();
8018
Alexey Bataeve3727102018-04-18 15:57:46 +00008019 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li1851df52017-01-03 05:23:48 +00008020 // 1.2.2 OpenMP Language Terminology
8021 // Structured block - An executable statement with a single entry at the
8022 // top and a single exit at the bottom.
8023 // The point of exit cannot be a branch out of the structured block.
8024 // longjmp() and throw() must not violate the entry/exit criteria.
8025 CS->getCapturedDecl()->setNothrow();
Alexey Bataev647dd842018-01-15 20:59:40 +00008026 for (int ThisCaptureLevel = getOpenMPCaptureLevels(
8027 OMPD_target_teams_distribute_parallel_for_simd);
8028 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8029 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8030 // 1.2.2 OpenMP Language Terminology
8031 // Structured block - An executable statement with a single entry at the
8032 // top and a single exit at the bottom.
8033 // The point of exit cannot be a branch out of the structured block.
8034 // longjmp() and throw() must not violate the entry/exit criteria.
8035 CS->getCapturedDecl()->setNothrow();
8036 }
Kelvin Li1851df52017-01-03 05:23:48 +00008037
8038 OMPLoopDirective::HelperExprs B;
8039 // In presence of clause 'collapse' with number of loops, it will
8040 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00008041 unsigned NestedLoopCount =
8042 checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd,
Alexey Bataev647dd842018-01-15 20:59:40 +00008043 getCollapseNumberExpr(Clauses),
8044 nullptr /*ordered not a clause on distribute*/, CS, *this,
8045 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li1851df52017-01-03 05:23:48 +00008046 if (NestedLoopCount == 0)
8047 return StmtError();
8048
8049 assert((CurContext->isDependentContext() || B.builtAll()) &&
8050 "omp target teams distribute parallel for simd loop exprs were not "
8051 "built");
8052
8053 if (!CurContext->isDependentContext()) {
8054 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00008055 for (OMPClause *C : Clauses) {
Kelvin Li1851df52017-01-03 05:23:48 +00008056 if (auto *LC = dyn_cast<OMPLinearClause>(C))
8057 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8058 B.NumIterations, *this, CurScope,
8059 DSAStack))
8060 return StmtError();
8061 }
8062 }
8063
Alexey Bataev438388c2017-11-22 18:34:02 +00008064 if (checkSimdlenSafelenSpecified(*this, Clauses))
8065 return StmtError();
8066
Reid Kleckner87a31802018-03-12 21:43:02 +00008067 setFunctionHasBranchProtectedScope();
Kelvin Li1851df52017-01-03 05:23:48 +00008068 return OMPTargetTeamsDistributeParallelForSimdDirective::Create(
8069 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
8070}
8071
Kelvin Lida681182017-01-10 18:08:18 +00008072StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective(
8073 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00008074 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Lida681182017-01-10 18:08:18 +00008075 if (!AStmt)
8076 return StmtError();
8077
8078 auto *CS = cast<CapturedStmt>(AStmt);
8079 // 1.2.2 OpenMP Language Terminology
8080 // Structured block - An executable statement with a single entry at the
8081 // top and a single exit at the bottom.
8082 // The point of exit cannot be a branch out of the structured block.
8083 // longjmp() and throw() must not violate the entry/exit criteria.
8084 CS->getCapturedDecl()->setNothrow();
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00008085 for (int ThisCaptureLevel =
8086 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd);
8087 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8088 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8089 // 1.2.2 OpenMP Language Terminology
8090 // Structured block - An executable statement with a single entry at the
8091 // top and a single exit at the bottom.
8092 // The point of exit cannot be a branch out of the structured block.
8093 // longjmp() and throw() must not violate the entry/exit criteria.
8094 CS->getCapturedDecl()->setNothrow();
8095 }
Kelvin Lida681182017-01-10 18:08:18 +00008096
8097 OMPLoopDirective::HelperExprs B;
8098 // In presence of clause 'collapse' with number of loops, it will
8099 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00008100 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Lida681182017-01-10 18:08:18 +00008101 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00008102 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Lida681182017-01-10 18:08:18 +00008103 VarsWithImplicitDSA, B);
8104 if (NestedLoopCount == 0)
8105 return StmtError();
8106
8107 assert((CurContext->isDependentContext() || B.builtAll()) &&
8108 "omp target teams distribute simd loop exprs were not built");
8109
Alexey Bataev438388c2017-11-22 18:34:02 +00008110 if (!CurContext->isDependentContext()) {
8111 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00008112 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00008113 if (auto *LC = dyn_cast<OMPLinearClause>(C))
8114 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8115 B.NumIterations, *this, CurScope,
8116 DSAStack))
8117 return StmtError();
8118 }
8119 }
8120
8121 if (checkSimdlenSafelenSpecified(*this, Clauses))
8122 return StmtError();
8123
Reid Kleckner87a31802018-03-12 21:43:02 +00008124 setFunctionHasBranchProtectedScope();
Kelvin Lida681182017-01-10 18:08:18 +00008125 return OMPTargetTeamsDistributeSimdDirective::Create(
8126 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
8127}
8128
Alexey Bataeved09d242014-05-28 05:53:51 +00008129OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008130 SourceLocation StartLoc,
8131 SourceLocation LParenLoc,
8132 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008133 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008134 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00008135 case OMPC_final:
8136 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
8137 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00008138 case OMPC_num_threads:
8139 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
8140 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00008141 case OMPC_safelen:
8142 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
8143 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00008144 case OMPC_simdlen:
8145 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
8146 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00008147 case OMPC_collapse:
8148 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
8149 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00008150 case OMPC_ordered:
8151 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
8152 break;
Michael Wonge710d542015-08-07 16:16:36 +00008153 case OMPC_device:
8154 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
8155 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00008156 case OMPC_num_teams:
8157 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
8158 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008159 case OMPC_thread_limit:
8160 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
8161 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00008162 case OMPC_priority:
8163 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
8164 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008165 case OMPC_grainsize:
8166 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
8167 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00008168 case OMPC_num_tasks:
8169 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
8170 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00008171 case OMPC_hint:
8172 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
8173 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008174 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008175 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008176 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008177 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008178 case OMPC_private:
8179 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00008180 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008181 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00008182 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008183 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008184 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00008185 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008186 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008187 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008188 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00008189 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008190 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008191 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008192 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00008193 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008194 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008195 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008196 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008197 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008198 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008199 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00008200 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008201 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008202 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00008203 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008204 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008205 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008206 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008207 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00008208 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00008209 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00008210 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00008211 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00008212 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00008213 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00008214 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00008215 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00008216 case OMPC_atomic_default_mem_order:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008217 llvm_unreachable("Clause is not allowed.");
8218 }
8219 return Res;
8220}
8221
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008222// An OpenMP directive such as 'target parallel' has two captured regions:
8223// for the 'target' and 'parallel' respectively. This function returns
8224// the region in which to capture expressions associated with a clause.
8225// A return value of OMPD_unknown signifies that the expression should not
8226// be captured.
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008227static OpenMPDirectiveKind getOpenMPCaptureRegionForClause(
8228 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
8229 OpenMPDirectiveKind NameModifier = OMPD_unknown) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008230 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008231 switch (CKind) {
8232 case OMPC_if:
8233 switch (DKind) {
8234 case OMPD_target_parallel:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008235 case OMPD_target_parallel_for:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00008236 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008237 // If this clause applies to the nested 'parallel' region, capture within
8238 // the 'target' region, otherwise do not capture.
8239 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
8240 CaptureRegion = OMPD_target;
8241 break;
Carlo Bertolli52978c32018-01-03 21:12:44 +00008242 case OMPD_target_teams_distribute_parallel_for:
8243 case OMPD_target_teams_distribute_parallel_for_simd:
8244 // If this clause applies to the nested 'parallel' region, capture within
8245 // the 'teams' region, otherwise do not capture.
8246 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
8247 CaptureRegion = OMPD_teams;
8248 break;
Carlo Bertolli62fae152017-11-20 20:46:39 +00008249 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008250 case OMPD_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008251 CaptureRegion = OMPD_teams;
8252 break;
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008253 case OMPD_target_update:
Alexey Bataevfab20e42017-12-27 18:49:38 +00008254 case OMPD_target_enter_data:
8255 case OMPD_target_exit_data:
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008256 CaptureRegion = OMPD_task;
8257 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008258 case OMPD_cancel:
8259 case OMPD_parallel:
8260 case OMPD_parallel_sections:
8261 case OMPD_parallel_for:
8262 case OMPD_parallel_for_simd:
8263 case OMPD_target:
8264 case OMPD_target_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008265 case OMPD_target_teams:
8266 case OMPD_target_teams_distribute:
8267 case OMPD_target_teams_distribute_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008268 case OMPD_distribute_parallel_for:
8269 case OMPD_distribute_parallel_for_simd:
8270 case OMPD_task:
8271 case OMPD_taskloop:
8272 case OMPD_taskloop_simd:
8273 case OMPD_target_data:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008274 // Do not capture if-clause expressions.
8275 break;
8276 case OMPD_threadprivate:
8277 case OMPD_taskyield:
8278 case OMPD_barrier:
8279 case OMPD_taskwait:
8280 case OMPD_cancellation_point:
8281 case OMPD_flush:
8282 case OMPD_declare_reduction:
8283 case OMPD_declare_simd:
8284 case OMPD_declare_target:
8285 case OMPD_end_declare_target:
8286 case OMPD_teams:
8287 case OMPD_simd:
8288 case OMPD_for:
8289 case OMPD_for_simd:
8290 case OMPD_sections:
8291 case OMPD_section:
8292 case OMPD_single:
8293 case OMPD_master:
8294 case OMPD_critical:
8295 case OMPD_taskgroup:
8296 case OMPD_distribute:
8297 case OMPD_ordered:
8298 case OMPD_atomic:
8299 case OMPD_distribute_simd:
8300 case OMPD_teams_distribute:
8301 case OMPD_teams_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008302 case OMPD_requires:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008303 llvm_unreachable("Unexpected OpenMP directive with if-clause");
8304 case OMPD_unknown:
8305 llvm_unreachable("Unknown OpenMP directive");
8306 }
8307 break;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008308 case OMPC_num_threads:
8309 switch (DKind) {
8310 case OMPD_target_parallel:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008311 case OMPD_target_parallel_for:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00008312 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008313 CaptureRegion = OMPD_target;
8314 break;
Carlo Bertolli62fae152017-11-20 20:46:39 +00008315 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008316 case OMPD_teams_distribute_parallel_for_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008317 case OMPD_target_teams_distribute_parallel_for:
8318 case OMPD_target_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008319 CaptureRegion = OMPD_teams;
8320 break;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008321 case OMPD_parallel:
8322 case OMPD_parallel_sections:
8323 case OMPD_parallel_for:
8324 case OMPD_parallel_for_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008325 case OMPD_distribute_parallel_for:
8326 case OMPD_distribute_parallel_for_simd:
8327 // Do not capture num_threads-clause expressions.
8328 break;
8329 case OMPD_target_data:
8330 case OMPD_target_enter_data:
8331 case OMPD_target_exit_data:
8332 case OMPD_target_update:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008333 case OMPD_target:
8334 case OMPD_target_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008335 case OMPD_target_teams:
8336 case OMPD_target_teams_distribute:
8337 case OMPD_target_teams_distribute_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008338 case OMPD_cancel:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008339 case OMPD_task:
8340 case OMPD_taskloop:
8341 case OMPD_taskloop_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008342 case OMPD_threadprivate:
8343 case OMPD_taskyield:
8344 case OMPD_barrier:
8345 case OMPD_taskwait:
8346 case OMPD_cancellation_point:
8347 case OMPD_flush:
8348 case OMPD_declare_reduction:
8349 case OMPD_declare_simd:
8350 case OMPD_declare_target:
8351 case OMPD_end_declare_target:
8352 case OMPD_teams:
8353 case OMPD_simd:
8354 case OMPD_for:
8355 case OMPD_for_simd:
8356 case OMPD_sections:
8357 case OMPD_section:
8358 case OMPD_single:
8359 case OMPD_master:
8360 case OMPD_critical:
8361 case OMPD_taskgroup:
8362 case OMPD_distribute:
8363 case OMPD_ordered:
8364 case OMPD_atomic:
8365 case OMPD_distribute_simd:
8366 case OMPD_teams_distribute:
8367 case OMPD_teams_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008368 case OMPD_requires:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008369 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause");
8370 case OMPD_unknown:
8371 llvm_unreachable("Unknown OpenMP directive");
8372 }
8373 break;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008374 case OMPC_num_teams:
8375 switch (DKind) {
8376 case OMPD_target_teams:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008377 case OMPD_target_teams_distribute:
8378 case OMPD_target_teams_distribute_simd:
8379 case OMPD_target_teams_distribute_parallel_for:
8380 case OMPD_target_teams_distribute_parallel_for_simd:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008381 CaptureRegion = OMPD_target;
8382 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008383 case OMPD_teams_distribute_parallel_for:
8384 case OMPD_teams_distribute_parallel_for_simd:
8385 case OMPD_teams:
8386 case OMPD_teams_distribute:
8387 case OMPD_teams_distribute_simd:
8388 // Do not capture num_teams-clause expressions.
8389 break;
8390 case OMPD_distribute_parallel_for:
8391 case OMPD_distribute_parallel_for_simd:
8392 case OMPD_task:
8393 case OMPD_taskloop:
8394 case OMPD_taskloop_simd:
8395 case OMPD_target_data:
8396 case OMPD_target_enter_data:
8397 case OMPD_target_exit_data:
8398 case OMPD_target_update:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008399 case OMPD_cancel:
8400 case OMPD_parallel:
8401 case OMPD_parallel_sections:
8402 case OMPD_parallel_for:
8403 case OMPD_parallel_for_simd:
8404 case OMPD_target:
8405 case OMPD_target_simd:
8406 case OMPD_target_parallel:
8407 case OMPD_target_parallel_for:
8408 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008409 case OMPD_threadprivate:
8410 case OMPD_taskyield:
8411 case OMPD_barrier:
8412 case OMPD_taskwait:
8413 case OMPD_cancellation_point:
8414 case OMPD_flush:
8415 case OMPD_declare_reduction:
8416 case OMPD_declare_simd:
8417 case OMPD_declare_target:
8418 case OMPD_end_declare_target:
8419 case OMPD_simd:
8420 case OMPD_for:
8421 case OMPD_for_simd:
8422 case OMPD_sections:
8423 case OMPD_section:
8424 case OMPD_single:
8425 case OMPD_master:
8426 case OMPD_critical:
8427 case OMPD_taskgroup:
8428 case OMPD_distribute:
8429 case OMPD_ordered:
8430 case OMPD_atomic:
8431 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008432 case OMPD_requires:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008433 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
8434 case OMPD_unknown:
8435 llvm_unreachable("Unknown OpenMP directive");
8436 }
8437 break;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008438 case OMPC_thread_limit:
8439 switch (DKind) {
8440 case OMPD_target_teams:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008441 case OMPD_target_teams_distribute:
8442 case OMPD_target_teams_distribute_simd:
8443 case OMPD_target_teams_distribute_parallel_for:
8444 case OMPD_target_teams_distribute_parallel_for_simd:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008445 CaptureRegion = OMPD_target;
8446 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008447 case OMPD_teams_distribute_parallel_for:
8448 case OMPD_teams_distribute_parallel_for_simd:
8449 case OMPD_teams:
8450 case OMPD_teams_distribute:
8451 case OMPD_teams_distribute_simd:
8452 // Do not capture thread_limit-clause expressions.
8453 break;
8454 case OMPD_distribute_parallel_for:
8455 case OMPD_distribute_parallel_for_simd:
8456 case OMPD_task:
8457 case OMPD_taskloop:
8458 case OMPD_taskloop_simd:
8459 case OMPD_target_data:
8460 case OMPD_target_enter_data:
8461 case OMPD_target_exit_data:
8462 case OMPD_target_update:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008463 case OMPD_cancel:
8464 case OMPD_parallel:
8465 case OMPD_parallel_sections:
8466 case OMPD_parallel_for:
8467 case OMPD_parallel_for_simd:
8468 case OMPD_target:
8469 case OMPD_target_simd:
8470 case OMPD_target_parallel:
8471 case OMPD_target_parallel_for:
8472 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008473 case OMPD_threadprivate:
8474 case OMPD_taskyield:
8475 case OMPD_barrier:
8476 case OMPD_taskwait:
8477 case OMPD_cancellation_point:
8478 case OMPD_flush:
8479 case OMPD_declare_reduction:
8480 case OMPD_declare_simd:
8481 case OMPD_declare_target:
8482 case OMPD_end_declare_target:
8483 case OMPD_simd:
8484 case OMPD_for:
8485 case OMPD_for_simd:
8486 case OMPD_sections:
8487 case OMPD_section:
8488 case OMPD_single:
8489 case OMPD_master:
8490 case OMPD_critical:
8491 case OMPD_taskgroup:
8492 case OMPD_distribute:
8493 case OMPD_ordered:
8494 case OMPD_atomic:
8495 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008496 case OMPD_requires:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008497 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause");
8498 case OMPD_unknown:
8499 llvm_unreachable("Unknown OpenMP directive");
8500 }
8501 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008502 case OMPC_schedule:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008503 switch (DKind) {
Alexey Bataev2ba67042017-11-28 21:11:44 +00008504 case OMPD_parallel_for:
8505 case OMPD_parallel_for_simd:
Alexey Bataev7f96c372017-11-22 17:19:31 +00008506 case OMPD_distribute_parallel_for:
Alexey Bataev974acd62017-11-27 19:38:52 +00008507 case OMPD_distribute_parallel_for_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008508 case OMPD_teams_distribute_parallel_for:
8509 case OMPD_teams_distribute_parallel_for_simd:
8510 case OMPD_target_parallel_for:
8511 case OMPD_target_parallel_for_simd:
8512 case OMPD_target_teams_distribute_parallel_for:
8513 case OMPD_target_teams_distribute_parallel_for_simd:
Alexey Bataev7f96c372017-11-22 17:19:31 +00008514 CaptureRegion = OMPD_parallel;
8515 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008516 case OMPD_for:
8517 case OMPD_for_simd:
8518 // Do not capture schedule-clause expressions.
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008519 break;
8520 case OMPD_task:
8521 case OMPD_taskloop:
8522 case OMPD_taskloop_simd:
8523 case OMPD_target_data:
8524 case OMPD_target_enter_data:
8525 case OMPD_target_exit_data:
8526 case OMPD_target_update:
8527 case OMPD_teams:
8528 case OMPD_teams_distribute:
8529 case OMPD_teams_distribute_simd:
8530 case OMPD_target_teams_distribute:
8531 case OMPD_target_teams_distribute_simd:
8532 case OMPD_target:
8533 case OMPD_target_simd:
8534 case OMPD_target_parallel:
8535 case OMPD_cancel:
8536 case OMPD_parallel:
8537 case OMPD_parallel_sections:
8538 case OMPD_threadprivate:
8539 case OMPD_taskyield:
8540 case OMPD_barrier:
8541 case OMPD_taskwait:
8542 case OMPD_cancellation_point:
8543 case OMPD_flush:
8544 case OMPD_declare_reduction:
8545 case OMPD_declare_simd:
8546 case OMPD_declare_target:
8547 case OMPD_end_declare_target:
8548 case OMPD_simd:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008549 case OMPD_sections:
8550 case OMPD_section:
8551 case OMPD_single:
8552 case OMPD_master:
8553 case OMPD_critical:
8554 case OMPD_taskgroup:
8555 case OMPD_distribute:
8556 case OMPD_ordered:
8557 case OMPD_atomic:
8558 case OMPD_distribute_simd:
8559 case OMPD_target_teams:
Kelvin Li1408f912018-09-26 04:28:39 +00008560 case OMPD_requires:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008561 llvm_unreachable("Unexpected OpenMP directive with schedule clause");
8562 case OMPD_unknown:
8563 llvm_unreachable("Unknown OpenMP directive");
8564 }
8565 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008566 case OMPC_dist_schedule:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008567 switch (DKind) {
8568 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008569 case OMPD_teams_distribute_parallel_for_simd:
8570 case OMPD_teams_distribute:
8571 case OMPD_teams_distribute_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008572 case OMPD_target_teams_distribute_parallel_for:
8573 case OMPD_target_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008574 case OMPD_target_teams_distribute:
8575 case OMPD_target_teams_distribute_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008576 CaptureRegion = OMPD_teams;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008577 break;
8578 case OMPD_distribute_parallel_for:
8579 case OMPD_distribute_parallel_for_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008580 case OMPD_distribute:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008581 case OMPD_distribute_simd:
8582 // Do not capture thread_limit-clause expressions.
8583 break;
8584 case OMPD_parallel_for:
8585 case OMPD_parallel_for_simd:
8586 case OMPD_target_parallel_for_simd:
8587 case OMPD_target_parallel_for:
8588 case OMPD_task:
8589 case OMPD_taskloop:
8590 case OMPD_taskloop_simd:
8591 case OMPD_target_data:
8592 case OMPD_target_enter_data:
8593 case OMPD_target_exit_data:
8594 case OMPD_target_update:
8595 case OMPD_teams:
8596 case OMPD_target:
8597 case OMPD_target_simd:
8598 case OMPD_target_parallel:
8599 case OMPD_cancel:
8600 case OMPD_parallel:
8601 case OMPD_parallel_sections:
8602 case OMPD_threadprivate:
8603 case OMPD_taskyield:
8604 case OMPD_barrier:
8605 case OMPD_taskwait:
8606 case OMPD_cancellation_point:
8607 case OMPD_flush:
8608 case OMPD_declare_reduction:
8609 case OMPD_declare_simd:
8610 case OMPD_declare_target:
8611 case OMPD_end_declare_target:
8612 case OMPD_simd:
8613 case OMPD_for:
8614 case OMPD_for_simd:
8615 case OMPD_sections:
8616 case OMPD_section:
8617 case OMPD_single:
8618 case OMPD_master:
8619 case OMPD_critical:
8620 case OMPD_taskgroup:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008621 case OMPD_ordered:
8622 case OMPD_atomic:
8623 case OMPD_target_teams:
Kelvin Li1408f912018-09-26 04:28:39 +00008624 case OMPD_requires:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008625 llvm_unreachable("Unexpected OpenMP directive with schedule clause");
8626 case OMPD_unknown:
8627 llvm_unreachable("Unknown OpenMP directive");
8628 }
8629 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008630 case OMPC_device:
8631 switch (DKind) {
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008632 case OMPD_target_update:
Alexey Bataevfab20e42017-12-27 18:49:38 +00008633 case OMPD_target_enter_data:
8634 case OMPD_target_exit_data:
Alexey Bataev8451efa2018-01-15 19:06:12 +00008635 case OMPD_target:
Alexey Bataevf41c88f2018-01-16 15:05:16 +00008636 case OMPD_target_simd:
Alexey Bataev0c869ef2018-01-16 15:57:07 +00008637 case OMPD_target_teams:
Alexey Bataev54d5c7d2018-01-16 16:27:49 +00008638 case OMPD_target_parallel:
Alexey Bataev79df7562018-01-16 16:46:46 +00008639 case OMPD_target_teams_distribute:
Alexey Bataev8d16a432018-01-16 17:22:50 +00008640 case OMPD_target_teams_distribute_simd:
Alexey Bataev8ed895512018-01-16 17:41:04 +00008641 case OMPD_target_parallel_for:
Alexey Bataevd60d1ba2018-01-16 17:55:15 +00008642 case OMPD_target_parallel_for_simd:
Alexey Bataev9f9fb0b2018-01-16 19:02:33 +00008643 case OMPD_target_teams_distribute_parallel_for:
Alexey Bataev9350fc32018-01-16 19:18:24 +00008644 case OMPD_target_teams_distribute_parallel_for_simd:
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008645 CaptureRegion = OMPD_task;
8646 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008647 case OMPD_target_data:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008648 // Do not capture device-clause expressions.
8649 break;
8650 case OMPD_teams_distribute_parallel_for:
8651 case OMPD_teams_distribute_parallel_for_simd:
8652 case OMPD_teams:
8653 case OMPD_teams_distribute:
8654 case OMPD_teams_distribute_simd:
8655 case OMPD_distribute_parallel_for:
8656 case OMPD_distribute_parallel_for_simd:
8657 case OMPD_task:
8658 case OMPD_taskloop:
8659 case OMPD_taskloop_simd:
8660 case OMPD_cancel:
8661 case OMPD_parallel:
8662 case OMPD_parallel_sections:
8663 case OMPD_parallel_for:
8664 case OMPD_parallel_for_simd:
8665 case OMPD_threadprivate:
8666 case OMPD_taskyield:
8667 case OMPD_barrier:
8668 case OMPD_taskwait:
8669 case OMPD_cancellation_point:
8670 case OMPD_flush:
8671 case OMPD_declare_reduction:
8672 case OMPD_declare_simd:
8673 case OMPD_declare_target:
8674 case OMPD_end_declare_target:
8675 case OMPD_simd:
8676 case OMPD_for:
8677 case OMPD_for_simd:
8678 case OMPD_sections:
8679 case OMPD_section:
8680 case OMPD_single:
8681 case OMPD_master:
8682 case OMPD_critical:
8683 case OMPD_taskgroup:
8684 case OMPD_distribute:
8685 case OMPD_ordered:
8686 case OMPD_atomic:
8687 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008688 case OMPD_requires:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008689 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
8690 case OMPD_unknown:
8691 llvm_unreachable("Unknown OpenMP directive");
8692 }
8693 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008694 case OMPC_firstprivate:
8695 case OMPC_lastprivate:
8696 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008697 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008698 case OMPC_in_reduction:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008699 case OMPC_linear:
8700 case OMPC_default:
8701 case OMPC_proc_bind:
8702 case OMPC_final:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008703 case OMPC_safelen:
8704 case OMPC_simdlen:
8705 case OMPC_collapse:
8706 case OMPC_private:
8707 case OMPC_shared:
8708 case OMPC_aligned:
8709 case OMPC_copyin:
8710 case OMPC_copyprivate:
8711 case OMPC_ordered:
8712 case OMPC_nowait:
8713 case OMPC_untied:
8714 case OMPC_mergeable:
8715 case OMPC_threadprivate:
8716 case OMPC_flush:
8717 case OMPC_read:
8718 case OMPC_write:
8719 case OMPC_update:
8720 case OMPC_capture:
8721 case OMPC_seq_cst:
8722 case OMPC_depend:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008723 case OMPC_threads:
8724 case OMPC_simd:
8725 case OMPC_map:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008726 case OMPC_priority:
8727 case OMPC_grainsize:
8728 case OMPC_nogroup:
8729 case OMPC_num_tasks:
8730 case OMPC_hint:
8731 case OMPC_defaultmap:
8732 case OMPC_unknown:
8733 case OMPC_uniform:
8734 case OMPC_to:
8735 case OMPC_from:
8736 case OMPC_use_device_ptr:
8737 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00008738 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00008739 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00008740 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00008741 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00008742 case OMPC_atomic_default_mem_order:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008743 llvm_unreachable("Unexpected OpenMP clause.");
8744 }
8745 return CaptureRegion;
8746}
8747
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008748OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
8749 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008750 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008751 SourceLocation NameModifierLoc,
8752 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008753 SourceLocation EndLoc) {
8754 Expr *ValExpr = Condition;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008755 Stmt *HelperValStmt = nullptr;
8756 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008757 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
8758 !Condition->isInstantiationDependent() &&
8759 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00008760 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008761 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008762 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008763
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008764 ValExpr = Val.get();
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008765
8766 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
8767 CaptureRegion =
8768 getOpenMPCaptureRegionForClause(DKind, OMPC_if, NameModifier);
Alexey Bataev2ba67042017-11-28 21:11:44 +00008769 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008770 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00008771 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008772 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8773 HelperValStmt = buildPreInits(Context, Captures);
8774 }
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008775 }
8776
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008777 return new (Context)
8778 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
8779 LParenLoc, NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008780}
8781
Alexey Bataev3778b602014-07-17 07:32:53 +00008782OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
8783 SourceLocation StartLoc,
8784 SourceLocation LParenLoc,
8785 SourceLocation EndLoc) {
8786 Expr *ValExpr = Condition;
8787 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
8788 !Condition->isInstantiationDependent() &&
8789 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00008790 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataev3778b602014-07-17 07:32:53 +00008791 if (Val.isInvalid())
8792 return nullptr;
8793
Richard Smith03a4aa32016-06-23 19:02:52 +00008794 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataev3778b602014-07-17 07:32:53 +00008795 }
8796
8797 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8798}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00008799ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
8800 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00008801 if (!Op)
8802 return ExprError();
8803
8804 class IntConvertDiagnoser : public ICEConvertDiagnoser {
8805 public:
8806 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00008807 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00008808 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
8809 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008810 return S.Diag(Loc, diag::err_omp_not_integral) << T;
8811 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008812 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
8813 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008814 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
8815 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008816 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
8817 QualType T,
8818 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008819 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
8820 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008821 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
8822 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008823 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00008824 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00008825 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008826 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
8827 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008828 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
8829 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008830 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
8831 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008832 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00008833 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00008834 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008835 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
8836 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008837 llvm_unreachable("conversion functions are permitted");
8838 }
8839 } ConvertDiagnoser;
8840 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
8841}
8842
Alexey Bataeve3727102018-04-18 15:57:46 +00008843static bool isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00008844 OpenMPClauseKind CKind,
8845 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008846 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
8847 !ValExpr->isInstantiationDependent()) {
8848 SourceLocation Loc = ValExpr->getExprLoc();
8849 ExprResult Value =
8850 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
8851 if (Value.isInvalid())
8852 return false;
8853
8854 ValExpr = Value.get();
8855 // The expression must evaluate to a non-negative integer value.
8856 llvm::APSInt Result;
8857 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00008858 Result.isSigned() &&
8859 !((!StrictlyPositive && Result.isNonNegative()) ||
8860 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008861 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00008862 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
8863 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008864 return false;
8865 }
8866 }
8867 return true;
8868}
8869
Alexey Bataev568a8332014-03-06 06:15:19 +00008870OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
8871 SourceLocation StartLoc,
8872 SourceLocation LParenLoc,
8873 SourceLocation EndLoc) {
8874 Expr *ValExpr = NumThreads;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008875 Stmt *HelperValStmt = nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00008876
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008877 // OpenMP [2.5, Restrictions]
8878 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +00008879 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
Alexey Bataeva0569352015-12-01 10:17:31 +00008880 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008881 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00008882
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008883 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +00008884 OpenMPDirectiveKind CaptureRegion =
8885 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads);
8886 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008887 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00008888 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008889 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8890 HelperValStmt = buildPreInits(Context, Captures);
8891 }
8892
8893 return new (Context) OMPNumThreadsClause(
8894 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00008895}
8896
Alexey Bataev62c87d22014-03-21 04:51:18 +00008897ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008898 OpenMPClauseKind CKind,
8899 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00008900 if (!E)
8901 return ExprError();
8902 if (E->isValueDependent() || E->isTypeDependent() ||
8903 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00008904 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00008905 llvm::APSInt Result;
8906 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
8907 if (ICE.isInvalid())
8908 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008909 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
8910 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00008911 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008912 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
8913 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00008914 return ExprError();
8915 }
Alexander Musman09184fe2014-09-30 05:29:28 +00008916 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
8917 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
8918 << E->getSourceRange();
8919 return ExprError();
8920 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008921 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
8922 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00008923 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008924 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00008925 return ICE;
8926}
8927
8928OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
8929 SourceLocation LParenLoc,
8930 SourceLocation EndLoc) {
8931 // OpenMP [2.8.1, simd construct, Description]
8932 // The parameter of the safelen clause must be a constant
8933 // positive integer expression.
8934 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
8935 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008936 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00008937 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00008938 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00008939}
8940
Alexey Bataev66b15b52015-08-21 11:14:16 +00008941OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
8942 SourceLocation LParenLoc,
8943 SourceLocation EndLoc) {
8944 // OpenMP [2.8.1, simd construct, Description]
8945 // The parameter of the simdlen clause must be a constant
8946 // positive integer expression.
8947 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
8948 if (Simdlen.isInvalid())
8949 return nullptr;
8950 return new (Context)
8951 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
8952}
8953
Alexander Musman64d33f12014-06-04 07:53:32 +00008954OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
8955 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00008956 SourceLocation LParenLoc,
8957 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00008958 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00008959 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00008960 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00008961 // The parameter of the collapse clause must be a constant
8962 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00008963 ExprResult NumForLoopsResult =
8964 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
8965 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00008966 return nullptr;
8967 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00008968 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00008969}
8970
Alexey Bataev10e775f2015-07-30 11:36:16 +00008971OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
8972 SourceLocation EndLoc,
8973 SourceLocation LParenLoc,
8974 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00008975 // OpenMP [2.7.1, loop construct, Description]
8976 // OpenMP [2.8.1, simd construct, Description]
8977 // OpenMP [2.9.6, distribute construct, Description]
8978 // The parameter of the ordered clause must be a constant
8979 // positive integer expression if any.
8980 if (NumForLoops && LParenLoc.isValid()) {
8981 ExprResult NumForLoopsResult =
8982 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
8983 if (NumForLoopsResult.isInvalid())
8984 return nullptr;
8985 NumForLoops = NumForLoopsResult.get();
Alexey Bataeve3727102018-04-18 15:57:46 +00008986 } else {
Alexey Bataev346265e2015-09-25 10:37:12 +00008987 NumForLoops = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +00008988 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00008989 auto *Clause = OMPOrderedClause::Create(
8990 Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0,
8991 StartLoc, LParenLoc, EndLoc);
8992 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause);
8993 return Clause;
Alexey Bataev10e775f2015-07-30 11:36:16 +00008994}
8995
Alexey Bataeved09d242014-05-28 05:53:51 +00008996OMPClause *Sema::ActOnOpenMPSimpleClause(
8997 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
8998 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008999 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009000 switch (Kind) {
9001 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00009002 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00009003 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
9004 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009005 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009006 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00009007 Res = ActOnOpenMPProcBindClause(
9008 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
9009 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009010 break;
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009011 case OMPC_atomic_default_mem_order:
9012 Res = ActOnOpenMPAtomicDefaultMemOrderClause(
9013 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument),
9014 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
9015 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009016 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00009017 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00009018 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00009019 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009020 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00009021 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009022 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009023 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009024 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00009025 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00009026 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00009027 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00009028 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00009029 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00009030 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009031 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009032 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009033 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009034 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00009035 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009036 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009037 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009038 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00009039 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009040 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00009041 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00009042 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00009043 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009044 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009045 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00009046 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00009047 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009048 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00009049 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009050 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009051 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009052 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009053 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00009054 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00009055 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009056 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00009057 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009058 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009059 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009060 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00009061 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00009062 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00009063 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00009064 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00009065 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00009066 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009067 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009068 case OMPC_dynamic_allocators:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009069 llvm_unreachable("Clause is not allowed.");
9070 }
9071 return Res;
9072}
9073
Alexey Bataev6402bca2015-12-28 07:25:51 +00009074static std::string
9075getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
9076 ArrayRef<unsigned> Exclude = llvm::None) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009077 SmallString<256> Buffer;
9078 llvm::raw_svector_ostream Out(Buffer);
Alexey Bataev6402bca2015-12-28 07:25:51 +00009079 unsigned Bound = Last >= 2 ? Last - 2 : 0;
9080 unsigned Skipped = Exclude.size();
9081 auto S = Exclude.begin(), E = Exclude.end();
Alexey Bataeve3727102018-04-18 15:57:46 +00009082 for (unsigned I = First; I < Last; ++I) {
9083 if (std::find(S, E, I) != E) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00009084 --Skipped;
9085 continue;
9086 }
Alexey Bataeve3727102018-04-18 15:57:46 +00009087 Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'";
9088 if (I == Bound - Skipped)
9089 Out << " or ";
9090 else if (I != Bound + 1 - Skipped)
9091 Out << ", ";
Alexey Bataev6402bca2015-12-28 07:25:51 +00009092 }
Alexey Bataeve3727102018-04-18 15:57:46 +00009093 return Out.str();
Alexey Bataev6402bca2015-12-28 07:25:51 +00009094}
9095
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009096OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
9097 SourceLocation KindKwLoc,
9098 SourceLocation StartLoc,
9099 SourceLocation LParenLoc,
9100 SourceLocation EndLoc) {
9101 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00009102 static_assert(OMPC_DEFAULT_unknown > 0,
9103 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009104 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009105 << getListOfPossibleValues(OMPC_default, /*First=*/0,
9106 /*Last=*/OMPC_DEFAULT_unknown)
9107 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009108 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009109 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00009110 switch (Kind) {
9111 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009112 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009113 break;
9114 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009115 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009116 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009117 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009118 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00009119 break;
9120 }
Alexey Bataeved09d242014-05-28 05:53:51 +00009121 return new (Context)
9122 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009123}
9124
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009125OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
9126 SourceLocation KindKwLoc,
9127 SourceLocation StartLoc,
9128 SourceLocation LParenLoc,
9129 SourceLocation EndLoc) {
9130 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009131 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009132 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
9133 /*Last=*/OMPC_PROC_BIND_unknown)
9134 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009135 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009136 }
Alexey Bataeved09d242014-05-28 05:53:51 +00009137 return new (Context)
9138 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009139}
9140
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009141OMPClause *Sema::ActOnOpenMPAtomicDefaultMemOrderClause(
9142 OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc,
9143 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
9144 if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) {
9145 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
9146 << getListOfPossibleValues(
9147 OMPC_atomic_default_mem_order, /*First=*/0,
9148 /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown)
9149 << getOpenMPClauseName(OMPC_atomic_default_mem_order);
9150 return nullptr;
9151 }
9152 return new (Context) OMPAtomicDefaultMemOrderClause(Kind, KindKwLoc, StartLoc,
9153 LParenLoc, EndLoc);
9154}
9155
Alexey Bataev56dafe82014-06-20 07:16:17 +00009156OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00009157 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00009158 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00009159 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00009160 SourceLocation EndLoc) {
9161 OMPClause *Res = nullptr;
9162 switch (Kind) {
9163 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00009164 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
9165 assert(Argument.size() == NumberOfElements &&
9166 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009167 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00009168 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
9169 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
9170 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
9171 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
9172 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009173 break;
9174 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00009175 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
9176 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
9177 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
9178 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009179 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00009180 case OMPC_dist_schedule:
9181 Res = ActOnOpenMPDistScheduleClause(
9182 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
9183 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
9184 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009185 case OMPC_defaultmap:
9186 enum { Modifier, DefaultmapKind };
9187 Res = ActOnOpenMPDefaultmapClause(
9188 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
9189 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
David Majnemer9d168222016-08-05 17:44:54 +00009190 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind],
9191 EndLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009192 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00009193 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009194 case OMPC_num_threads:
9195 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009196 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009197 case OMPC_collapse:
9198 case OMPC_default:
9199 case OMPC_proc_bind:
9200 case OMPC_private:
9201 case OMPC_firstprivate:
9202 case OMPC_lastprivate:
9203 case OMPC_shared:
9204 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00009205 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00009206 case OMPC_in_reduction:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009207 case OMPC_linear:
9208 case OMPC_aligned:
9209 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009210 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009211 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00009212 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009213 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009214 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009215 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00009216 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009217 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00009218 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00009219 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00009220 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009221 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009222 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00009223 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00009224 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009225 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00009226 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009227 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009228 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009229 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009230 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00009231 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00009232 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009233 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009234 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009235 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00009236 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00009237 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00009238 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00009239 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00009240 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00009241 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009242 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009243 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009244 case OMPC_atomic_default_mem_order:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009245 llvm_unreachable("Clause is not allowed.");
9246 }
9247 return Res;
9248}
9249
Alexey Bataev6402bca2015-12-28 07:25:51 +00009250static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
9251 OpenMPScheduleClauseModifier M2,
9252 SourceLocation M1Loc, SourceLocation M2Loc) {
9253 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
9254 SmallVector<unsigned, 2> Excluded;
9255 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
9256 Excluded.push_back(M2);
9257 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
9258 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
9259 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
9260 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
9261 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
9262 << getListOfPossibleValues(OMPC_schedule,
9263 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
9264 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
9265 Excluded)
9266 << getOpenMPClauseName(OMPC_schedule);
9267 return true;
9268 }
9269 return false;
9270}
9271
Alexey Bataev56dafe82014-06-20 07:16:17 +00009272OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00009273 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00009274 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00009275 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
9276 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
9277 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
9278 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
9279 return nullptr;
9280 // OpenMP, 2.7.1, Loop Construct, Restrictions
9281 // Either the monotonic modifier or the nonmonotonic modifier can be specified
9282 // but not both.
9283 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
9284 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
9285 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
9286 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
9287 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
9288 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
9289 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
9290 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
9291 return nullptr;
9292 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00009293 if (Kind == OMPC_SCHEDULE_unknown) {
9294 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00009295 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
9296 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
9297 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
9298 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
9299 Exclude);
9300 } else {
9301 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
9302 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009303 }
9304 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
9305 << Values << getOpenMPClauseName(OMPC_schedule);
9306 return nullptr;
9307 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00009308 // OpenMP, 2.7.1, Loop Construct, Restrictions
9309 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
9310 // schedule(guided).
9311 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
9312 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
9313 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
9314 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
9315 diag::err_omp_schedule_nonmonotonic_static);
9316 return nullptr;
9317 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00009318 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00009319 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00009320 if (ChunkSize) {
9321 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
9322 !ChunkSize->isInstantiationDependent() &&
9323 !ChunkSize->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00009324 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
Alexey Bataev56dafe82014-06-20 07:16:17 +00009325 ExprResult Val =
9326 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
9327 if (Val.isInvalid())
9328 return nullptr;
9329
9330 ValExpr = Val.get();
9331
9332 // OpenMP [2.7.1, Restrictions]
9333 // chunk_size must be a loop invariant integer expression with a positive
9334 // value.
9335 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00009336 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
9337 if (Result.isSigned() && !Result.isStrictlyPositive()) {
9338 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00009339 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00009340 return nullptr;
9341 }
Alexey Bataev2ba67042017-11-28 21:11:44 +00009342 } else if (getOpenMPCaptureRegionForClause(
9343 DSAStack->getCurrentDirective(), OMPC_schedule) !=
9344 OMPD_unknown &&
Alexey Bataevb46cdea2016-06-15 11:20:48 +00009345 !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00009346 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00009347 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev5a3af132016-03-29 08:58:54 +00009348 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
9349 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009350 }
9351 }
9352 }
9353
Alexey Bataev6402bca2015-12-28 07:25:51 +00009354 return new (Context)
9355 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00009356 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009357}
9358
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009359OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
9360 SourceLocation StartLoc,
9361 SourceLocation EndLoc) {
9362 OMPClause *Res = nullptr;
9363 switch (Kind) {
9364 case OMPC_ordered:
9365 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
9366 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00009367 case OMPC_nowait:
9368 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
9369 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009370 case OMPC_untied:
9371 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
9372 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009373 case OMPC_mergeable:
9374 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
9375 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009376 case OMPC_read:
9377 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
9378 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00009379 case OMPC_write:
9380 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
9381 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00009382 case OMPC_update:
9383 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
9384 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00009385 case OMPC_capture:
9386 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
9387 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009388 case OMPC_seq_cst:
9389 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
9390 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00009391 case OMPC_threads:
9392 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
9393 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009394 case OMPC_simd:
9395 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
9396 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00009397 case OMPC_nogroup:
9398 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
9399 break;
Kelvin Li1408f912018-09-26 04:28:39 +00009400 case OMPC_unified_address:
9401 Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc);
9402 break;
Patrick Lyster4a370b92018-10-01 13:47:43 +00009403 case OMPC_unified_shared_memory:
9404 Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
9405 break;
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009406 case OMPC_reverse_offload:
9407 Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc);
9408 break;
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009409 case OMPC_dynamic_allocators:
9410 Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc);
9411 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009412 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00009413 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009414 case OMPC_num_threads:
9415 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009416 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009417 case OMPC_collapse:
9418 case OMPC_schedule:
9419 case OMPC_private:
9420 case OMPC_firstprivate:
9421 case OMPC_lastprivate:
9422 case OMPC_shared:
9423 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00009424 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00009425 case OMPC_in_reduction:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009426 case OMPC_linear:
9427 case OMPC_aligned:
9428 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009429 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009430 case OMPC_default:
9431 case OMPC_proc_bind:
9432 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00009433 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009434 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00009435 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00009436 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009437 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009438 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009439 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009440 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00009441 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009442 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00009443 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009444 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009445 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009446 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00009447 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00009448 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00009449 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00009450 case OMPC_is_device_ptr:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009451 case OMPC_atomic_default_mem_order:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009452 llvm_unreachable("Clause is not allowed.");
9453 }
9454 return Res;
9455}
9456
Alexey Bataev236070f2014-06-20 11:19:47 +00009457OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
9458 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00009459 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00009460 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
9461}
9462
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009463OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
9464 SourceLocation EndLoc) {
9465 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
9466}
9467
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009468OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
9469 SourceLocation EndLoc) {
9470 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
9471}
9472
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009473OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
9474 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009475 return new (Context) OMPReadClause(StartLoc, EndLoc);
9476}
9477
Alexey Bataevdea47612014-07-23 07:46:59 +00009478OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
9479 SourceLocation EndLoc) {
9480 return new (Context) OMPWriteClause(StartLoc, EndLoc);
9481}
9482
Alexey Bataev67a4f222014-07-23 10:25:33 +00009483OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
9484 SourceLocation EndLoc) {
9485 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
9486}
9487
Alexey Bataev459dec02014-07-24 06:46:57 +00009488OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
9489 SourceLocation EndLoc) {
9490 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
9491}
9492
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009493OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
9494 SourceLocation EndLoc) {
9495 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
9496}
9497
Alexey Bataev346265e2015-09-25 10:37:12 +00009498OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
9499 SourceLocation EndLoc) {
9500 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
9501}
9502
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009503OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
9504 SourceLocation EndLoc) {
9505 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
9506}
9507
Alexey Bataevb825de12015-12-07 10:51:44 +00009508OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
9509 SourceLocation EndLoc) {
9510 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
9511}
9512
Kelvin Li1408f912018-09-26 04:28:39 +00009513OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc,
9514 SourceLocation EndLoc) {
9515 return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc);
9516}
9517
Patrick Lyster4a370b92018-10-01 13:47:43 +00009518OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc,
9519 SourceLocation EndLoc) {
9520 return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
9521}
9522
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009523OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc,
9524 SourceLocation EndLoc) {
9525 return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc);
9526}
9527
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009528OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc,
9529 SourceLocation EndLoc) {
9530 return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc);
9531}
9532
Alexey Bataevc5e02582014-06-16 07:08:35 +00009533OMPClause *Sema::ActOnOpenMPVarListClause(
9534 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
9535 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
9536 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009537 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00009538 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
9539 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
9540 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009541 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009542 switch (Kind) {
9543 case OMPC_private:
9544 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9545 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009546 case OMPC_firstprivate:
9547 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9548 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00009549 case OMPC_lastprivate:
9550 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9551 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00009552 case OMPC_shared:
9553 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
9554 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009555 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00009556 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9557 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00009558 break;
Alexey Bataev169d96a2017-07-18 20:17:46 +00009559 case OMPC_task_reduction:
9560 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9561 EndLoc, ReductionIdScopeSpec,
9562 ReductionId);
9563 break;
Alexey Bataevfa312f32017-07-21 18:48:21 +00009564 case OMPC_in_reduction:
9565 Res =
9566 ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9567 EndLoc, ReductionIdScopeSpec, ReductionId);
9568 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00009569 case OMPC_linear:
9570 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00009571 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00009572 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009573 case OMPC_aligned:
9574 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
9575 ColonLoc, EndLoc);
9576 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009577 case OMPC_copyin:
9578 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
9579 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009580 case OMPC_copyprivate:
9581 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9582 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00009583 case OMPC_flush:
9584 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
9585 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009586 case OMPC_depend:
David Majnemer9d168222016-08-05 17:44:54 +00009587 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
Kelvin Li0bff7af2015-11-23 05:32:03 +00009588 StartLoc, LParenLoc, EndLoc);
9589 break;
9590 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00009591 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
9592 DepLinMapLoc, ColonLoc, VarList, StartLoc,
9593 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009594 break;
Samuel Antao661c0902016-05-26 17:39:58 +00009595 case OMPC_to:
9596 Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc);
9597 break;
Samuel Antaoec172c62016-05-26 17:49:04 +00009598 case OMPC_from:
9599 Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc);
9600 break;
Carlo Bertolli2404b172016-07-13 15:37:16 +00009601 case OMPC_use_device_ptr:
9602 Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
9603 break;
Carlo Bertolli70594e92016-07-13 17:16:49 +00009604 case OMPC_is_device_ptr:
9605 Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
9606 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009607 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00009608 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00009609 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00009610 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009611 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00009612 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009613 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009614 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009615 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009616 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00009617 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009618 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009619 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009620 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009621 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00009622 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00009623 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00009624 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009625 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00009626 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00009627 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009628 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009629 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009630 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009631 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009632 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00009633 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00009634 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009635 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00009636 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009637 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009638 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009639 case OMPC_uniform:
Kelvin Li1408f912018-09-26 04:28:39 +00009640 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00009641 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009642 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009643 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009644 case OMPC_atomic_default_mem_order:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009645 llvm_unreachable("Clause is not allowed.");
9646 }
9647 return Res;
9648}
9649
Alexey Bataev90c228f2016-02-08 09:29:13 +00009650ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00009651 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00009652 ExprResult Res = BuildDeclRefExpr(
9653 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
9654 if (!Res.isUsable())
9655 return ExprError();
9656 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
9657 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
9658 if (!Res.isUsable())
9659 return ExprError();
9660 }
9661 if (VK != VK_LValue && Res.get()->isGLValue()) {
9662 Res = DefaultLvalueConversion(Res.get());
9663 if (!Res.isUsable())
9664 return ExprError();
9665 }
9666 return Res;
9667}
9668
Alexey Bataev60da77e2016-02-29 05:54:20 +00009669static std::pair<ValueDecl *, bool>
9670getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
9671 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00009672 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
9673 RefExpr->containsUnexpandedParameterPack())
9674 return std::make_pair(nullptr, true);
9675
Alexey Bataevd985eda2016-02-10 11:29:16 +00009676 // OpenMP [3.1, C/C++]
9677 // A list item is a variable name.
9678 // OpenMP [2.9.3.3, Restrictions, p.1]
9679 // A variable that is part of another variable (as an array or
9680 // structure element) cannot appear in a private clause.
9681 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009682 enum {
9683 NoArrayExpr = -1,
9684 ArraySubscript = 0,
9685 OMPArraySection = 1
9686 } IsArrayExpr = NoArrayExpr;
9687 if (AllowArraySection) {
9688 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009689 Expr *Base = ASE->getBase()->IgnoreParenImpCasts();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009690 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
9691 Base = TempASE->getBase()->IgnoreParenImpCasts();
9692 RefExpr = Base;
9693 IsArrayExpr = ArraySubscript;
9694 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009695 Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009696 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
9697 Base = TempOASE->getBase()->IgnoreParenImpCasts();
9698 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
9699 Base = TempASE->getBase()->IgnoreParenImpCasts();
9700 RefExpr = Base;
9701 IsArrayExpr = OMPArraySection;
9702 }
9703 }
9704 ELoc = RefExpr->getExprLoc();
9705 ERange = RefExpr->getSourceRange();
9706 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00009707 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
9708 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
9709 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
9710 (S.getCurrentThisType().isNull() || !ME ||
9711 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
9712 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009713 if (IsArrayExpr != NoArrayExpr) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009714 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
9715 << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +00009716 } else {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009717 S.Diag(ELoc,
9718 AllowArraySection
9719 ? diag::err_omp_expected_var_name_member_expr_or_array_item
9720 : diag::err_omp_expected_var_name_member_expr)
9721 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
9722 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009723 return std::make_pair(nullptr, false);
9724 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +00009725 return std::make_pair(
9726 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009727}
9728
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009729OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
9730 SourceLocation StartLoc,
9731 SourceLocation LParenLoc,
9732 SourceLocation EndLoc) {
9733 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00009734 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeve3727102018-04-18 15:57:46 +00009735 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +00009736 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009737 SourceLocation ELoc;
9738 SourceRange ERange;
9739 Expr *SimpleRefExpr = RefExpr;
9740 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009741 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009742 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009743 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009744 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009745 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009746 ValueDecl *D = Res.first;
9747 if (!D)
9748 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009749
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009750 QualType Type = D->getType();
9751 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009752
9753 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9754 // A variable that appears in a private clause must not have an incomplete
9755 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009756 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009757 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009758 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009759
Alexey Bataev758e55e2013-09-06 18:03:48 +00009760 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9761 // in a Construct]
9762 // Variables with the predetermined data-sharing attributes may not be
9763 // listed in data-sharing attributes clauses, except for the cases
9764 // listed below. For these exceptions only, listing a predetermined
9765 // variable in a data-sharing attribute clause is allowed and overrides
9766 // the variable's predetermined data-sharing attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +00009767 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009768 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00009769 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9770 << getOpenMPClauseName(OMPC_private);
Alexey Bataeve3727102018-04-18 15:57:46 +00009771 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009772 continue;
9773 }
9774
Alexey Bataeve3727102018-04-18 15:57:46 +00009775 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009776 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00009777 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Kelvin Libf594a52016-12-17 05:48:59 +00009778 isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009779 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
9780 << getOpenMPClauseName(OMPC_private) << Type
Kelvin Libf594a52016-12-17 05:48:59 +00009781 << getOpenMPDirectiveName(CurrDir);
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009782 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009783 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009784 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009785 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009786 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009787 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009788 continue;
9789 }
9790
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009791 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
9792 // A list item cannot appear in both a map clause and a data-sharing
9793 // attribute clause on the same construct
Alexey Bataeve3727102018-04-18 15:57:46 +00009794 if (isOpenMPTargetExecutionDirective(CurrDir)) {
Samuel Antao6890b092016-07-28 14:25:09 +00009795 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +00009796 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +00009797 VD, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +00009798 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
9799 OpenMPClauseKind WhereFoundClauseKind) -> bool {
9800 ConflictKind = WhereFoundClauseKind;
9801 return true;
9802 })) {
9803 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009804 << getOpenMPClauseName(OMPC_private)
Samuel Antao6890b092016-07-28 14:25:09 +00009805 << getOpenMPClauseName(ConflictKind)
Kelvin Libf594a52016-12-17 05:48:59 +00009806 << getOpenMPDirectiveName(CurrDir);
Alexey Bataeve3727102018-04-18 15:57:46 +00009807 reportOriginalDsa(*this, DSAStack, D, DVar);
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009808 continue;
9809 }
9810 }
9811
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009812 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
9813 // A variable of class type (or array thereof) that appears in a private
9814 // clause requires an accessible, unambiguous default constructor for the
9815 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00009816 // Generate helper private variable and initialize it with the default
9817 // value. The address of the original variable is replaced by the address of
9818 // the new private variable in CodeGen. This new variable is not added to
9819 // IdResolver, so the code in the OpenMP region uses original variable for
9820 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009821 Type = Type.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +00009822 VarDecl *VDPrivate =
Alexey Bataev63cc8e92018-03-20 14:45:59 +00009823 buildVarDecl(*this, ELoc, Type, D->getName(),
9824 D->hasAttrs() ? &D->getAttrs() : nullptr,
9825 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Richard Smith3beb7c62017-01-12 02:27:38 +00009826 ActOnUninitializedDecl(VDPrivate);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009827 if (VDPrivate->isInvalidDecl())
9828 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00009829 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009830 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009831
Alexey Bataev90c228f2016-02-08 09:29:13 +00009832 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009833 if (!VD && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00009834 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00009835 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009836 Vars.push_back((VD || CurContext->isDependentContext())
9837 ? RefExpr->IgnoreParens()
9838 : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009839 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009840 }
9841
Alexey Bataeved09d242014-05-28 05:53:51 +00009842 if (Vars.empty())
9843 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009844
Alexey Bataev03b340a2014-10-21 03:16:40 +00009845 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
9846 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009847}
9848
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009849namespace {
9850class DiagsUninitializedSeveretyRAII {
9851private:
9852 DiagnosticsEngine &Diags;
9853 SourceLocation SavedLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00009854 bool IsIgnored = false;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009855
9856public:
9857 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
9858 bool IsIgnored)
9859 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
9860 if (!IsIgnored) {
9861 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
9862 /*Map*/ diag::Severity::Ignored, Loc);
9863 }
9864 }
9865 ~DiagsUninitializedSeveretyRAII() {
9866 if (!IsIgnored)
9867 Diags.popMappings(SavedLoc);
9868 }
9869};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00009870}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009871
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009872OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
9873 SourceLocation StartLoc,
9874 SourceLocation LParenLoc,
9875 SourceLocation EndLoc) {
9876 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009877 SmallVector<Expr *, 8> PrivateCopies;
9878 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00009879 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009880 bool IsImplicitClause =
9881 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
Alexey Bataeve3727102018-04-18 15:57:46 +00009882 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009883
Alexey Bataeve3727102018-04-18 15:57:46 +00009884 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +00009885 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009886 SourceLocation ELoc;
9887 SourceRange ERange;
9888 Expr *SimpleRefExpr = RefExpr;
9889 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009890 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009891 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009892 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009893 PrivateCopies.push_back(nullptr);
9894 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009895 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009896 ValueDecl *D = Res.first;
9897 if (!D)
9898 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009899
Alexey Bataev60da77e2016-02-29 05:54:20 +00009900 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00009901 QualType Type = D->getType();
9902 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009903
9904 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9905 // A variable that appears in a private clause must not have an incomplete
9906 // type or a reference type.
9907 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00009908 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009909 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009910 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009911
9912 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
9913 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00009914 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009915 // class type.
Alexey Bataeve3727102018-04-18 15:57:46 +00009916 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009917
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009918 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00009919 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009920 if (!IsImplicitClause) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009921 DSAStackTy::DSAVarData DVar =
9922 DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00009923 TopDVar = DVar;
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009924 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009925 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009926 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
9927 // A list item that specifies a given variable may not appear in more
9928 // than one clause on the same directive, except that a variable may be
9929 // specified in both firstprivate and lastprivate clauses.
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009930 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
9931 // A list item may appear in a firstprivate or lastprivate clause but not
9932 // both.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009933 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevb358f992017-12-01 17:40:15 +00009934 (isOpenMPDistributeDirective(CurrDir) ||
9935 DVar.CKind != OMPC_lastprivate) &&
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009936 DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009937 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00009938 << getOpenMPClauseName(DVar.CKind)
9939 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +00009940 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009941 continue;
9942 }
9943
9944 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9945 // in a Construct]
9946 // Variables with the predetermined data-sharing attributes may not be
9947 // listed in data-sharing attributes clauses, except for the cases
9948 // listed below. For these exceptions only, listing a predetermined
9949 // variable in a data-sharing attribute clause is allowed and overrides
9950 // the variable's predetermined data-sharing attributes.
9951 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9952 // in a Construct, C/C++, p.2]
9953 // Variables with const-qualified type having no mutable member may be
9954 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00009955 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009956 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
9957 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00009958 << getOpenMPClauseName(DVar.CKind)
9959 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +00009960 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009961 continue;
9962 }
9963
9964 // OpenMP [2.9.3.4, Restrictions, p.2]
9965 // A list item that is private within a parallel region must not appear
9966 // in a firstprivate clause on a worksharing construct if any of the
9967 // worksharing regions arising from the worksharing construct ever bind
9968 // to any of the parallel regions arising from the parallel construct.
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009969 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
9970 // A list item that is private within a teams region must not appear in a
9971 // firstprivate clause on a distribute construct if any of the distribute
9972 // regions arising from the distribute construct ever bind to any of the
9973 // teams regions arising from the teams construct.
9974 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
9975 // A list item that appears in a reduction clause of a teams construct
9976 // must not appear in a firstprivate clause on a distribute construct if
9977 // any of the distribute regions arising from the distribute construct
9978 // ever bind to any of the teams regions arising from the teams construct.
9979 if ((isOpenMPWorksharingDirective(CurrDir) ||
9980 isOpenMPDistributeDirective(CurrDir)) &&
Kelvin Li579e41c2016-11-30 23:51:03 +00009981 !isOpenMPParallelDirective(CurrDir) &&
9982 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00009983 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009984 if (DVar.CKind != OMPC_shared &&
9985 (isOpenMPParallelDirective(DVar.DKind) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009986 isOpenMPTeamsDirective(DVar.DKind) ||
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009987 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00009988 Diag(ELoc, diag::err_omp_required_access)
9989 << getOpenMPClauseName(OMPC_firstprivate)
9990 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +00009991 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00009992 continue;
9993 }
9994 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009995 // OpenMP [2.9.3.4, Restrictions, p.3]
9996 // A list item that appears in a reduction clause of a parallel construct
9997 // must not appear in a firstprivate clause on a worksharing or task
9998 // construct if any of the worksharing or task regions arising from the
9999 // worksharing or task construct ever bind to any of the parallel regions
10000 // arising from the parallel construct.
10001 // OpenMP [2.9.3.4, Restrictions, p.4]
10002 // A list item that appears in a reduction clause in worksharing
10003 // construct must not appear in a firstprivate clause in a task construct
10004 // encountered during execution of any of the worksharing regions arising
10005 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +000010006 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010007 DVar = DSAStack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +000010008 D, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
10009 [](OpenMPDirectiveKind K) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010010 return isOpenMPParallelDirective(K) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010011 isOpenMPWorksharingDirective(K) ||
10012 isOpenMPTeamsDirective(K);
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010013 },
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010014 /*FromParent=*/true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010015 if (DVar.CKind == OMPC_reduction &&
10016 (isOpenMPParallelDirective(DVar.DKind) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010017 isOpenMPWorksharingDirective(DVar.DKind) ||
10018 isOpenMPTeamsDirective(DVar.DKind))) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010019 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
10020 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataeve3727102018-04-18 15:57:46 +000010021 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010022 continue;
10023 }
10024 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +000010025
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010026 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
10027 // A list item cannot appear in both a map clause and a data-sharing
10028 // attribute clause on the same construct
Alexey Bataevb358f992017-12-01 17:40:15 +000010029 if (isOpenMPTargetExecutionDirective(CurrDir)) {
Samuel Antao6890b092016-07-28 14:25:09 +000010030 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +000010031 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +000010032 VD, /*CurrentRegionOnly=*/true,
Alexey Bataeve3727102018-04-18 15:57:46 +000010033 [&ConflictKind](
10034 OMPClauseMappableExprCommon::MappableExprComponentListRef,
10035 OpenMPClauseKind WhereFoundClauseKind) {
Samuel Antao6890b092016-07-28 14:25:09 +000010036 ConflictKind = WhereFoundClauseKind;
10037 return true;
10038 })) {
10039 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010040 << getOpenMPClauseName(OMPC_firstprivate)
Samuel Antao6890b092016-07-28 14:25:09 +000010041 << getOpenMPClauseName(ConflictKind)
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010042 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000010043 reportOriginalDsa(*this, DSAStack, D, DVar);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010044 continue;
10045 }
10046 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010047 }
10048
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010049 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000010050 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +000010051 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010052 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
10053 << getOpenMPClauseName(OMPC_firstprivate) << Type
10054 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
10055 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +000010056 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010057 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +000010058 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010059 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +000010060 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010061 continue;
10062 }
10063
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010064 Type = Type.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010065 VarDecl *VDPrivate =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000010066 buildVarDecl(*this, ELoc, Type, D->getName(),
10067 D->hasAttrs() ? &D->getAttrs() : nullptr,
10068 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010069 // Generate helper private variable and initialize it with the value of the
10070 // original variable. The address of the original variable is replaced by
10071 // the address of the new private variable in the CodeGen. This new variable
10072 // is not added to IdResolver, so the code in the OpenMP region uses
10073 // original variable for proper diagnostics and variable capturing.
10074 Expr *VDInitRefExpr = nullptr;
10075 // For arrays generate initializer for single element and replace it by the
10076 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010077 if (Type->isArrayType()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010078 VarDecl *VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +000010079 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010080 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataeve3727102018-04-18 15:57:46 +000010081 Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010082 ElemType = ElemType.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010083 VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
10084 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +000010085 InitializedEntity Entity =
10086 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010087 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
10088
10089 InitializationSequence InitSeq(*this, Entity, Kind, Init);
10090 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
10091 if (Result.isInvalid())
10092 VDPrivate->setInvalidDecl();
10093 else
10094 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010095 // Remove temp variable declaration.
10096 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010097 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +000010098 VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
10099 ".firstprivate.temp");
Alexey Bataevd985eda2016-02-10 11:29:16 +000010100 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
10101 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +000010102 AddInitializerToDecl(VDPrivate,
10103 DefaultLvalueConversion(VDInitRefExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000010104 /*DirectInit=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010105 }
10106 if (VDPrivate->isInvalidDecl()) {
10107 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +000010108 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010109 diag::note_omp_task_predetermined_firstprivate_here);
10110 }
10111 continue;
10112 }
10113 CurContext->addDecl(VDPrivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000010114 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +000010115 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
10116 RefExpr->getExprLoc());
10117 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010118 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010119 if (TopDVar.CKind == OMPC_lastprivate) {
Alexey Bataev005248a2016-02-25 05:25:57 +000010120 Ref = TopDVar.PrivateCopy;
Alexey Bataeve3727102018-04-18 15:57:46 +000010121 } else {
Alexey Bataev61205072016-03-02 04:57:40 +000010122 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataeve3727102018-04-18 15:57:46 +000010123 if (!isOpenMPCapturedDecl(D))
Alexey Bataev005248a2016-02-25 05:25:57 +000010124 ExprCaptures.push_back(Ref->getDecl());
10125 }
Alexey Bataev417089f2016-02-17 13:19:37 +000010126 }
Alexey Bataevd985eda2016-02-10 11:29:16 +000010127 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010128 Vars.push_back((VD || CurContext->isDependentContext())
10129 ? RefExpr->IgnoreParens()
10130 : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010131 PrivateCopies.push_back(VDPrivateRefExpr);
10132 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010133 }
10134
Alexey Bataeved09d242014-05-28 05:53:51 +000010135 if (Vars.empty())
10136 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010137
10138 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +000010139 Vars, PrivateCopies, Inits,
10140 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010141}
10142
Alexander Musman1bb328c2014-06-04 13:06:39 +000010143OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
10144 SourceLocation StartLoc,
10145 SourceLocation LParenLoc,
10146 SourceLocation EndLoc) {
10147 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +000010148 SmallVector<Expr *, 8> SrcExprs;
10149 SmallVector<Expr *, 8> DstExprs;
10150 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +000010151 SmallVector<Decl *, 4> ExprCaptures;
10152 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeve3727102018-04-18 15:57:46 +000010153 for (Expr *RefExpr : VarList) {
Alexander Musman1bb328c2014-06-04 13:06:39 +000010154 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +000010155 SourceLocation ELoc;
10156 SourceRange ERange;
10157 Expr *SimpleRefExpr = RefExpr;
10158 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +000010159 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +000010160 // It will be analyzed later.
10161 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +000010162 SrcExprs.push_back(nullptr);
10163 DstExprs.push_back(nullptr);
10164 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010165 }
Alexey Bataev74caaf22016-02-20 04:09:36 +000010166 ValueDecl *D = Res.first;
10167 if (!D)
10168 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +000010169
Alexey Bataev74caaf22016-02-20 04:09:36 +000010170 QualType Type = D->getType();
10171 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010172
10173 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
10174 // A variable that appears in a lastprivate clause must not have an
10175 // incomplete type or a reference type.
10176 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +000010177 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +000010178 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010179 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +000010180
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010181 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexander Musman1bb328c2014-06-04 13:06:39 +000010182 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
10183 // in a Construct]
10184 // Variables with the predetermined data-sharing attributes may not be
10185 // listed in data-sharing attributes clauses, except for the cases
10186 // listed below.
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010187 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
10188 // A list item may appear in a firstprivate or lastprivate clause but not
10189 // both.
Alexey Bataeve3727102018-04-18 15:57:46 +000010190 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010191 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
Alexey Bataevb358f992017-12-01 17:40:15 +000010192 (isOpenMPDistributeDirective(CurrDir) ||
10193 DVar.CKind != OMPC_firstprivate) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +000010194 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
10195 Diag(ELoc, diag::err_omp_wrong_dsa)
10196 << getOpenMPClauseName(DVar.CKind)
10197 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000010198 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010199 continue;
10200 }
10201
Alexey Bataevf29276e2014-06-18 04:14:57 +000010202 // OpenMP [2.14.3.5, Restrictions, p.2]
10203 // A list item that is private within a parallel region, or that appears in
10204 // the reduction clause of a parallel construct, must not appear in a
10205 // lastprivate clause on a worksharing construct if any of the corresponding
10206 // worksharing regions ever binds to any of the corresponding parallel
10207 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +000010208 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +000010209 if (isOpenMPWorksharingDirective(CurrDir) &&
Kelvin Li579e41c2016-11-30 23:51:03 +000010210 !isOpenMPParallelDirective(CurrDir) &&
10211 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +000010212 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +000010213 if (DVar.CKind != OMPC_shared) {
10214 Diag(ELoc, diag::err_omp_required_access)
10215 << getOpenMPClauseName(OMPC_lastprivate)
10216 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +000010217 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +000010218 continue;
10219 }
10220 }
Alexey Bataev74caaf22016-02-20 04:09:36 +000010221
Alexander Musman1bb328c2014-06-04 13:06:39 +000010222 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +000010223 // A variable of class type (or array thereof) that appears in a
10224 // lastprivate clause requires an accessible, unambiguous default
10225 // constructor for the class type, unless the list item is also specified
10226 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +000010227 // A variable of class type (or array thereof) that appears in a
10228 // lastprivate clause requires an accessible, unambiguous copy assignment
10229 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +000010230 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010231 VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(),
10232 Type.getUnqualifiedType(), ".lastprivate.src",
10233 D->hasAttrs() ? &D->getAttrs() : nullptr);
10234 DeclRefExpr *PseudoSrcExpr =
Alexey Bataev74caaf22016-02-20 04:09:36 +000010235 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataeve3727102018-04-18 15:57:46 +000010236 VarDecl *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +000010237 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +000010238 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000010239 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +000010240 // For arrays generate assignment operation for single element and replace
10241 // it by the original array element in CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +000010242 ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
10243 PseudoDstExpr, PseudoSrcExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +000010244 if (AssignmentOp.isInvalid())
10245 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +000010246 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +000010247 /*DiscardedValue=*/true);
10248 if (AssignmentOp.isInvalid())
10249 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +000010250
Alexey Bataev74caaf22016-02-20 04:09:36 +000010251 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010252 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010253 if (TopDVar.CKind == OMPC_firstprivate) {
Alexey Bataev005248a2016-02-25 05:25:57 +000010254 Ref = TopDVar.PrivateCopy;
Alexey Bataeve3727102018-04-18 15:57:46 +000010255 } else {
Alexey Bataev61205072016-03-02 04:57:40 +000010256 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000010257 if (!isOpenMPCapturedDecl(D))
Alexey Bataev005248a2016-02-25 05:25:57 +000010258 ExprCaptures.push_back(Ref->getDecl());
10259 }
10260 if (TopDVar.CKind == OMPC_firstprivate ||
Alexey Bataeve3727102018-04-18 15:57:46 +000010261 (!isOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010262 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +000010263 ExprResult RefRes = DefaultLvalueConversion(Ref);
10264 if (!RefRes.isUsable())
10265 continue;
10266 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +000010267 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
10268 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +000010269 if (!PostUpdateRes.isUsable())
10270 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +000010271 ExprPostUpdates.push_back(
10272 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +000010273 }
10274 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010275 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010276 Vars.push_back((VD || CurContext->isDependentContext())
10277 ? RefExpr->IgnoreParens()
10278 : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +000010279 SrcExprs.push_back(PseudoSrcExpr);
10280 DstExprs.push_back(PseudoDstExpr);
10281 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +000010282 }
10283
10284 if (Vars.empty())
10285 return nullptr;
10286
10287 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +000010288 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +000010289 buildPreInits(Context, ExprCaptures),
10290 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +000010291}
10292
Alexey Bataev758e55e2013-09-06 18:03:48 +000010293OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
10294 SourceLocation StartLoc,
10295 SourceLocation LParenLoc,
10296 SourceLocation EndLoc) {
10297 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +000010298 for (Expr *RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010299 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +000010300 SourceLocation ELoc;
10301 SourceRange ERange;
10302 Expr *SimpleRefExpr = RefExpr;
10303 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010304 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +000010305 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000010306 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010307 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010308 ValueDecl *D = Res.first;
10309 if (!D)
10310 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +000010311
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010312 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010313 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
10314 // in a Construct]
10315 // Variables with the predetermined data-sharing attributes may not be
10316 // listed in data-sharing attributes clauses, except for the cases
10317 // listed below. For these exceptions only, listing a predetermined
10318 // variable in a data-sharing attribute clause is allowed and overrides
10319 // the variable's predetermined data-sharing attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +000010320 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataeved09d242014-05-28 05:53:51 +000010321 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
10322 DVar.RefExpr) {
10323 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
10324 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +000010325 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010326 continue;
10327 }
10328
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010329 DeclRefExpr *Ref = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000010330 if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +000010331 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010332 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010333 Vars.push_back((VD || !Ref || CurContext->isDependentContext())
10334 ? RefExpr->IgnoreParens()
10335 : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010336 }
10337
Alexey Bataeved09d242014-05-28 05:53:51 +000010338 if (Vars.empty())
10339 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +000010340
10341 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
10342}
10343
Alexey Bataevc5e02582014-06-16 07:08:35 +000010344namespace {
10345class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
10346 DSAStackTy *Stack;
10347
10348public:
10349 bool VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010350 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
10351 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataevc5e02582014-06-16 07:08:35 +000010352 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
10353 return false;
10354 if (DVar.CKind != OMPC_unknown)
10355 return true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010356 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +000010357 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) { return true; },
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010358 /*FromParent=*/true);
Alexey Bataeve3727102018-04-18 15:57:46 +000010359 return DVarPrivate.CKind != OMPC_unknown;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010360 }
10361 return false;
10362 }
10363 bool VisitStmt(Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010364 for (Stmt *Child : S->children()) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010365 if (Child && Visit(Child))
10366 return true;
10367 }
10368 return false;
10369 }
Alexey Bataev23b69422014-06-18 07:08:49 +000010370 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +000010371};
Alexey Bataev23b69422014-06-18 07:08:49 +000010372} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +000010373
Alexey Bataev60da77e2016-02-29 05:54:20 +000010374namespace {
10375// Transform MemberExpression for specified FieldDecl of current class to
10376// DeclRefExpr to specified OMPCapturedExprDecl.
10377class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
10378 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
Alexey Bataeve3727102018-04-18 15:57:46 +000010379 ValueDecl *Field = nullptr;
10380 DeclRefExpr *CapturedExpr = nullptr;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010381
10382public:
10383 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
10384 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
10385
10386 ExprResult TransformMemberExpr(MemberExpr *E) {
10387 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
10388 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +000010389 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +000010390 return CapturedExpr;
10391 }
10392 return BaseTransform::TransformMemberExpr(E);
10393 }
10394 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
10395};
10396} // namespace
10397
Alexey Bataev97d18bf2018-04-11 19:21:00 +000010398template <typename T, typename U>
10399static T filterLookupForUDR(SmallVectorImpl<U> &Lookups,
10400 const llvm::function_ref<T(ValueDecl *)> Gen) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010401 for (U &Set : Lookups) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010402 for (auto *D : Set) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010403 if (T Res = Gen(cast<ValueDecl>(D)))
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010404 return Res;
10405 }
10406 }
10407 return T();
10408}
10409
Alexey Bataev43b90b72018-09-12 16:31:59 +000010410static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) {
10411 assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case");
10412
10413 for (auto RD : D->redecls()) {
10414 // Don't bother with extra checks if we already know this one isn't visible.
10415 if (RD == D)
10416 continue;
10417
10418 auto ND = cast<NamedDecl>(RD);
10419 if (LookupResult::isVisible(SemaRef, ND))
10420 return ND;
10421 }
10422
10423 return nullptr;
10424}
10425
10426static void
10427argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &ReductionId,
10428 SourceLocation Loc, QualType Ty,
10429 SmallVectorImpl<UnresolvedSet<8>> &Lookups) {
10430 // Find all of the associated namespaces and classes based on the
10431 // arguments we have.
10432 Sema::AssociatedNamespaceSet AssociatedNamespaces;
10433 Sema::AssociatedClassSet AssociatedClasses;
10434 OpaqueValueExpr OVE(Loc, Ty, VK_LValue);
10435 SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces,
10436 AssociatedClasses);
10437
10438 // C++ [basic.lookup.argdep]p3:
10439 // Let X be the lookup set produced by unqualified lookup (3.4.1)
10440 // and let Y be the lookup set produced by argument dependent
10441 // lookup (defined as follows). If X contains [...] then Y is
10442 // empty. Otherwise Y is the set of declarations found in the
10443 // namespaces associated with the argument types as described
10444 // below. The set of declarations found by the lookup of the name
10445 // is the union of X and Y.
10446 //
10447 // Here, we compute Y and add its members to the overloaded
10448 // candidate set.
10449 for (auto *NS : AssociatedNamespaces) {
10450 // When considering an associated namespace, the lookup is the
10451 // same as the lookup performed when the associated namespace is
10452 // used as a qualifier (3.4.3.2) except that:
10453 //
10454 // -- Any using-directives in the associated namespace are
10455 // ignored.
10456 //
10457 // -- Any namespace-scope friend functions declared in
10458 // associated classes are visible within their respective
10459 // namespaces even if they are not visible during an ordinary
10460 // lookup (11.4).
10461 DeclContext::lookup_result R = NS->lookup(ReductionId.getName());
10462 for (auto *D : R) {
10463 auto *Underlying = D;
10464 if (auto *USD = dyn_cast<UsingShadowDecl>(D))
10465 Underlying = USD->getTargetDecl();
10466
10467 if (!isa<OMPDeclareReductionDecl>(Underlying))
10468 continue;
10469
10470 if (!SemaRef.isVisible(D)) {
10471 D = findAcceptableDecl(SemaRef, D);
10472 if (!D)
10473 continue;
10474 if (auto *USD = dyn_cast<UsingShadowDecl>(D))
10475 Underlying = USD->getTargetDecl();
10476 }
10477 Lookups.emplace_back();
10478 Lookups.back().addDecl(Underlying);
10479 }
10480 }
10481}
10482
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010483static ExprResult
10484buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
10485 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
10486 const DeclarationNameInfo &ReductionId, QualType Ty,
10487 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
10488 if (ReductionIdScopeSpec.isInvalid())
10489 return ExprError();
10490 SmallVector<UnresolvedSet<8>, 4> Lookups;
10491 if (S) {
10492 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
10493 Lookup.suppressDiagnostics();
10494 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010495 NamedDecl *D = Lookup.getRepresentativeDecl();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010496 do {
10497 S = S->getParent();
10498 } while (S && !S->isDeclScope(D));
10499 if (S)
10500 S = S->getParent();
Alexey Bataev43b90b72018-09-12 16:31:59 +000010501 Lookups.emplace_back();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010502 Lookups.back().append(Lookup.begin(), Lookup.end());
10503 Lookup.clear();
10504 }
10505 } else if (auto *ULE =
10506 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
10507 Lookups.push_back(UnresolvedSet<8>());
10508 Decl *PrevD = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000010509 for (NamedDecl *D : ULE->decls()) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010510 if (D == PrevD)
10511 Lookups.push_back(UnresolvedSet<8>());
10512 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
10513 Lookups.back().addDecl(DRD);
10514 PrevD = D;
10515 }
10516 }
Alexey Bataevfdc20352017-08-25 15:43:55 +000010517 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() ||
10518 Ty->isInstantiationDependentType() ||
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010519 Ty->containsUnexpandedParameterPack() ||
Alexey Bataeve3727102018-04-18 15:57:46 +000010520 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010521 return !D->isInvalidDecl() &&
10522 (D->getType()->isDependentType() ||
10523 D->getType()->isInstantiationDependentType() ||
10524 D->getType()->containsUnexpandedParameterPack());
10525 })) {
10526 UnresolvedSet<8> ResSet;
Alexey Bataeve3727102018-04-18 15:57:46 +000010527 for (const UnresolvedSet<8> &Set : Lookups) {
Alexey Bataev43b90b72018-09-12 16:31:59 +000010528 if (Set.empty())
10529 continue;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010530 ResSet.append(Set.begin(), Set.end());
10531 // The last item marks the end of all declarations at the specified scope.
10532 ResSet.addDecl(Set[Set.size() - 1]);
10533 }
10534 return UnresolvedLookupExpr::Create(
10535 SemaRef.Context, /*NamingClass=*/nullptr,
10536 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
10537 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
10538 }
Alexey Bataev43b90b72018-09-12 16:31:59 +000010539 // Lookup inside the classes.
10540 // C++ [over.match.oper]p3:
10541 // For a unary operator @ with an operand of a type whose
10542 // cv-unqualified version is T1, and for a binary operator @ with
10543 // a left operand of a type whose cv-unqualified version is T1 and
10544 // a right operand of a type whose cv-unqualified version is T2,
10545 // three sets of candidate functions, designated member
10546 // candidates, non-member candidates and built-in candidates, are
10547 // constructed as follows:
10548 // -- If T1 is a complete class type or a class currently being
10549 // defined, the set of member candidates is the result of the
10550 // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise,
10551 // the set of member candidates is empty.
10552 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
10553 Lookup.suppressDiagnostics();
10554 if (const auto *TyRec = Ty->getAs<RecordType>()) {
10555 // Complete the type if it can be completed.
10556 // If the type is neither complete nor being defined, bail out now.
10557 if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() ||
10558 TyRec->getDecl()->getDefinition()) {
10559 Lookup.clear();
10560 SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl());
10561 if (Lookup.empty()) {
10562 Lookups.emplace_back();
10563 Lookups.back().append(Lookup.begin(), Lookup.end());
10564 }
10565 }
10566 }
10567 // Perform ADL.
10568 argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010569 if (auto *VD = filterLookupForUDR<ValueDecl *>(
10570 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
10571 if (!D->isInvalidDecl() &&
10572 SemaRef.Context.hasSameType(D->getType(), Ty))
10573 return D;
10574 return nullptr;
10575 }))
10576 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
10577 if (auto *VD = filterLookupForUDR<ValueDecl *>(
10578 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
10579 if (!D->isInvalidDecl() &&
10580 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
10581 !Ty.isMoreQualifiedThan(D->getType()))
10582 return D;
10583 return nullptr;
10584 })) {
10585 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
10586 /*DetectVirtual=*/false);
10587 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
10588 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
10589 VD->getType().getUnqualifiedType()))) {
10590 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
10591 /*DiagID=*/0) !=
10592 Sema::AR_inaccessible) {
10593 SemaRef.BuildBasePathArray(Paths, BasePath);
10594 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
10595 }
10596 }
10597 }
10598 }
10599 if (ReductionIdScopeSpec.isSet()) {
10600 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
10601 return ExprError();
10602 }
10603 return ExprEmpty();
10604}
10605
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010606namespace {
10607/// Data for the reduction-based clauses.
10608struct ReductionData {
10609 /// List of original reduction items.
10610 SmallVector<Expr *, 8> Vars;
10611 /// List of private copies of the reduction items.
10612 SmallVector<Expr *, 8> Privates;
10613 /// LHS expressions for the reduction_op expressions.
10614 SmallVector<Expr *, 8> LHSs;
10615 /// RHS expressions for the reduction_op expressions.
10616 SmallVector<Expr *, 8> RHSs;
10617 /// Reduction operation expression.
10618 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev88202be2017-07-27 13:20:36 +000010619 /// Taskgroup descriptors for the corresponding reduction items in
10620 /// in_reduction clauses.
10621 SmallVector<Expr *, 8> TaskgroupDescriptors;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010622 /// List of captures for clause.
10623 SmallVector<Decl *, 4> ExprCaptures;
10624 /// List of postupdate expressions.
10625 SmallVector<Expr *, 4> ExprPostUpdates;
10626 ReductionData() = delete;
10627 /// Reserves required memory for the reduction data.
10628 ReductionData(unsigned Size) {
10629 Vars.reserve(Size);
10630 Privates.reserve(Size);
10631 LHSs.reserve(Size);
10632 RHSs.reserve(Size);
10633 ReductionOps.reserve(Size);
Alexey Bataev88202be2017-07-27 13:20:36 +000010634 TaskgroupDescriptors.reserve(Size);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010635 ExprCaptures.reserve(Size);
10636 ExprPostUpdates.reserve(Size);
10637 }
10638 /// Stores reduction item and reduction operation only (required for dependent
10639 /// reduction item).
10640 void push(Expr *Item, Expr *ReductionOp) {
10641 Vars.emplace_back(Item);
10642 Privates.emplace_back(nullptr);
10643 LHSs.emplace_back(nullptr);
10644 RHSs.emplace_back(nullptr);
10645 ReductionOps.emplace_back(ReductionOp);
Alexey Bataev88202be2017-07-27 13:20:36 +000010646 TaskgroupDescriptors.emplace_back(nullptr);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010647 }
10648 /// Stores reduction data.
Alexey Bataev88202be2017-07-27 13:20:36 +000010649 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp,
10650 Expr *TaskgroupDescriptor) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010651 Vars.emplace_back(Item);
10652 Privates.emplace_back(Private);
10653 LHSs.emplace_back(LHS);
10654 RHSs.emplace_back(RHS);
10655 ReductionOps.emplace_back(ReductionOp);
Alexey Bataev88202be2017-07-27 13:20:36 +000010656 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010657 }
10658};
10659} // namespace
10660
Alexey Bataeve3727102018-04-18 15:57:46 +000010661static bool checkOMPArraySectionConstantForReduction(
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010662 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement,
10663 SmallVectorImpl<llvm::APSInt> &ArraySizes) {
10664 const Expr *Length = OASE->getLength();
10665 if (Length == nullptr) {
10666 // For array sections of the form [1:] or [:], we would need to analyze
10667 // the lower bound...
10668 if (OASE->getColonLoc().isValid())
10669 return false;
10670
10671 // This is an array subscript which has implicit length 1!
10672 SingleElement = true;
10673 ArraySizes.push_back(llvm::APSInt::get(1));
10674 } else {
Fangrui Song407659a2018-11-30 23:41:18 +000010675 Expr::EvalResult Result;
10676 if (!Length->EvaluateAsInt(Result, Context))
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010677 return false;
10678
Fangrui Song407659a2018-11-30 23:41:18 +000010679 llvm::APSInt ConstantLengthValue = Result.Val.getInt();
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010680 SingleElement = (ConstantLengthValue.getSExtValue() == 1);
10681 ArraySizes.push_back(ConstantLengthValue);
10682 }
10683
10684 // Get the base of this array section and walk up from there.
10685 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
10686
10687 // We require length = 1 for all array sections except the right-most to
10688 // guarantee that the memory region is contiguous and has no holes in it.
10689 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) {
10690 Length = TempOASE->getLength();
10691 if (Length == nullptr) {
10692 // For array sections of the form [1:] or [:], we would need to analyze
10693 // the lower bound...
10694 if (OASE->getColonLoc().isValid())
10695 return false;
10696
10697 // This is an array subscript which has implicit length 1!
10698 ArraySizes.push_back(llvm::APSInt::get(1));
10699 } else {
Fangrui Song407659a2018-11-30 23:41:18 +000010700 Expr::EvalResult Result;
10701 if (!Length->EvaluateAsInt(Result, Context))
10702 return false;
10703
10704 llvm::APSInt ConstantLengthValue = Result.Val.getInt();
10705 if (ConstantLengthValue.getSExtValue() != 1)
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010706 return false;
10707
10708 ArraySizes.push_back(ConstantLengthValue);
10709 }
10710 Base = TempOASE->getBase()->IgnoreParenImpCasts();
10711 }
10712
10713 // If we have a single element, we don't need to add the implicit lengths.
10714 if (!SingleElement) {
10715 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) {
10716 // Has implicit length 1!
10717 ArraySizes.push_back(llvm::APSInt::get(1));
10718 Base = TempASE->getBase()->IgnoreParenImpCasts();
10719 }
10720 }
10721
10722 // This array section can be privatized as a single value or as a constant
10723 // sized array.
10724 return true;
10725}
10726
Alexey Bataeve3727102018-04-18 15:57:46 +000010727static bool actOnOMPReductionKindClause(
Alexey Bataev169d96a2017-07-18 20:17:46 +000010728 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind,
10729 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
10730 SourceLocation ColonLoc, SourceLocation EndLoc,
10731 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010732 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010733 DeclarationName DN = ReductionId.getName();
10734 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator();
Alexey Bataevc5e02582014-06-16 07:08:35 +000010735 BinaryOperatorKind BOK = BO_Comma;
10736
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010737 ASTContext &Context = S.Context;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010738 // OpenMP [2.14.3.6, reduction clause]
10739 // C
10740 // reduction-identifier is either an identifier or one of the following
10741 // operators: +, -, *, &, |, ^, && and ||
10742 // C++
10743 // reduction-identifier is either an id-expression or one of the following
10744 // operators: +, -, *, &, |, ^, && and ||
Alexey Bataevc5e02582014-06-16 07:08:35 +000010745 switch (OOK) {
10746 case OO_Plus:
10747 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010748 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010749 break;
10750 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010751 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010752 break;
10753 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010754 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010755 break;
10756 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010757 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010758 break;
10759 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010760 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010761 break;
10762 case OO_AmpAmp:
10763 BOK = BO_LAnd;
10764 break;
10765 case OO_PipePipe:
10766 BOK = BO_LOr;
10767 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010768 case OO_New:
10769 case OO_Delete:
10770 case OO_Array_New:
10771 case OO_Array_Delete:
10772 case OO_Slash:
10773 case OO_Percent:
10774 case OO_Tilde:
10775 case OO_Exclaim:
10776 case OO_Equal:
10777 case OO_Less:
10778 case OO_Greater:
10779 case OO_LessEqual:
10780 case OO_GreaterEqual:
10781 case OO_PlusEqual:
10782 case OO_MinusEqual:
10783 case OO_StarEqual:
10784 case OO_SlashEqual:
10785 case OO_PercentEqual:
10786 case OO_CaretEqual:
10787 case OO_AmpEqual:
10788 case OO_PipeEqual:
10789 case OO_LessLess:
10790 case OO_GreaterGreater:
10791 case OO_LessLessEqual:
10792 case OO_GreaterGreaterEqual:
10793 case OO_EqualEqual:
10794 case OO_ExclaimEqual:
Richard Smithd30b23d2017-12-01 02:13:10 +000010795 case OO_Spaceship:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010796 case OO_PlusPlus:
10797 case OO_MinusMinus:
10798 case OO_Comma:
10799 case OO_ArrowStar:
10800 case OO_Arrow:
10801 case OO_Call:
10802 case OO_Subscript:
10803 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +000010804 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010805 case NUM_OVERLOADED_OPERATORS:
10806 llvm_unreachable("Unexpected reduction identifier");
10807 case OO_None:
Alexey Bataeve3727102018-04-18 15:57:46 +000010808 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010809 if (II->isStr("max"))
10810 BOK = BO_GT;
10811 else if (II->isStr("min"))
10812 BOK = BO_LT;
10813 }
10814 break;
10815 }
10816 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010817 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +000010818 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataev4d4624c2017-07-20 16:47:47 +000010819 else
10820 ReductionIdRange.setBegin(ReductionId.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +000010821 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +000010822
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010823 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
10824 bool FirstIter = true;
Alexey Bataeve3727102018-04-18 15:57:46 +000010825 for (Expr *RefExpr : VarList) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010826 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +000010827 // OpenMP [2.1, C/C++]
10828 // A list item is a variable or array section, subject to the restrictions
10829 // specified in Section 2.4 on page 42 and in each of the sections
10830 // describing clauses and directives for which a list appears.
10831 // OpenMP [2.14.3.3, Restrictions, p.1]
10832 // A variable that is part of another variable (as an array or
10833 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010834 if (!FirstIter && IR != ER)
10835 ++IR;
10836 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010837 SourceLocation ELoc;
10838 SourceRange ERange;
10839 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010840 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
Alexey Bataev60da77e2016-02-29 05:54:20 +000010841 /*AllowArraySection=*/true);
10842 if (Res.second) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010843 // Try to find 'declare reduction' corresponding construct before using
10844 // builtin/overloaded operators.
10845 QualType Type = Context.DependentTy;
10846 CXXCastPath BasePath;
10847 ExprResult DeclareReductionRef = buildDeclareReductionRef(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010848 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010849 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010850 Expr *ReductionOp = nullptr;
10851 if (S.CurContext->isDependentContext() &&
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010852 (DeclareReductionRef.isUnset() ||
10853 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010854 ReductionOp = DeclareReductionRef.get();
10855 // It will be analyzed later.
10856 RD.push(RefExpr, ReductionOp);
Alexey Bataevc5e02582014-06-16 07:08:35 +000010857 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000010858 ValueDecl *D = Res.first;
10859 if (!D)
10860 continue;
10861
Alexey Bataev88202be2017-07-27 13:20:36 +000010862 Expr *TaskgroupDescriptor = nullptr;
Alexey Bataeva1764212015-09-30 09:22:36 +000010863 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010864 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
10865 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
Alexey Bataeve3727102018-04-18 15:57:46 +000010866 if (ASE) {
Alexey Bataev31300ed2016-02-04 11:27:03 +000010867 Type = ASE->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010868 } else if (OASE) {
10869 QualType BaseType =
10870 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
10871 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
Alexey Bataeva1764212015-09-30 09:22:36 +000010872 Type = ATy->getElementType();
10873 else
10874 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +000010875 Type = Type.getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010876 } else {
Alexey Bataev60da77e2016-02-29 05:54:20 +000010877 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
Alexey Bataeve3727102018-04-18 15:57:46 +000010878 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000010879 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +000010880
Alexey Bataevc5e02582014-06-16 07:08:35 +000010881 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
10882 // A variable that appears in a private clause must not have an incomplete
10883 // type or a reference type.
Joel E. Denny3cabf732018-06-28 19:54:49 +000010884 if (S.RequireCompleteType(ELoc, D->getType(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010885 diag::err_omp_reduction_incomplete_type))
Alexey Bataevc5e02582014-06-16 07:08:35 +000010886 continue;
10887 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +000010888 // A list item that appears in a reduction clause must not be
10889 // const-qualified.
10890 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataev169d96a2017-07-18 20:17:46 +000010891 S.Diag(ELoc, diag::err_omp_const_reduction_list_item) << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010892 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010893 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10894 VarDecl::DeclarationOnly;
10895 S.Diag(D->getLocation(),
10896 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +000010897 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +000010898 }
Alexey Bataevc5e02582014-06-16 07:08:35 +000010899 continue;
10900 }
Alexey Bataevbc529672018-09-28 19:33:14 +000010901
10902 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective();
Alexey Bataevc5e02582014-06-16 07:08:35 +000010903 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
10904 // If a list-item is a reference type then it must bind to the same object
10905 // for all threads of the team.
Alexey Bataevbc529672018-09-28 19:33:14 +000010906 if (!ASE && !OASE) {
10907 if (VD) {
10908 VarDecl *VDDef = VD->getDefinition();
10909 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) {
10910 DSARefChecker Check(Stack);
10911 if (Check.Visit(VDDef->getInit())) {
10912 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg)
10913 << getOpenMPClauseName(ClauseKind) << ERange;
10914 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
10915 continue;
10916 }
Alexey Bataeva1764212015-09-30 09:22:36 +000010917 }
Alexey Bataevc5e02582014-06-16 07:08:35 +000010918 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010919
Alexey Bataevbc529672018-09-28 19:33:14 +000010920 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
10921 // in a Construct]
10922 // Variables with the predetermined data-sharing attributes may not be
10923 // listed in data-sharing attributes clauses, except for the cases
10924 // listed below. For these exceptions only, listing a predetermined
10925 // variable in a data-sharing attribute clause is allowed and overrides
10926 // the variable's predetermined data-sharing attributes.
10927 // OpenMP [2.14.3.6, Restrictions, p.3]
10928 // Any number of reduction clauses can be specified on the directive,
10929 // but a list item can appear only once in the reduction clauses for that
10930 // directive.
10931 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false);
10932 if (DVar.CKind == OMPC_reduction) {
10933 S.Diag(ELoc, diag::err_omp_once_referenced)
10934 << getOpenMPClauseName(ClauseKind);
10935 if (DVar.RefExpr)
10936 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
10937 continue;
10938 }
10939 if (DVar.CKind != OMPC_unknown) {
10940 S.Diag(ELoc, diag::err_omp_wrong_dsa)
10941 << getOpenMPClauseName(DVar.CKind)
10942 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataeve3727102018-04-18 15:57:46 +000010943 reportOriginalDsa(S, Stack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010944 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +000010945 }
Alexey Bataevbc529672018-09-28 19:33:14 +000010946
10947 // OpenMP [2.14.3.6, Restrictions, p.1]
10948 // A list item that appears in a reduction clause of a worksharing
10949 // construct must be shared in the parallel regions to which any of the
10950 // worksharing regions arising from the worksharing construct bind.
10951 if (isOpenMPWorksharingDirective(CurrDir) &&
10952 !isOpenMPParallelDirective(CurrDir) &&
10953 !isOpenMPTeamsDirective(CurrDir)) {
10954 DVar = Stack->getImplicitDSA(D, true);
10955 if (DVar.CKind != OMPC_shared) {
10956 S.Diag(ELoc, diag::err_omp_required_access)
10957 << getOpenMPClauseName(OMPC_reduction)
10958 << getOpenMPClauseName(OMPC_shared);
10959 reportOriginalDsa(S, Stack, D, DVar);
10960 continue;
10961 }
10962 }
Alexey Bataevf29276e2014-06-18 04:14:57 +000010963 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010964
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010965 // Try to find 'declare reduction' corresponding construct before using
10966 // builtin/overloaded operators.
10967 CXXCastPath BasePath;
10968 ExprResult DeclareReductionRef = buildDeclareReductionRef(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010969 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010970 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
10971 if (DeclareReductionRef.isInvalid())
10972 continue;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010973 if (S.CurContext->isDependentContext() &&
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010974 (DeclareReductionRef.isUnset() ||
10975 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010976 RD.push(RefExpr, DeclareReductionRef.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010977 continue;
10978 }
10979 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
10980 // Not allowed reduction identifier is found.
Stephen Kellyf2ceec42018-08-09 21:08:08 +000010981 S.Diag(ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010982 diag::err_omp_unknown_reduction_identifier)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010983 << Type << ReductionIdRange;
10984 continue;
10985 }
10986
10987 // OpenMP [2.14.3.6, reduction clause, Restrictions]
10988 // The type of a list item that appears in a reduction clause must be valid
10989 // for the reduction-identifier. For a max or min reduction in C, the type
10990 // of the list item must be an allowed arithmetic data type: char, int,
10991 // float, double, or _Bool, possibly modified with long, short, signed, or
10992 // unsigned. For a max or min reduction in C++, the type of the list item
10993 // must be an allowed arithmetic data type: char, wchar_t, int, float,
10994 // double, or bool, possibly modified with long, short, signed, or unsigned.
10995 if (DeclareReductionRef.isUnset()) {
10996 if ((BOK == BO_GT || BOK == BO_LT) &&
10997 !(Type->isScalarType() ||
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010998 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
10999 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
Alexey Bataev169d96a2017-07-18 20:17:46 +000011000 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011001 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011002 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
11003 VarDecl::DeclarationOnly;
11004 S.Diag(D->getLocation(),
11005 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011006 << D;
11007 }
11008 continue;
11009 }
11010 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011011 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) {
Alexey Bataev169d96a2017-07-18 20:17:46 +000011012 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg)
11013 << getOpenMPClauseName(ClauseKind);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011014 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011015 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
11016 VarDecl::DeclarationOnly;
11017 S.Diag(D->getLocation(),
11018 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011019 << D;
11020 }
11021 continue;
11022 }
11023 }
11024
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011025 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000011026 VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs",
11027 D->hasAttrs() ? &D->getAttrs() : nullptr);
11028 VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(),
11029 D->hasAttrs() ? &D->getAttrs() : nullptr);
11030 QualType PrivateTy = Type;
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000011031
11032 // Try if we can determine constant lengths for all array sections and avoid
11033 // the VLA.
11034 bool ConstantLengthOASE = false;
11035 if (OASE) {
11036 bool SingleElement;
11037 llvm::SmallVector<llvm::APSInt, 4> ArraySizes;
Alexey Bataeve3727102018-04-18 15:57:46 +000011038 ConstantLengthOASE = checkOMPArraySectionConstantForReduction(
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000011039 Context, OASE, SingleElement, ArraySizes);
11040
11041 // If we don't have a single element, we must emit a constant array type.
11042 if (ConstantLengthOASE && !SingleElement) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011043 for (llvm::APSInt &Size : ArraySizes)
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000011044 PrivateTy = Context.getConstantArrayType(
11045 PrivateTy, Size, ArrayType::Normal, /*IndexTypeQuals=*/0);
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000011046 }
11047 }
11048
11049 if ((OASE && !ConstantLengthOASE) ||
Jonas Hahnfeld96087f32017-11-02 13:30:42 +000011050 (!OASE && !ASE &&
Alexey Bataev60da77e2016-02-29 05:54:20 +000011051 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Jonas Hahnfeld87d44262017-11-18 21:00:46 +000011052 if (!Context.getTargetInfo().isVLASupported() &&
11053 S.shouldDiagnoseTargetSupportFromOpenMP()) {
11054 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE;
11055 S.Diag(ELoc, diag::note_vla_unsupported);
11056 continue;
11057 }
David Majnemer9d168222016-08-05 17:44:54 +000011058 // For arrays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011059 // Create pseudo array type for private copy. The size for this array will
11060 // be generated during codegen.
11061 // For array subscripts or single variables Private Ty is the same as Type
11062 // (type of the variable or single array element).
11063 PrivateTy = Context.getVariableArrayType(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011064 Type,
Alexey Bataevd070a582017-10-25 15:54:04 +000011065 new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue),
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011066 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +000011067 } else if (!ASE && !OASE &&
Alexey Bataeve3727102018-04-18 15:57:46 +000011068 Context.getAsArrayType(D->getType().getNonReferenceType())) {
Alexey Bataev60da77e2016-02-29 05:54:20 +000011069 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000011070 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011071 // Private copy.
Alexey Bataeve3727102018-04-18 15:57:46 +000011072 VarDecl *PrivateVD =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000011073 buildVarDecl(S, ELoc, PrivateTy, D->getName(),
11074 D->hasAttrs() ? &D->getAttrs() : nullptr,
11075 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011076 // Add initializer for private variable.
11077 Expr *Init = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000011078 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc);
11079 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011080 if (DeclareReductionRef.isUsable()) {
11081 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
11082 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
11083 if (DRD->getInitializer()) {
11084 Init = DRDRef;
11085 RHSVD->setInit(DRDRef);
11086 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011087 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011088 } else {
11089 switch (BOK) {
11090 case BO_Add:
11091 case BO_Xor:
11092 case BO_Or:
11093 case BO_LOr:
11094 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
11095 if (Type->isScalarType() || Type->isAnyComplexType())
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011096 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011097 break;
11098 case BO_Mul:
11099 case BO_LAnd:
11100 if (Type->isScalarType() || Type->isAnyComplexType()) {
11101 // '*' and '&&' reduction ops - initializer is '1'.
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011102 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +000011103 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011104 break;
11105 case BO_And: {
11106 // '&' reduction op - initializer is '~0'.
11107 QualType OrigType = Type;
11108 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
11109 Type = ComplexTy->getElementType();
11110 if (Type->isRealFloatingType()) {
11111 llvm::APFloat InitValue =
11112 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
11113 /*isIEEE=*/true);
11114 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
11115 Type, ELoc);
11116 } else if (Type->isScalarType()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011117 uint64_t Size = Context.getTypeSize(Type);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011118 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
11119 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
11120 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
11121 }
11122 if (Init && OrigType->isAnyComplexType()) {
11123 // Init = 0xFFFF + 0xFFFFi;
11124 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011125 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011126 }
11127 Type = OrigType;
11128 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011129 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011130 case BO_LT:
11131 case BO_GT: {
11132 // 'min' reduction op - initializer is 'Largest representable number in
11133 // the reduction list item type'.
11134 // 'max' reduction op - initializer is 'Least representable number in
11135 // the reduction list item type'.
11136 if (Type->isIntegerType() || Type->isPointerType()) {
11137 bool IsSigned = Type->hasSignedIntegerRepresentation();
Alexey Bataeve3727102018-04-18 15:57:46 +000011138 uint64_t Size = Context.getTypeSize(Type);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011139 QualType IntTy =
11140 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
11141 llvm::APInt InitValue =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011142 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
11143 : llvm::APInt::getMinValue(Size)
11144 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
11145 : llvm::APInt::getMaxValue(Size);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011146 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
11147 if (Type->isPointerType()) {
11148 // Cast to pointer type.
Alexey Bataeve3727102018-04-18 15:57:46 +000011149 ExprResult CastExpr = S.BuildCStyleCastExpr(
Alexey Bataevd070a582017-10-25 15:54:04 +000011150 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011151 if (CastExpr.isInvalid())
11152 continue;
11153 Init = CastExpr.get();
11154 }
11155 } else if (Type->isRealFloatingType()) {
11156 llvm::APFloat InitValue = llvm::APFloat::getLargest(
11157 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
11158 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
11159 Type, ELoc);
11160 }
11161 break;
11162 }
11163 case BO_PtrMemD:
11164 case BO_PtrMemI:
11165 case BO_MulAssign:
11166 case BO_Div:
11167 case BO_Rem:
11168 case BO_Sub:
11169 case BO_Shl:
11170 case BO_Shr:
11171 case BO_LE:
11172 case BO_GE:
11173 case BO_EQ:
11174 case BO_NE:
Richard Smithc70f1d62017-12-14 15:16:18 +000011175 case BO_Cmp:
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011176 case BO_AndAssign:
11177 case BO_XorAssign:
11178 case BO_OrAssign:
11179 case BO_Assign:
11180 case BO_AddAssign:
11181 case BO_SubAssign:
11182 case BO_DivAssign:
11183 case BO_RemAssign:
11184 case BO_ShlAssign:
11185 case BO_ShrAssign:
11186 case BO_Comma:
11187 llvm_unreachable("Unexpected reduction operation");
11188 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011189 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011190 if (Init && DeclareReductionRef.isUnset())
11191 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false);
11192 else if (!Init)
11193 S.ActOnUninitializedDecl(RHSVD);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011194 if (RHSVD->isInvalidDecl())
11195 continue;
11196 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011197 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible)
11198 << Type << ReductionIdRange;
11199 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
11200 VarDecl::DeclarationOnly;
11201 S.Diag(D->getLocation(),
11202 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +000011203 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011204 continue;
11205 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011206 // Store initializer for single element in private copy. Will be used during
11207 // codegen.
11208 PrivateVD->setInit(RHSVD->getInit());
11209 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataeve3727102018-04-18 15:57:46 +000011210 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011211 ExprResult ReductionOp;
11212 if (DeclareReductionRef.isUsable()) {
11213 QualType RedTy = DeclareReductionRef.get()->getType();
11214 QualType PtrRedTy = Context.getPointerType(RedTy);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011215 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
11216 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011217 if (!BasePath.empty()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011218 LHS = S.DefaultLvalueConversion(LHS.get());
11219 RHS = S.DefaultLvalueConversion(RHS.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011220 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
11221 CK_UncheckedDerivedToBase, LHS.get(),
11222 &BasePath, LHS.get()->getValueKind());
11223 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
11224 CK_UncheckedDerivedToBase, RHS.get(),
11225 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011226 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011227 FunctionProtoType::ExtProtoInfo EPI;
11228 QualType Params[] = {PtrRedTy, PtrRedTy};
11229 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
11230 auto *OVE = new (Context) OpaqueValueExpr(
11231 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011232 S.DefaultLvalueConversion(DeclareReductionRef.get()).get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011233 Expr *Args[] = {LHS.get(), RHS.get()};
11234 ReductionOp = new (Context)
11235 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
11236 } else {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011237 ReductionOp = S.BuildBinOp(
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011238 Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, LHSDRE, RHSDRE);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011239 if (ReductionOp.isUsable()) {
11240 if (BOK != BO_LT && BOK != BO_GT) {
11241 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, ReductionOp.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011244 } else {
Alexey Bataevd070a582017-10-25 15:54:04 +000011245 auto *ConditionalOp = new (Context)
11246 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE,
11247 Type, VK_LValue, OK_Ordinary);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011248 ReductionOp =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011249 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011250 BO_Assign, LHSDRE, ConditionalOp);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011251 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +000011252 if (ReductionOp.isUsable())
11253 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011254 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +000011255 if (!ReductionOp.isUsable())
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011256 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +000011257 }
11258
Alexey Bataevfa312f32017-07-21 18:48:21 +000011259 // OpenMP [2.15.4.6, Restrictions, p.2]
11260 // A list item that appears in an in_reduction clause of a task construct
11261 // must appear in a task_reduction clause of a construct associated with a
11262 // taskgroup region that includes the participating task in its taskgroup
11263 // set. The construct associated with the innermost region that meets this
11264 // condition must specify the same reduction-identifier as the in_reduction
11265 // clause.
11266 if (ClauseKind == OMPC_in_reduction) {
Alexey Bataevfa312f32017-07-21 18:48:21 +000011267 SourceRange ParentSR;
11268 BinaryOperatorKind ParentBOK;
11269 const Expr *ParentReductionOp;
Alexey Bataev88202be2017-07-27 13:20:36 +000011270 Expr *ParentBOKTD, *ParentReductionOpTD;
Alexey Bataevf189cb72017-07-24 14:52:13 +000011271 DSAStackTy::DSAVarData ParentBOKDSA =
Alexey Bataev88202be2017-07-27 13:20:36 +000011272 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK,
11273 ParentBOKTD);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011274 DSAStackTy::DSAVarData ParentReductionOpDSA =
Alexey Bataev88202be2017-07-27 13:20:36 +000011275 Stack->getTopMostTaskgroupReductionData(
11276 D, ParentSR, ParentReductionOp, ParentReductionOpTD);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011277 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown;
11278 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown;
11279 if (!IsParentBOK && !IsParentReductionOp) {
11280 S.Diag(ELoc, diag::err_omp_in_reduction_not_task_reduction);
11281 continue;
11282 }
Alexey Bataevfa312f32017-07-21 18:48:21 +000011283 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) ||
11284 (DeclareReductionRef.isUsable() && IsParentBOK) || BOK != ParentBOK ||
11285 IsParentReductionOp) {
11286 bool EmitError = true;
11287 if (IsParentReductionOp && DeclareReductionRef.isUsable()) {
11288 llvm::FoldingSetNodeID RedId, ParentRedId;
11289 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true);
11290 DeclareReductionRef.get()->Profile(RedId, Context,
11291 /*Canonical=*/true);
11292 EmitError = RedId != ParentRedId;
11293 }
11294 if (EmitError) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011295 S.Diag(ReductionId.getBeginLoc(),
Alexey Bataevfa312f32017-07-21 18:48:21 +000011296 diag::err_omp_reduction_identifier_mismatch)
11297 << ReductionIdRange << RefExpr->getSourceRange();
11298 S.Diag(ParentSR.getBegin(),
11299 diag::note_omp_previous_reduction_identifier)
Alexey Bataevf189cb72017-07-24 14:52:13 +000011300 << ParentSR
11301 << (IsParentBOK ? ParentBOKDSA.RefExpr
11302 : ParentReductionOpDSA.RefExpr)
11303 ->getSourceRange();
Alexey Bataevfa312f32017-07-21 18:48:21 +000011304 continue;
11305 }
11306 }
Alexey Bataev88202be2017-07-27 13:20:36 +000011307 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD;
11308 assert(TaskgroupDescriptor && "Taskgroup descriptor must be defined.");
Alexey Bataevfa312f32017-07-21 18:48:21 +000011309 }
11310
Alexey Bataev60da77e2016-02-29 05:54:20 +000011311 DeclRefExpr *Ref = nullptr;
11312 Expr *VarsExpr = RefExpr->IgnoreParens();
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011313 if (!VD && !S.CurContext->isDependentContext()) {
Alexey Bataev60da77e2016-02-29 05:54:20 +000011314 if (ASE || OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011315 TransformExprToCaptures RebuildToCapture(S, D);
Alexey Bataev60da77e2016-02-29 05:54:20 +000011316 VarsExpr =
11317 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
11318 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +000011319 } else {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011320 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +000011321 }
Alexey Bataeve3727102018-04-18 15:57:46 +000011322 if (!S.isOpenMPCapturedDecl(D)) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011323 RD.ExprCaptures.emplace_back(Ref->getDecl());
Alexey Bataev5a3af132016-03-29 08:58:54 +000011324 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011325 ExprResult RefRes = S.DefaultLvalueConversion(Ref);
Alexey Bataev5a3af132016-03-29 08:58:54 +000011326 if (!RefRes.isUsable())
11327 continue;
11328 ExprResult PostUpdateRes =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011329 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
11330 RefRes.get());
Alexey Bataev5a3af132016-03-29 08:58:54 +000011331 if (!PostUpdateRes.isUsable())
11332 continue;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011333 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
11334 Stack->getCurrentDirective() == OMPD_taskgroup) {
11335 S.Diag(RefExpr->getExprLoc(),
11336 diag::err_omp_reduction_non_addressable_expression)
Alexey Bataevbcd0ae02017-07-11 19:16:44 +000011337 << RefExpr->getSourceRange();
11338 continue;
11339 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011340 RD.ExprPostUpdates.emplace_back(
11341 S.IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +000011342 }
11343 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000011344 }
Alexey Bataev169d96a2017-07-18 20:17:46 +000011345 // All reduction items are still marked as reduction (to do not increase
11346 // code base size).
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011347 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011348 if (CurrDir == OMPD_taskgroup) {
11349 if (DeclareReductionRef.isUsable())
Alexey Bataev3b1b8952017-07-25 15:53:26 +000011350 Stack->addTaskgroupReductionData(D, ReductionIdRange,
11351 DeclareReductionRef.get());
Alexey Bataevf189cb72017-07-24 14:52:13 +000011352 else
Alexey Bataev3b1b8952017-07-25 15:53:26 +000011353 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011354 }
Alexey Bataev88202be2017-07-27 13:20:36 +000011355 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(),
11356 TaskgroupDescriptor);
Alexey Bataevc5e02582014-06-16 07:08:35 +000011357 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011358 return RD.Vars.empty();
11359}
Alexey Bataevc5e02582014-06-16 07:08:35 +000011360
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011361OMPClause *Sema::ActOnOpenMPReductionClause(
11362 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11363 SourceLocation ColonLoc, SourceLocation EndLoc,
11364 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11365 ArrayRef<Expr *> UnresolvedReductions) {
11366 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000011367 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList,
Alexey Bataev169d96a2017-07-18 20:17:46 +000011368 StartLoc, LParenLoc, ColonLoc, EndLoc,
11369 ReductionIdScopeSpec, ReductionId,
11370 UnresolvedReductions, RD))
Alexey Bataevc5e02582014-06-16 07:08:35 +000011371 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +000011372
Alexey Bataevc5e02582014-06-16 07:08:35 +000011373 return OMPReductionClause::Create(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011374 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11375 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
11376 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
11377 buildPreInits(Context, RD.ExprCaptures),
11378 buildPostUpdate(*this, RD.ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +000011379}
11380
Alexey Bataev169d96a2017-07-18 20:17:46 +000011381OMPClause *Sema::ActOnOpenMPTaskReductionClause(
11382 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11383 SourceLocation ColonLoc, SourceLocation EndLoc,
11384 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11385 ArrayRef<Expr *> UnresolvedReductions) {
11386 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000011387 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList,
11388 StartLoc, LParenLoc, ColonLoc, EndLoc,
11389 ReductionIdScopeSpec, ReductionId,
Alexey Bataev169d96a2017-07-18 20:17:46 +000011390 UnresolvedReductions, RD))
11391 return nullptr;
11392
11393 return OMPTaskReductionClause::Create(
11394 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11395 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
11396 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
11397 buildPreInits(Context, RD.ExprCaptures),
11398 buildPostUpdate(*this, RD.ExprPostUpdates));
11399}
11400
Alexey Bataevfa312f32017-07-21 18:48:21 +000011401OMPClause *Sema::ActOnOpenMPInReductionClause(
11402 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11403 SourceLocation ColonLoc, SourceLocation EndLoc,
11404 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11405 ArrayRef<Expr *> UnresolvedReductions) {
11406 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000011407 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList,
Alexey Bataevfa312f32017-07-21 18:48:21 +000011408 StartLoc, LParenLoc, ColonLoc, EndLoc,
11409 ReductionIdScopeSpec, ReductionId,
11410 UnresolvedReductions, RD))
11411 return nullptr;
11412
11413 return OMPInReductionClause::Create(
11414 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11415 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
Alexey Bataev88202be2017-07-27 13:20:36 +000011416 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors,
Alexey Bataevfa312f32017-07-21 18:48:21 +000011417 buildPreInits(Context, RD.ExprCaptures),
11418 buildPostUpdate(*this, RD.ExprPostUpdates));
11419}
11420
Alexey Bataevecba70f2016-04-12 11:02:11 +000011421bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
11422 SourceLocation LinLoc) {
11423 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
11424 LinKind == OMPC_LINEAR_unknown) {
11425 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
11426 return true;
11427 }
11428 return false;
11429}
11430
Alexey Bataeve3727102018-04-18 15:57:46 +000011431bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc,
Alexey Bataevecba70f2016-04-12 11:02:11 +000011432 OpenMPLinearClauseKind LinKind,
11433 QualType Type) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011434 const auto *VD = dyn_cast_or_null<VarDecl>(D);
Alexey Bataevecba70f2016-04-12 11:02:11 +000011435 // A variable must not have an incomplete type or a reference type.
11436 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
11437 return true;
11438 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
11439 !Type->isReferenceType()) {
11440 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
11441 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
11442 return true;
11443 }
11444 Type = Type.getNonReferenceType();
11445
11446 // A list item must not be const-qualified.
11447 if (Type.isConstant(Context)) {
11448 Diag(ELoc, diag::err_omp_const_variable)
11449 << getOpenMPClauseName(OMPC_linear);
11450 if (D) {
11451 bool IsDecl =
11452 !VD ||
11453 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
11454 Diag(D->getLocation(),
11455 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
11456 << D;
11457 }
11458 return true;
11459 }
11460
11461 // A list item must be of integral or pointer type.
11462 Type = Type.getUnqualifiedType().getCanonicalType();
11463 const auto *Ty = Type.getTypePtrOrNull();
11464 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
11465 !Ty->isPointerType())) {
11466 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
11467 if (D) {
11468 bool IsDecl =
11469 !VD ||
11470 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
11471 Diag(D->getLocation(),
11472 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
11473 << D;
11474 }
11475 return true;
11476 }
11477 return false;
11478}
11479
Alexey Bataev182227b2015-08-20 10:54:39 +000011480OMPClause *Sema::ActOnOpenMPLinearClause(
11481 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
11482 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
11483 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +000011484 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011485 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +000011486 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +000011487 SmallVector<Decl *, 4> ExprCaptures;
11488 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +000011489 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +000011490 LinKind = OMPC_LINEAR_val;
Alexey Bataeve3727102018-04-18 15:57:46 +000011491 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000011492 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011493 SourceLocation ELoc;
11494 SourceRange ERange;
11495 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011496 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011497 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +000011498 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000011499 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011500 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +000011501 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +000011502 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011503 ValueDecl *D = Res.first;
11504 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +000011505 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +000011506
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011507 QualType Type = D->getType();
11508 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +000011509
11510 // OpenMP [2.14.3.7, linear clause]
11511 // A list-item cannot appear in more than one linear clause.
11512 // A list-item that appears in a linear clause cannot appear in any
11513 // other data-sharing attribute clause.
Alexey Bataeve3727102018-04-18 15:57:46 +000011514 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexander Musman8dba6642014-04-22 13:09:42 +000011515 if (DVar.RefExpr) {
11516 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
11517 << getOpenMPClauseName(OMPC_linear);
Alexey Bataeve3727102018-04-18 15:57:46 +000011518 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +000011519 continue;
11520 }
11521
Alexey Bataevecba70f2016-04-12 11:02:11 +000011522 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +000011523 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +000011524 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +000011525
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011526 // Build private copy of original var.
Alexey Bataeve3727102018-04-18 15:57:46 +000011527 VarDecl *Private =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000011528 buildVarDecl(*this, ELoc, Type, D->getName(),
11529 D->hasAttrs() ? &D->getAttrs() : nullptr,
11530 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011531 DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +000011532 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011533 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011534 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011535 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000011536 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev78849fb2016-03-09 09:49:00 +000011537 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011538 if (!isOpenMPCapturedDecl(D)) {
Alexey Bataev78849fb2016-03-09 09:49:00 +000011539 ExprCaptures.push_back(Ref->getDecl());
11540 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
11541 ExprResult RefRes = DefaultLvalueConversion(Ref);
11542 if (!RefRes.isUsable())
11543 continue;
11544 ExprResult PostUpdateRes =
11545 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
11546 SimpleRefExpr, RefRes.get());
11547 if (!PostUpdateRes.isUsable())
11548 continue;
11549 ExprPostUpdates.push_back(
11550 IgnoredValueConversions(PostUpdateRes.get()).get());
11551 }
11552 }
11553 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011554 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011555 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011556 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011557 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011558 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000011559 /*DirectInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011560 DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011561
11562 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000011563 Vars.push_back((VD || CurContext->isDependentContext())
11564 ? RefExpr->IgnoreParens()
11565 : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011566 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +000011567 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +000011568 }
11569
11570 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000011571 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +000011572
11573 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +000011574 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +000011575 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
11576 !Step->isInstantiationDependent() &&
11577 !Step->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011578 SourceLocation StepLoc = Step->getBeginLoc();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +000011579 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +000011580 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000011581 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +000011582 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +000011583
Alexander Musman3276a272015-03-21 10:12:56 +000011584 // Build var to save the step value.
11585 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +000011586 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +000011587 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +000011588 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +000011589 ExprResult CalcStep =
11590 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +000011591 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +000011592
Alexander Musman8dba6642014-04-22 13:09:42 +000011593 // Warn about zero linear step (it would be probably better specified as
11594 // making corresponding variables 'const').
11595 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +000011596 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
11597 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +000011598 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
11599 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +000011600 if (!IsConstant && CalcStep.isUsable()) {
11601 // Calculate the step beforehand instead of doing this on each iteration.
11602 // (This is not used if the number of iterations may be kfold-ed).
11603 CalcStepExpr = CalcStep.get();
11604 }
Alexander Musman8dba6642014-04-22 13:09:42 +000011605 }
11606
Alexey Bataev182227b2015-08-20 10:54:39 +000011607 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
11608 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +000011609 StepExpr, CalcStepExpr,
11610 buildPreInits(Context, ExprCaptures),
11611 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +000011612}
11613
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011614static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
11615 Expr *NumIterations, Sema &SemaRef,
11616 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +000011617 // Walk the vars and build update/final expressions for the CodeGen.
11618 SmallVector<Expr *, 8> Updates;
11619 SmallVector<Expr *, 8> Finals;
11620 Expr *Step = Clause.getStep();
11621 Expr *CalcStep = Clause.getCalcStep();
11622 // OpenMP [2.14.3.7, linear clause]
11623 // If linear-step is not specified it is assumed to be 1.
Alexey Bataeve3727102018-04-18 15:57:46 +000011624 if (!Step)
Alexander Musman3276a272015-03-21 10:12:56 +000011625 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000011626 else if (CalcStep)
Alexander Musman3276a272015-03-21 10:12:56 +000011627 Step = cast<BinaryOperator>(CalcStep)->getLHS();
11628 bool HasErrors = false;
11629 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011630 auto CurPrivate = Clause.privates().begin();
Alexey Bataeve3727102018-04-18 15:57:46 +000011631 OpenMPLinearClauseKind LinKind = Clause.getModifier();
11632 for (Expr *RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011633 SourceLocation ELoc;
11634 SourceRange ERange;
11635 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011636 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange);
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011637 ValueDecl *D = Res.first;
11638 if (Res.second || !D) {
11639 Updates.push_back(nullptr);
11640 Finals.push_back(nullptr);
11641 HasErrors = true;
11642 continue;
11643 }
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011644 auto &&Info = Stack->isLoopControlVariable(D);
Alexey Bataev2b86f212017-11-29 21:31:48 +000011645 // OpenMP [2.15.11, distribute simd Construct]
11646 // A list item may not appear in a linear clause, unless it is the loop
11647 // iteration variable.
11648 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) &&
11649 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) {
11650 SemaRef.Diag(ELoc,
11651 diag::err_omp_linear_distribute_var_non_loop_iteration);
11652 Updates.push_back(nullptr);
11653 Finals.push_back(nullptr);
11654 HasErrors = true;
11655 continue;
11656 }
Alexander Musman3276a272015-03-21 10:12:56 +000011657 Expr *InitExpr = *CurInit;
11658
11659 // Build privatized reference to the current linear var.
David Majnemer9d168222016-08-05 17:44:54 +000011660 auto *DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011661 Expr *CapturedRef;
11662 if (LinKind == OMPC_LINEAR_uval)
11663 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
11664 else
11665 CapturedRef =
11666 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
11667 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
11668 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +000011669
11670 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011671 ExprResult Update;
Alexey Bataeve3727102018-04-18 15:57:46 +000011672 if (!Info.first)
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011673 Update =
Alexey Bataeve3727102018-04-18 15:57:46 +000011674 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011675 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011676 else
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011677 Update = *CurPrivate;
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011678 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +000011679 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +000011680
11681 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011682 ExprResult Final;
Alexey Bataeve3727102018-04-18 15:57:46 +000011683 if (!Info.first)
11684 Final =
11685 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
11686 InitExpr, NumIterations, Step, /*Subtract=*/false);
11687 else
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011688 Final = *CurPrivate;
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011689 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +000011690 /*DiscardedValue=*/true);
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011691
Alexander Musman3276a272015-03-21 10:12:56 +000011692 if (!Update.isUsable() || !Final.isUsable()) {
11693 Updates.push_back(nullptr);
11694 Finals.push_back(nullptr);
11695 HasErrors = true;
11696 } else {
11697 Updates.push_back(Update.get());
11698 Finals.push_back(Final.get());
11699 }
Richard Trieucc3949d2016-02-18 22:34:54 +000011700 ++CurInit;
11701 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +000011702 }
11703 Clause.setUpdates(Updates);
11704 Clause.setFinals(Finals);
11705 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +000011706}
11707
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011708OMPClause *Sema::ActOnOpenMPAlignedClause(
11709 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
11710 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011711 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +000011712 for (Expr *RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +000011713 assert(RefExpr && "NULL expr in OpenMP linear clause.");
11714 SourceLocation ELoc;
11715 SourceRange ERange;
11716 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011717 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev1efd1662016-03-29 10:59:56 +000011718 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011719 // It will be analyzed later.
11720 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011721 }
Alexey Bataev1efd1662016-03-29 10:59:56 +000011722 ValueDecl *D = Res.first;
11723 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011724 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011725
Alexey Bataev1efd1662016-03-29 10:59:56 +000011726 QualType QType = D->getType();
11727 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011728
11729 // OpenMP [2.8.1, simd construct, Restrictions]
11730 // The type of list items appearing in the aligned clause must be
11731 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011732 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011733 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +000011734 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011735 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +000011736 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011737 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +000011738 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011739 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +000011740 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011741 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +000011742 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011743 continue;
11744 }
11745
11746 // OpenMP [2.8.1, simd construct, Restrictions]
11747 // A list-item cannot appear in more than one aligned clause.
Alexey Bataeve3727102018-04-18 15:57:46 +000011748 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +000011749 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011750 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
11751 << getOpenMPClauseName(OMPC_aligned);
11752 continue;
11753 }
11754
Alexey Bataev1efd1662016-03-29 10:59:56 +000011755 DeclRefExpr *Ref = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000011756 if (!VD && isOpenMPCapturedDecl(D))
Alexey Bataev1efd1662016-03-29 10:59:56 +000011757 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
11758 Vars.push_back(DefaultFunctionArrayConversion(
11759 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
11760 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011761 }
11762
11763 // OpenMP [2.8.1, simd construct, Description]
11764 // The parameter of the aligned clause, alignment, must be a constant
11765 // positive integer expression.
11766 // If no optional parameter is specified, implementation-defined default
11767 // alignments for SIMD instructions on the target platforms are assumed.
11768 if (Alignment != nullptr) {
11769 ExprResult AlignResult =
11770 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
11771 if (AlignResult.isInvalid())
11772 return nullptr;
11773 Alignment = AlignResult.get();
11774 }
11775 if (Vars.empty())
11776 return nullptr;
11777
11778 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
11779 EndLoc, Vars, Alignment);
11780}
11781
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011782OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
11783 SourceLocation StartLoc,
11784 SourceLocation LParenLoc,
11785 SourceLocation EndLoc) {
11786 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011787 SmallVector<Expr *, 8> SrcExprs;
11788 SmallVector<Expr *, 8> DstExprs;
11789 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeve3727102018-04-18 15:57:46 +000011790 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000011791 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
11792 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011793 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000011794 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011795 SrcExprs.push_back(nullptr);
11796 DstExprs.push_back(nullptr);
11797 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011798 continue;
11799 }
11800
Alexey Bataeved09d242014-05-28 05:53:51 +000011801 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011802 // OpenMP [2.1, C/C++]
11803 // A list item is a variable name.
11804 // OpenMP [2.14.4.1, Restrictions, p.1]
11805 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeve3727102018-04-18 15:57:46 +000011806 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011807 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000011808 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
11809 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011810 continue;
11811 }
11812
11813 Decl *D = DE->getDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000011814 auto *VD = cast<VarDecl>(D);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011815
11816 QualType Type = VD->getType();
11817 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
11818 // It will be analyzed later.
11819 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011820 SrcExprs.push_back(nullptr);
11821 DstExprs.push_back(nullptr);
11822 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011823 continue;
11824 }
11825
11826 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
11827 // A list item that appears in a copyin clause must be threadprivate.
11828 if (!DSAStack->isThreadPrivate(VD)) {
11829 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +000011830 << getOpenMPClauseName(OMPC_copyin)
11831 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011832 continue;
11833 }
11834
11835 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
11836 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +000011837 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011838 // operator for the class type.
Alexey Bataeve3727102018-04-18 15:57:46 +000011839 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
11840 VarDecl *SrcVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011841 buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000011842 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011843 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011844 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +000011845 VarDecl *DstVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011846 buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst",
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000011847 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011848 DeclRefExpr *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011849 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011850 // For arrays generate assignment operation for single element and replace
11851 // it by the original array element in CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +000011852 ExprResult AssignmentOp =
11853 BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr,
11854 PseudoSrcExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011855 if (AssignmentOp.isInvalid())
11856 continue;
11857 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
11858 /*DiscardedValue=*/true);
11859 if (AssignmentOp.isInvalid())
11860 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011861
11862 DSAStack->addDSA(VD, DE, OMPC_copyin);
11863 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011864 SrcExprs.push_back(PseudoSrcExpr);
11865 DstExprs.push_back(PseudoDstExpr);
11866 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011867 }
11868
Alexey Bataeved09d242014-05-28 05:53:51 +000011869 if (Vars.empty())
11870 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011871
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011872 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
11873 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011874}
11875
Alexey Bataevbae9a792014-06-27 10:37:06 +000011876OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
11877 SourceLocation StartLoc,
11878 SourceLocation LParenLoc,
11879 SourceLocation EndLoc) {
11880 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +000011881 SmallVector<Expr *, 8> SrcExprs;
11882 SmallVector<Expr *, 8> DstExprs;
11883 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeve3727102018-04-18 15:57:46 +000011884 for (Expr *RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +000011885 assert(RefExpr && "NULL expr in OpenMP linear clause.");
11886 SourceLocation ELoc;
11887 SourceRange ERange;
11888 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011889 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataeve122da12016-03-17 10:50:17 +000011890 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000011891 // It will be analyzed later.
11892 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000011893 SrcExprs.push_back(nullptr);
11894 DstExprs.push_back(nullptr);
11895 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011896 }
Alexey Bataeve122da12016-03-17 10:50:17 +000011897 ValueDecl *D = Res.first;
11898 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +000011899 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000011900
Alexey Bataeve122da12016-03-17 10:50:17 +000011901 QualType Type = D->getType();
11902 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011903
11904 // OpenMP [2.14.4.2, Restrictions, p.2]
11905 // A list item that appears in a copyprivate clause may not appear in a
11906 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +000011907 if (!VD || !DSAStack->isThreadPrivate(VD)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011908 DSAStackTy::DSAVarData DVar =
11909 DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataeva63048e2015-03-23 06:18:07 +000011910 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
11911 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000011912 Diag(ELoc, diag::err_omp_wrong_dsa)
11913 << getOpenMPClauseName(DVar.CKind)
11914 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000011915 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011916 continue;
11917 }
11918
11919 // OpenMP [2.11.4.2, Restrictions, p.1]
11920 // All list items that appear in a copyprivate clause must be either
11921 // threadprivate or private in the enclosing context.
11922 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +000011923 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011924 if (DVar.CKind == OMPC_shared) {
11925 Diag(ELoc, diag::err_omp_required_access)
11926 << getOpenMPClauseName(OMPC_copyprivate)
11927 << "threadprivate or private in the enclosing context";
Alexey Bataeve3727102018-04-18 15:57:46 +000011928 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011929 continue;
11930 }
11931 }
11932 }
11933
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011934 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000011935 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011936 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +000011937 << getOpenMPClauseName(OMPC_copyprivate) << Type
11938 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011939 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +000011940 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011941 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +000011942 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011943 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +000011944 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011945 continue;
11946 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +000011947
Alexey Bataevbae9a792014-06-27 10:37:06 +000011948 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
11949 // A variable of class type (or array thereof) that appears in a
11950 // copyin clause requires an accessible, unambiguous copy assignment
11951 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011952 Type = Context.getBaseElementType(Type.getNonReferenceType())
11953 .getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000011954 VarDecl *SrcVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011955 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src",
Alexey Bataeve122da12016-03-17 10:50:17 +000011956 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011957 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
11958 VarDecl *DstVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011959 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst",
Alexey Bataeve122da12016-03-17 10:50:17 +000011960 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011961 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
11962 ExprResult AssignmentOp = BuildBinOp(
11963 DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000011964 if (AssignmentOp.isInvalid())
11965 continue;
Alexey Bataeve122da12016-03-17 10:50:17 +000011966 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataeva63048e2015-03-23 06:18:07 +000011967 /*DiscardedValue=*/true);
11968 if (AssignmentOp.isInvalid())
11969 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000011970
11971 // No need to mark vars as copyprivate, they are already threadprivate or
11972 // implicitly private.
Alexey Bataeve3727102018-04-18 15:57:46 +000011973 assert(VD || isOpenMPCapturedDecl(D));
Alexey Bataeve122da12016-03-17 10:50:17 +000011974 Vars.push_back(
11975 VD ? RefExpr->IgnoreParens()
11976 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +000011977 SrcExprs.push_back(PseudoSrcExpr);
11978 DstExprs.push_back(PseudoDstExpr);
11979 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +000011980 }
11981
11982 if (Vars.empty())
11983 return nullptr;
11984
Alexey Bataeva63048e2015-03-23 06:18:07 +000011985 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11986 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011987}
11988
Alexey Bataev6125da92014-07-21 11:26:11 +000011989OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
11990 SourceLocation StartLoc,
11991 SourceLocation LParenLoc,
11992 SourceLocation EndLoc) {
11993 if (VarList.empty())
11994 return nullptr;
11995
11996 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
11997}
Alexey Bataevdea47612014-07-23 07:46:59 +000011998
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011999OMPClause *
12000Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
12001 SourceLocation DepLoc, SourceLocation ColonLoc,
12002 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
12003 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +000012004 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012005 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +000012006 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000012007 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +000012008 return nullptr;
12009 }
12010 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012011 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
12012 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +000012013 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012014 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000012015 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
12016 /*Last=*/OMPC_DEPEND_unknown, Except)
12017 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012018 return nullptr;
12019 }
12020 SmallVector<Expr *, 8> Vars;
Alexey Bataev8b427062016-05-25 12:36:08 +000012021 DSAStackTy::OperatorOffsetTy OpsOffs;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012022 llvm::APSInt DepCounter(/*BitWidth=*/32);
12023 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
Alexey Bataevf138fda2018-08-13 19:04:24 +000012024 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) {
12025 if (const Expr *OrderedCountExpr =
12026 DSAStack->getParentOrderedRegionParam().first) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012027 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
12028 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012029 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012030 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012031 for (Expr *RefExpr : VarList) {
Alexey Bataev17daedf2018-02-15 22:42:57 +000012032 assert(RefExpr && "NULL expr in OpenMP shared clause.");
12033 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
12034 // It will be analyzed later.
12035 Vars.push_back(RefExpr);
12036 continue;
12037 }
12038
12039 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataeve3727102018-04-18 15:57:46 +000012040 Expr *SimpleExpr = RefExpr->IgnoreParenCasts();
Alexey Bataev17daedf2018-02-15 22:42:57 +000012041 if (DepKind == OMPC_DEPEND_sink) {
Alexey Bataevf138fda2018-08-13 19:04:24 +000012042 if (DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000012043 DepCounter >= TotalDepCount) {
12044 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
12045 continue;
12046 }
12047 ++DepCounter;
12048 // OpenMP [2.13.9, Summary]
12049 // depend(dependence-type : vec), where dependence-type is:
12050 // 'sink' and where vec is the iteration vector, which has the form:
12051 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
12052 // where n is the value specified by the ordered clause in the loop
12053 // directive, xi denotes the loop iteration variable of the i-th nested
12054 // loop associated with the loop directive, and di is a constant
12055 // non-negative integer.
12056 if (CurContext->isDependentContext()) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012057 // It will be analyzed later.
12058 Vars.push_back(RefExpr);
12059 continue;
12060 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000012061 SimpleExpr = SimpleExpr->IgnoreImplicit();
12062 OverloadedOperatorKind OOK = OO_None;
12063 SourceLocation OOLoc;
12064 Expr *LHS = SimpleExpr;
12065 Expr *RHS = nullptr;
12066 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
12067 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
12068 OOLoc = BO->getOperatorLoc();
12069 LHS = BO->getLHS()->IgnoreParenImpCasts();
12070 RHS = BO->getRHS()->IgnoreParenImpCasts();
12071 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
12072 OOK = OCE->getOperator();
12073 OOLoc = OCE->getOperatorLoc();
12074 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
12075 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
12076 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
12077 OOK = MCE->getMethodDecl()
12078 ->getNameInfo()
12079 .getName()
12080 .getCXXOverloadedOperator();
12081 OOLoc = MCE->getCallee()->getExprLoc();
12082 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
12083 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012084 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000012085 SourceLocation ELoc;
12086 SourceRange ERange;
Alexey Bataevbc529672018-09-28 19:33:14 +000012087 auto Res = getPrivateItem(*this, LHS, ELoc, ERange);
Alexey Bataev17daedf2018-02-15 22:42:57 +000012088 if (Res.second) {
12089 // It will be analyzed later.
12090 Vars.push_back(RefExpr);
12091 }
12092 ValueDecl *D = Res.first;
12093 if (!D)
12094 continue;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012095
Alexey Bataev17daedf2018-02-15 22:42:57 +000012096 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
12097 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
12098 continue;
12099 }
12100 if (RHS) {
12101 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
12102 RHS, OMPC_depend, /*StrictlyPositive=*/false);
12103 if (RHSRes.isInvalid())
12104 continue;
12105 }
12106 if (!CurContext->isDependentContext() &&
Alexey Bataevf138fda2018-08-13 19:04:24 +000012107 DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000012108 DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012109 const ValueDecl *VD =
Alexey Bataev17daedf2018-02-15 22:42:57 +000012110 DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue());
Alexey Bataeve3727102018-04-18 15:57:46 +000012111 if (VD)
Alexey Bataev17daedf2018-02-15 22:42:57 +000012112 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
12113 << 1 << VD;
Alexey Bataeve3727102018-04-18 15:57:46 +000012114 else
Alexey Bataev17daedf2018-02-15 22:42:57 +000012115 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0;
Alexey Bataev17daedf2018-02-15 22:42:57 +000012116 continue;
12117 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012118 OpsOffs.emplace_back(RHS, OOK);
Alexey Bataev17daedf2018-02-15 22:42:57 +000012119 } else {
12120 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
12121 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
12122 (ASE &&
12123 !ASE->getBase()->getType().getNonReferenceType()->isPointerType() &&
12124 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
12125 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
12126 << RefExpr->getSourceRange();
12127 continue;
12128 }
12129 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
12130 getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
12131 ExprResult Res =
12132 CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RefExpr->IgnoreParenImpCasts());
12133 getDiagnostics().setSuppressAllDiagnostics(Suppress);
12134 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr)) {
12135 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
12136 << RefExpr->getSourceRange();
12137 continue;
12138 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012139 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000012140 Vars.push_back(RefExpr->IgnoreParenImpCasts());
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012141 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000012142
12143 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
12144 TotalDepCount > VarList.size() &&
Alexey Bataevf138fda2018-08-13 19:04:24 +000012145 DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000012146 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) {
12147 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
12148 << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
12149 }
12150 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
12151 Vars.empty())
12152 return nullptr;
12153
Alexey Bataev8b427062016-05-25 12:36:08 +000012154 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataevf138fda2018-08-13 19:04:24 +000012155 DepKind, DepLoc, ColonLoc, Vars,
12156 TotalDepCount.getZExtValue());
Alexey Bataev17daedf2018-02-15 22:42:57 +000012157 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) &&
12158 DSAStack->isParentOrderedRegion())
Alexey Bataev8b427062016-05-25 12:36:08 +000012159 DSAStack->addDoacrossDependClause(C, OpsOffs);
12160 return C;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012161}
Michael Wonge710d542015-08-07 16:16:36 +000012162
12163OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
12164 SourceLocation LParenLoc,
12165 SourceLocation EndLoc) {
12166 Expr *ValExpr = Device;
Alexey Bataev931e19b2017-10-02 16:32:39 +000012167 Stmt *HelperValStmt = nullptr;
Michael Wonge710d542015-08-07 16:16:36 +000012168
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012169 // OpenMP [2.9.1, Restrictions]
12170 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000012171 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
Alexey Bataeva0569352015-12-01 10:17:31 +000012172 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012173 return nullptr;
12174
Alexey Bataev931e19b2017-10-02 16:32:39 +000012175 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000012176 OpenMPDirectiveKind CaptureRegion =
12177 getOpenMPCaptureRegionForClause(DKind, OMPC_device);
12178 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000012179 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000012180 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev931e19b2017-10-02 16:32:39 +000012181 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
12182 HelperValStmt = buildPreInits(Context, Captures);
12183 }
12184
Alexey Bataev8451efa2018-01-15 19:06:12 +000012185 return new (Context) OMPDeviceClause(ValExpr, HelperValStmt, CaptureRegion,
12186 StartLoc, LParenLoc, EndLoc);
Michael Wonge710d542015-08-07 16:16:36 +000012187}
Kelvin Li0bff7af2015-11-23 05:32:03 +000012188
Alexey Bataeve3727102018-04-18 15:57:46 +000012189static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
Alexey Bataev95c23e72018-02-27 21:31:11 +000012190 DSAStackTy *Stack, QualType QTy,
12191 bool FullCheck = true) {
Kelvin Li0bff7af2015-11-23 05:32:03 +000012192 NamedDecl *ND;
12193 if (QTy->isIncompleteType(&ND)) {
12194 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
12195 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +000012196 }
Alexey Bataev95c23e72018-02-27 21:31:11 +000012197 if (FullCheck && !SemaRef.CurContext->isDependentContext() &&
12198 !QTy.isTrivialType(SemaRef.Context))
12199 SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR;
Kelvin Li0bff7af2015-11-23 05:32:03 +000012200 return true;
12201}
12202
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000012203/// Return true if it can be proven that the provided array expression
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012204/// (array section or array subscript) does NOT specify the whole size of the
12205/// array whose base type is \a BaseQTy.
Alexey Bataeve3727102018-04-18 15:57:46 +000012206static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012207 const Expr *E,
12208 QualType BaseQTy) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012209 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012210
12211 // If this is an array subscript, it refers to the whole size if the size of
12212 // the dimension is constant and equals 1. Also, an array section assumes the
12213 // format of an array subscript if no colon is used.
12214 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012215 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012216 return ATy->getSize().getSExtValue() != 1;
12217 // Size can't be evaluated statically.
12218 return false;
12219 }
12220
12221 assert(OASE && "Expecting array section if not an array subscript.");
Alexey Bataeve3727102018-04-18 15:57:46 +000012222 const Expr *LowerBound = OASE->getLowerBound();
12223 const Expr *Length = OASE->getLength();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012224
12225 // If there is a lower bound that does not evaluates to zero, we are not
David Majnemer9d168222016-08-05 17:44:54 +000012226 // covering the whole dimension.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012227 if (LowerBound) {
Fangrui Song407659a2018-11-30 23:41:18 +000012228 Expr::EvalResult Result;
12229 if (!LowerBound->EvaluateAsInt(Result, SemaRef.getASTContext()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012230 return false; // Can't get the integer value as a constant.
Fangrui Song407659a2018-11-30 23:41:18 +000012231
12232 llvm::APSInt ConstLowerBound = Result.Val.getInt();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012233 if (ConstLowerBound.getSExtValue())
12234 return true;
12235 }
12236
12237 // If we don't have a length we covering the whole dimension.
12238 if (!Length)
12239 return false;
12240
12241 // If the base is a pointer, we don't have a way to get the size of the
12242 // pointee.
12243 if (BaseQTy->isPointerType())
12244 return false;
12245
12246 // We can only check if the length is the same as the size of the dimension
12247 // if we have a constant array.
Alexey Bataeve3727102018-04-18 15:57:46 +000012248 const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012249 if (!CATy)
12250 return false;
12251
Fangrui Song407659a2018-11-30 23:41:18 +000012252 Expr::EvalResult Result;
12253 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012254 return false; // Can't get the integer value as a constant.
12255
Fangrui Song407659a2018-11-30 23:41:18 +000012256 llvm::APSInt ConstLength = Result.Val.getInt();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012257 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
12258}
12259
12260// Return true if it can be proven that the provided array expression (array
12261// section or array subscript) does NOT specify a single element of the array
12262// whose base type is \a BaseQTy.
Alexey Bataeve3727102018-04-18 15:57:46 +000012263static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
David Majnemer9d168222016-08-05 17:44:54 +000012264 const Expr *E,
12265 QualType BaseQTy) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012266 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012267
12268 // An array subscript always refer to a single element. Also, an array section
12269 // assumes the format of an array subscript if no colon is used.
12270 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
12271 return false;
12272
12273 assert(OASE && "Expecting array section if not an array subscript.");
Alexey Bataeve3727102018-04-18 15:57:46 +000012274 const Expr *Length = OASE->getLength();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012275
12276 // If we don't have a length we have to check if the array has unitary size
12277 // for this dimension. Also, we should always expect a length if the base type
12278 // is pointer.
12279 if (!Length) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012280 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012281 return ATy->getSize().getSExtValue() != 1;
12282 // We cannot assume anything.
12283 return false;
12284 }
12285
12286 // Check if the length evaluates to 1.
Fangrui Song407659a2018-11-30 23:41:18 +000012287 Expr::EvalResult Result;
12288 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012289 return false; // Can't get the integer value as a constant.
12290
Fangrui Song407659a2018-11-30 23:41:18 +000012291 llvm::APSInt ConstLength = Result.Val.getInt();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012292 return ConstLength.getSExtValue() != 1;
12293}
12294
Samuel Antao661c0902016-05-26 17:39:58 +000012295// Return the expression of the base of the mappable expression or null if it
12296// cannot be determined and do all the necessary checks to see if the expression
12297// is valid as a standalone mappable expression. In the process, record all the
Samuel Antao90927002016-04-26 14:54:23 +000012298// components of the expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000012299static const Expr *checkMapClauseExpressionBase(
Samuel Antao90927002016-04-26 14:54:23 +000012300 Sema &SemaRef, Expr *E,
Samuel Antao661c0902016-05-26 17:39:58 +000012301 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012302 OpenMPClauseKind CKind, bool NoDiagnose) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012303 SourceLocation ELoc = E->getExprLoc();
12304 SourceRange ERange = E->getSourceRange();
12305
12306 // The base of elements of list in a map clause have to be either:
12307 // - a reference to variable or field.
12308 // - a member expression.
12309 // - an array expression.
12310 //
12311 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
12312 // reference to 'r'.
12313 //
12314 // If we have:
12315 //
12316 // struct SS {
12317 // Bla S;
12318 // foo() {
12319 // #pragma omp target map (S.Arr[:12]);
12320 // }
12321 // }
12322 //
12323 // We want to retrieve the member expression 'this->S';
12324
Alexey Bataeve3727102018-04-18 15:57:46 +000012325 const Expr *RelevantExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012326
Samuel Antao5de996e2016-01-22 20:21:36 +000012327 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
12328 // If a list item is an array section, it must specify contiguous storage.
12329 //
12330 // For this restriction it is sufficient that we make sure only references
12331 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012332 // exist except in the rightmost expression (unless they cover the whole
12333 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +000012334 //
12335 // r.ArrS[3:5].Arr[6:7]
12336 //
12337 // r.ArrS[3:5].x
12338 //
12339 // but these would be valid:
12340 // r.ArrS[3].Arr[6:7]
12341 //
12342 // r.ArrS[3].x
12343
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012344 bool AllowUnitySizeArraySection = true;
12345 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +000012346
Dmitry Polukhin644a9252016-03-11 07:58:34 +000012347 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012348 E = E->IgnoreParenImpCasts();
12349
12350 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
12351 if (!isa<VarDecl>(CurE->getDecl()))
Alexey Bataev27041fa2017-12-05 15:22:49 +000012352 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012353
12354 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012355
12356 // If we got a reference to a declaration, we should not expect any array
12357 // section before that.
12358 AllowUnitySizeArraySection = false;
12359 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000012360
12361 // Record the component.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012362 CurComponents.emplace_back(CurE, CurE->getDecl());
12363 } else if (auto *CurE = dyn_cast<MemberExpr>(E)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012364 Expr *BaseE = CurE->getBase()->IgnoreParenImpCasts();
Samuel Antao5de996e2016-01-22 20:21:36 +000012365
12366 if (isa<CXXThisExpr>(BaseE))
12367 // We found a base expression: this->Val.
12368 RelevantExpr = CurE;
12369 else
12370 E = BaseE;
12371
12372 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012373 if (!NoDiagnose) {
12374 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
12375 << CurE->getSourceRange();
12376 return nullptr;
12377 }
12378 if (RelevantExpr)
12379 return nullptr;
12380 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000012381 }
12382
12383 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
12384
12385 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
12386 // A bit-field cannot appear in a map clause.
12387 //
12388 if (FD->isBitField()) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012389 if (!NoDiagnose) {
12390 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
12391 << CurE->getSourceRange() << getOpenMPClauseName(CKind);
12392 return nullptr;
12393 }
12394 if (RelevantExpr)
12395 return nullptr;
12396 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000012397 }
12398
12399 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12400 // If the type of a list item is a reference to a type T then the type
12401 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012402 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000012403
12404 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
12405 // A list item cannot be a variable that is a member of a structure with
12406 // a union type.
12407 //
Alexey Bataeve3727102018-04-18 15:57:46 +000012408 if (CurType->isUnionType()) {
12409 if (!NoDiagnose) {
12410 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
12411 << CurE->getSourceRange();
12412 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012413 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012414 continue;
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012415 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012416
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012417 // If we got a member expression, we should not expect any array section
12418 // before that:
12419 //
12420 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
12421 // If a list item is an element of a structure, only the rightmost symbol
12422 // of the variable reference can be an array section.
12423 //
12424 AllowUnitySizeArraySection = false;
12425 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000012426
12427 // Record the component.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012428 CurComponents.emplace_back(CurE, FD);
12429 } else if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012430 E = CurE->getBase()->IgnoreParenImpCasts();
12431
12432 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012433 if (!NoDiagnose) {
12434 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
12435 << 0 << CurE->getSourceRange();
12436 return nullptr;
12437 }
12438 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000012439 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012440
12441 // If we got an array subscript that express the whole dimension we
12442 // can have any array expressions before. If it only expressing part of
12443 // the dimension, we can only have unitary-size array expressions.
Alexey Bataeve3727102018-04-18 15:57:46 +000012444 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012445 E->getType()))
12446 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000012447
12448 // Record the component - we don't have any declaration associated.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012449 CurComponents.emplace_back(CurE, nullptr);
12450 } else if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012451 assert(!NoDiagnose && "Array sections cannot be implicitly mapped.");
Samuel Antao5de996e2016-01-22 20:21:36 +000012452 E = CurE->getBase()->IgnoreParenImpCasts();
12453
Alexey Bataev27041fa2017-12-05 15:22:49 +000012454 QualType CurType =
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012455 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
12456
Samuel Antao5de996e2016-01-22 20:21:36 +000012457 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12458 // If the type of a list item is a reference to a type T then the type
12459 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +000012460 if (CurType->isReferenceType())
12461 CurType = CurType->getPointeeType();
12462
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012463 bool IsPointer = CurType->isAnyPointerType();
12464
12465 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012466 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
12467 << 0 << CurE->getSourceRange();
Alexey Bataev27041fa2017-12-05 15:22:49 +000012468 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012469 }
12470
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012471 bool NotWhole =
Alexey Bataeve3727102018-04-18 15:57:46 +000012472 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012473 bool NotUnity =
Alexey Bataeve3727102018-04-18 15:57:46 +000012474 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012475
Samuel Antaodab51bb2016-07-18 23:22:11 +000012476 if (AllowWholeSizeArraySection) {
12477 // Any array section is currently allowed. Allowing a whole size array
12478 // section implies allowing a unity array section as well.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012479 //
12480 // If this array section refers to the whole dimension we can still
12481 // accept other array sections before this one, except if the base is a
12482 // pointer. Otherwise, only unitary sections are accepted.
12483 if (NotWhole || IsPointer)
12484 AllowWholeSizeArraySection = false;
Samuel Antaodab51bb2016-07-18 23:22:11 +000012485 } else if (AllowUnitySizeArraySection && NotUnity) {
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012486 // A unity or whole array section is not allowed and that is not
12487 // compatible with the properties of the current array section.
12488 SemaRef.Diag(
12489 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
12490 << CurE->getSourceRange();
Alexey Bataev27041fa2017-12-05 15:22:49 +000012491 return nullptr;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012492 }
Samuel Antao90927002016-04-26 14:54:23 +000012493
12494 // Record the component - we don't have any declaration associated.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012495 CurComponents.emplace_back(CurE, nullptr);
12496 } else {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012497 if (!NoDiagnose) {
12498 // If nothing else worked, this is not a valid map clause expression.
12499 SemaRef.Diag(
12500 ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
12501 << ERange;
12502 }
Alexey Bataev27041fa2017-12-05 15:22:49 +000012503 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012504 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012505 }
12506
12507 return RelevantExpr;
12508}
12509
12510// Return true if expression E associated with value VD has conflicts with other
12511// map information.
Alexey Bataeve3727102018-04-18 15:57:46 +000012512static bool checkMapConflicts(
12513 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E,
Samuel Antao90927002016-04-26 14:54:23 +000012514 bool CurrentRegionOnly,
Samuel Antao661c0902016-05-26 17:39:58 +000012515 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
12516 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012517 assert(VD && E);
Samuel Antao5de996e2016-01-22 20:21:36 +000012518 SourceLocation ELoc = E->getExprLoc();
12519 SourceRange ERange = E->getSourceRange();
12520
12521 // In order to easily check the conflicts we need to match each component of
12522 // the expression under test with the components of the expressions that are
12523 // already in the stack.
12524
Samuel Antao5de996e2016-01-22 20:21:36 +000012525 assert(!CurComponents.empty() && "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000012526 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000012527 "Map clause expression with unexpected base!");
12528
12529 // Variables to help detecting enclosing problems in data environment nests.
12530 bool IsEnclosedByDataEnvironmentExpr = false;
Samuel Antao90927002016-04-26 14:54:23 +000012531 const Expr *EnclosingExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012532
Samuel Antao90927002016-04-26 14:54:23 +000012533 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
12534 VD, CurrentRegionOnly,
Alexey Bataeve3727102018-04-18 15:57:46 +000012535 [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc,
12536 ERange, CKind, &EnclosingExpr,
12537 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef
12538 StackComponents,
12539 OpenMPClauseKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012540 assert(!StackComponents.empty() &&
12541 "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000012542 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000012543 "Map clause expression with unexpected base!");
Fangrui Song16fe49a2018-04-18 19:32:01 +000012544 (void)VD;
Samuel Antao5de996e2016-01-22 20:21:36 +000012545
Samuel Antao90927002016-04-26 14:54:23 +000012546 // The whole expression in the stack.
Alexey Bataeve3727102018-04-18 15:57:46 +000012547 const Expr *RE = StackComponents.front().getAssociatedExpression();
Samuel Antao90927002016-04-26 14:54:23 +000012548
Samuel Antao5de996e2016-01-22 20:21:36 +000012549 // Expressions must start from the same base. Here we detect at which
12550 // point both expressions diverge from each other and see if we can
12551 // detect if the memory referred to both expressions is contiguous and
12552 // do not overlap.
12553 auto CI = CurComponents.rbegin();
12554 auto CE = CurComponents.rend();
12555 auto SI = StackComponents.rbegin();
12556 auto SE = StackComponents.rend();
12557 for (; CI != CE && SI != SE; ++CI, ++SI) {
12558
12559 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
12560 // At most one list item can be an array item derived from a given
12561 // variable in map clauses of the same construct.
Samuel Antao90927002016-04-26 14:54:23 +000012562 if (CurrentRegionOnly &&
12563 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
12564 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
12565 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
12566 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
12567 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
Samuel Antao5de996e2016-01-22 20:21:36 +000012568 diag::err_omp_multiple_array_items_in_map_clause)
Samuel Antao90927002016-04-26 14:54:23 +000012569 << CI->getAssociatedExpression()->getSourceRange();
12570 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
12571 diag::note_used_here)
12572 << SI->getAssociatedExpression()->getSourceRange();
Samuel Antao5de996e2016-01-22 20:21:36 +000012573 return true;
12574 }
12575
12576 // Do both expressions have the same kind?
Samuel Antao90927002016-04-26 14:54:23 +000012577 if (CI->getAssociatedExpression()->getStmtClass() !=
12578 SI->getAssociatedExpression()->getStmtClass())
Samuel Antao5de996e2016-01-22 20:21:36 +000012579 break;
12580
12581 // Are we dealing with different variables/fields?
Samuel Antao90927002016-04-26 14:54:23 +000012582 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
Samuel Antao5de996e2016-01-22 20:21:36 +000012583 break;
12584 }
Kelvin Li9f645ae2016-07-18 22:49:16 +000012585 // Check if the extra components of the expressions in the enclosing
12586 // data environment are redundant for the current base declaration.
12587 // If they are, the maps completely overlap, which is legal.
12588 for (; SI != SE; ++SI) {
12589 QualType Type;
Alexey Bataeve3727102018-04-18 15:57:46 +000012590 if (const auto *ASE =
David Majnemer9d168222016-08-05 17:44:54 +000012591 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
Kelvin Li9f645ae2016-07-18 22:49:16 +000012592 Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
Alexey Bataeve3727102018-04-18 15:57:46 +000012593 } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(
David Majnemer9d168222016-08-05 17:44:54 +000012594 SI->getAssociatedExpression())) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012595 const Expr *E = OASE->getBase()->IgnoreParenImpCasts();
Kelvin Li9f645ae2016-07-18 22:49:16 +000012596 Type =
12597 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
12598 }
12599 if (Type.isNull() || Type->isAnyPointerType() ||
Alexey Bataeve3727102018-04-18 15:57:46 +000012600 checkArrayExpressionDoesNotReferToWholeSize(
Kelvin Li9f645ae2016-07-18 22:49:16 +000012601 SemaRef, SI->getAssociatedExpression(), Type))
12602 break;
12603 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012604
12605 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
12606 // List items of map clauses in the same construct must not share
12607 // original storage.
12608 //
12609 // If the expressions are exactly the same or one is a subset of the
12610 // other, it means they are sharing storage.
12611 if (CI == CE && SI == SE) {
12612 if (CurrentRegionOnly) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012613 if (CKind == OMPC_map) {
Samuel Antao661c0902016-05-26 17:39:58 +000012614 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +000012615 } else {
Samuel Antaoec172c62016-05-26 17:49:04 +000012616 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000012617 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
12618 << ERange;
12619 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012620 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12621 << RE->getSourceRange();
12622 return true;
Samuel Antao5de996e2016-01-22 20:21:36 +000012623 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012624 // If we find the same expression in the enclosing data environment,
12625 // that is legal.
12626 IsEnclosedByDataEnvironmentExpr = true;
12627 return false;
Samuel Antao5de996e2016-01-22 20:21:36 +000012628 }
12629
Samuel Antao90927002016-04-26 14:54:23 +000012630 QualType DerivedType =
12631 std::prev(CI)->getAssociatedDeclaration()->getType();
12632 SourceLocation DerivedLoc =
12633 std::prev(CI)->getAssociatedExpression()->getExprLoc();
Samuel Antao5de996e2016-01-22 20:21:36 +000012634
12635 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12636 // If the type of a list item is a reference to a type T then the type
12637 // will be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000012638 DerivedType = DerivedType.getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000012639
12640 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
12641 // A variable for which the type is pointer and an array section
12642 // derived from that variable must not appear as list items of map
12643 // clauses of the same construct.
12644 //
12645 // Also, cover one of the cases in:
12646 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
12647 // If any part of the original storage of a list item has corresponding
12648 // storage in the device data environment, all of the original storage
12649 // must have corresponding storage in the device data environment.
12650 //
12651 if (DerivedType->isAnyPointerType()) {
12652 if (CI == CE || SI == SE) {
12653 SemaRef.Diag(
12654 DerivedLoc,
12655 diag::err_omp_pointer_mapped_along_with_derived_section)
12656 << DerivedLoc;
Alexey Bataev2819260b2018-02-27 17:42:00 +000012657 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12658 << RE->getSourceRange();
12659 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +000012660 }
12661 if (CI->getAssociatedExpression()->getStmtClass() !=
Alexey Bataev2819260b2018-02-27 17:42:00 +000012662 SI->getAssociatedExpression()->getStmtClass() ||
12663 CI->getAssociatedDeclaration()->getCanonicalDecl() ==
12664 SI->getAssociatedDeclaration()->getCanonicalDecl()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012665 assert(CI != CE && SI != SE);
Alexey Bataev2819260b2018-02-27 17:42:00 +000012666 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced)
Samuel Antao5de996e2016-01-22 20:21:36 +000012667 << DerivedLoc;
Alexey Bataev2819260b2018-02-27 17:42:00 +000012668 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12669 << RE->getSourceRange();
12670 return true;
Samuel Antao5de996e2016-01-22 20:21:36 +000012671 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012672 }
12673
12674 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
12675 // List items of map clauses in the same construct must not share
12676 // original storage.
12677 //
12678 // An expression is a subset of the other.
12679 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012680 if (CKind == OMPC_map) {
Alexey Bataeve82445f2018-09-20 13:54:02 +000012681 if (CI != CE || SI != SE) {
12682 // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is
12683 // a pointer.
12684 auto Begin =
12685 CI != CE ? CurComponents.begin() : StackComponents.begin();
12686 auto End = CI != CE ? CurComponents.end() : StackComponents.end();
12687 auto It = Begin;
12688 while (It != End && !It->getAssociatedDeclaration())
12689 std::advance(It, 1);
12690 assert(It != End &&
12691 "Expected at least one component with the declaration.");
12692 if (It != Begin && It->getAssociatedDeclaration()
12693 ->getType()
12694 .getCanonicalType()
12695 ->isAnyPointerType()) {
12696 IsEnclosedByDataEnvironmentExpr = false;
12697 EnclosingExpr = nullptr;
12698 return false;
12699 }
12700 }
Samuel Antao661c0902016-05-26 17:39:58 +000012701 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +000012702 } else {
Samuel Antaoec172c62016-05-26 17:49:04 +000012703 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000012704 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
12705 << ERange;
12706 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012707 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12708 << RE->getSourceRange();
12709 return true;
12710 }
12711
12712 // The current expression uses the same base as other expression in the
Samuel Antao90927002016-04-26 14:54:23 +000012713 // data environment but does not contain it completely.
Samuel Antao5de996e2016-01-22 20:21:36 +000012714 if (!CurrentRegionOnly && SI != SE)
12715 EnclosingExpr = RE;
12716
12717 // The current expression is a subset of the expression in the data
12718 // environment.
12719 IsEnclosedByDataEnvironmentExpr |=
12720 (!CurrentRegionOnly && CI != CE && SI == SE);
12721
12722 return false;
12723 });
12724
12725 if (CurrentRegionOnly)
12726 return FoundError;
12727
12728 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
12729 // If any part of the original storage of a list item has corresponding
12730 // storage in the device data environment, all of the original storage must
12731 // have corresponding storage in the device data environment.
12732 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
12733 // If a list item is an element of a structure, and a different element of
12734 // the structure has a corresponding list item in the device data environment
12735 // prior to a task encountering the construct associated with the map clause,
Samuel Antao90927002016-04-26 14:54:23 +000012736 // then the list item must also have a corresponding list item in the device
Samuel Antao5de996e2016-01-22 20:21:36 +000012737 // data environment prior to the task encountering the construct.
12738 //
12739 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
12740 SemaRef.Diag(ELoc,
12741 diag::err_omp_original_storage_is_shared_and_does_not_contain)
12742 << ERange;
12743 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
12744 << EnclosingExpr->getSourceRange();
12745 return true;
12746 }
12747
12748 return FoundError;
12749}
12750
Samuel Antao661c0902016-05-26 17:39:58 +000012751namespace {
12752// Utility struct that gathers all the related lists associated with a mappable
12753// expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000012754struct MappableVarListInfo {
Samuel Antao661c0902016-05-26 17:39:58 +000012755 // The list of expressions.
12756 ArrayRef<Expr *> VarList;
12757 // The list of processed expressions.
12758 SmallVector<Expr *, 16> ProcessedVarList;
12759 // The mappble components for each expression.
12760 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
12761 // The base declaration of the variable.
12762 SmallVector<ValueDecl *, 16> VarBaseDeclarations;
12763
12764 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
12765 // We have a list of components and base declarations for each entry in the
12766 // variable list.
12767 VarComponents.reserve(VarList.size());
12768 VarBaseDeclarations.reserve(VarList.size());
12769 }
12770};
12771}
12772
12773// Check the validity of the provided variable list for the provided clause kind
12774// \a CKind. In the check process the valid expressions, and mappable expression
12775// components and variables are extracted and used to fill \a Vars,
12776// \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and
12777// \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'.
12778static void
12779checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS,
12780 OpenMPClauseKind CKind, MappableVarListInfo &MVLI,
12781 SourceLocation StartLoc,
12782 OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
12783 bool IsMapTypeImplicit = false) {
Samuel Antaoec172c62016-05-26 17:49:04 +000012784 // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
12785 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
Samuel Antao661c0902016-05-26 17:39:58 +000012786 "Unexpected clause kind with mappable expressions!");
Kelvin Li0bff7af2015-11-23 05:32:03 +000012787
Samuel Antao90927002016-04-26 14:54:23 +000012788 // Keep track of the mappable components and base declarations in this clause.
12789 // Each entry in the list is going to have a list of components associated. We
12790 // record each set of the components so that we can build the clause later on.
12791 // In the end we should have the same amount of declarations and component
12792 // lists.
Samuel Antao90927002016-04-26 14:54:23 +000012793
Alexey Bataeve3727102018-04-18 15:57:46 +000012794 for (Expr *RE : MVLI.VarList) {
Samuel Antaoec172c62016-05-26 17:49:04 +000012795 assert(RE && "Null expr in omp to/from/map clause");
Kelvin Li0bff7af2015-11-23 05:32:03 +000012796 SourceLocation ELoc = RE->getExprLoc();
12797
Alexey Bataeve3727102018-04-18 15:57:46 +000012798 const Expr *VE = RE->IgnoreParenLValueCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000012799
12800 if (VE->isValueDependent() || VE->isTypeDependent() ||
12801 VE->isInstantiationDependent() ||
12802 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012803 // We can only analyze this information once the missing information is
12804 // resolved.
Samuel Antao661c0902016-05-26 17:39:58 +000012805 MVLI.ProcessedVarList.push_back(RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012806 continue;
12807 }
12808
Alexey Bataeve3727102018-04-18 15:57:46 +000012809 Expr *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000012810
Samuel Antao5de996e2016-01-22 20:21:36 +000012811 if (!RE->IgnoreParenImpCasts()->isLValue()) {
Samuel Antao661c0902016-05-26 17:39:58 +000012812 SemaRef.Diag(ELoc,
12813 diag::err_omp_expected_named_var_member_or_array_expression)
Samuel Antao5de996e2016-01-22 20:21:36 +000012814 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000012815 continue;
12816 }
12817
Samuel Antao90927002016-04-26 14:54:23 +000012818 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
12819 ValueDecl *CurDeclaration = nullptr;
12820
12821 // Obtain the array or member expression bases if required. Also, fill the
12822 // components array with all the components identified in the process.
Alexey Bataeve3727102018-04-18 15:57:46 +000012823 const Expr *BE = checkMapClauseExpressionBase(
12824 SemaRef, SimpleExpr, CurComponents, CKind, /*NoDiagnose=*/false);
Samuel Antao5de996e2016-01-22 20:21:36 +000012825 if (!BE)
12826 continue;
12827
Samuel Antao90927002016-04-26 14:54:23 +000012828 assert(!CurComponents.empty() &&
12829 "Invalid mappable expression information.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000012830
Samuel Antao90927002016-04-26 14:54:23 +000012831 // For the following checks, we rely on the base declaration which is
12832 // expected to be associated with the last component. The declaration is
12833 // expected to be a variable or a field (if 'this' is being mapped).
12834 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
12835 assert(CurDeclaration && "Null decl on map clause.");
12836 assert(
12837 CurDeclaration->isCanonicalDecl() &&
12838 "Expecting components to have associated only canonical declarations.");
12839
12840 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
Alexey Bataeve3727102018-04-18 15:57:46 +000012841 const auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
Samuel Antao5de996e2016-01-22 20:21:36 +000012842
12843 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000012844 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000012845
12846 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
Samuel Antao661c0902016-05-26 17:39:58 +000012847 // threadprivate variables cannot appear in a map clause.
12848 // OpenMP 4.5 [2.10.5, target update Construct]
12849 // threadprivate variables cannot appear in a from clause.
12850 if (VD && DSAS->isThreadPrivate(VD)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012851 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
Samuel Antao661c0902016-05-26 17:39:58 +000012852 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
12853 << getOpenMPClauseName(CKind);
Alexey Bataeve3727102018-04-18 15:57:46 +000012854 reportOriginalDsa(SemaRef, DSAS, VD, DVar);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012855 continue;
12856 }
12857
Samuel Antao5de996e2016-01-22 20:21:36 +000012858 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
12859 // A list item cannot appear in both a map clause and a data-sharing
12860 // attribute clause on the same construct.
Kelvin Li0bff7af2015-11-23 05:32:03 +000012861
Samuel Antao5de996e2016-01-22 20:21:36 +000012862 // Check conflicts with other map clause expressions. We check the conflicts
12863 // with the current construct separately from the enclosing data
Samuel Antao661c0902016-05-26 17:39:58 +000012864 // environment, because the restrictions are different. We only have to
12865 // check conflicts across regions for the map clauses.
Alexey Bataeve3727102018-04-18 15:57:46 +000012866 if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
Samuel Antao661c0902016-05-26 17:39:58 +000012867 /*CurrentRegionOnly=*/true, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000012868 break;
Samuel Antao661c0902016-05-26 17:39:58 +000012869 if (CKind == OMPC_map &&
Alexey Bataeve3727102018-04-18 15:57:46 +000012870 checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
Samuel Antao661c0902016-05-26 17:39:58 +000012871 /*CurrentRegionOnly=*/false, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000012872 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000012873
Samuel Antao661c0902016-05-26 17:39:58 +000012874 // OpenMP 4.5 [2.10.5, target update Construct]
Samuel Antao5de996e2016-01-22 20:21:36 +000012875 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12876 // If the type of a list item is a reference to a type T then the type will
12877 // be considered to be T for all purposes of this clause.
Alexey Bataev354df2e2018-05-02 18:44:10 +000012878 auto I = llvm::find_if(
12879 CurComponents,
12880 [](const OMPClauseMappableExprCommon::MappableComponent &MC) {
12881 return MC.getAssociatedDeclaration();
12882 });
12883 assert(I != CurComponents.end() && "Null decl on map clause.");
12884 QualType Type =
12885 I->getAssociatedDeclaration()->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000012886
Samuel Antao661c0902016-05-26 17:39:58 +000012887 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
12888 // A list item in a to or from clause must have a mappable type.
Samuel Antao5de996e2016-01-22 20:21:36 +000012889 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000012890 // A list item must have a mappable type.
Alexey Bataeve3727102018-04-18 15:57:46 +000012891 if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
Samuel Antao661c0902016-05-26 17:39:58 +000012892 DSAS, Type))
Kelvin Li0bff7af2015-11-23 05:32:03 +000012893 continue;
12894
Samuel Antao661c0902016-05-26 17:39:58 +000012895 if (CKind == OMPC_map) {
12896 // target enter data
12897 // OpenMP [2.10.2, Restrictions, p. 99]
12898 // A map-type must be specified in all map clauses and must be either
12899 // to or alloc.
12900 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
12901 if (DKind == OMPD_target_enter_data &&
12902 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
12903 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
12904 << (IsMapTypeImplicit ? 1 : 0)
12905 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
12906 << getOpenMPDirectiveName(DKind);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000012907 continue;
12908 }
Samuel Antao661c0902016-05-26 17:39:58 +000012909
12910 // target exit_data
12911 // OpenMP [2.10.3, Restrictions, p. 102]
12912 // A map-type must be specified in all map clauses and must be either
12913 // from, release, or delete.
12914 if (DKind == OMPD_target_exit_data &&
12915 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
12916 MapType == OMPC_MAP_delete)) {
12917 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
12918 << (IsMapTypeImplicit ? 1 : 0)
12919 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
12920 << getOpenMPDirectiveName(DKind);
12921 continue;
12922 }
12923
12924 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
12925 // A list item cannot appear in both a map clause and a data-sharing
12926 // attribute clause on the same construct
Alexey Bataeve3727102018-04-18 15:57:46 +000012927 if (VD && isOpenMPTargetExecutionDirective(DKind)) {
12928 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
Samuel Antao661c0902016-05-26 17:39:58 +000012929 if (isOpenMPPrivate(DVar.CKind)) {
Samuel Antao6890b092016-07-28 14:25:09 +000012930 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Samuel Antao661c0902016-05-26 17:39:58 +000012931 << getOpenMPClauseName(DVar.CKind)
Samuel Antao6890b092016-07-28 14:25:09 +000012932 << getOpenMPClauseName(OMPC_map)
Samuel Antao661c0902016-05-26 17:39:58 +000012933 << getOpenMPDirectiveName(DSAS->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000012934 reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar);
Samuel Antao661c0902016-05-26 17:39:58 +000012935 continue;
12936 }
12937 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +000012938 }
12939
Samuel Antao90927002016-04-26 14:54:23 +000012940 // Save the current expression.
Samuel Antao661c0902016-05-26 17:39:58 +000012941 MVLI.ProcessedVarList.push_back(RE);
Samuel Antao90927002016-04-26 14:54:23 +000012942
12943 // Store the components in the stack so that they can be used to check
12944 // against other clauses later on.
Samuel Antao6890b092016-07-28 14:25:09 +000012945 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents,
12946 /*WhereFoundClauseKind=*/OMPC_map);
Samuel Antao90927002016-04-26 14:54:23 +000012947
12948 // Save the components and declaration to create the clause. For purposes of
12949 // the clause creation, any component list that has has base 'this' uses
Samuel Antao686c70c2016-05-26 17:30:50 +000012950 // null as base declaration.
Samuel Antao661c0902016-05-26 17:39:58 +000012951 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
12952 MVLI.VarComponents.back().append(CurComponents.begin(),
12953 CurComponents.end());
12954 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
12955 : CurDeclaration);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012956 }
Samuel Antao661c0902016-05-26 17:39:58 +000012957}
12958
12959OMPClause *
12960Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
12961 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
12962 SourceLocation MapLoc, SourceLocation ColonLoc,
12963 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
12964 SourceLocation LParenLoc, SourceLocation EndLoc) {
12965 MappableVarListInfo MVLI(VarList);
12966 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc,
12967 MapType, IsMapTypeImplicit);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012968
Samuel Antao5de996e2016-01-22 20:21:36 +000012969 // We need to produce a map clause even if we don't have variables so that
12970 // other diagnostics related with non-existing map clauses are accurate.
Samuel Antao661c0902016-05-26 17:39:58 +000012971 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc,
12972 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
12973 MVLI.VarComponents, MapTypeModifier, MapType,
12974 IsMapTypeImplicit, MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012975}
Kelvin Li099bb8c2015-11-24 20:50:12 +000012976
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012977QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
12978 TypeResult ParsedType) {
12979 assert(ParsedType.isUsable());
12980
12981 QualType ReductionType = GetTypeFromParser(ParsedType.get());
12982 if (ReductionType.isNull())
12983 return QualType();
12984
12985 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
12986 // A type name in a declare reduction directive cannot be a function type, an
12987 // array type, a reference type, or a type qualified with const, volatile or
12988 // restrict.
12989 if (ReductionType.hasQualifiers()) {
12990 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
12991 return QualType();
12992 }
12993
12994 if (ReductionType->isFunctionType()) {
12995 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
12996 return QualType();
12997 }
12998 if (ReductionType->isReferenceType()) {
12999 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
13000 return QualType();
13001 }
13002 if (ReductionType->isArrayType()) {
13003 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
13004 return QualType();
13005 }
13006 return ReductionType;
13007}
13008
13009Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
13010 Scope *S, DeclContext *DC, DeclarationName Name,
13011 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
13012 AccessSpecifier AS, Decl *PrevDeclInScope) {
13013 SmallVector<Decl *, 8> Decls;
13014 Decls.reserve(ReductionTypes.size());
13015
13016 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
Richard Smithbecb92d2017-10-10 22:33:17 +000013017 forRedeclarationInCurContext());
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013018 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
13019 // A reduction-identifier may not be re-declared in the current scope for the
13020 // same type or for a type that is compatible according to the base language
13021 // rules.
13022 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
13023 OMPDeclareReductionDecl *PrevDRD = nullptr;
13024 bool InCompoundScope = true;
13025 if (S != nullptr) {
13026 // Find previous declaration with the same name not referenced in other
13027 // declarations.
13028 FunctionScopeInfo *ParentFn = getEnclosingFunction();
13029 InCompoundScope =
13030 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
13031 LookupName(Lookup, S);
13032 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
13033 /*AllowInlineNamespace=*/false);
13034 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
Alexey Bataeve3727102018-04-18 15:57:46 +000013035 LookupResult::Filter Filter = Lookup.makeFilter();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013036 while (Filter.hasNext()) {
13037 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
13038 if (InCompoundScope) {
13039 auto I = UsedAsPrevious.find(PrevDecl);
13040 if (I == UsedAsPrevious.end())
13041 UsedAsPrevious[PrevDecl] = false;
Alexey Bataeve3727102018-04-18 15:57:46 +000013042 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope())
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013043 UsedAsPrevious[D] = true;
13044 }
13045 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
13046 PrevDecl->getLocation();
13047 }
13048 Filter.done();
13049 if (InCompoundScope) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013050 for (const auto &PrevData : UsedAsPrevious) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013051 if (!PrevData.second) {
13052 PrevDRD = PrevData.first;
13053 break;
13054 }
13055 }
13056 }
13057 } else if (PrevDeclInScope != nullptr) {
13058 auto *PrevDRDInScope = PrevDRD =
13059 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
13060 do {
13061 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
13062 PrevDRDInScope->getLocation();
13063 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
13064 } while (PrevDRDInScope != nullptr);
13065 }
Alexey Bataeve3727102018-04-18 15:57:46 +000013066 for (const auto &TyData : ReductionTypes) {
13067 const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013068 bool Invalid = false;
13069 if (I != PreviousRedeclTypes.end()) {
13070 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
13071 << TyData.first;
13072 Diag(I->second, diag::note_previous_definition);
13073 Invalid = true;
13074 }
13075 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
13076 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
13077 Name, TyData.first, PrevDRD);
13078 DC->addDecl(DRD);
13079 DRD->setAccess(AS);
13080 Decls.push_back(DRD);
13081 if (Invalid)
13082 DRD->setInvalidDecl();
13083 else
13084 PrevDRD = DRD;
13085 }
13086
13087 return DeclGroupPtrTy::make(
13088 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
13089}
13090
13091void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
13092 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13093
13094 // Enter new function scope.
13095 PushFunctionScope();
Reid Kleckner87a31802018-03-12 21:43:02 +000013096 setFunctionHasBranchProtectedScope();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013097 getCurFunction()->setHasOMPDeclareReductionCombiner();
13098
13099 if (S != nullptr)
13100 PushDeclContext(S, DRD);
13101 else
13102 CurContext = DRD;
13103
Faisal Valid143a0c2017-04-01 21:30:49 +000013104 PushExpressionEvaluationContext(
13105 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013106
13107 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013108 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
13109 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
13110 // uses semantics of argument handles by value, but it should be passed by
13111 // reference. C lang does not support references, so pass all parameters as
13112 // pointers.
13113 // Create 'T omp_in;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000013114 VarDecl *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013115 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013116 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
13117 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
13118 // uses semantics of argument handles by value, but it should be passed by
13119 // reference. C lang does not support references, so pass all parameters as
13120 // pointers.
13121 // Create 'T omp_out;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000013122 VarDecl *OmpOutParm =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013123 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
13124 if (S != nullptr) {
13125 PushOnScopeChains(OmpInParm, S);
13126 PushOnScopeChains(OmpOutParm, S);
13127 } else {
13128 DRD->addDecl(OmpInParm);
13129 DRD->addDecl(OmpOutParm);
13130 }
Alexey Bataeve6aa4692018-09-13 16:54:05 +000013131 Expr *InE =
13132 ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation());
13133 Expr *OutE =
13134 ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation());
13135 DRD->setCombinerData(InE, OutE);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013136}
13137
13138void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
13139 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13140 DiscardCleanupsInEvaluationContext();
13141 PopExpressionEvaluationContext();
13142
13143 PopDeclContext();
13144 PopFunctionScopeInfo();
13145
13146 if (Combiner != nullptr)
13147 DRD->setCombiner(Combiner);
13148 else
13149 DRD->setInvalidDecl();
13150}
13151
Alexey Bataev070f43a2017-09-06 14:49:58 +000013152VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013153 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13154
13155 // Enter new function scope.
13156 PushFunctionScope();
Reid Kleckner87a31802018-03-12 21:43:02 +000013157 setFunctionHasBranchProtectedScope();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013158
13159 if (S != nullptr)
13160 PushDeclContext(S, DRD);
13161 else
13162 CurContext = DRD;
13163
Faisal Valid143a0c2017-04-01 21:30:49 +000013164 PushExpressionEvaluationContext(
13165 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013166
13167 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013168 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
13169 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
13170 // uses semantics of argument handles by value, but it should be passed by
13171 // reference. C lang does not support references, so pass all parameters as
13172 // pointers.
13173 // Create 'T omp_priv;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000013174 VarDecl *OmpPrivParm =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013175 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013176 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
13177 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
13178 // uses semantics of argument handles by value, but it should be passed by
13179 // reference. C lang does not support references, so pass all parameters as
13180 // pointers.
13181 // Create 'T omp_orig;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000013182 VarDecl *OmpOrigParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013183 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013184 if (S != nullptr) {
13185 PushOnScopeChains(OmpPrivParm, S);
13186 PushOnScopeChains(OmpOrigParm, S);
13187 } else {
13188 DRD->addDecl(OmpPrivParm);
13189 DRD->addDecl(OmpOrigParm);
13190 }
Alexey Bataeve6aa4692018-09-13 16:54:05 +000013191 Expr *OrigE =
13192 ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation());
13193 Expr *PrivE =
13194 ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation());
13195 DRD->setInitializerData(OrigE, PrivE);
Alexey Bataev070f43a2017-09-06 14:49:58 +000013196 return OmpPrivParm;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013197}
13198
Alexey Bataev070f43a2017-09-06 14:49:58 +000013199void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer,
13200 VarDecl *OmpPrivParm) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013201 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13202 DiscardCleanupsInEvaluationContext();
13203 PopExpressionEvaluationContext();
13204
13205 PopDeclContext();
13206 PopFunctionScopeInfo();
13207
Alexey Bataev070f43a2017-09-06 14:49:58 +000013208 if (Initializer != nullptr) {
13209 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit);
13210 } else if (OmpPrivParm->hasInit()) {
13211 DRD->setInitializer(OmpPrivParm->getInit(),
13212 OmpPrivParm->isDirectInit()
13213 ? OMPDeclareReductionDecl::DirectInit
13214 : OMPDeclareReductionDecl::CopyInit);
13215 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013216 DRD->setInvalidDecl();
Alexey Bataev070f43a2017-09-06 14:49:58 +000013217 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013218}
13219
13220Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
13221 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013222 for (Decl *D : DeclReductions.get()) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013223 if (IsValid) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013224 if (S)
13225 PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S,
13226 /*AddToContext=*/false);
13227 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013228 D->setInvalidDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000013229 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013230 }
13231 return DeclReductions;
13232}
13233
David Majnemer9d168222016-08-05 17:44:54 +000013234OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
Kelvin Li099bb8c2015-11-24 20:50:12 +000013235 SourceLocation StartLoc,
13236 SourceLocation LParenLoc,
13237 SourceLocation EndLoc) {
13238 Expr *ValExpr = NumTeams;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000013239 Stmt *HelperValStmt = nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000013240
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013241 // OpenMP [teams Constrcut, Restrictions]
13242 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000013243 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
Alexey Bataeva0569352015-12-01 10:17:31 +000013244 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013245 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000013246
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000013247 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000013248 OpenMPDirectiveKind CaptureRegion =
13249 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams);
13250 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000013251 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000013252 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000013253 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
13254 HelperValStmt = buildPreInits(Context, Captures);
13255 }
13256
13257 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion,
13258 StartLoc, LParenLoc, EndLoc);
Kelvin Li099bb8c2015-11-24 20:50:12 +000013259}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013260
13261OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
13262 SourceLocation StartLoc,
13263 SourceLocation LParenLoc,
13264 SourceLocation EndLoc) {
13265 Expr *ValExpr = ThreadLimit;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000013266 Stmt *HelperValStmt = nullptr;
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013267
13268 // OpenMP [teams Constrcut, Restrictions]
13269 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000013270 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
Alexey Bataeva0569352015-12-01 10:17:31 +000013271 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013272 return nullptr;
13273
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000013274 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000013275 OpenMPDirectiveKind CaptureRegion =
13276 getOpenMPCaptureRegionForClause(DKind, OMPC_thread_limit);
13277 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000013278 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000013279 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000013280 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
13281 HelperValStmt = buildPreInits(Context, Captures);
13282 }
13283
13284 return new (Context) OMPThreadLimitClause(
13285 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013286}
Alexey Bataeva0569352015-12-01 10:17:31 +000013287
13288OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
13289 SourceLocation StartLoc,
13290 SourceLocation LParenLoc,
13291 SourceLocation EndLoc) {
13292 Expr *ValExpr = Priority;
13293
13294 // OpenMP [2.9.1, task Constrcut]
13295 // The priority-value is a non-negative numerical scalar expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000013296 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
Alexey Bataeva0569352015-12-01 10:17:31 +000013297 /*StrictlyPositive=*/false))
13298 return nullptr;
13299
13300 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
13301}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000013302
13303OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
13304 SourceLocation StartLoc,
13305 SourceLocation LParenLoc,
13306 SourceLocation EndLoc) {
13307 Expr *ValExpr = Grainsize;
13308
13309 // OpenMP [2.9.2, taskloop Constrcut]
13310 // The parameter of the grainsize clause must be a positive integer
13311 // expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000013312 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000013313 /*StrictlyPositive=*/true))
13314 return nullptr;
13315
13316 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
13317}
Alexey Bataev382967a2015-12-08 12:06:20 +000013318
13319OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
13320 SourceLocation StartLoc,
13321 SourceLocation LParenLoc,
13322 SourceLocation EndLoc) {
13323 Expr *ValExpr = NumTasks;
13324
13325 // OpenMP [2.9.2, taskloop Constrcut]
13326 // The parameter of the num_tasks clause must be a positive integer
13327 // expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000013328 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
Alexey Bataev382967a2015-12-08 12:06:20 +000013329 /*StrictlyPositive=*/true))
13330 return nullptr;
13331
13332 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
13333}
13334
Alexey Bataev28c75412015-12-15 08:19:24 +000013335OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
13336 SourceLocation LParenLoc,
13337 SourceLocation EndLoc) {
13338 // OpenMP [2.13.2, critical construct, Description]
13339 // ... where hint-expression is an integer constant expression that evaluates
13340 // to a valid lock hint.
13341 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
13342 if (HintExpr.isInvalid())
13343 return nullptr;
13344 return new (Context)
13345 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
13346}
13347
Carlo Bertollib4adf552016-01-15 18:50:31 +000013348OMPClause *Sema::ActOnOpenMPDistScheduleClause(
13349 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
13350 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
13351 SourceLocation EndLoc) {
13352 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
13353 std::string Values;
13354 Values += "'";
13355 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
13356 Values += "'";
13357 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
13358 << Values << getOpenMPClauseName(OMPC_dist_schedule);
13359 return nullptr;
13360 }
13361 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000013362 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000013363 if (ChunkSize) {
13364 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
13365 !ChunkSize->isInstantiationDependent() &&
13366 !ChunkSize->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000013367 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
Carlo Bertollib4adf552016-01-15 18:50:31 +000013368 ExprResult Val =
13369 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
13370 if (Val.isInvalid())
13371 return nullptr;
13372
13373 ValExpr = Val.get();
13374
13375 // OpenMP [2.7.1, Restrictions]
13376 // chunk_size must be a loop invariant integer expression with a positive
13377 // value.
13378 llvm::APSInt Result;
13379 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
13380 if (Result.isSigned() && !Result.isStrictlyPositive()) {
13381 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
13382 << "dist_schedule" << ChunkSize->getSourceRange();
13383 return nullptr;
13384 }
Alexey Bataev2ba67042017-11-28 21:11:44 +000013385 } else if (getOpenMPCaptureRegionForClause(
13386 DSAStack->getCurrentDirective(), OMPC_dist_schedule) !=
13387 OMPD_unknown &&
Alexey Bataevb46cdea2016-06-15 11:20:48 +000013388 !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000013389 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000013390 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev5a3af132016-03-29 08:58:54 +000013391 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
13392 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000013393 }
13394 }
13395 }
13396
13397 return new (Context)
13398 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000013399 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000013400}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013401
13402OMPClause *Sema::ActOnOpenMPDefaultmapClause(
13403 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
13404 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
13405 SourceLocation KindLoc, SourceLocation EndLoc) {
13406 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
David Majnemer9d168222016-08-05 17:44:54 +000013407 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) {
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013408 std::string Value;
13409 SourceLocation Loc;
13410 Value += "'";
13411 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
13412 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000013413 OMPC_DEFAULTMAP_MODIFIER_tofrom);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013414 Loc = MLoc;
13415 } else {
13416 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000013417 OMPC_DEFAULTMAP_scalar);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013418 Loc = KindLoc;
13419 }
13420 Value += "'";
13421 Diag(Loc, diag::err_omp_unexpected_clause_value)
13422 << Value << getOpenMPClauseName(OMPC_defaultmap);
13423 return nullptr;
13424 }
Alexey Bataev2fd0cb22017-10-05 17:51:39 +000013425 DSAStack->setDefaultDMAToFromScalar(StartLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013426
13427 return new (Context)
13428 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
13429}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013430
13431bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
13432 DeclContext *CurLexicalContext = getCurLexicalContext();
13433 if (!CurLexicalContext->isFileContext() &&
13434 !CurLexicalContext->isExternCContext() &&
Alexey Bataev502ec492017-10-03 20:00:00 +000013435 !CurLexicalContext->isExternCXXContext() &&
13436 !isa<CXXRecordDecl>(CurLexicalContext) &&
13437 !isa<ClassTemplateDecl>(CurLexicalContext) &&
13438 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) &&
13439 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013440 Diag(Loc, diag::err_omp_region_not_file_context);
13441 return false;
13442 }
Kelvin Libc38e632018-09-10 02:07:09 +000013443 ++DeclareTargetNestingLevel;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013444 return true;
13445}
13446
13447void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
Kelvin Libc38e632018-09-10 02:07:09 +000013448 assert(DeclareTargetNestingLevel > 0 &&
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013449 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
Kelvin Libc38e632018-09-10 02:07:09 +000013450 --DeclareTargetNestingLevel;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013451}
13452
David Majnemer9d168222016-08-05 17:44:54 +000013453void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope,
13454 CXXScopeSpec &ScopeSpec,
13455 const DeclarationNameInfo &Id,
13456 OMPDeclareTargetDeclAttr::MapTypeTy MT,
13457 NamedDeclSetType &SameDirectiveDecls) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013458 LookupResult Lookup(*this, Id, LookupOrdinaryName);
13459 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
13460
13461 if (Lookup.isAmbiguous())
13462 return;
13463 Lookup.suppressDiagnostics();
13464
13465 if (!Lookup.isSingleResult()) {
13466 if (TypoCorrection Corrected =
13467 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
13468 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
13469 CTK_ErrorRecovery)) {
13470 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
13471 << Id.getName());
13472 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
13473 return;
13474 }
13475
13476 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
13477 return;
13478 }
13479
13480 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
Alexey Bataev30a78212018-09-11 13:59:10 +000013481 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND) ||
13482 isa<FunctionTemplateDecl>(ND)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013483 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
13484 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
Alexey Bataev30a78212018-09-11 13:59:10 +000013485 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
13486 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
13487 cast<ValueDecl>(ND));
13488 if (!Res) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013489 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013490 ND->addAttr(A);
13491 if (ASTMutationListener *ML = Context.getASTMutationListener())
13492 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
Kelvin Li1ce87c72017-12-12 20:08:12 +000013493 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Id.getLoc());
Alexey Bataev30a78212018-09-11 13:59:10 +000013494 } else if (*Res != MT) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013495 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
13496 << Id.getName();
13497 }
Alexey Bataeve3727102018-04-18 15:57:46 +000013498 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013499 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
Alexey Bataeve3727102018-04-18 15:57:46 +000013500 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013501}
13502
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013503static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
13504 Sema &SemaRef, Decl *D) {
Alexey Bataev30a78212018-09-11 13:59:10 +000013505 if (!D || !isa<VarDecl>(D))
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013506 return;
Alexey Bataev30a78212018-09-11 13:59:10 +000013507 auto *VD = cast<VarDecl>(D);
13508 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
13509 return;
13510 SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context);
13511 SemaRef.Diag(SL, diag::note_used_here) << SR;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013512}
13513
13514static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
13515 Sema &SemaRef, DSAStackTy *Stack,
13516 ValueDecl *VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013517 return VD->hasAttr<OMPDeclareTargetDeclAttr>() ||
13518 checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(),
13519 /*FullCheck=*/false);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013520}
13521
Kelvin Li1ce87c72017-12-12 20:08:12 +000013522void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D,
13523 SourceLocation IdLoc) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013524 if (!D || D->isInvalidDecl())
13525 return;
13526 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
Stephen Kellyf2ceec42018-08-09 21:08:08 +000013527 SourceLocation SL = E ? E->getBeginLoc() : D->getLocation();
Alexey Bataeve3727102018-04-18 15:57:46 +000013528 if (auto *VD = dyn_cast<VarDecl>(D)) {
Alexey Bataevc1943e72018-07-09 19:58:08 +000013529 // Only global variables can be marked as declare target.
Alexey Bataev30a78212018-09-11 13:59:10 +000013530 if (!VD->isFileVarDecl() && !VD->isStaticLocal() &&
13531 !VD->isStaticDataMember())
Alexey Bataevc1943e72018-07-09 19:58:08 +000013532 return;
13533 // 2.10.6: threadprivate variable cannot appear in a declare target
13534 // directive.
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013535 if (DSAStack->isThreadPrivate(VD)) {
13536 Diag(SL, diag::err_omp_threadprivate_in_target);
Alexey Bataeve3727102018-04-18 15:57:46 +000013537 reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013538 return;
13539 }
13540 }
Alexey Bataev97b72212018-08-14 18:31:20 +000013541 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
13542 D = FTD->getTemplatedDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000013543 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Alexey Bataev30a78212018-09-11 13:59:10 +000013544 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
13545 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD);
13546 if (Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) {
Kelvin Li1ce87c72017-12-12 20:08:12 +000013547 assert(IdLoc.isValid() && "Source location is expected");
13548 Diag(IdLoc, diag::err_omp_function_in_link_clause);
13549 Diag(FD->getLocation(), diag::note_defined_here) << FD;
13550 return;
13551 }
13552 }
Alexey Bataev30a78212018-09-11 13:59:10 +000013553 if (auto *VD = dyn_cast<ValueDecl>(D)) {
13554 // Problem if any with var declared with incomplete type will be reported
13555 // as normal, so no need to check it here.
13556 if ((E || !VD->getType()->isIncompleteType()) &&
13557 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD))
13558 return;
13559 if (!E && !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) {
13560 // Checking declaration inside declare target region.
13561 if (isa<VarDecl>(D) || isa<FunctionDecl>(D) ||
13562 isa<FunctionTemplateDecl>(D)) {
13563 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
13564 Context, OMPDeclareTargetDeclAttr::MT_To);
13565 D->addAttr(A);
13566 if (ASTMutationListener *ML = Context.getASTMutationListener())
13567 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
13568 }
13569 return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013570 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013571 }
Alexey Bataev30a78212018-09-11 13:59:10 +000013572 if (!E)
13573 return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013574 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
13575}
Samuel Antao661c0902016-05-26 17:39:58 +000013576
13577OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
13578 SourceLocation StartLoc,
13579 SourceLocation LParenLoc,
13580 SourceLocation EndLoc) {
13581 MappableVarListInfo MVLI(VarList);
13582 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc);
13583 if (MVLI.ProcessedVarList.empty())
13584 return nullptr;
13585
13586 return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13587 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
13588 MVLI.VarComponents);
13589}
Samuel Antaoec172c62016-05-26 17:49:04 +000013590
13591OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
13592 SourceLocation StartLoc,
13593 SourceLocation LParenLoc,
13594 SourceLocation EndLoc) {
13595 MappableVarListInfo MVLI(VarList);
13596 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc);
13597 if (MVLI.ProcessedVarList.empty())
13598 return nullptr;
13599
13600 return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13601 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
13602 MVLI.VarComponents);
13603}
Carlo Bertolli2404b172016-07-13 15:37:16 +000013604
13605OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
13606 SourceLocation StartLoc,
13607 SourceLocation LParenLoc,
13608 SourceLocation EndLoc) {
Samuel Antaocc10b852016-07-28 14:23:26 +000013609 MappableVarListInfo MVLI(VarList);
13610 SmallVector<Expr *, 8> PrivateCopies;
13611 SmallVector<Expr *, 8> Inits;
13612
Alexey Bataeve3727102018-04-18 15:57:46 +000013613 for (Expr *RefExpr : VarList) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000013614 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
13615 SourceLocation ELoc;
13616 SourceRange ERange;
13617 Expr *SimpleRefExpr = RefExpr;
13618 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
13619 if (Res.second) {
13620 // It will be analyzed later.
Samuel Antaocc10b852016-07-28 14:23:26 +000013621 MVLI.ProcessedVarList.push_back(RefExpr);
13622 PrivateCopies.push_back(nullptr);
13623 Inits.push_back(nullptr);
Carlo Bertolli2404b172016-07-13 15:37:16 +000013624 }
13625 ValueDecl *D = Res.first;
13626 if (!D)
13627 continue;
13628
13629 QualType Type = D->getType();
Samuel Antaocc10b852016-07-28 14:23:26 +000013630 Type = Type.getNonReferenceType().getUnqualifiedType();
13631
13632 auto *VD = dyn_cast<VarDecl>(D);
13633
13634 // Item should be a pointer or reference to pointer.
13635 if (!Type->isPointerType()) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000013636 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
13637 << 0 << RefExpr->getSourceRange();
13638 continue;
13639 }
Samuel Antaocc10b852016-07-28 14:23:26 +000013640
13641 // Build the private variable and the expression that refers to it.
Alexey Bataev63cc8e92018-03-20 14:45:59 +000013642 auto VDPrivate =
13643 buildVarDecl(*this, ELoc, Type, D->getName(),
13644 D->hasAttrs() ? &D->getAttrs() : nullptr,
13645 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Samuel Antaocc10b852016-07-28 14:23:26 +000013646 if (VDPrivate->isInvalidDecl())
13647 continue;
13648
13649 CurContext->addDecl(VDPrivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000013650 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Samuel Antaocc10b852016-07-28 14:23:26 +000013651 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
13652
13653 // Add temporary variable to initialize the private copy of the pointer.
Alexey Bataeve3727102018-04-18 15:57:46 +000013654 VarDecl *VDInit =
Samuel Antaocc10b852016-07-28 14:23:26 +000013655 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp");
Alexey Bataeve3727102018-04-18 15:57:46 +000013656 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr(
13657 *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc());
Samuel Antaocc10b852016-07-28 14:23:26 +000013658 AddInitializerToDecl(VDPrivate,
13659 DefaultLvalueConversion(VDInitRefExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000013660 /*DirectInit=*/false);
Samuel Antaocc10b852016-07-28 14:23:26 +000013661
13662 // If required, build a capture to implement the privatization initialized
13663 // with the current list item value.
13664 DeclRefExpr *Ref = nullptr;
13665 if (!VD)
13666 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
13667 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
13668 PrivateCopies.push_back(VDPrivateRefExpr);
13669 Inits.push_back(VDInitRefExpr);
13670
13671 // We need to add a data sharing attribute for this variable to make sure it
13672 // is correctly captured. A variable that shows up in a use_device_ptr has
13673 // similar properties of a first private variable.
13674 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
13675
13676 // Create a mappable component for the list item. List items in this clause
13677 // only need a component.
13678 MVLI.VarBaseDeclarations.push_back(D);
13679 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13680 MVLI.VarComponents.back().push_back(
13681 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D));
Carlo Bertolli2404b172016-07-13 15:37:16 +000013682 }
13683
Samuel Antaocc10b852016-07-28 14:23:26 +000013684 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli2404b172016-07-13 15:37:16 +000013685 return nullptr;
13686
Samuel Antaocc10b852016-07-28 14:23:26 +000013687 return OMPUseDevicePtrClause::Create(
13688 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
13689 PrivateCopies, Inits, MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli2404b172016-07-13 15:37:16 +000013690}
Carlo Bertolli70594e92016-07-13 17:16:49 +000013691
13692OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
13693 SourceLocation StartLoc,
13694 SourceLocation LParenLoc,
13695 SourceLocation EndLoc) {
Samuel Antao6890b092016-07-28 14:25:09 +000013696 MappableVarListInfo MVLI(VarList);
Alexey Bataeve3727102018-04-18 15:57:46 +000013697 for (Expr *RefExpr : VarList) {
Kelvin Li84376252016-12-14 15:39:58 +000013698 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause.");
Carlo Bertolli70594e92016-07-13 17:16:49 +000013699 SourceLocation ELoc;
13700 SourceRange ERange;
13701 Expr *SimpleRefExpr = RefExpr;
13702 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
13703 if (Res.second) {
13704 // It will be analyzed later.
Samuel Antao6890b092016-07-28 14:25:09 +000013705 MVLI.ProcessedVarList.push_back(RefExpr);
Carlo Bertolli70594e92016-07-13 17:16:49 +000013706 }
13707 ValueDecl *D = Res.first;
13708 if (!D)
13709 continue;
13710
13711 QualType Type = D->getType();
13712 // item should be a pointer or array or reference to pointer or array
13713 if (!Type.getNonReferenceType()->isPointerType() &&
13714 !Type.getNonReferenceType()->isArrayType()) {
13715 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
13716 << 0 << RefExpr->getSourceRange();
13717 continue;
13718 }
Samuel Antao6890b092016-07-28 14:25:09 +000013719
13720 // Check if the declaration in the clause does not show up in any data
13721 // sharing attribute.
Alexey Bataeve3727102018-04-18 15:57:46 +000013722 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Samuel Antao6890b092016-07-28 14:25:09 +000013723 if (isOpenMPPrivate(DVar.CKind)) {
13724 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
13725 << getOpenMPClauseName(DVar.CKind)
13726 << getOpenMPClauseName(OMPC_is_device_ptr)
13727 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000013728 reportOriginalDsa(*this, DSAStack, D, DVar);
Samuel Antao6890b092016-07-28 14:25:09 +000013729 continue;
13730 }
13731
Alexey Bataeve3727102018-04-18 15:57:46 +000013732 const Expr *ConflictExpr;
Samuel Antao6890b092016-07-28 14:25:09 +000013733 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +000013734 D, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +000013735 [&ConflictExpr](
13736 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
13737 OpenMPClauseKind) -> bool {
13738 ConflictExpr = R.front().getAssociatedExpression();
13739 return true;
13740 })) {
13741 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
13742 Diag(ConflictExpr->getExprLoc(), diag::note_used_here)
13743 << ConflictExpr->getSourceRange();
13744 continue;
13745 }
13746
13747 // Store the components in the stack so that they can be used to check
13748 // against other clauses later on.
13749 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D);
13750 DSAStack->addMappableExpressionComponents(
13751 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
13752
13753 // Record the expression we've just processed.
13754 MVLI.ProcessedVarList.push_back(SimpleRefExpr);
13755
13756 // Create a mappable component for the list item. List items in this clause
13757 // only need a component. We use a null declaration to signal fields in
13758 // 'this'.
13759 assert((isa<DeclRefExpr>(SimpleRefExpr) ||
13760 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
13761 "Unexpected device pointer expression!");
13762 MVLI.VarBaseDeclarations.push_back(
13763 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr);
13764 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13765 MVLI.VarComponents.back().push_back(MC);
Carlo Bertolli70594e92016-07-13 17:16:49 +000013766 }
13767
Samuel Antao6890b092016-07-28 14:25:09 +000013768 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli70594e92016-07-13 17:16:49 +000013769 return nullptr;
13770
Samuel Antao6890b092016-07-28 14:25:09 +000013771 return OMPIsDevicePtrClause::Create(
13772 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
13773 MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli70594e92016-07-13 17:16:49 +000013774}