blob: ac8f740d0deff52e1236abfdba26391ff6fd3989 [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) {
672 if (const auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000673 E = ExprTemp->getSubExpr();
674
Alexey Bataeve3727102018-04-18 15:57:46 +0000675 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000676 E = MTE->GetTemporaryExpr();
677
Alexey Bataeve3727102018-04-18 15:57:46 +0000678 while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000679 E = Binder->getSubExpr();
680
Alexey Bataeve3727102018-04-18 15:57:46 +0000681 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000682 E = ICE->getSubExprAsWritten();
683 return E->IgnoreParens();
684}
685
Alexey Bataeve3727102018-04-18 15:57:46 +0000686static Expr *getExprAsWritten(Expr *E) {
687 return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E)));
688}
689
690static const ValueDecl *getCanonicalDecl(const ValueDecl *D) {
691 if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D))
692 if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000693 D = ME->getMemberDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +0000694 const auto *VD = dyn_cast<VarDecl>(D);
695 const auto *FD = dyn_cast<FieldDecl>(D);
David Majnemer9d168222016-08-05 17:44:54 +0000696 if (VD != nullptr) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000697 VD = VD->getCanonicalDecl();
698 D = VD;
699 } else {
700 assert(FD);
701 FD = FD->getCanonicalDecl();
702 D = FD;
703 }
704 return D;
705}
706
Alexey Bataeve3727102018-04-18 15:57:46 +0000707static ValueDecl *getCanonicalDecl(ValueDecl *D) {
708 return const_cast<ValueDecl *>(
709 getCanonicalDecl(const_cast<const ValueDecl *>(D)));
710}
711
712DSAStackTy::DSAVarData DSAStackTy::getDSA(iterator &Iter,
713 ValueDecl *D) const {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000714 D = getCanonicalDecl(D);
715 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000716 const auto *FD = dyn_cast<FieldDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000717 DSAVarData DVar;
Alexey Bataev4b465392017-04-26 15:06:24 +0000718 if (isStackEmpty() || Iter == Stack.back().first.rend()) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000719 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
720 // in a region but not in construct]
721 // File-scope or namespace-scope variables referenced in called routines
722 // in the region are shared unless they appear in a threadprivate
723 // directive.
Alexey Bataeve3727102018-04-18 15:57:46 +0000724 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000725 DVar.CKind = OMPC_shared;
726
727 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
728 // in a region but not in construct]
729 // Variables with static storage duration that are declared in called
730 // routines in the region are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000731 if (VD && VD->hasGlobalStorage())
732 DVar.CKind = OMPC_shared;
733
734 // Non-static data members are shared by default.
735 if (FD)
Alexey Bataev750a58b2014-03-18 12:19:12 +0000736 DVar.CKind = OMPC_shared;
737
Alexey Bataev758e55e2013-09-06 18:03:48 +0000738 return DVar;
739 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000740
Alexey Bataevec3da872014-01-31 05:15:34 +0000741 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
742 // in a Construct, C/C++, predetermined, p.1]
743 // Variables with automatic storage duration that are declared in a scope
744 // inside the construct are private.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000745 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
746 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000747 DVar.CKind = OMPC_private;
748 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000749 }
750
Alexey Bataeveffbdf12017-07-21 17:24:30 +0000751 DVar.DKind = Iter->Directive;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000752 // Explicitly specified attributes and local variables with predetermined
753 // attributes.
754 if (Iter->SharingMap.count(D)) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000755 const DSAInfo &Data = Iter->SharingMap.lookup(D);
756 DVar.RefExpr = Data.RefExpr.getPointer();
757 DVar.PrivateCopy = Data.PrivateCopy;
758 DVar.CKind = Data.Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000759 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000760 return DVar;
761 }
762
763 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
764 // in a Construct, C/C++, implicitly determined, p.1]
765 // In a parallel or task construct, the data-sharing attributes of these
766 // variables are determined by the default clause, if present.
767 switch (Iter->DefaultAttr) {
768 case DSA_shared:
769 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000770 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000771 return DVar;
772 case DSA_none:
773 return DVar;
774 case DSA_unspecified:
775 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
776 // in a Construct, implicitly determined, p.2]
777 // In a parallel construct, if no default clause is present, these
778 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000779 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000780 if (isOpenMPParallelDirective(DVar.DKind) ||
781 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000782 DVar.CKind = OMPC_shared;
783 return DVar;
784 }
785
786 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
787 // in a Construct, implicitly determined, p.4]
788 // In a task construct, if no default clause is present, a variable that in
789 // the enclosing context is determined to be shared by all implicit tasks
790 // bound to the current team is shared.
Alexey Bataev35aaee62016-04-13 13:36:48 +0000791 if (isOpenMPTaskingDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000792 DSAVarData DVarTemp;
Alexey Bataeve3727102018-04-18 15:57:46 +0000793 iterator I = Iter, E = Stack.back().first.rend();
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000794 do {
795 ++I;
Alexey Bataeved09d242014-05-28 05:53:51 +0000796 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
Alexey Bataev35aaee62016-04-13 13:36:48 +0000797 // Referenced in a Construct, implicitly determined, p.6]
Alexey Bataev758e55e2013-09-06 18:03:48 +0000798 // In a task construct, if no default clause is present, a variable
799 // whose data-sharing attribute is not determined by the rules above is
800 // firstprivate.
801 DVarTemp = getDSA(I, D);
802 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000803 DVar.RefExpr = nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000804 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000805 return DVar;
806 }
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000807 } while (I != E && !isParallelOrTaskRegion(I->Directive));
Alexey Bataev758e55e2013-09-06 18:03:48 +0000808 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000809 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000810 return DVar;
811 }
812 }
813 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
814 // in a Construct, implicitly determined, p.3]
815 // For constructs other than task, if no default clause is present, these
816 // variables inherit their data-sharing attributes from the enclosing
817 // context.
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000818 return getDSA(++Iter, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000819}
820
Alexey Bataeve3727102018-04-18 15:57:46 +0000821const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D,
822 const Expr *NewDE) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000823 assert(!isStackEmpty() && "Data sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000824 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000825 SharingMapTy &StackElem = Stack.back().first.back();
Alexey Bataev4b465392017-04-26 15:06:24 +0000826 auto It = StackElem.AlignedMap.find(D);
827 if (It == StackElem.AlignedMap.end()) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000828 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
Alexey Bataev4b465392017-04-26 15:06:24 +0000829 StackElem.AlignedMap[D] = NewDE;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000830 return nullptr;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000831 }
Alexey Bataeve3727102018-04-18 15:57:46 +0000832 assert(It->second && "Unexpected nullptr expr in the aligned map");
833 return It->second;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000834}
835
Alexey Bataeve3727102018-04-18 15:57:46 +0000836void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000837 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000838 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000839 SharingMapTy &StackElem = Stack.back().first.back();
840 StackElem.LCVMap.try_emplace(
841 D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture));
Alexey Bataev9c821032015-04-30 04:23:23 +0000842}
843
Alexey Bataeve3727102018-04-18 15:57:46 +0000844const DSAStackTy::LCDeclInfo
845DSAStackTy::isLoopControlVariable(const ValueDecl *D) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000846 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000847 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000848 const SharingMapTy &StackElem = Stack.back().first.back();
Alexey Bataev4b465392017-04-26 15:06:24 +0000849 auto It = StackElem.LCVMap.find(D);
850 if (It != StackElem.LCVMap.end())
851 return It->second;
852 return {0, nullptr};
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000853}
854
Alexey Bataeve3727102018-04-18 15:57:46 +0000855const DSAStackTy::LCDeclInfo
856DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000857 assert(!isStackEmpty() && Stack.back().first.size() > 1 &&
858 "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000859 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000860 const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
Alexey Bataev4b465392017-04-26 15:06:24 +0000861 auto It = StackElem.LCVMap.find(D);
862 if (It != StackElem.LCVMap.end())
863 return It->second;
864 return {0, nullptr};
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000865}
866
Alexey Bataeve3727102018-04-18 15:57:46 +0000867const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000868 assert(!isStackEmpty() && Stack.back().first.size() > 1 &&
869 "Data-sharing attributes stack is empty");
Alexey Bataeve3727102018-04-18 15:57:46 +0000870 const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
Alexey Bataev4b465392017-04-26 15:06:24 +0000871 if (StackElem.LCVMap.size() < I)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000872 return nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +0000873 for (const auto &Pair : StackElem.LCVMap)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000874 if (Pair.second.first == I)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000875 return Pair.first;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000876 return nullptr;
Alexey Bataev9c821032015-04-30 04:23:23 +0000877}
878
Alexey Bataeve3727102018-04-18 15:57:46 +0000879void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
Alexey Bataev90c228f2016-02-08 09:29:13 +0000880 DeclRefExpr *PrivateCopy) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000881 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000882 if (A == OMPC_threadprivate) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000883 DSAInfo &Data = Threadprivates[D];
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000884 Data.Attributes = A;
885 Data.RefExpr.setPointer(E);
886 Data.PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000887 } else {
Alexey Bataev4b465392017-04-26 15:06:24 +0000888 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataeve3727102018-04-18 15:57:46 +0000889 DSAInfo &Data = Stack.back().first.back().SharingMap[D];
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000890 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
891 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
892 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
893 (isLoopControlVariable(D).first && A == OMPC_private));
894 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
895 Data.RefExpr.setInt(/*IntVal=*/true);
896 return;
897 }
898 const bool IsLastprivate =
899 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
900 Data.Attributes = A;
901 Data.RefExpr.setPointerAndInt(E, IsLastprivate);
902 Data.PrivateCopy = PrivateCopy;
903 if (PrivateCopy) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000904 DSAInfo &Data =
905 Stack.back().first.back().SharingMap[PrivateCopy->getDecl()];
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000906 Data.Attributes = A;
907 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
908 Data.PrivateCopy = nullptr;
909 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000910 }
911}
912
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000913/// Build a variable declaration for OpenMP loop iteration variable.
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000914static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev63cc8e92018-03-20 14:45:59 +0000915 StringRef Name, const AttrVec *Attrs = nullptr,
916 DeclRefExpr *OrigRef = nullptr) {
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000917 DeclContext *DC = SemaRef.CurContext;
918 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
919 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
Alexey Bataeve3727102018-04-18 15:57:46 +0000920 auto *Decl =
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000921 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
922 if (Attrs) {
923 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
924 I != E; ++I)
925 Decl->addAttr(*I);
926 }
927 Decl->setImplicit();
Alexey Bataev63cc8e92018-03-20 14:45:59 +0000928 if (OrigRef) {
929 Decl->addAttr(
930 OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef));
931 }
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000932 return Decl;
933}
934
935static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
936 SourceLocation Loc,
937 bool RefersToCapture = false) {
938 D->setReferenced();
939 D->markUsed(S.Context);
940 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
941 SourceLocation(), D, RefersToCapture, Loc, Ty,
942 VK_LValue);
943}
944
Alexey Bataeve3727102018-04-18 15:57:46 +0000945void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000946 BinaryOperatorKind BOK) {
Alexey Bataevfa312f32017-07-21 18:48:21 +0000947 D = getCanonicalDecl(D);
948 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataevfa312f32017-07-21 18:48:21 +0000949 assert(
Richard Trieu09f14112017-07-21 21:29:35 +0000950 Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000951 "Additional reduction info may be specified only for reduction items.");
Alexey Bataeve3727102018-04-18 15:57:46 +0000952 ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D];
Alexey Bataevfa312f32017-07-21 18:48:21 +0000953 assert(ReductionData.ReductionRange.isInvalid() &&
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000954 Stack.back().first.back().Directive == OMPD_taskgroup &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000955 "Additional reduction info may be specified only once for reduction "
956 "items.");
957 ReductionData.set(BOK, SR);
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000958 Expr *&TaskgroupReductionRef =
959 Stack.back().first.back().TaskgroupReductionRef;
960 if (!TaskgroupReductionRef) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000961 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
962 SemaRef.Context.VoidPtrTy, ".task_red.");
Alexey Bataevd070a582017-10-25 15:54:04 +0000963 TaskgroupReductionRef =
964 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000965 }
Alexey Bataevfa312f32017-07-21 18:48:21 +0000966}
967
Alexey Bataeve3727102018-04-18 15:57:46 +0000968void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000969 const Expr *ReductionRef) {
Alexey Bataevfa312f32017-07-21 18:48:21 +0000970 D = getCanonicalDecl(D);
971 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataevfa312f32017-07-21 18:48:21 +0000972 assert(
Richard Trieu09f14112017-07-21 21:29:35 +0000973 Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000974 "Additional reduction info may be specified only for reduction items.");
Alexey Bataeve3727102018-04-18 15:57:46 +0000975 ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D];
Alexey Bataevfa312f32017-07-21 18:48:21 +0000976 assert(ReductionData.ReductionRange.isInvalid() &&
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000977 Stack.back().first.back().Directive == OMPD_taskgroup &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000978 "Additional reduction info may be specified only once for reduction "
979 "items.");
980 ReductionData.set(ReductionRef, SR);
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000981 Expr *&TaskgroupReductionRef =
982 Stack.back().first.back().TaskgroupReductionRef;
983 if (!TaskgroupReductionRef) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000984 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
985 SemaRef.Context.VoidPtrTy, ".task_red.");
Alexey Bataevd070a582017-10-25 15:54:04 +0000986 TaskgroupReductionRef =
987 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000988 }
Alexey Bataevfa312f32017-07-21 18:48:21 +0000989}
990
Alexey Bataeve3727102018-04-18 15:57:46 +0000991const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
992 const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK,
993 Expr *&TaskgroupDescriptor) const {
Alexey Bataevfa312f32017-07-21 18:48:21 +0000994 D = getCanonicalDecl(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +0000995 assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
996 if (Stack.back().first.empty())
997 return DSAVarData();
Alexey Bataeve3727102018-04-18 15:57:46 +0000998 for (iterator I = std::next(Stack.back().first.rbegin(), 1),
999 E = Stack.back().first.rend();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001000 I != E; std::advance(I, 1)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001001 const DSAInfo &Data = I->SharingMap.lookup(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +00001002 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
Alexey Bataevfa312f32017-07-21 18:48:21 +00001003 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00001004 const ReductionData &ReductionData = I->ReductionMap.lookup(D);
Alexey Bataevfa312f32017-07-21 18:48:21 +00001005 if (!ReductionData.ReductionOp ||
1006 ReductionData.ReductionOp.is<const Expr *>())
Alexey Bataevf189cb72017-07-24 14:52:13 +00001007 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001008 SR = ReductionData.ReductionRange;
Alexey Bataevf87fa882017-07-21 19:26:22 +00001009 BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>();
Alexey Bataev88202be2017-07-27 13:20:36 +00001010 assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1011 "expression for the descriptor is not "
1012 "set.");
1013 TaskgroupDescriptor = I->TaskgroupReductionRef;
Alexey Bataevf189cb72017-07-24 14:52:13 +00001014 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
1015 Data.PrivateCopy, I->DefaultAttrLoc);
Alexey Bataevfa312f32017-07-21 18:48:21 +00001016 }
Alexey Bataevf189cb72017-07-24 14:52:13 +00001017 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001018}
1019
Alexey Bataeve3727102018-04-18 15:57:46 +00001020const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1021 const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef,
1022 Expr *&TaskgroupDescriptor) const {
Alexey Bataevfa312f32017-07-21 18:48:21 +00001023 D = getCanonicalDecl(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +00001024 assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1025 if (Stack.back().first.empty())
1026 return DSAVarData();
Alexey Bataeve3727102018-04-18 15:57:46 +00001027 for (iterator I = std::next(Stack.back().first.rbegin(), 1),
1028 E = Stack.back().first.rend();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001029 I != E; std::advance(I, 1)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001030 const DSAInfo &Data = I->SharingMap.lookup(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +00001031 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
Alexey Bataevfa312f32017-07-21 18:48:21 +00001032 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00001033 const ReductionData &ReductionData = I->ReductionMap.lookup(D);
Alexey Bataevfa312f32017-07-21 18:48:21 +00001034 if (!ReductionData.ReductionOp ||
1035 !ReductionData.ReductionOp.is<const Expr *>())
Alexey Bataevf189cb72017-07-24 14:52:13 +00001036 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001037 SR = ReductionData.ReductionRange;
1038 ReductionRef = ReductionData.ReductionOp.get<const Expr *>();
Alexey Bataev88202be2017-07-27 13:20:36 +00001039 assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1040 "expression for the descriptor is not "
1041 "set.");
1042 TaskgroupDescriptor = I->TaskgroupReductionRef;
Alexey Bataevf189cb72017-07-24 14:52:13 +00001043 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
1044 Data.PrivateCopy, I->DefaultAttrLoc);
Alexey Bataevfa312f32017-07-21 18:48:21 +00001045 }
Alexey Bataevf189cb72017-07-24 14:52:13 +00001046 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001047}
1048
Alexey Bataeve3727102018-04-18 15:57:46 +00001049bool DSAStackTy::isOpenMPLocal(VarDecl *D, iterator Iter) const {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001050 D = D->getCanonicalDecl();
Alexey Bataev852525d2018-03-02 17:17:12 +00001051 if (!isStackEmpty()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001052 iterator I = Iter, E = Stack.back().first.rend();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001053 Scope *TopScope = nullptr;
Alexey Bataev852525d2018-03-02 17:17:12 +00001054 while (I != E && !isParallelOrTaskRegion(I->Directive) &&
1055 !isOpenMPTargetExecutionDirective(I->Directive))
Alexey Bataevec3da872014-01-31 05:15:34 +00001056 ++I;
Alexey Bataeved09d242014-05-28 05:53:51 +00001057 if (I == E)
1058 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001059 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001060 Scope *CurScope = getCurScope();
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001061 while (CurScope != TopScope && !CurScope->isDeclScope(D))
Alexey Bataev758e55e2013-09-06 18:03:48 +00001062 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +00001063 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001064 }
Alexey Bataevec3da872014-01-31 05:15:34 +00001065 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001066}
1067
Alexey Bataeve3727102018-04-18 15:57:46 +00001068const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
1069 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001070 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001071 DSAVarData DVar;
1072
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001073 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001074 auto TI = Threadprivates.find(D);
1075 if (TI != Threadprivates.end()) {
1076 DVar.RefExpr = TI->getSecond().RefExpr.getPointer();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001077 DVar.CKind = OMPC_threadprivate;
1078 return DVar;
Alexey Bataeve3727102018-04-18 15:57:46 +00001079 }
1080 if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
Alexey Bataev817d7f32017-11-14 21:01:01 +00001081 DVar.RefExpr = buildDeclRefExpr(
1082 SemaRef, VD, D->getType().getNonReferenceType(),
1083 VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation());
1084 DVar.CKind = OMPC_threadprivate;
1085 addDSA(D, DVar.RefExpr, OMPC_threadprivate);
Alexey Bataev852525d2018-03-02 17:17:12 +00001086 return DVar;
1087 }
1088 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1089 // in a Construct, C/C++, predetermined, p.1]
1090 // Variables appearing in threadprivate directives are threadprivate.
1091 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
1092 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1093 SemaRef.getLangOpts().OpenMPUseTLS &&
1094 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
1095 (VD && VD->getStorageClass() == SC_Register &&
1096 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
1097 DVar.RefExpr = buildDeclRefExpr(
1098 SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation());
1099 DVar.CKind = OMPC_threadprivate;
1100 addDSA(D, DVar.RefExpr, OMPC_threadprivate);
1101 return DVar;
1102 }
1103 if (SemaRef.getLangOpts().OpenMPCUDAMode && VD &&
1104 VD->isLocalVarDeclOrParm() && !isStackEmpty() &&
1105 !isLoopControlVariable(D).first) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001106 iterator IterTarget =
Alexey Bataev852525d2018-03-02 17:17:12 +00001107 std::find_if(Stack.back().first.rbegin(), Stack.back().first.rend(),
1108 [](const SharingMapTy &Data) {
1109 return isOpenMPTargetExecutionDirective(Data.Directive);
1110 });
1111 if (IterTarget != Stack.back().first.rend()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001112 iterator ParentIterTarget = std::next(IterTarget, 1);
1113 for (iterator Iter = Stack.back().first.rbegin();
1114 Iter != ParentIterTarget; std::advance(Iter, 1)) {
Alexey Bataev852525d2018-03-02 17:17:12 +00001115 if (isOpenMPLocal(VD, Iter)) {
1116 DVar.RefExpr =
1117 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1118 D->getLocation());
1119 DVar.CKind = OMPC_threadprivate;
1120 return DVar;
1121 }
Alexey Bataev852525d2018-03-02 17:17:12 +00001122 }
1123 if (!isClauseParsingMode() || IterTarget != Stack.back().first.rbegin()) {
1124 auto DSAIter = IterTarget->SharingMap.find(D);
1125 if (DSAIter != IterTarget->SharingMap.end() &&
1126 isOpenMPPrivate(DSAIter->getSecond().Attributes)) {
1127 DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer();
1128 DVar.CKind = OMPC_threadprivate;
1129 return DVar;
Alexey Bataeve3727102018-04-18 15:57:46 +00001130 }
1131 iterator End = Stack.back().first.rend();
1132 if (!SemaRef.isOpenMPCapturedByRef(
1133 D, std::distance(ParentIterTarget, End))) {
Alexey Bataev852525d2018-03-02 17:17:12 +00001134 DVar.RefExpr =
1135 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1136 IterTarget->ConstructLoc);
1137 DVar.CKind = OMPC_threadprivate;
1138 return DVar;
1139 }
1140 }
1141 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001142 }
1143
Alexey Bataev4b465392017-04-26 15:06:24 +00001144 if (isStackEmpty())
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001145 // Not in OpenMP execution region and top scope was already checked.
1146 return DVar;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001147
Alexey Bataev758e55e2013-09-06 18:03:48 +00001148 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +00001149 // in a Construct, C/C++, predetermined, p.4]
1150 // Static data members are shared.
1151 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1152 // in a Construct, C/C++, predetermined, p.7]
1153 // Variables with static storage duration that are declared in a scope
1154 // inside the construct are shared.
Alexey Bataeve3727102018-04-18 15:57:46 +00001155 auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; };
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001156 if (VD && VD->isStaticDataMember()) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001157 DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent);
Alexey Bataevdffa93a2015-12-10 08:20:58 +00001158 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
Alexey Bataevec3da872014-01-31 05:15:34 +00001159 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001160
Alexey Bataevdffa93a2015-12-10 08:20:58 +00001161 DVar.CKind = OMPC_shared;
1162 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001163 }
1164
1165 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00001166 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
1167 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001168 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1169 // in a Construct, C/C++, predetermined, p.6]
1170 // Variables with const qualified type having no mutable member are
1171 // shared.
Alexey Bataeve3727102018-04-18 15:57:46 +00001172 const CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +00001173 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +00001174 if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
1175 if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate())
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00001176 RD = CTD->getTemplatedDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001177 if (IsConstant &&
Alexey Bataev4bcad7f2016-02-10 10:50:12 +00001178 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() &&
1179 RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001180 // Variables with const-qualified type having no mutable member may be
1181 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataeve3727102018-04-18 15:57:46 +00001182 DSAVarData DVarTemp =
1183 hasDSA(D, [](OpenMPClauseKind C) { return C == OMPC_firstprivate; },
1184 MatchesAlways, FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001185 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
Alexey Bataev9a757382018-02-16 19:16:54 +00001186 return DVarTemp;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001187
Alexey Bataev758e55e2013-09-06 18:03:48 +00001188 DVar.CKind = OMPC_shared;
1189 return DVar;
1190 }
1191
Alexey Bataev758e55e2013-09-06 18:03:48 +00001192 // Explicitly specified attributes and local variables with predetermined
1193 // attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +00001194 iterator I = Stack.back().first.rbegin();
1195 iterator EndI = Stack.back().first.rend();
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001196 if (FromParent && I != EndI)
1197 std::advance(I, 1);
Alexey Bataeve3727102018-04-18 15:57:46 +00001198 auto It = I->SharingMap.find(D);
1199 if (It != I->SharingMap.end()) {
1200 const DSAInfo &Data = It->getSecond();
1201 DVar.RefExpr = Data.RefExpr.getPointer();
1202 DVar.PrivateCopy = Data.PrivateCopy;
1203 DVar.CKind = Data.Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001204 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev4d4624c2017-07-20 16:47:47 +00001205 DVar.DKind = I->Directive;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001206 }
1207
1208 return DVar;
1209}
1210
Alexey Bataeve3727102018-04-18 15:57:46 +00001211const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1212 bool FromParent) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001213 if (isStackEmpty()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001214 iterator I;
Alexey Bataev4b465392017-04-26 15:06:24 +00001215 return getDSA(I, D);
1216 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001217 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +00001218 iterator StartI = Stack.back().first.rbegin();
1219 iterator EndI = Stack.back().first.rend();
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001220 if (FromParent && StartI != EndI)
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001221 std::advance(StartI, 1);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001222 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001223}
1224
Alexey Bataeve3727102018-04-18 15:57:46 +00001225const DSAStackTy::DSAVarData
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001226DSAStackTy::hasDSA(ValueDecl *D,
Alexey Bataev97d18bf2018-04-11 19:21:00 +00001227 const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1228 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +00001229 bool FromParent) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001230 if (isStackEmpty())
1231 return {};
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001232 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +00001233 iterator I = Stack.back().first.rbegin();
1234 iterator EndI = Stack.back().first.rend();
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001235 if (FromParent && I != EndI)
Alexey Bataev0e6fc1c2017-04-27 14:46:26 +00001236 std::advance(I, 1);
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001237 for (; I != EndI; std::advance(I, 1)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001238 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +00001239 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00001240 iterator NewI = I;
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001241 DSAVarData DVar = getDSA(NewI, D);
1242 if (I == NewI && CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001243 return DVar;
Alexey Bataev60859c02017-04-27 15:10:33 +00001244 }
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001245 return {};
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001246}
1247
Alexey Bataeve3727102018-04-18 15:57:46 +00001248const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
Alexey Bataev97d18bf2018-04-11 19:21:00 +00001249 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1250 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +00001251 bool FromParent) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001252 if (isStackEmpty())
1253 return {};
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001254 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +00001255 iterator StartI = Stack.back().first.rbegin();
1256 iterator EndI = Stack.back().first.rend();
Alexey Bataeve3978122016-07-19 05:06:39 +00001257 if (FromParent && StartI != EndI)
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001258 std::advance(StartI, 1);
Alexey Bataeve3978122016-07-19 05:06:39 +00001259 if (StartI == EndI || !DPred(StartI->Directive))
Alexey Bataev4b465392017-04-26 15:06:24 +00001260 return {};
Alexey Bataeve3727102018-04-18 15:57:46 +00001261 iterator NewI = StartI;
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001262 DSAVarData DVar = getDSA(NewI, D);
1263 return (NewI == StartI && CPred(DVar.CKind)) ? DVar : DSAVarData();
Alexey Bataevc5e02582014-06-16 07:08:35 +00001264}
1265
Alexey Bataevaac108a2015-06-23 04:51:00 +00001266bool DSAStackTy::hasExplicitDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00001267 const ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1268 unsigned Level, bool NotLastprivate) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001269 if (isStackEmpty())
1270 return false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001271 D = getCanonicalDecl(D);
Alexey Bataev4b465392017-04-26 15:06:24 +00001272 auto StartI = Stack.back().first.begin();
1273 auto EndI = Stack.back().first.end();
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +00001274 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +00001275 return false;
1276 std::advance(StartI, Level);
Alexey Bataeve3727102018-04-18 15:57:46 +00001277 auto I = StartI->SharingMap.find(D);
1278 return (I != StartI->SharingMap.end()) &&
1279 I->getSecond().RefExpr.getPointer() &&
1280 CPred(I->getSecond().Attributes) &&
1281 (!NotLastprivate || !I->getSecond().RefExpr.getInt());
Alexey Bataevaac108a2015-06-23 04:51:00 +00001282}
1283
Samuel Antao4be30e92015-10-02 17:14:03 +00001284bool DSAStackTy::hasExplicitDirective(
Alexey Bataeve3727102018-04-18 15:57:46 +00001285 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1286 unsigned Level) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001287 if (isStackEmpty())
1288 return false;
1289 auto StartI = Stack.back().first.begin();
1290 auto EndI = Stack.back().first.end();
Samuel Antao4be30e92015-10-02 17:14:03 +00001291 if (std::distance(StartI, EndI) <= (int)Level)
1292 return false;
1293 std::advance(StartI, Level);
1294 return DPred(StartI->Directive);
1295}
1296
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001297bool DSAStackTy::hasDirective(
1298 const llvm::function_ref<bool(OpenMPDirectiveKind,
1299 const DeclarationNameInfo &, SourceLocation)>
Alexey Bataev97d18bf2018-04-11 19:21:00 +00001300 DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +00001301 bool FromParent) const {
Samuel Antaof0d79752016-05-27 15:21:27 +00001302 // We look only in the enclosing region.
Alexey Bataev4b465392017-04-26 15:06:24 +00001303 if (isStackEmpty())
Samuel Antaof0d79752016-05-27 15:21:27 +00001304 return false;
Alexey Bataev4b465392017-04-26 15:06:24 +00001305 auto StartI = std::next(Stack.back().first.rbegin());
1306 auto EndI = Stack.back().first.rend();
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001307 if (FromParent && StartI != EndI)
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001308 StartI = std::next(StartI);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001309 for (auto I = StartI, EE = EndI; I != EE; ++I) {
1310 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
1311 return true;
1312 }
1313 return false;
1314}
1315
Alexey Bataev758e55e2013-09-06 18:03:48 +00001316void Sema::InitDataSharingAttributesStack() {
1317 VarDataSharingAttributesStack = new DSAStackTy(*this);
1318}
1319
1320#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
1321
Alexey Bataev4b465392017-04-26 15:06:24 +00001322void Sema::pushOpenMPFunctionRegion() {
1323 DSAStack->pushFunction();
1324}
1325
1326void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) {
1327 DSAStack->popFunction(OldFSI);
1328}
1329
Alexey Bataeve3727102018-04-18 15:57:46 +00001330bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level) const {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001331 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1332
Alexey Bataeve3727102018-04-18 15:57:46 +00001333 ASTContext &Ctx = getASTContext();
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001334 bool IsByRef = true;
1335
1336 // Find the directive that is associated with the provided scope.
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00001337 D = cast<ValueDecl>(D->getCanonicalDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00001338 QualType Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001339
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001340 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001341 // This table summarizes how a given variable should be passed to the device
1342 // given its type and the clauses where it appears. This table is based on
1343 // the description in OpenMP 4.5 [2.10.4, target Construct] and
1344 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
1345 //
1346 // =========================================================================
1347 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
1348 // | |(tofrom:scalar)| | pvt | | | |
1349 // =========================================================================
1350 // | scl | | | | - | | bycopy|
1351 // | scl | | - | x | - | - | bycopy|
1352 // | scl | | x | - | - | - | null |
1353 // | scl | x | | | - | | byref |
1354 // | scl | x | - | x | - | - | bycopy|
1355 // | scl | x | x | - | - | - | null |
1356 // | scl | | - | - | - | x | byref |
1357 // | scl | x | - | - | - | x | byref |
1358 //
1359 // | agg | n.a. | | | - | | byref |
1360 // | agg | n.a. | - | x | - | - | byref |
1361 // | agg | n.a. | x | - | - | - | null |
1362 // | agg | n.a. | - | - | - | x | byref |
1363 // | agg | n.a. | - | - | - | x[] | byref |
1364 //
1365 // | ptr | n.a. | | | - | | bycopy|
1366 // | ptr | n.a. | - | x | - | - | bycopy|
1367 // | ptr | n.a. | x | - | - | - | null |
1368 // | ptr | n.a. | - | - | - | x | byref |
1369 // | ptr | n.a. | - | - | - | x[] | bycopy|
1370 // | ptr | n.a. | - | - | x | | bycopy|
1371 // | ptr | n.a. | - | - | x | x | bycopy|
1372 // | ptr | n.a. | - | - | x | x[] | bycopy|
1373 // =========================================================================
1374 // Legend:
1375 // scl - scalar
1376 // ptr - pointer
1377 // agg - aggregate
1378 // x - applies
1379 // - - invalid in this combination
1380 // [] - mapped with an array section
1381 // byref - should be mapped by reference
1382 // byval - should be mapped by value
1383 // null - initialize a local variable to null on the device
1384 //
1385 // Observations:
1386 // - All scalar declarations that show up in a map clause have to be passed
1387 // by reference, because they may have been mapped in the enclosing data
1388 // environment.
1389 // - If the scalar value does not fit the size of uintptr, it has to be
1390 // passed by reference, regardless the result in the table above.
1391 // - For pointers mapped by value that have either an implicit map or an
1392 // array section, the runtime library may pass the NULL value to the
1393 // device instead of the value passed to it by the compiler.
1394
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001395 if (Ty->isReferenceType())
1396 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
Samuel Antao86ace552016-04-27 22:40:57 +00001397
1398 // Locate map clauses and see if the variable being captured is referred to
1399 // in any of those clauses. Here we only care about variables, not fields,
1400 // because fields are part of aggregates.
1401 bool IsVariableUsedInMapClause = false;
1402 bool IsVariableAssociatedWithSection = false;
1403
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +00001404 DSAStack->checkMappableExprComponentListsForDeclAtLevel(
Alexey Bataeve3727102018-04-18 15:57:46 +00001405 D, Level,
1406 [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D](
1407 OMPClauseMappableExprCommon::MappableExprComponentListRef
Samuel Antao6890b092016-07-28 14:25:09 +00001408 MapExprComponents,
1409 OpenMPClauseKind WhereFoundClauseKind) {
1410 // Only the map clause information influences how a variable is
1411 // captured. E.g. is_device_ptr does not require changing the default
Samuel Antao4c8035b2016-12-12 18:00:20 +00001412 // behavior.
Samuel Antao6890b092016-07-28 14:25:09 +00001413 if (WhereFoundClauseKind != OMPC_map)
1414 return false;
Samuel Antao86ace552016-04-27 22:40:57 +00001415
1416 auto EI = MapExprComponents.rbegin();
1417 auto EE = MapExprComponents.rend();
1418
1419 assert(EI != EE && "Invalid map expression!");
1420
1421 if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
1422 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
1423
1424 ++EI;
1425 if (EI == EE)
1426 return false;
1427
1428 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
1429 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
1430 isa<MemberExpr>(EI->getAssociatedExpression())) {
1431 IsVariableAssociatedWithSection = true;
1432 // There is nothing more we need to know about this variable.
1433 return true;
1434 }
1435
1436 // Keep looking for more map info.
1437 return false;
1438 });
1439
1440 if (IsVariableUsedInMapClause) {
1441 // If variable is identified in a map clause it is always captured by
1442 // reference except if it is a pointer that is dereferenced somehow.
1443 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
1444 } else {
Alexey Bataev3f96fe62017-12-13 17:31:39 +00001445 // By default, all the data that has a scalar type is mapped by copy
1446 // (except for reduction variables).
1447 IsByRef =
Alexey Bataev60705422018-10-30 15:50:12 +00001448 (DSAStack->isForceCaptureByReferenceInTargetExecutable() &&
1449 !Ty->isAnyPointerType()) ||
Alexey Bataev3f96fe62017-12-13 17:31:39 +00001450 !Ty->isScalarType() ||
1451 DSAStack->getDefaultDMAAtLevel(Level) == DMA_tofrom_scalar ||
1452 DSAStack->hasExplicitDSA(
1453 D, [](OpenMPClauseKind K) { return K == OMPC_reduction; }, Level);
Samuel Antao86ace552016-04-27 22:40:57 +00001454 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001455 }
1456
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001457 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00001458 IsByRef =
Alexey Bataev60705422018-10-30 15:50:12 +00001459 ((DSAStack->isForceCaptureByReferenceInTargetExecutable() &&
1460 !Ty->isAnyPointerType()) ||
1461 !DSAStack->hasExplicitDSA(
1462 D,
1463 [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; },
1464 Level, /*NotLastprivate=*/true)) &&
Alexey Bataev8e769ee2017-12-22 21:01:52 +00001465 // If the variable is artificial and must be captured by value - try to
1466 // capture by value.
Alexey Bataevd2202ca2017-12-27 17:58:32 +00001467 !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() &&
1468 !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue());
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001469 }
1470
Samuel Antao86ace552016-04-27 22:40:57 +00001471 // When passing data by copy, we need to make sure it fits the uintptr size
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001472 // and alignment, because the runtime library only deals with uintptr types.
1473 // If it does not fit the uintptr size, we need to pass the data by reference
1474 // instead.
1475 if (!IsByRef &&
1476 (Ctx.getTypeSizeInChars(Ty) >
1477 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001478 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001479 IsByRef = true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001480 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001481
1482 return IsByRef;
1483}
1484
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001485unsigned Sema::getOpenMPNestingLevel() const {
1486 assert(getLangOpts().OpenMP);
1487 return DSAStack->getNestingLevel();
1488}
1489
Jonas Hahnfeld87d44262017-11-18 21:00:46 +00001490bool Sema::isInOpenMPTargetExecutionDirective() const {
1491 return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) &&
1492 !DSAStack->isClauseParsingMode()) ||
1493 DSAStack->hasDirective(
1494 [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
1495 SourceLocation) -> bool {
1496 return isOpenMPTargetExecutionDirective(K);
1497 },
1498 false);
1499}
1500
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001501VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D) {
Alexey Bataevf841bd92014-12-16 07:00:22 +00001502 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001503 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +00001504
1505 // If we are attempting to capture a global variable in a directive with
1506 // 'target' we return true so that this global is also mapped to the device.
1507 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001508 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001509 if (VD && !VD->hasLocalStorage()) {
1510 if (isInOpenMPDeclareTargetContext() &&
1511 (getCurCapturedRegion() || getCurBlock() || getCurLambda())) {
1512 // Try to mark variable as declare target if it is used in capturing
1513 // regions.
Alexey Bataev97b72212018-08-14 18:31:20 +00001514 if (!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001515 checkDeclIsAllowedInOpenMPTarget(nullptr, VD);
Alexey Bataev92327c52018-03-26 16:40:55 +00001516 return nullptr;
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001517 } else if (isInOpenMPTargetExecutionDirective()) {
1518 // If the declaration is enclosed in a 'declare target' directive,
1519 // then it should not be captured.
1520 //
Alexey Bataev97b72212018-08-14 18:31:20 +00001521 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001522 return nullptr;
1523 return VD;
1524 }
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001525 }
Alexey Bataev60705422018-10-30 15:50:12 +00001526 // Capture variables captured by reference in lambdas for target-based
1527 // directives.
1528 if (VD && !DSAStack->isClauseParsingMode()) {
1529 if (const auto *RD = VD->getType()
1530 .getCanonicalType()
1531 .getNonReferenceType()
1532 ->getAsCXXRecordDecl()) {
1533 bool SavedForceCaptureByReferenceInTargetExecutable =
1534 DSAStack->isForceCaptureByReferenceInTargetExecutable();
1535 DSAStack->setForceCaptureByReferenceInTargetExecutable(/*V=*/true);
1536 if (RD->isLambda())
1537 for (const LambdaCapture &LC : RD->captures()) {
1538 if (LC.getCaptureKind() == LCK_ByRef) {
1539 VarDecl *VD = LC.getCapturedVar();
1540 DeclContext *VDC = VD->getDeclContext();
1541 if (!VDC->Encloses(CurContext))
1542 continue;
1543 DSAStackTy::DSAVarData DVarPrivate =
1544 DSAStack->getTopDSA(VD, /*FromParent=*/false);
1545 // Do not capture already captured variables.
1546 if (!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) &&
1547 DVarPrivate.CKind == OMPC_unknown &&
1548 !DSAStack->checkMappableExprComponentListsForDecl(
1549 D, /*CurrentRegionOnly=*/true,
1550 [](OMPClauseMappableExprCommon::
1551 MappableExprComponentListRef,
1552 OpenMPClauseKind) { return true; }))
1553 MarkVariableReferenced(LC.getLocation(), LC.getCapturedVar());
1554 } else if (LC.getCaptureKind() == LCK_This) {
1555 CheckCXXThisCapture(LC.getLocation());
1556 }
1557 }
1558 DSAStack->setForceCaptureByReferenceInTargetExecutable(
1559 SavedForceCaptureByReferenceInTargetExecutable);
1560 }
1561 }
Samuel Antao4be30e92015-10-02 17:14:03 +00001562
Alexey Bataev48977c32015-08-04 08:10:48 +00001563 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
1564 (!DSAStack->isClauseParsingMode() ||
1565 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001566 auto &&Info = DSAStack->isLoopControlVariable(D);
1567 if (Info.first ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001568 (VD && VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001569 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001570 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001571 return VD ? VD : Info.second;
Alexey Bataeve3727102018-04-18 15:57:46 +00001572 DSAStackTy::DSAVarData DVarPrivate =
1573 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001574 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
Alexey Bataev90c228f2016-02-08 09:29:13 +00001575 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00001576 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate,
1577 [](OpenMPDirectiveKind) { return true; },
1578 DSAStack->isClauseParsingMode());
Alexey Bataev90c228f2016-02-08 09:29:13 +00001579 if (DVarPrivate.CKind != OMPC_unknown)
1580 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001581 }
Alexey Bataev90c228f2016-02-08 09:29:13 +00001582 return nullptr;
Alexey Bataevf841bd92014-12-16 07:00:22 +00001583}
1584
Alexey Bataevdfa430f2017-12-08 15:03:50 +00001585void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex,
1586 unsigned Level) const {
1587 SmallVector<OpenMPDirectiveKind, 4> Regions;
1588 getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level));
1589 FunctionScopesIndex -= Regions.size();
1590}
1591
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00001592void Sema::startOpenMPLoop() {
1593 assert(LangOpts.OpenMP && "OpenMP must be enabled.");
1594 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective()))
1595 DSAStack->loopInit();
1596}
1597
Alexey Bataeve3727102018-04-18 15:57:46 +00001598bool Sema::isOpenMPPrivateDecl(const ValueDecl *D, unsigned Level) const {
Alexey Bataevaac108a2015-06-23 04:51:00 +00001599 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00001600 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
1601 if (DSAStack->getAssociatedLoops() > 0 &&
1602 !DSAStack->isLoopStarted()) {
1603 DSAStack->resetPossibleLoopCounter(D);
1604 DSAStack->loopStart();
1605 return true;
1606 }
1607 if ((DSAStack->getPossiblyLoopCunter() == D->getCanonicalDecl() ||
1608 DSAStack->isLoopControlVariable(D).first) &&
1609 !DSAStack->hasExplicitDSA(
1610 D, [](OpenMPClauseKind K) { return K != OMPC_private; }, Level) &&
1611 !isOpenMPSimdDirective(DSAStack->getCurrentDirective()))
1612 return true;
1613 }
Alexey Bataevaac108a2015-06-23 04:51:00 +00001614 return DSAStack->hasExplicitDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00001615 D, [](OpenMPClauseKind K) { return K == OMPC_private; }, Level) ||
Alexey Bataev3f82cfc2017-12-13 15:28:44 +00001616 (DSAStack->isClauseParsingMode() &&
1617 DSAStack->getClauseParsingMode() == OMPC_private) ||
Alexey Bataev88202be2017-07-27 13:20:36 +00001618 // Consider taskgroup reduction descriptor variable a private to avoid
1619 // possible capture in the region.
1620 (DSAStack->hasExplicitDirective(
1621 [](OpenMPDirectiveKind K) { return K == OMPD_taskgroup; },
1622 Level) &&
1623 DSAStack->isTaskgroupReductionRef(D, Level));
Alexey Bataevaac108a2015-06-23 04:51:00 +00001624}
1625
Alexey Bataeve3727102018-04-18 15:57:46 +00001626void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D,
1627 unsigned Level) {
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001628 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1629 D = getCanonicalDecl(D);
1630 OpenMPClauseKind OMPC = OMPC_unknown;
1631 for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) {
1632 const unsigned NewLevel = I - 1;
1633 if (DSAStack->hasExplicitDSA(D,
1634 [&OMPC](const OpenMPClauseKind K) {
1635 if (isOpenMPPrivate(K)) {
1636 OMPC = K;
1637 return true;
1638 }
1639 return false;
1640 },
1641 NewLevel))
1642 break;
1643 if (DSAStack->checkMappableExprComponentListsForDeclAtLevel(
1644 D, NewLevel,
1645 [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
1646 OpenMPClauseKind) { return true; })) {
1647 OMPC = OMPC_map;
1648 break;
1649 }
1650 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1651 NewLevel)) {
Alexey Bataev63cc8e92018-03-20 14:45:59 +00001652 OMPC = OMPC_map;
1653 if (D->getType()->isScalarType() &&
1654 DSAStack->getDefaultDMAAtLevel(NewLevel) !=
1655 DefaultMapAttributes::DMA_tofrom_scalar)
1656 OMPC = OMPC_firstprivate;
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001657 break;
1658 }
1659 }
1660 if (OMPC != OMPC_unknown)
1661 FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, OMPC));
1662}
1663
Alexey Bataeve3727102018-04-18 15:57:46 +00001664bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D,
1665 unsigned Level) const {
Samuel Antao4be30e92015-10-02 17:14:03 +00001666 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1667 // Return true if the current level is no longer enclosed in a target region.
1668
Alexey Bataeve3727102018-04-18 15:57:46 +00001669 const auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001670 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00001671 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1672 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +00001673}
1674
Alexey Bataeved09d242014-05-28 05:53:51 +00001675void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001676
1677void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
1678 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001679 Scope *CurScope, SourceLocation Loc) {
1680 DSAStack->push(DKind, DirName, CurScope, Loc);
Faisal Valid143a0c2017-04-01 21:30:49 +00001681 PushExpressionEvaluationContext(
1682 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001683}
1684
Alexey Bataevaac108a2015-06-23 04:51:00 +00001685void Sema::StartOpenMPClause(OpenMPClauseKind K) {
1686 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001687}
1688
Alexey Bataevaac108a2015-06-23 04:51:00 +00001689void Sema::EndOpenMPClause() {
1690 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001691}
1692
Alexey Bataev758e55e2013-09-06 18:03:48 +00001693void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001694 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
1695 // A variable of class type (or array thereof) that appears in a lastprivate
1696 // clause requires an accessible, unambiguous default constructor for the
1697 // class type, unless the list item is also specified in a firstprivate
1698 // clause.
Alexey Bataeve3727102018-04-18 15:57:46 +00001699 if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
1700 for (OMPClause *C : D->clauses()) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001701 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
1702 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeve3727102018-04-18 15:57:46 +00001703 for (Expr *DE : Clause->varlists()) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001704 if (DE->isValueDependent() || DE->isTypeDependent()) {
1705 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001706 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +00001707 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00001708 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
Alexey Bataeve3727102018-04-18 15:57:46 +00001709 auto *VD = cast<VarDecl>(DRE->getDecl());
Alexey Bataev005248a2016-02-25 05:25:57 +00001710 QualType Type = VD->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +00001711 const DSAStackTy::DSAVarData DVar =
1712 DSAStack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001713 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001714 // Generate helper private variable and initialize it with the
1715 // default value. The address of the original variable is replaced
1716 // by the address of the new private variable in CodeGen. This new
1717 // variable is not added to IdResolver, so the code in the OpenMP
1718 // region uses original variable for proper diagnostics.
Alexey Bataeve3727102018-04-18 15:57:46 +00001719 VarDecl *VDPrivate = buildVarDecl(
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001720 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev63cc8e92018-03-20 14:45:59 +00001721 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE);
Richard Smith3beb7c62017-01-12 02:27:38 +00001722 ActOnUninitializedDecl(VDPrivate);
Alexey Bataev38e89532015-04-16 04:54:05 +00001723 if (VDPrivate->isInvalidDecl())
1724 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001725 PrivateCopies.push_back(buildDeclRefExpr(
1726 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001727 } else {
1728 // The variable is also a firstprivate, so initialization sequence
1729 // for private copy is generated already.
1730 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001731 }
1732 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001733 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001734 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001735 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001736 }
1737 }
1738 }
1739
Alexey Bataev758e55e2013-09-06 18:03:48 +00001740 DSAStack->pop();
1741 DiscardCleanupsInEvaluationContext();
1742 PopExpressionEvaluationContext();
1743}
1744
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001745static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1746 Expr *NumIterations, Sema &SemaRef,
1747 Scope *S, DSAStackTy *Stack);
Alexander Musman3276a272015-03-21 10:12:56 +00001748
Alexey Bataeva769e072013-03-22 06:34:35 +00001749namespace {
1750
Alexey Bataeve3727102018-04-18 15:57:46 +00001751class VarDeclFilterCCC final : public CorrectionCandidateCallback {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001752private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001753 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001754
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001755public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001756 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001757 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001758 NamedDecl *ND = Candidate.getCorrectionDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +00001759 if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001760 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001761 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1762 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001763 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001764 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001765 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001766};
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001767
Alexey Bataeve3727102018-04-18 15:57:46 +00001768class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001769private:
1770 Sema &SemaRef;
1771
1772public:
1773 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
1774 bool ValidateCandidate(const TypoCorrection &Candidate) override {
1775 NamedDecl *ND = Candidate.getCorrectionDecl();
Kelvin Li59e3d192017-11-30 18:52:06 +00001776 if (ND && (isa<VarDecl>(ND) || isa<FunctionDecl>(ND))) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001777 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1778 SemaRef.getCurScope());
1779 }
1780 return false;
1781 }
1782};
1783
Alexey Bataeved09d242014-05-28 05:53:51 +00001784} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001785
1786ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1787 CXXScopeSpec &ScopeSpec,
1788 const DeclarationNameInfo &Id) {
1789 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1790 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1791
1792 if (Lookup.isAmbiguous())
1793 return ExprError();
1794
1795 VarDecl *VD;
1796 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001797 if (TypoCorrection Corrected = CorrectTypo(
1798 Id, LookupOrdinaryName, CurScope, nullptr,
1799 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001800 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001801 PDiag(Lookup.empty()
1802 ? diag::err_undeclared_var_use_suggest
1803 : diag::err_omp_expected_var_arg_suggest)
1804 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001805 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001806 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001807 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1808 : diag::err_omp_expected_var_arg)
1809 << Id.getName();
1810 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001811 }
Alexey Bataeve3727102018-04-18 15:57:46 +00001812 } else if (!(VD = Lookup.getAsSingle<VarDecl>())) {
1813 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
1814 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1815 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001816 }
1817 Lookup.suppressDiagnostics();
1818
1819 // OpenMP [2.9.2, Syntax, C/C++]
1820 // Variables must be file-scope, namespace-scope, or static block-scope.
1821 if (!VD->hasGlobalStorage()) {
1822 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001823 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1824 bool IsDecl =
1825 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001826 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001827 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1828 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001829 return ExprError();
1830 }
1831
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001832 VarDecl *CanonicalVD = VD->getCanonicalDecl();
George Burgess IV00f70bd2018-03-01 05:43:23 +00001833 NamedDecl *ND = CanonicalVD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001834 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1835 // A threadprivate directive for file-scope variables must appear outside
1836 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001837 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1838 !getCurLexicalContext()->isTranslationUnit()) {
1839 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001840 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1841 bool IsDecl =
1842 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1843 Diag(VD->getLocation(),
1844 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1845 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001846 return ExprError();
1847 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001848 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1849 // A threadprivate directive for static class member variables must appear
1850 // in the class definition, in the same scope in which the member
1851 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001852 if (CanonicalVD->isStaticDataMember() &&
1853 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1854 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001855 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1856 bool IsDecl =
1857 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1858 Diag(VD->getLocation(),
1859 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1860 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001861 return ExprError();
1862 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001863 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1864 // A threadprivate directive for namespace-scope variables must appear
1865 // outside any definition or declaration other than the namespace
1866 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001867 if (CanonicalVD->getDeclContext()->isNamespace() &&
1868 (!getCurLexicalContext()->isFileContext() ||
1869 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1870 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001871 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1872 bool IsDecl =
1873 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1874 Diag(VD->getLocation(),
1875 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1876 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001877 return ExprError();
1878 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001879 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1880 // A threadprivate directive for static block-scope variables must appear
1881 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001882 if (CanonicalVD->isStaticLocal() && CurScope &&
1883 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001884 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001885 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1886 bool IsDecl =
1887 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1888 Diag(VD->getLocation(),
1889 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1890 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001891 return ExprError();
1892 }
1893
1894 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1895 // A threadprivate directive must lexically precede all references to any
1896 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001897 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001898 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001899 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001900 return ExprError();
1901 }
1902
1903 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev376b4a42016-02-09 09:41:09 +00001904 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1905 SourceLocation(), VD,
1906 /*RefersToEnclosingVariableOrCapture=*/false,
1907 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001908}
1909
Alexey Bataeved09d242014-05-28 05:53:51 +00001910Sema::DeclGroupPtrTy
1911Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1912 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001913 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001914 CurContext->addDecl(D);
1915 return DeclGroupPtrTy::make(DeclGroupRef(D));
1916 }
David Blaikie0403cb12016-01-15 23:43:25 +00001917 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001918}
1919
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001920namespace {
Alexey Bataeve3727102018-04-18 15:57:46 +00001921class LocalVarRefChecker final
1922 : public ConstStmtVisitor<LocalVarRefChecker, bool> {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001923 Sema &SemaRef;
1924
1925public:
1926 bool VisitDeclRefExpr(const DeclRefExpr *E) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001927 if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001928 if (VD->hasLocalStorage()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001929 SemaRef.Diag(E->getBeginLoc(),
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001930 diag::err_omp_local_var_in_threadprivate_init)
1931 << E->getSourceRange();
1932 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1933 << VD << VD->getSourceRange();
1934 return true;
1935 }
1936 }
1937 return false;
1938 }
1939 bool VisitStmt(const Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001940 for (const Stmt *Child : S->children()) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001941 if (Child && Visit(Child))
1942 return true;
1943 }
1944 return false;
1945 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001946 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001947};
1948} // namespace
1949
Alexey Bataeved09d242014-05-28 05:53:51 +00001950OMPThreadPrivateDecl *
1951Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001952 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +00001953 for (Expr *RefExpr : VarList) {
1954 auto *DE = cast<DeclRefExpr>(RefExpr);
1955 auto *VD = cast<VarDecl>(DE->getDecl());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001956 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001957
Alexey Bataev376b4a42016-02-09 09:41:09 +00001958 // Mark variable as used.
1959 VD->setReferenced();
1960 VD->markUsed(Context);
1961
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001962 QualType QType = VD->getType();
1963 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1964 // It will be analyzed later.
1965 Vars.push_back(DE);
1966 continue;
1967 }
1968
Alexey Bataeva769e072013-03-22 06:34:35 +00001969 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1970 // A threadprivate variable must not have an incomplete type.
1971 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001972 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001973 continue;
1974 }
1975
1976 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1977 // A threadprivate variable must not have a reference type.
1978 if (VD->getType()->isReferenceType()) {
1979 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001980 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1981 bool IsDecl =
1982 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1983 Diag(VD->getLocation(),
1984 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1985 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001986 continue;
1987 }
1988
Samuel Antaof8b50122015-07-13 22:54:53 +00001989 // Check if this is a TLS variable. If TLS is not being supported, produce
1990 // the corresponding diagnostic.
1991 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1992 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1993 getLangOpts().OpenMPUseTLS &&
1994 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001995 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1996 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001997 Diag(ILoc, diag::err_omp_var_thread_local)
1998 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001999 bool IsDecl =
2000 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2001 Diag(VD->getLocation(),
2002 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2003 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00002004 continue;
2005 }
2006
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002007 // Check if initial value of threadprivate variable reference variable with
2008 // local storage (it is not supported by runtime).
Alexey Bataeve3727102018-04-18 15:57:46 +00002009 if (const Expr *Init = VD->getAnyInitializer()) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002010 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002011 if (Checker.Visit(Init))
2012 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002013 }
2014
Alexey Bataeved09d242014-05-28 05:53:51 +00002015 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00002016 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00002017 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
2018 Context, SourceRange(Loc, Loc)));
Alexey Bataeve3727102018-04-18 15:57:46 +00002019 if (ASTMutationListener *ML = Context.getASTMutationListener())
Alexey Bataev97720002014-11-11 04:05:39 +00002020 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00002021 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002022 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00002023 if (!Vars.empty()) {
2024 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
2025 Vars);
2026 D->setAccess(AS_public);
2027 }
2028 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00002029}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002030
Kelvin Li1408f912018-09-26 04:28:39 +00002031Sema::DeclGroupPtrTy
2032Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc,
2033 ArrayRef<OMPClause *> ClauseList) {
2034 OMPRequiresDecl *D = nullptr;
2035 if (!CurContext->isFileContext()) {
2036 Diag(Loc, diag::err_omp_invalid_scope) << "requires";
2037 } else {
2038 D = CheckOMPRequiresDecl(Loc, ClauseList);
2039 if (D) {
2040 CurContext->addDecl(D);
2041 DSAStack->addRequiresDecl(D);
2042 }
2043 }
2044 return DeclGroupPtrTy::make(DeclGroupRef(D));
2045}
2046
2047OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc,
2048 ArrayRef<OMPClause *> ClauseList) {
2049 if (!DSAStack->hasDuplicateRequiresClause(ClauseList))
2050 return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc,
2051 ClauseList);
2052 return nullptr;
2053}
2054
Alexey Bataeve3727102018-04-18 15:57:46 +00002055static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
2056 const ValueDecl *D,
2057 const DSAStackTy::DSAVarData &DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00002058 bool IsLoopIterVar = false) {
2059 if (DVar.RefExpr) {
2060 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
2061 << getOpenMPClauseName(DVar.CKind);
2062 return;
2063 }
2064 enum {
2065 PDSA_StaticMemberShared,
2066 PDSA_StaticLocalVarShared,
2067 PDSA_LoopIterVarPrivate,
2068 PDSA_LoopIterVarLinear,
2069 PDSA_LoopIterVarLastprivate,
2070 PDSA_ConstVarShared,
2071 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002072 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00002073 PDSA_LocalVarPrivate,
2074 PDSA_Implicit
2075 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00002076 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002077 auto ReportLoc = D->getLocation();
2078 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002079 if (IsLoopIterVar) {
2080 if (DVar.CKind == OMPC_private)
2081 Reason = PDSA_LoopIterVarPrivate;
2082 else if (DVar.CKind == OMPC_lastprivate)
2083 Reason = PDSA_LoopIterVarLastprivate;
2084 else
2085 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev35aaee62016-04-13 13:36:48 +00002086 } else if (isOpenMPTaskingDirective(DVar.DKind) &&
2087 DVar.CKind == OMPC_firstprivate) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002088 Reason = PDSA_TaskVarFirstprivate;
2089 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002090 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00002091 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002092 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00002093 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002094 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00002095 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002096 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00002097 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002098 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00002099 ReportHint = true;
2100 Reason = PDSA_LocalVarPrivate;
2101 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00002102 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002103 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00002104 << Reason << ReportHint
2105 << getOpenMPDirectiveName(Stack->getCurrentDirective());
2106 } else if (DVar.ImplicitDSALoc.isValid()) {
2107 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
2108 << getOpenMPClauseName(DVar.CKind);
2109 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00002110}
2111
Alexey Bataev758e55e2013-09-06 18:03:48 +00002112namespace {
Alexey Bataeve3727102018-04-18 15:57:46 +00002113class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> {
Alexey Bataev758e55e2013-09-06 18:03:48 +00002114 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00002115 Sema &SemaRef;
Alexey Bataeve3727102018-04-18 15:57:46 +00002116 bool ErrorFound = false;
2117 CapturedStmt *CS = nullptr;
2118 llvm::SmallVector<Expr *, 4> ImplicitFirstprivate;
2119 llvm::SmallVector<Expr *, 4> ImplicitMap;
2120 Sema::VarsWithInheritedDSAType VarsWithInheritedDSA;
2121 llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations;
Alexey Bataeved09d242014-05-28 05:53:51 +00002122
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00002123 void VisitSubCaptures(OMPExecutableDirective *S) {
2124 // Check implicitly captured variables.
2125 if (!S->hasAssociatedStmt() || !S->getAssociatedStmt())
2126 return;
2127 for (const CapturedStmt::Capture &Cap :
2128 S->getInnermostCapturedStmt()->captures()) {
2129 if (!Cap.capturesVariable())
2130 continue;
2131 VarDecl *VD = Cap.getCapturedVar();
2132 // Do not try to map the variable if it or its sub-component was mapped
2133 // already.
2134 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) &&
2135 Stack->checkMappableExprComponentListsForDecl(
2136 VD, /*CurrentRegionOnly=*/true,
2137 [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
2138 OpenMPClauseKind) { return true; }))
2139 continue;
2140 DeclRefExpr *DRE = buildDeclRefExpr(
2141 SemaRef, VD, VD->getType().getNonLValueExprType(SemaRef.Context),
2142 Cap.getLocation(), /*RefersToCapture=*/true);
2143 Visit(DRE);
2144 }
2145 }
2146
Alexey Bataev758e55e2013-09-06 18:03:48 +00002147public:
2148 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00002149 if (E->isTypeDependent() || E->isValueDependent() ||
2150 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
2151 return;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002152 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00002153 VD = VD->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002154 // Skip internally declared variables.
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002155 if (VD->hasLocalStorage() && !CS->capturesVariable(VD))
Alexey Bataeved09d242014-05-28 05:53:51 +00002156 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002157
Alexey Bataeve3727102018-04-18 15:57:46 +00002158 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002159 // Check if the variable has explicit DSA set and stop analysis if it so.
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00002160 if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second)
David Majnemer9d168222016-08-05 17:44:54 +00002161 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002162
Alexey Bataevafe50572017-10-06 17:00:28 +00002163 // Skip internally declared static variables.
Alexey Bataev92327c52018-03-26 16:40:55 +00002164 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
Alexey Bataev97b72212018-08-14 18:31:20 +00002165 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
Alexey Bataev92327c52018-03-26 16:40:55 +00002166 if (VD->hasGlobalStorage() && !CS->capturesVariable(VD) &&
2167 (!Res || *Res != OMPDeclareTargetDeclAttr::MT_Link))
Alexey Bataevafe50572017-10-06 17:00:28 +00002168 return;
2169
Alexey Bataeve3727102018-04-18 15:57:46 +00002170 SourceLocation ELoc = E->getExprLoc();
2171 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002172 // The default(none) clause requires that each variable that is referenced
2173 // in the construct, and does not have a predetermined data-sharing
2174 // attribute, must have its data-sharing attribute explicitly determined
2175 // by being listed in a data-sharing attribute clause.
2176 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002177 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00002178 VarsWithInheritedDSA.count(VD) == 0) {
2179 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002180 return;
2181 }
2182
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002183 if (isOpenMPTargetExecutionDirective(DKind) &&
2184 !Stack->isLoopControlVariable(VD).first) {
2185 if (!Stack->checkMappableExprComponentListsForDecl(
2186 VD, /*CurrentRegionOnly=*/true,
2187 [](OMPClauseMappableExprCommon::MappableExprComponentListRef
2188 StackComponents,
2189 OpenMPClauseKind) {
2190 // Variable is used if it has been marked as an array, array
2191 // section or the variable iself.
2192 return StackComponents.size() == 1 ||
2193 std::all_of(
2194 std::next(StackComponents.rbegin()),
2195 StackComponents.rend(),
2196 [](const OMPClauseMappableExprCommon::
2197 MappableComponent &MC) {
2198 return MC.getAssociatedDeclaration() ==
2199 nullptr &&
2200 (isa<OMPArraySectionExpr>(
2201 MC.getAssociatedExpression()) ||
2202 isa<ArraySubscriptExpr>(
2203 MC.getAssociatedExpression()));
2204 });
2205 })) {
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002206 bool IsFirstprivate = false;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002207 // By default lambdas are captured as firstprivates.
2208 if (const auto *RD =
2209 VD->getType().getNonReferenceType()->getAsCXXRecordDecl())
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002210 IsFirstprivate = RD->isLambda();
2211 IsFirstprivate =
2212 IsFirstprivate ||
2213 (VD->getType().getNonReferenceType()->isScalarType() &&
Alexey Bataev92327c52018-03-26 16:40:55 +00002214 Stack->getDefaultDMA() != DMA_tofrom_scalar && !Res);
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002215 if (IsFirstprivate)
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002216 ImplicitFirstprivate.emplace_back(E);
2217 else
2218 ImplicitMap.emplace_back(E);
2219 return;
2220 }
2221 }
2222
Alexey Bataev758e55e2013-09-06 18:03:48 +00002223 // OpenMP [2.9.3.6, Restrictions, p.2]
2224 // A list item that appears in a reduction clause of the innermost
2225 // enclosing worksharing or parallel construct may not be accessed in an
2226 // explicit task.
Alexey Bataev7ace49d2016-05-17 08:55:33 +00002227 DVar = Stack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00002228 VD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
2229 [](OpenMPDirectiveKind K) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00002230 return isOpenMPParallelDirective(K) ||
2231 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
2232 },
Alexey Bataeveffbdf12017-07-21 17:24:30 +00002233 /*FromParent=*/true);
Alexey Bataev35aaee62016-04-13 13:36:48 +00002234 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00002235 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00002236 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
Alexey Bataeve3727102018-04-18 15:57:46 +00002237 reportOriginalDsa(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002238 return;
2239 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002240
2241 // Define implicit data-sharing attributes for task.
Alexey Bataeve3727102018-04-18 15:57:46 +00002242 DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00002243 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
2244 !Stack->isLoopControlVariable(VD).first)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002245 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002246 }
2247 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002248 void VisitMemberExpr(MemberExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00002249 if (E->isTypeDependent() || E->isValueDependent() ||
2250 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
2251 return;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002252 auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002253 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002254 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002255 if (!FD)
2256 return;
Alexey Bataeve3727102018-04-18 15:57:46 +00002257 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002258 // Check if the variable has explicit DSA set and stop analysis if it
2259 // so.
2260 if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second)
2261 return;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002262
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002263 if (isOpenMPTargetExecutionDirective(DKind) &&
2264 !Stack->isLoopControlVariable(FD).first &&
2265 !Stack->checkMappableExprComponentListsForDecl(
2266 FD, /*CurrentRegionOnly=*/true,
2267 [](OMPClauseMappableExprCommon::MappableExprComponentListRef
2268 StackComponents,
2269 OpenMPClauseKind) {
2270 return isa<CXXThisExpr>(
2271 cast<MemberExpr>(
2272 StackComponents.back().getAssociatedExpression())
2273 ->getBase()
2274 ->IgnoreParens());
2275 })) {
2276 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
2277 // A bit-field cannot appear in a map clause.
2278 //
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002279 if (FD->isBitField())
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002280 return;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002281 ImplicitMap.emplace_back(E);
2282 return;
2283 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002284
Alexey Bataeve3727102018-04-18 15:57:46 +00002285 SourceLocation ELoc = E->getExprLoc();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002286 // OpenMP [2.9.3.6, Restrictions, p.2]
2287 // A list item that appears in a reduction clause of the innermost
2288 // enclosing worksharing or parallel construct may not be accessed in
2289 // an explicit task.
2290 DVar = Stack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00002291 FD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
2292 [](OpenMPDirectiveKind K) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002293 return isOpenMPParallelDirective(K) ||
2294 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
2295 },
2296 /*FromParent=*/true);
2297 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
2298 ErrorFound = true;
2299 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
Alexey Bataeve3727102018-04-18 15:57:46 +00002300 reportOriginalDsa(SemaRef, Stack, FD, DVar);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002301 return;
2302 }
2303
2304 // Define implicit data-sharing attributes for task.
Alexey Bataeve3727102018-04-18 15:57:46 +00002305 DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002306 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
Alexey Bataevb40e05202018-10-24 18:53:12 +00002307 !Stack->isLoopControlVariable(FD).first) {
2308 // Check if there is a captured expression for the current field in the
2309 // region. Do not mark it as firstprivate unless there is no captured
2310 // expression.
2311 // TODO: try to make it firstprivate.
2312 if (DVar.CKind != OMPC_unknown)
2313 ImplicitFirstprivate.push_back(E);
2314 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002315 return;
2316 }
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002317 if (isOpenMPTargetExecutionDirective(DKind)) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002318 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
Alexey Bataeve3727102018-04-18 15:57:46 +00002319 if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map,
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002320 /*NoDiagnose=*/true))
Alexey Bataev27041fa2017-12-05 15:22:49 +00002321 return;
Alexey Bataeve3727102018-04-18 15:57:46 +00002322 const auto *VD = cast<ValueDecl>(
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002323 CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl());
2324 if (!Stack->checkMappableExprComponentListsForDecl(
2325 VD, /*CurrentRegionOnly=*/true,
2326 [&CurComponents](
2327 OMPClauseMappableExprCommon::MappableExprComponentListRef
2328 StackComponents,
2329 OpenMPClauseKind) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002330 auto CCI = CurComponents.rbegin();
Alexey Bataev5ec38932017-09-26 16:19:04 +00002331 auto CCE = CurComponents.rend();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002332 for (const auto &SC : llvm::reverse(StackComponents)) {
2333 // Do both expressions have the same kind?
2334 if (CCI->getAssociatedExpression()->getStmtClass() !=
2335 SC.getAssociatedExpression()->getStmtClass())
2336 if (!(isa<OMPArraySectionExpr>(
2337 SC.getAssociatedExpression()) &&
2338 isa<ArraySubscriptExpr>(
2339 CCI->getAssociatedExpression())))
2340 return false;
2341
Alexey Bataeve3727102018-04-18 15:57:46 +00002342 const Decl *CCD = CCI->getAssociatedDeclaration();
2343 const Decl *SCD = SC.getAssociatedDeclaration();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002344 CCD = CCD ? CCD->getCanonicalDecl() : nullptr;
2345 SCD = SCD ? SCD->getCanonicalDecl() : nullptr;
2346 if (SCD != CCD)
2347 return false;
2348 std::advance(CCI, 1);
Alexey Bataev5ec38932017-09-26 16:19:04 +00002349 if (CCI == CCE)
2350 break;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002351 }
2352 return true;
2353 })) {
2354 Visit(E->getBase());
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002355 }
Alexey Bataeve3727102018-04-18 15:57:46 +00002356 } else {
Alexey Bataev7fcacd82016-11-28 15:55:15 +00002357 Visit(E->getBase());
Alexey Bataeve3727102018-04-18 15:57:46 +00002358 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002359 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002360 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002361 for (OMPClause *C : S->clauses()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002362 // Skip analysis of arguments of implicitly defined firstprivate clause
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002363 // for task|target directives.
2364 // Skip analysis of arguments of implicitly defined map clause for target
2365 // directives.
2366 if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) &&
2367 C->isImplicit())) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002368 for (Stmt *CC : C->children()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002369 if (CC)
2370 Visit(CC);
2371 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002372 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002373 }
Alexey Bataevf07946e2018-10-29 20:17:42 +00002374 // Check implicitly captured variables.
2375 VisitSubCaptures(S);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002376 }
2377 void VisitStmt(Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002378 for (Stmt *C : S->children()) {
Alexey Bataev8fc7b5f2018-10-25 15:35:27 +00002379 if (C) {
2380 if (auto *OED = dyn_cast<OMPExecutableDirective>(C)) {
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00002381 // Check implicitly captured variables in the task-based directives to
Alexey Bataev8fc7b5f2018-10-25 15:35:27 +00002382 // check if they must be firstprivatized.
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00002383 VisitSubCaptures(OED);
Alexey Bataev8fc7b5f2018-10-25 15:35:27 +00002384 } else {
2385 Visit(C);
2386 }
2387 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002388 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002389 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002390
Alexey Bataeve3727102018-04-18 15:57:46 +00002391 bool isErrorFound() const { return ErrorFound; }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002392 ArrayRef<Expr *> getImplicitFirstprivate() const {
2393 return ImplicitFirstprivate;
2394 }
2395 ArrayRef<Expr *> getImplicitMap() const { return ImplicitMap; }
Alexey Bataeve3727102018-04-18 15:57:46 +00002396 const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const {
Alexey Bataev4acb8592014-07-07 13:01:15 +00002397 return VarsWithInheritedDSA;
2398 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002399
Alexey Bataev7ff55242014-06-19 09:13:45 +00002400 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
2401 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00002402};
Alexey Bataeved09d242014-05-28 05:53:51 +00002403} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00002404
Alexey Bataevbae9a792014-06-27 10:37:06 +00002405void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00002406 switch (DKind) {
Kelvin Li70a12c52016-07-13 21:51:49 +00002407 case OMPD_parallel:
2408 case OMPD_parallel_for:
2409 case OMPD_parallel_for_simd:
2410 case OMPD_parallel_sections:
Carlo Bertolliba1487b2017-10-04 14:12:09 +00002411 case OMPD_teams:
Alexey Bataev999277a2017-12-06 14:31:09 +00002412 case OMPD_teams_distribute:
2413 case OMPD_teams_distribute_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002414 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Alexey Bataev2377fe92015-09-10 08:12:02 +00002415 QualType KmpInt32PtrTy =
2416 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002417 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002418 std::make_pair(".global_tid.", KmpInt32PtrTy),
2419 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2420 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00002421 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00002422 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2423 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00002424 break;
2425 }
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002426 case OMPD_target_teams:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00002427 case OMPD_target_parallel:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00002428 case OMPD_target_parallel_for:
Alexey Bataevdfa430f2017-12-08 15:03:50 +00002429 case OMPD_target_parallel_for_simd:
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00002430 case OMPD_target_teams_distribute:
2431 case OMPD_target_teams_distribute_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002432 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2433 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2434 QualType KmpInt32PtrTy =
2435 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2436 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00002437 FunctionProtoType::ExtProtoInfo EPI;
2438 EPI.Variadic = true;
2439 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2440 Sema::CapturedParamNameType Params[] = {
2441 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002442 std::make_pair(".part_id.", KmpInt32PtrTy),
2443 std::make_pair(".privates.", VoidPtrTy),
2444 std::make_pair(
2445 ".copy_fn.",
2446 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00002447 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2448 std::make_pair(StringRef(), QualType()) // __context with shared vars
2449 };
2450 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2451 Params);
Alexey Bataev0c869ef2018-01-16 15:57:07 +00002452 // Mark this captured region as inlined, because we don't use outlined
2453 // function directly.
2454 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2455 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002456 Context, AlwaysInlineAttr::Keyword_forceinline));
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002457 Sema::CapturedParamNameType ParamsTarget[] = {
2458 std::make_pair(StringRef(), QualType()) // __context with shared vars
2459 };
2460 // Start a captured region for 'target' with no implicit parameters.
2461 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2462 ParamsTarget);
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002463 Sema::CapturedParamNameType ParamsTeamsOrParallel[] = {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002464 std::make_pair(".global_tid.", KmpInt32PtrTy),
2465 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2466 std::make_pair(StringRef(), QualType()) // __context with shared vars
2467 };
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002468 // Start a captured region for 'teams' or 'parallel'. Both regions have
2469 // the same implicit parameters.
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002470 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002471 ParamsTeamsOrParallel);
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002472 break;
2473 }
Alexey Bataev8451efa2018-01-15 19:06:12 +00002474 case OMPD_target:
2475 case OMPD_target_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002476 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2477 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2478 QualType KmpInt32PtrTy =
2479 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2480 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00002481 FunctionProtoType::ExtProtoInfo EPI;
2482 EPI.Variadic = true;
2483 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2484 Sema::CapturedParamNameType Params[] = {
2485 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002486 std::make_pair(".part_id.", KmpInt32PtrTy),
2487 std::make_pair(".privates.", VoidPtrTy),
2488 std::make_pair(
2489 ".copy_fn.",
2490 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00002491 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2492 std::make_pair(StringRef(), QualType()) // __context with shared vars
2493 };
2494 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2495 Params);
2496 // Mark this captured region as inlined, because we don't use outlined
2497 // function directly.
2498 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2499 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002500 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev8451efa2018-01-15 19:06:12 +00002501 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2502 std::make_pair(StringRef(), QualType()));
2503 break;
2504 }
Kelvin Li70a12c52016-07-13 21:51:49 +00002505 case OMPD_simd:
2506 case OMPD_for:
2507 case OMPD_for_simd:
2508 case OMPD_sections:
2509 case OMPD_section:
2510 case OMPD_single:
2511 case OMPD_master:
2512 case OMPD_critical:
Kelvin Lia579b912016-07-14 02:54:56 +00002513 case OMPD_taskgroup:
2514 case OMPD_distribute:
Alexey Bataev46506272017-12-05 17:41:34 +00002515 case OMPD_distribute_simd:
Kelvin Li70a12c52016-07-13 21:51:49 +00002516 case OMPD_ordered:
2517 case OMPD_atomic:
Alexey Bataev8451efa2018-01-15 19:06:12 +00002518 case OMPD_target_data: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002519 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002520 std::make_pair(StringRef(), QualType()) // __context with shared vars
2521 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00002522 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2523 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002524 break;
2525 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002526 case OMPD_task: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002527 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2528 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2529 QualType KmpInt32PtrTy =
2530 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2531 QualType Args[] = {VoidPtrTy};
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002532 FunctionProtoType::ExtProtoInfo EPI;
2533 EPI.Variadic = true;
2534 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002535 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00002536 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002537 std::make_pair(".part_id.", KmpInt32PtrTy),
2538 std::make_pair(".privates.", VoidPtrTy),
2539 std::make_pair(
2540 ".copy_fn.",
2541 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev48591dd2016-04-20 04:01:36 +00002542 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002543 std::make_pair(StringRef(), QualType()) // __context with shared vars
2544 };
2545 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2546 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00002547 // Mark this captured region as inlined, because we don't use outlined
2548 // function directly.
2549 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2550 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002551 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002552 break;
2553 }
Alexey Bataev1e73ef32016-04-28 12:14:51 +00002554 case OMPD_taskloop:
2555 case OMPD_taskloop_simd: {
Alexey Bataev7292c292016-04-25 12:22:29 +00002556 QualType KmpInt32Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002557 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1)
2558 .withConst();
Alexey Bataev7292c292016-04-25 12:22:29 +00002559 QualType KmpUInt64Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002560 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0)
2561 .withConst();
Alexey Bataev7292c292016-04-25 12:22:29 +00002562 QualType KmpInt64Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002563 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1)
2564 .withConst();
2565 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2566 QualType KmpInt32PtrTy =
2567 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2568 QualType Args[] = {VoidPtrTy};
Alexey Bataev7292c292016-04-25 12:22:29 +00002569 FunctionProtoType::ExtProtoInfo EPI;
2570 EPI.Variadic = true;
2571 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev49f6e782015-12-01 04:18:41 +00002572 Sema::CapturedParamNameType Params[] = {
Alexey Bataev7292c292016-04-25 12:22:29 +00002573 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002574 std::make_pair(".part_id.", KmpInt32PtrTy),
2575 std::make_pair(".privates.", VoidPtrTy),
Alexey Bataev7292c292016-04-25 12:22:29 +00002576 std::make_pair(
2577 ".copy_fn.",
2578 Context.getPointerType(CopyFnType).withConst().withRestrict()),
2579 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2580 std::make_pair(".lb.", KmpUInt64Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002581 std::make_pair(".ub.", KmpUInt64Ty),
2582 std::make_pair(".st.", KmpInt64Ty),
Alexey Bataev7292c292016-04-25 12:22:29 +00002583 std::make_pair(".liter.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002584 std::make_pair(".reductions.", VoidPtrTy),
Alexey Bataev49f6e782015-12-01 04:18:41 +00002585 std::make_pair(StringRef(), QualType()) // __context with shared vars
2586 };
2587 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2588 Params);
Alexey Bataev7292c292016-04-25 12:22:29 +00002589 // Mark this captured region as inlined, because we don't use outlined
2590 // function directly.
2591 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2592 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002593 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev49f6e782015-12-01 04:18:41 +00002594 break;
2595 }
Kelvin Li4a39add2016-07-05 05:00:15 +00002596 case OMPD_distribute_parallel_for_simd:
Alexey Bataev647dd842018-01-15 20:59:40 +00002597 case OMPD_distribute_parallel_for: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002598 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli9925f152016-06-27 14:55:37 +00002599 QualType KmpInt32PtrTy =
2600 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2601 Sema::CapturedParamNameType Params[] = {
2602 std::make_pair(".global_tid.", KmpInt32PtrTy),
2603 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002604 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2605 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli9925f152016-06-27 14:55:37 +00002606 std::make_pair(StringRef(), QualType()) // __context with shared vars
2607 };
2608 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2609 Params);
2610 break;
2611 }
Alexey Bataev647dd842018-01-15 20:59:40 +00002612 case OMPD_target_teams_distribute_parallel_for:
2613 case OMPD_target_teams_distribute_parallel_for_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002614 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli52978c32018-01-03 21:12:44 +00002615 QualType KmpInt32PtrTy =
2616 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002617 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
Carlo Bertolli52978c32018-01-03 21:12:44 +00002618
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002619 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00002620 FunctionProtoType::ExtProtoInfo EPI;
2621 EPI.Variadic = true;
2622 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2623 Sema::CapturedParamNameType Params[] = {
2624 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002625 std::make_pair(".part_id.", KmpInt32PtrTy),
2626 std::make_pair(".privates.", VoidPtrTy),
2627 std::make_pair(
2628 ".copy_fn.",
2629 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00002630 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2631 std::make_pair(StringRef(), QualType()) // __context with shared vars
2632 };
2633 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2634 Params);
Alexey Bataev9f9fb0b2018-01-16 19:02:33 +00002635 // Mark this captured region as inlined, because we don't use outlined
2636 // function directly.
2637 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2638 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002639 Context, AlwaysInlineAttr::Keyword_forceinline));
Carlo Bertolli52978c32018-01-03 21:12:44 +00002640 Sema::CapturedParamNameType ParamsTarget[] = {
2641 std::make_pair(StringRef(), QualType()) // __context with shared vars
2642 };
2643 // Start a captured region for 'target' with no implicit parameters.
2644 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2645 ParamsTarget);
2646
2647 Sema::CapturedParamNameType ParamsTeams[] = {
2648 std::make_pair(".global_tid.", KmpInt32PtrTy),
2649 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2650 std::make_pair(StringRef(), QualType()) // __context with shared vars
2651 };
2652 // Start a captured region for 'target' with no implicit parameters.
2653 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2654 ParamsTeams);
2655
2656 Sema::CapturedParamNameType ParamsParallel[] = {
2657 std::make_pair(".global_tid.", KmpInt32PtrTy),
2658 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002659 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2660 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli52978c32018-01-03 21:12:44 +00002661 std::make_pair(StringRef(), QualType()) // __context with shared vars
2662 };
2663 // Start a captured region for 'teams' or 'parallel'. Both regions have
2664 // the same implicit parameters.
2665 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2666 ParamsParallel);
2667 break;
2668 }
2669
Alexey Bataev46506272017-12-05 17:41:34 +00002670 case OMPD_teams_distribute_parallel_for:
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00002671 case OMPD_teams_distribute_parallel_for_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002672 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli62fae152017-11-20 20:46:39 +00002673 QualType KmpInt32PtrTy =
2674 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2675
2676 Sema::CapturedParamNameType ParamsTeams[] = {
2677 std::make_pair(".global_tid.", KmpInt32PtrTy),
2678 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2679 std::make_pair(StringRef(), QualType()) // __context with shared vars
2680 };
2681 // Start a captured region for 'target' with no implicit parameters.
2682 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2683 ParamsTeams);
2684
2685 Sema::CapturedParamNameType ParamsParallel[] = {
2686 std::make_pair(".global_tid.", KmpInt32PtrTy),
2687 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002688 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2689 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli62fae152017-11-20 20:46:39 +00002690 std::make_pair(StringRef(), QualType()) // __context with shared vars
2691 };
2692 // Start a captured region for 'teams' or 'parallel'. Both regions have
2693 // the same implicit parameters.
2694 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2695 ParamsParallel);
2696 break;
2697 }
Alexey Bataev7828b252017-11-21 17:08:48 +00002698 case OMPD_target_update:
2699 case OMPD_target_enter_data:
2700 case OMPD_target_exit_data: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002701 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2702 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2703 QualType KmpInt32PtrTy =
2704 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2705 QualType Args[] = {VoidPtrTy};
Alexey Bataev7828b252017-11-21 17:08:48 +00002706 FunctionProtoType::ExtProtoInfo EPI;
2707 EPI.Variadic = true;
2708 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2709 Sema::CapturedParamNameType Params[] = {
2710 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002711 std::make_pair(".part_id.", KmpInt32PtrTy),
2712 std::make_pair(".privates.", VoidPtrTy),
2713 std::make_pair(
2714 ".copy_fn.",
2715 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev7828b252017-11-21 17:08:48 +00002716 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2717 std::make_pair(StringRef(), QualType()) // __context with shared vars
2718 };
2719 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2720 Params);
2721 // Mark this captured region as inlined, because we don't use outlined
2722 // function directly.
2723 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2724 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002725 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev7828b252017-11-21 17:08:48 +00002726 break;
2727 }
Alexey Bataev9959db52014-05-06 10:08:46 +00002728 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00002729 case OMPD_taskyield:
2730 case OMPD_barrier:
2731 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002732 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00002733 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00002734 case OMPD_flush:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002735 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00002736 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00002737 case OMPD_declare_target:
2738 case OMPD_end_declare_target:
Kelvin Li1408f912018-09-26 04:28:39 +00002739 case OMPD_requires:
Alexey Bataev9959db52014-05-06 10:08:46 +00002740 llvm_unreachable("OpenMP Directive is not allowed");
2741 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00002742 llvm_unreachable("Unknown OpenMP directive");
2743 }
2744}
2745
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002746int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) {
2747 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2748 getOpenMPCaptureRegions(CaptureRegions, DKind);
2749 return CaptureRegions.size();
2750}
2751
Alexey Bataev3392d762016-02-16 11:18:12 +00002752static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00002753 Expr *CaptureExpr, bool WithInit,
2754 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00002755 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00002756 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00002757 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00002758 QualType Ty = Init->getType();
2759 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002760 if (S.getLangOpts().CPlusPlus) {
Alexey Bataev4244be22016-02-11 05:35:55 +00002761 Ty = C.getLValueReferenceType(Ty);
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002762 } else {
Alexey Bataev4244be22016-02-11 05:35:55 +00002763 Ty = C.getPointerType(Ty);
2764 ExprResult Res =
2765 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
2766 if (!Res.isUsable())
2767 return nullptr;
2768 Init = Res.get();
2769 }
Alexey Bataev61205072016-03-02 04:57:40 +00002770 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00002771 }
Alexey Bataeva7206b92016-12-20 16:51:02 +00002772 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty,
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002773 CaptureExpr->getBeginLoc());
Alexey Bataev2bbf7212016-03-03 03:52:24 +00002774 if (!WithInit)
Alexey Bataeve3727102018-04-18 15:57:46 +00002775 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C));
Alexey Bataev4244be22016-02-11 05:35:55 +00002776 S.CurContext->addHiddenDecl(CED);
Richard Smith3beb7c62017-01-12 02:27:38 +00002777 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00002778 return CED;
2779}
2780
Alexey Bataev61205072016-03-02 04:57:40 +00002781static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
2782 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00002783 OMPCapturedExprDecl *CD;
Alexey Bataeve3727102018-04-18 15:57:46 +00002784 if (VarDecl *VD = S.isOpenMPCapturedDecl(D))
Alexey Bataevb7a34b62016-02-25 03:59:29 +00002785 CD = cast<OMPCapturedExprDecl>(VD);
Alexey Bataeve3727102018-04-18 15:57:46 +00002786 else
Alexey Bataev5a3af132016-03-29 08:58:54 +00002787 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
2788 /*AsExpression=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00002789 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00002790 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00002791}
2792
Alexey Bataev5a3af132016-03-29 08:58:54 +00002793static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002794 CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00002795 if (!Ref) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002796 OMPCapturedExprDecl *CD = buildCaptureDecl(
2797 S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr,
2798 /*WithInit=*/true, /*AsExpression=*/true);
Alexey Bataev5a3af132016-03-29 08:58:54 +00002799 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
2800 CaptureExpr->getExprLoc());
2801 }
2802 ExprResult Res = Ref;
2803 if (!S.getLangOpts().CPlusPlus &&
2804 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002805 Ref->getType()->isPointerType()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00002806 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002807 if (!Res.isUsable())
2808 return ExprError();
2809 }
2810 return S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00002811}
2812
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002813namespace {
2814// OpenMP directives parsed in this section are represented as a
2815// CapturedStatement with an associated statement. If a syntax error
2816// is detected during the parsing of the associated statement, the
2817// compiler must abort processing and close the CapturedStatement.
2818//
2819// Combined directives such as 'target parallel' have more than one
2820// nested CapturedStatements. This RAII ensures that we unwind out
2821// of all the nested CapturedStatements when an error is found.
2822class CaptureRegionUnwinderRAII {
2823private:
2824 Sema &S;
2825 bool &ErrorFound;
Alexey Bataeve3727102018-04-18 15:57:46 +00002826 OpenMPDirectiveKind DKind = OMPD_unknown;
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002827
2828public:
2829 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound,
2830 OpenMPDirectiveKind DKind)
2831 : S(S), ErrorFound(ErrorFound), DKind(DKind) {}
2832 ~CaptureRegionUnwinderRAII() {
2833 if (ErrorFound) {
2834 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind);
2835 while (--ThisCaptureLevel >= 0)
2836 S.ActOnCapturedRegionError();
2837 }
2838 }
2839};
2840} // namespace
2841
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002842StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
2843 ArrayRef<OMPClause *> Clauses) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002844 bool ErrorFound = false;
2845 CaptureRegionUnwinderRAII CaptureRegionUnwinder(
2846 *this, ErrorFound, DSAStack->getCurrentDirective());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002847 if (!S.isUsable()) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002848 ErrorFound = true;
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002849 return StmtError();
2850 }
Alexey Bataev993d2802015-12-28 06:23:08 +00002851
Alexey Bataev2ba67042017-11-28 21:11:44 +00002852 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2853 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective());
Alexey Bataev993d2802015-12-28 06:23:08 +00002854 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00002855 OMPScheduleClause *SC = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +00002856 SmallVector<const OMPLinearClause *, 4> LCs;
2857 SmallVector<const OMPClauseWithPreInit *, 4> PICs;
Alexey Bataev040d5402015-05-12 08:35:28 +00002858 // This is required for proper codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00002859 for (OMPClause *Clause : Clauses) {
Alexey Bataev88202be2017-07-27 13:20:36 +00002860 if (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) &&
2861 Clause->getClauseKind() == OMPC_in_reduction) {
2862 // Capture taskgroup task_reduction descriptors inside the tasking regions
2863 // with the corresponding in_reduction items.
2864 auto *IRC = cast<OMPInReductionClause>(Clause);
Alexey Bataeve3727102018-04-18 15:57:46 +00002865 for (Expr *E : IRC->taskgroup_descriptors())
Alexey Bataev88202be2017-07-27 13:20:36 +00002866 if (E)
2867 MarkDeclarationsReferencedInExpr(E);
2868 }
Alexey Bataev16dc7b62015-05-20 03:46:04 +00002869 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00002870 Clause->getClauseKind() == OMPC_copyprivate ||
2871 (getLangOpts().OpenMPUseTLS &&
2872 getASTContext().getTargetInfo().isTLSSupported() &&
2873 Clause->getClauseKind() == OMPC_copyin)) {
2874 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00002875 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeve3727102018-04-18 15:57:46 +00002876 for (Stmt *VarRef : Clause->children()) {
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002877 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00002878 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002879 }
2880 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00002881 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev2ba67042017-11-28 21:11:44 +00002882 } else if (CaptureRegions.size() > 1 ||
2883 CaptureRegions.back() != OMPD_unknown) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002884 if (auto *C = OMPClauseWithPreInit::get(Clause))
2885 PICs.push_back(C);
Alexey Bataev005248a2016-02-25 05:25:57 +00002886 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002887 if (Expr *E = C->getPostUpdateExpr())
Alexey Bataev005248a2016-02-25 05:25:57 +00002888 MarkDeclarationsReferencedInExpr(E);
2889 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002890 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002891 if (Clause->getClauseKind() == OMPC_schedule)
2892 SC = cast<OMPScheduleClause>(Clause);
2893 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00002894 OC = cast<OMPOrderedClause>(Clause);
2895 else if (Clause->getClauseKind() == OMPC_linear)
2896 LCs.push_back(cast<OMPLinearClause>(Clause));
2897 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002898 // OpenMP, 2.7.1 Loop Construct, Restrictions
2899 // The nonmonotonic modifier cannot be specified if an ordered clause is
2900 // specified.
2901 if (SC &&
2902 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
2903 SC->getSecondScheduleModifier() ==
2904 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
2905 OC) {
2906 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
2907 ? SC->getFirstScheduleModifierLoc()
2908 : SC->getSecondScheduleModifierLoc(),
2909 diag::err_omp_schedule_nonmonotonic_ordered)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00002910 << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
Alexey Bataev6402bca2015-12-28 07:25:51 +00002911 ErrorFound = true;
2912 }
Alexey Bataev993d2802015-12-28 06:23:08 +00002913 if (!LCs.empty() && OC && OC->getNumForLoops()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002914 for (const OMPLinearClause *C : LCs) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002915 Diag(C->getBeginLoc(), diag::err_omp_linear_ordered)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00002916 << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
Alexey Bataev993d2802015-12-28 06:23:08 +00002917 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002918 ErrorFound = true;
2919 }
Alexey Bataev113438c2015-12-30 12:06:23 +00002920 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
2921 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
2922 OC->getNumForLoops()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002923 Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd)
Alexey Bataev113438c2015-12-30 12:06:23 +00002924 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
2925 ErrorFound = true;
2926 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002927 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00002928 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002929 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002930 StmtResult SR = S;
Alexey Bataev2ba67042017-11-28 21:11:44 +00002931 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002932 // Mark all variables in private list clauses as used in inner region.
2933 // Required for proper codegen of combined directives.
2934 // TODO: add processing for other clauses.
Alexey Bataev2ba67042017-11-28 21:11:44 +00002935 if (ThisCaptureRegion != OMPD_unknown) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002936 for (const clang::OMPClauseWithPreInit *C : PICs) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002937 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion();
2938 // Find the particular capture region for the clause if the
2939 // directive is a combined one with multiple capture regions.
2940 // If the directive is not a combined one, the capture region
2941 // associated with the clause is OMPD_unknown and is generated
2942 // only once.
2943 if (CaptureRegion == ThisCaptureRegion ||
2944 CaptureRegion == OMPD_unknown) {
2945 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002946 for (Decl *D : DS->decls())
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002947 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
2948 }
2949 }
2950 }
2951 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002952 SR = ActOnCapturedRegionEnd(SR.get());
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002953 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002954 return SR;
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002955}
2956
Jonas Hahnfeld64a9e3c2017-02-22 06:49:10 +00002957static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion,
2958 OpenMPDirectiveKind CancelRegion,
2959 SourceLocation StartLoc) {
2960 // CancelRegion is only needed for cancel and cancellation_point.
2961 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point)
2962 return false;
2963
2964 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for ||
2965 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup)
2966 return false;
2967
2968 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region)
2969 << getOpenMPDirectiveName(CancelRegion);
2970 return true;
2971}
2972
Alexey Bataeve3727102018-04-18 15:57:46 +00002973static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002974 OpenMPDirectiveKind CurrentRegion,
2975 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002976 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002977 SourceLocation StartLoc) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002978 if (Stack->getCurScope()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002979 OpenMPDirectiveKind ParentRegion = Stack->getParentDirective();
2980 OpenMPDirectiveKind OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002981 bool NestingProhibited = false;
2982 bool CloseNesting = true;
David Majnemer9d168222016-08-05 17:44:54 +00002983 bool OrphanSeen = false;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002984 enum {
2985 NoRecommend,
2986 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002987 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002988 ShouldBeInTargetRegion,
2989 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002990 } Recommend = NoRecommend;
Kelvin Lifd8b5742016-07-01 14:30:25 +00002991 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002992 // OpenMP [2.16, Nesting of Regions]
2993 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002994 // OpenMP [2.8.1,simd Construct, Restrictions]
Kelvin Lifd8b5742016-07-01 14:30:25 +00002995 // An ordered construct with the simd clause is the only OpenMP
2996 // construct that can appear in the simd region.
David Majnemer9d168222016-08-05 17:44:54 +00002997 // Allowing a SIMD construct nested in another SIMD construct is an
Kelvin Lifd8b5742016-07-01 14:30:25 +00002998 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
2999 // message.
3000 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
3001 ? diag::err_omp_prohibited_region_simd
3002 : diag::warn_omp_nesting_simd);
3003 return CurrentRegion != OMPD_simd;
Alexey Bataev549210e2014-06-24 04:39:47 +00003004 }
Alexey Bataev0162e452014-07-22 10:10:35 +00003005 if (ParentRegion == OMPD_atomic) {
3006 // OpenMP [2.16, Nesting of Regions]
3007 // OpenMP constructs may not be nested inside an atomic region.
3008 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
3009 return true;
3010 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003011 if (CurrentRegion == OMPD_section) {
3012 // OpenMP [2.7.2, sections Construct, Restrictions]
3013 // Orphaned section directives are prohibited. That is, the section
3014 // directives must appear within the sections construct and must not be
3015 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003016 if (ParentRegion != OMPD_sections &&
3017 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003018 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
3019 << (ParentRegion != OMPD_unknown)
3020 << getOpenMPDirectiveName(ParentRegion);
3021 return true;
3022 }
3023 return false;
3024 }
Kelvin Li2b51f722016-07-26 04:32:50 +00003025 // Allow some constructs (except teams) to be orphaned (they could be
David Majnemer9d168222016-08-05 17:44:54 +00003026 // used in functions, called from OpenMP regions with the required
Kelvin Li2b51f722016-07-26 04:32:50 +00003027 // preconditions).
Kelvin Libf594a52016-12-17 05:48:59 +00003028 if (ParentRegion == OMPD_unknown &&
3029 !isOpenMPNestingTeamsDirective(CurrentRegion))
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003030 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00003031 if (CurrentRegion == OMPD_cancellation_point ||
3032 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003033 // OpenMP [2.16, Nesting of Regions]
3034 // A cancellation point construct for which construct-type-clause is
3035 // taskgroup must be nested inside a task construct. A cancellation
3036 // point construct for which construct-type-clause is not taskgroup must
3037 // be closely nested inside an OpenMP construct that matches the type
3038 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00003039 // A cancel construct for which construct-type-clause is taskgroup must be
3040 // nested inside a task construct. A cancel construct for which
3041 // construct-type-clause is not taskgroup must be closely nested inside an
3042 // OpenMP construct that matches the type specified in
3043 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003044 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003045 !((CancelRegion == OMPD_parallel &&
3046 (ParentRegion == OMPD_parallel ||
3047 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00003048 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003049 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00003050 ParentRegion == OMPD_target_parallel_for ||
3051 ParentRegion == OMPD_distribute_parallel_for ||
Alexey Bataev16e79882017-11-22 21:12:03 +00003052 ParentRegion == OMPD_teams_distribute_parallel_for ||
3053 ParentRegion == OMPD_target_teams_distribute_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003054 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
3055 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00003056 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
3057 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003058 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00003059 // OpenMP [2.16, Nesting of Regions]
3060 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003061 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00003062 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003063 isOpenMPTaskingDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003064 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
3065 // OpenMP [2.16, Nesting of Regions]
3066 // A critical region may not be nested (closely or otherwise) inside a
3067 // critical region with the same name. Note that this restriction is not
3068 // sufficient to prevent deadlock.
3069 SourceLocation PreviousCriticalLoc;
David Majnemer9d168222016-08-05 17:44:54 +00003070 bool DeadLock = Stack->hasDirective(
3071 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
3072 const DeclarationNameInfo &DNI,
Alexey Bataeve3727102018-04-18 15:57:46 +00003073 SourceLocation Loc) {
David Majnemer9d168222016-08-05 17:44:54 +00003074 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
3075 PreviousCriticalLoc = Loc;
3076 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003077 }
3078 return false;
David Majnemer9d168222016-08-05 17:44:54 +00003079 },
3080 false /* skip top directive */);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003081 if (DeadLock) {
3082 SemaRef.Diag(StartLoc,
3083 diag::err_omp_prohibited_region_critical_same_name)
3084 << CurrentName.getName();
3085 if (PreviousCriticalLoc.isValid())
3086 SemaRef.Diag(PreviousCriticalLoc,
3087 diag::note_omp_previous_critical_region);
3088 return true;
3089 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003090 } else if (CurrentRegion == OMPD_barrier) {
3091 // OpenMP [2.16, Nesting of Regions]
3092 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003093 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003094 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3095 isOpenMPTaskingDirective(ParentRegion) ||
3096 ParentRegion == OMPD_master ||
3097 ParentRegion == OMPD_critical ||
3098 ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00003099 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Kelvin Li579e41c2016-11-30 23:51:03 +00003100 !isOpenMPParallelDirective(CurrentRegion) &&
3101 !isOpenMPTeamsDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003102 // OpenMP [2.16, Nesting of Regions]
3103 // A worksharing region may not be closely nested inside a worksharing,
3104 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003105 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3106 isOpenMPTaskingDirective(ParentRegion) ||
3107 ParentRegion == OMPD_master ||
3108 ParentRegion == OMPD_critical ||
3109 ParentRegion == OMPD_ordered;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003110 Recommend = ShouldBeInParallelRegion;
3111 } else if (CurrentRegion == OMPD_ordered) {
3112 // OpenMP [2.16, Nesting of Regions]
3113 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00003114 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003115 // An ordered region must be closely nested inside a loop region (or
3116 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003117 // OpenMP [2.8.1,simd Construct, Restrictions]
3118 // An ordered construct with the simd clause is the only OpenMP construct
3119 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003120 NestingProhibited = ParentRegion == OMPD_critical ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003121 isOpenMPTaskingDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003122 !(isOpenMPSimdDirective(ParentRegion) ||
3123 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003124 Recommend = ShouldBeInOrderedRegion;
Kelvin Libf594a52016-12-17 05:48:59 +00003125 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00003126 // OpenMP [2.16, Nesting of Regions]
3127 // If specified, a teams construct must be contained within a target
3128 // construct.
3129 NestingProhibited = ParentRegion != OMPD_target;
Kelvin Li2b51f722016-07-26 04:32:50 +00003130 OrphanSeen = ParentRegion == OMPD_unknown;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003131 Recommend = ShouldBeInTargetRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003132 }
Kelvin Libf594a52016-12-17 05:48:59 +00003133 if (!NestingProhibited &&
3134 !isOpenMPTargetExecutionDirective(CurrentRegion) &&
3135 !isOpenMPTargetDataManagementDirective(CurrentRegion) &&
3136 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00003137 // OpenMP [2.16, Nesting of Regions]
3138 // distribute, parallel, parallel sections, parallel workshare, and the
3139 // parallel loop and parallel loop SIMD constructs are the only OpenMP
3140 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003141 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
3142 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00003143 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003144 }
David Majnemer9d168222016-08-05 17:44:54 +00003145 if (!NestingProhibited &&
Kelvin Li02532872016-08-05 14:37:37 +00003146 isOpenMPNestingDistributeDirective(CurrentRegion)) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003147 // OpenMP 4.5 [2.17 Nesting of Regions]
3148 // The region associated with the distribute construct must be strictly
3149 // nested inside a teams region
Kelvin Libf594a52016-12-17 05:48:59 +00003150 NestingProhibited =
3151 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003152 Recommend = ShouldBeInTeamsRegion;
3153 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003154 if (!NestingProhibited &&
3155 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
3156 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
3157 // OpenMP 4.5 [2.17 Nesting of Regions]
3158 // If a target, target update, target data, target enter data, or
3159 // target exit data construct is encountered during execution of a
3160 // target region, the behavior is unspecified.
3161 NestingProhibited = Stack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +00003162 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
Alexey Bataeve3727102018-04-18 15:57:46 +00003163 SourceLocation) {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003164 if (isOpenMPTargetExecutionDirective(K)) {
3165 OffendingRegion = K;
3166 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003167 }
3168 return false;
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003169 },
3170 false /* don't skip top directive */);
3171 CloseNesting = false;
3172 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003173 if (NestingProhibited) {
Kelvin Li2b51f722016-07-26 04:32:50 +00003174 if (OrphanSeen) {
3175 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
3176 << getOpenMPDirectiveName(CurrentRegion) << Recommend;
3177 } else {
3178 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
3179 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
3180 << Recommend << getOpenMPDirectiveName(CurrentRegion);
3181 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003182 return true;
3183 }
3184 }
3185 return false;
3186}
3187
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003188static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
3189 ArrayRef<OMPClause *> Clauses,
3190 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
3191 bool ErrorFound = false;
3192 unsigned NamedModifiersNumber = 0;
3193 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
3194 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00003195 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00003196 for (const OMPClause *C : Clauses) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003197 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
3198 // At most one if clause without a directive-name-modifier can appear on
3199 // the directive.
3200 OpenMPDirectiveKind CurNM = IC->getNameModifier();
3201 if (FoundNameModifiers[CurNM]) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003202 S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003203 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
3204 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
3205 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003206 } else if (CurNM != OMPD_unknown) {
3207 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003208 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003209 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003210 FoundNameModifiers[CurNM] = IC;
3211 if (CurNM == OMPD_unknown)
3212 continue;
3213 // Check if the specified name modifier is allowed for the current
3214 // directive.
3215 // At most one if clause with the particular directive-name-modifier can
3216 // appear on the directive.
3217 bool MatchFound = false;
3218 for (auto NM : AllowedNameModifiers) {
3219 if (CurNM == NM) {
3220 MatchFound = true;
3221 break;
3222 }
3223 }
3224 if (!MatchFound) {
3225 S.Diag(IC->getNameModifierLoc(),
3226 diag::err_omp_wrong_if_directive_name_modifier)
3227 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
3228 ErrorFound = true;
3229 }
3230 }
3231 }
3232 // If any if clause on the directive includes a directive-name-modifier then
3233 // all if clauses on the directive must include a directive-name-modifier.
3234 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
3235 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003236 S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003237 diag::err_omp_no_more_if_clause);
3238 } else {
3239 std::string Values;
3240 std::string Sep(", ");
3241 unsigned AllowedCnt = 0;
3242 unsigned TotalAllowedNum =
3243 AllowedNameModifiers.size() - NamedModifiersNumber;
3244 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3245 ++Cnt) {
3246 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
3247 if (!FoundNameModifiers[NM]) {
3248 Values += "'";
3249 Values += getOpenMPDirectiveName(NM);
3250 Values += "'";
3251 if (AllowedCnt + 2 == TotalAllowedNum)
3252 Values += " or ";
3253 else if (AllowedCnt + 1 != TotalAllowedNum)
3254 Values += Sep;
3255 ++AllowedCnt;
3256 }
3257 }
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003258 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003259 diag::err_omp_unnamed_if_clause)
3260 << (TotalAllowedNum > 1) << Values;
3261 }
Alexey Bataeve3727102018-04-18 15:57:46 +00003262 for (SourceLocation Loc : NameModifierLoc) {
Alexey Bataevecb156a2015-09-15 17:23:56 +00003263 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3264 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003265 ErrorFound = true;
3266 }
3267 return ErrorFound;
3268}
3269
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003270StmtResult Sema::ActOnOpenMPExecutableDirective(
3271 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
3272 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3273 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003274 StmtResult Res = StmtError();
Jonas Hahnfeld64a9e3c2017-02-22 06:49:10 +00003275 // First check CancelRegion which is then used in checkNestingOfRegions.
3276 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) ||
3277 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003278 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00003279 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003280
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003281 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataeve3727102018-04-18 15:57:46 +00003282 VarsWithInheritedDSAType VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003283 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00003284 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00003285 if (AStmt && !CurContext->isDependentContext()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00003286 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
3287
3288 // Check default data sharing attributes for referenced variables.
3289 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
Arpith Chacko Jacob1f46b702017-01-23 15:38:49 +00003290 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind);
3291 Stmt *S = AStmt;
3292 while (--ThisCaptureLevel >= 0)
3293 S = cast<CapturedStmt>(S)->getCapturedStmt();
3294 DSAChecker.Visit(S);
Alexey Bataev68446b72014-07-18 07:47:19 +00003295 if (DSAChecker.isErrorFound())
3296 return StmtError();
3297 // Generate list of implicitly defined firstprivate variables.
3298 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00003299
Alexey Bataev88202be2017-07-27 13:20:36 +00003300 SmallVector<Expr *, 4> ImplicitFirstprivates(
3301 DSAChecker.getImplicitFirstprivate().begin(),
3302 DSAChecker.getImplicitFirstprivate().end());
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003303 SmallVector<Expr *, 4> ImplicitMaps(DSAChecker.getImplicitMap().begin(),
3304 DSAChecker.getImplicitMap().end());
Alexey Bataev88202be2017-07-27 13:20:36 +00003305 // Mark taskgroup task_reduction descriptors as implicitly firstprivate.
Alexey Bataeve3727102018-04-18 15:57:46 +00003306 for (OMPClause *C : Clauses) {
Alexey Bataev88202be2017-07-27 13:20:36 +00003307 if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003308 for (Expr *E : IRC->taskgroup_descriptors())
Alexey Bataev88202be2017-07-27 13:20:36 +00003309 if (E)
3310 ImplicitFirstprivates.emplace_back(E);
3311 }
3312 }
3313 if (!ImplicitFirstprivates.empty()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00003314 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
Alexey Bataev88202be2017-07-27 13:20:36 +00003315 ImplicitFirstprivates, SourceLocation(), SourceLocation(),
3316 SourceLocation())) {
Alexey Bataev68446b72014-07-18 07:47:19 +00003317 ClausesWithImplicit.push_back(Implicit);
3318 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
Alexey Bataev88202be2017-07-27 13:20:36 +00003319 ImplicitFirstprivates.size();
Alexey Bataeve3727102018-04-18 15:57:46 +00003320 } else {
Alexey Bataev68446b72014-07-18 07:47:19 +00003321 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003322 }
Alexey Bataev68446b72014-07-18 07:47:19 +00003323 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003324 if (!ImplicitMaps.empty()) {
3325 if (OMPClause *Implicit = ActOnOpenMPMapClause(
3326 OMPC_MAP_unknown, OMPC_MAP_tofrom, /*IsMapTypeImplicit=*/true,
3327 SourceLocation(), SourceLocation(), ImplicitMaps,
3328 SourceLocation(), SourceLocation(), SourceLocation())) {
3329 ClausesWithImplicit.emplace_back(Implicit);
3330 ErrorFound |=
3331 cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMaps.size();
Alexey Bataeve3727102018-04-18 15:57:46 +00003332 } else {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003333 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003334 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003335 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003336 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003337
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003338 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003339 switch (Kind) {
3340 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00003341 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3342 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003343 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003344 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003345 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003346 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3347 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003348 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003349 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003350 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3351 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003352 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003353 case OMPD_for_simd:
3354 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3355 EndLoc, VarsWithInheritedDSA);
3356 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003357 case OMPD_sections:
3358 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3359 EndLoc);
3360 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003361 case OMPD_section:
3362 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003363 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003364 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3365 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003366 case OMPD_single:
3367 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3368 EndLoc);
3369 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003370 case OMPD_master:
3371 assert(ClausesWithImplicit.empty() &&
3372 "No clauses are allowed for 'omp master' directive");
3373 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3374 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003375 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003376 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3377 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003378 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003379 case OMPD_parallel_for:
3380 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3381 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003382 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003383 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003384 case OMPD_parallel_for_simd:
3385 Res = ActOnOpenMPParallelForSimdDirective(
3386 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003387 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003388 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003389 case OMPD_parallel_sections:
3390 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3391 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003392 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003393 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003394 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003395 Res =
3396 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003397 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003398 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003399 case OMPD_taskyield:
3400 assert(ClausesWithImplicit.empty() &&
3401 "No clauses are allowed for 'omp taskyield' directive");
3402 assert(AStmt == nullptr &&
3403 "No associated statement allowed for 'omp taskyield' directive");
3404 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3405 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003406 case OMPD_barrier:
3407 assert(ClausesWithImplicit.empty() &&
3408 "No clauses are allowed for 'omp barrier' directive");
3409 assert(AStmt == nullptr &&
3410 "No associated statement allowed for 'omp barrier' directive");
3411 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3412 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003413 case OMPD_taskwait:
3414 assert(ClausesWithImplicit.empty() &&
3415 "No clauses are allowed for 'omp taskwait' directive");
3416 assert(AStmt == nullptr &&
3417 "No associated statement allowed for 'omp taskwait' directive");
3418 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3419 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003420 case OMPD_taskgroup:
Alexey Bataev169d96a2017-07-18 20:17:46 +00003421 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc,
3422 EndLoc);
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003423 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003424 case OMPD_flush:
3425 assert(AStmt == nullptr &&
3426 "No associated statement allowed for 'omp flush' directive");
3427 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3428 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003429 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003430 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3431 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003432 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003433 case OMPD_atomic:
3434 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3435 EndLoc);
3436 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003437 case OMPD_teams:
3438 Res =
3439 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3440 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003441 case OMPD_target:
3442 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3443 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003444 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003445 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003446 case OMPD_target_parallel:
3447 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3448 StartLoc, EndLoc);
3449 AllowedNameModifiers.push_back(OMPD_target);
3450 AllowedNameModifiers.push_back(OMPD_parallel);
3451 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003452 case OMPD_target_parallel_for:
3453 Res = ActOnOpenMPTargetParallelForDirective(
3454 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3455 AllowedNameModifiers.push_back(OMPD_target);
3456 AllowedNameModifiers.push_back(OMPD_parallel);
3457 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003458 case OMPD_cancellation_point:
3459 assert(ClausesWithImplicit.empty() &&
3460 "No clauses are allowed for 'omp cancellation point' directive");
3461 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3462 "cancellation point' directive");
3463 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3464 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003465 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003466 assert(AStmt == nullptr &&
3467 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003468 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3469 CancelRegion);
3470 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003471 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003472 case OMPD_target_data:
3473 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3474 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003475 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003476 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003477 case OMPD_target_enter_data:
3478 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00003479 EndLoc, AStmt);
Samuel Antaodf67fc42016-01-19 19:15:56 +00003480 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3481 break;
Samuel Antao72590762016-01-19 20:04:50 +00003482 case OMPD_target_exit_data:
3483 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00003484 EndLoc, AStmt);
Samuel Antao72590762016-01-19 20:04:50 +00003485 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3486 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003487 case OMPD_taskloop:
3488 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3489 EndLoc, VarsWithInheritedDSA);
3490 AllowedNameModifiers.push_back(OMPD_taskloop);
3491 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003492 case OMPD_taskloop_simd:
3493 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3494 EndLoc, VarsWithInheritedDSA);
3495 AllowedNameModifiers.push_back(OMPD_taskloop);
3496 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003497 case OMPD_distribute:
3498 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3499 EndLoc, VarsWithInheritedDSA);
3500 break;
Samuel Antao686c70c2016-05-26 17:30:50 +00003501 case OMPD_target_update:
Alexey Bataev7828b252017-11-21 17:08:48 +00003502 Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc,
3503 EndLoc, AStmt);
Samuel Antao686c70c2016-05-26 17:30:50 +00003504 AllowedNameModifiers.push_back(OMPD_target_update);
3505 break;
Carlo Bertolli9925f152016-06-27 14:55:37 +00003506 case OMPD_distribute_parallel_for:
3507 Res = ActOnOpenMPDistributeParallelForDirective(
3508 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3509 AllowedNameModifiers.push_back(OMPD_parallel);
3510 break;
Kelvin Li4a39add2016-07-05 05:00:15 +00003511 case OMPD_distribute_parallel_for_simd:
3512 Res = ActOnOpenMPDistributeParallelForSimdDirective(
3513 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3514 AllowedNameModifiers.push_back(OMPD_parallel);
3515 break;
Kelvin Li787f3fc2016-07-06 04:45:38 +00003516 case OMPD_distribute_simd:
3517 Res = ActOnOpenMPDistributeSimdDirective(
3518 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3519 break;
Kelvin Lia579b912016-07-14 02:54:56 +00003520 case OMPD_target_parallel_for_simd:
3521 Res = ActOnOpenMPTargetParallelForSimdDirective(
3522 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3523 AllowedNameModifiers.push_back(OMPD_target);
3524 AllowedNameModifiers.push_back(OMPD_parallel);
3525 break;
Kelvin Li986330c2016-07-20 22:57:10 +00003526 case OMPD_target_simd:
3527 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3528 EndLoc, VarsWithInheritedDSA);
3529 AllowedNameModifiers.push_back(OMPD_target);
3530 break;
Kelvin Li02532872016-08-05 14:37:37 +00003531 case OMPD_teams_distribute:
David Majnemer9d168222016-08-05 17:44:54 +00003532 Res = ActOnOpenMPTeamsDistributeDirective(
3533 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Kelvin Li02532872016-08-05 14:37:37 +00003534 break;
Kelvin Li4e325f72016-10-25 12:50:55 +00003535 case OMPD_teams_distribute_simd:
3536 Res = ActOnOpenMPTeamsDistributeSimdDirective(
3537 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3538 break;
Kelvin Li579e41c2016-11-30 23:51:03 +00003539 case OMPD_teams_distribute_parallel_for_simd:
3540 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective(
3541 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3542 AllowedNameModifiers.push_back(OMPD_parallel);
3543 break;
Kelvin Li7ade93f2016-12-09 03:24:30 +00003544 case OMPD_teams_distribute_parallel_for:
3545 Res = ActOnOpenMPTeamsDistributeParallelForDirective(
3546 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3547 AllowedNameModifiers.push_back(OMPD_parallel);
3548 break;
Kelvin Libf594a52016-12-17 05:48:59 +00003549 case OMPD_target_teams:
3550 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc,
3551 EndLoc);
3552 AllowedNameModifiers.push_back(OMPD_target);
3553 break;
Kelvin Li83c451e2016-12-25 04:52:54 +00003554 case OMPD_target_teams_distribute:
3555 Res = ActOnOpenMPTargetTeamsDistributeDirective(
3556 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3557 AllowedNameModifiers.push_back(OMPD_target);
3558 break;
Kelvin Li80e8f562016-12-29 22:16:30 +00003559 case OMPD_target_teams_distribute_parallel_for:
3560 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective(
3561 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3562 AllowedNameModifiers.push_back(OMPD_target);
3563 AllowedNameModifiers.push_back(OMPD_parallel);
3564 break;
Kelvin Li1851df52017-01-03 05:23:48 +00003565 case OMPD_target_teams_distribute_parallel_for_simd:
3566 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
3567 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3568 AllowedNameModifiers.push_back(OMPD_target);
3569 AllowedNameModifiers.push_back(OMPD_parallel);
3570 break;
Kelvin Lida681182017-01-10 18:08:18 +00003571 case OMPD_target_teams_distribute_simd:
3572 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective(
3573 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3574 AllowedNameModifiers.push_back(OMPD_target);
3575 break;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00003576 case OMPD_declare_target:
3577 case OMPD_end_declare_target:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003578 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003579 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003580 case OMPD_declare_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00003581 case OMPD_requires:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003582 llvm_unreachable("OpenMP Directive is not allowed");
3583 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003584 llvm_unreachable("Unknown OpenMP directive");
3585 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003586
Alexey Bataeve3727102018-04-18 15:57:46 +00003587 for (const auto &P : VarsWithInheritedDSA) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003588 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3589 << P.first << P.second->getSourceRange();
3590 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003591 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3592
3593 if (!AllowedNameModifiers.empty())
3594 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3595 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003596
Alexey Bataeved09d242014-05-28 05:53:51 +00003597 if (ErrorFound)
3598 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003599 return Res;
3600}
3601
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003602Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3603 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
Alexey Bataevd93d3762016-04-12 09:35:56 +00003604 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
Alexey Bataevecba70f2016-04-12 11:02:11 +00003605 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3606 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003607 assert(Aligneds.size() == Alignments.size());
Alexey Bataevecba70f2016-04-12 11:02:11 +00003608 assert(Linears.size() == LinModifiers.size());
3609 assert(Linears.size() == Steps.size());
Alexey Bataev587e1de2016-03-30 10:43:55 +00003610 if (!DG || DG.get().isNull())
3611 return DeclGroupPtrTy();
3612
3613 if (!DG.get().isSingleDecl()) {
Alexey Bataev20dfd772016-04-04 10:12:15 +00003614 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003615 return DG;
3616 }
Alexey Bataeve3727102018-04-18 15:57:46 +00003617 Decl *ADecl = DG.get().getSingleDecl();
Alexey Bataev587e1de2016-03-30 10:43:55 +00003618 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3619 ADecl = FTD->getTemplatedDecl();
3620
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003621 auto *FD = dyn_cast<FunctionDecl>(ADecl);
3622 if (!FD) {
3623 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003624 return DeclGroupPtrTy();
3625 }
3626
Alexey Bataev2af33e32016-04-07 12:45:37 +00003627 // OpenMP [2.8.2, declare simd construct, Description]
3628 // The parameter of the simdlen clause must be a constant positive integer
3629 // expression.
3630 ExprResult SL;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003631 if (Simdlen)
Alexey Bataev2af33e32016-04-07 12:45:37 +00003632 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003633 // OpenMP [2.8.2, declare simd construct, Description]
3634 // The special this pointer can be used as if was one of the arguments to the
3635 // function in any of the linear, aligned, or uniform clauses.
3636 // The uniform clause declares one or more arguments to have an invariant
3637 // value for all concurrent invocations of the function in the execution of a
3638 // single SIMD loop.
Alexey Bataeve3727102018-04-18 15:57:46 +00003639 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs;
3640 const Expr *UniformedLinearThis = nullptr;
3641 for (const Expr *E : Uniforms) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003642 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00003643 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3644 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003645 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3646 FD->getParamDecl(PVD->getFunctionScopeIndex())
Alexey Bataevecba70f2016-04-12 11:02:11 +00003647 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
Alexey Bataev43a919f2018-04-13 17:48:43 +00003648 UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003649 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003650 }
3651 if (isa<CXXThisExpr>(E)) {
3652 UniformedLinearThis = E;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003653 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003654 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003655 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3656 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
Alexey Bataev2af33e32016-04-07 12:45:37 +00003657 }
Alexey Bataevd93d3762016-04-12 09:35:56 +00003658 // OpenMP [2.8.2, declare simd construct, Description]
3659 // The aligned clause declares that the object to which each list item points
3660 // is aligned to the number of bytes expressed in the optional parameter of
3661 // the aligned clause.
3662 // The special this pointer can be used as if was one of the arguments to the
3663 // function in any of the linear, aligned, or uniform clauses.
3664 // The type of list items appearing in the aligned clause must be array,
3665 // pointer, reference to array, or reference to pointer.
Alexey Bataeve3727102018-04-18 15:57:46 +00003666 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs;
3667 const Expr *AlignedThis = nullptr;
3668 for (const Expr *E : Aligneds) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003669 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00003670 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3671 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3672 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevd93d3762016-04-12 09:35:56 +00003673 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3674 FD->getParamDecl(PVD->getFunctionScopeIndex())
3675 ->getCanonicalDecl() == CanonPVD) {
3676 // OpenMP [2.8.1, simd construct, Restrictions]
3677 // A list-item cannot appear in more than one aligned clause.
3678 if (AlignedArgs.count(CanonPVD) > 0) {
3679 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3680 << 1 << E->getSourceRange();
3681 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3682 diag::note_omp_explicit_dsa)
3683 << getOpenMPClauseName(OMPC_aligned);
3684 continue;
3685 }
3686 AlignedArgs[CanonPVD] = E;
3687 QualType QTy = PVD->getType()
3688 .getNonReferenceType()
3689 .getUnqualifiedType()
3690 .getCanonicalType();
3691 const Type *Ty = QTy.getTypePtrOrNull();
3692 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3693 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3694 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3695 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3696 }
3697 continue;
3698 }
3699 }
3700 if (isa<CXXThisExpr>(E)) {
3701 if (AlignedThis) {
3702 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3703 << 2 << E->getSourceRange();
3704 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3705 << getOpenMPClauseName(OMPC_aligned);
3706 }
3707 AlignedThis = E;
3708 continue;
3709 }
3710 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3711 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3712 }
3713 // The optional parameter of the aligned clause, alignment, must be a constant
3714 // positive integer expression. If no optional parameter is specified,
3715 // implementation-defined default alignments for SIMD instructions on the
3716 // target platforms are assumed.
Alexey Bataeve3727102018-04-18 15:57:46 +00003717 SmallVector<const Expr *, 4> NewAligns;
3718 for (Expr *E : Alignments) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003719 ExprResult Align;
3720 if (E)
3721 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3722 NewAligns.push_back(Align.get());
3723 }
Alexey Bataevecba70f2016-04-12 11:02:11 +00003724 // OpenMP [2.8.2, declare simd construct, Description]
3725 // The linear clause declares one or more list items to be private to a SIMD
3726 // lane and to have a linear relationship with respect to the iteration space
3727 // of a loop.
3728 // The special this pointer can be used as if was one of the arguments to the
3729 // function in any of the linear, aligned, or uniform clauses.
3730 // When a linear-step expression is specified in a linear clause it must be
3731 // either a constant integer expression or an integer-typed parameter that is
3732 // specified in a uniform clause on the directive.
Alexey Bataeve3727102018-04-18 15:57:46 +00003733 llvm::DenseMap<const Decl *, const Expr *> LinearArgs;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003734 const bool IsUniformedThis = UniformedLinearThis != nullptr;
3735 auto MI = LinModifiers.begin();
Alexey Bataeve3727102018-04-18 15:57:46 +00003736 for (const Expr *E : Linears) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003737 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3738 ++MI;
3739 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00003740 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3741 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3742 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevecba70f2016-04-12 11:02:11 +00003743 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3744 FD->getParamDecl(PVD->getFunctionScopeIndex())
3745 ->getCanonicalDecl() == CanonPVD) {
3746 // OpenMP [2.15.3.7, linear Clause, Restrictions]
3747 // A list-item cannot appear in more than one linear clause.
3748 if (LinearArgs.count(CanonPVD) > 0) {
3749 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3750 << getOpenMPClauseName(OMPC_linear)
3751 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3752 Diag(LinearArgs[CanonPVD]->getExprLoc(),
3753 diag::note_omp_explicit_dsa)
3754 << getOpenMPClauseName(OMPC_linear);
3755 continue;
3756 }
3757 // Each argument can appear in at most one uniform or linear clause.
3758 if (UniformedArgs.count(CanonPVD) > 0) {
3759 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3760 << getOpenMPClauseName(OMPC_linear)
3761 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3762 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3763 diag::note_omp_explicit_dsa)
3764 << getOpenMPClauseName(OMPC_uniform);
3765 continue;
3766 }
3767 LinearArgs[CanonPVD] = E;
3768 if (E->isValueDependent() || E->isTypeDependent() ||
3769 E->isInstantiationDependent() ||
3770 E->containsUnexpandedParameterPack())
3771 continue;
3772 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3773 PVD->getOriginalType());
3774 continue;
3775 }
3776 }
3777 if (isa<CXXThisExpr>(E)) {
3778 if (UniformedLinearThis) {
3779 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3780 << getOpenMPClauseName(OMPC_linear)
3781 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3782 << E->getSourceRange();
3783 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3784 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3785 : OMPC_linear);
3786 continue;
3787 }
3788 UniformedLinearThis = E;
3789 if (E->isValueDependent() || E->isTypeDependent() ||
3790 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3791 continue;
3792 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3793 E->getType());
3794 continue;
3795 }
3796 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3797 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3798 }
3799 Expr *Step = nullptr;
3800 Expr *NewStep = nullptr;
3801 SmallVector<Expr *, 4> NewSteps;
Alexey Bataeve3727102018-04-18 15:57:46 +00003802 for (Expr *E : Steps) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003803 // Skip the same step expression, it was checked already.
3804 if (Step == E || !E) {
3805 NewSteps.push_back(E ? NewStep : nullptr);
3806 continue;
3807 }
3808 Step = E;
Alexey Bataeve3727102018-04-18 15:57:46 +00003809 if (const auto *DRE = dyn_cast<DeclRefExpr>(Step))
3810 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3811 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevecba70f2016-04-12 11:02:11 +00003812 if (UniformedArgs.count(CanonPVD) == 0) {
3813 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
3814 << Step->getSourceRange();
3815 } else if (E->isValueDependent() || E->isTypeDependent() ||
3816 E->isInstantiationDependent() ||
3817 E->containsUnexpandedParameterPack() ||
Alexey Bataeve3727102018-04-18 15:57:46 +00003818 CanonPVD->getType()->hasIntegerRepresentation()) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003819 NewSteps.push_back(Step);
Alexey Bataeve3727102018-04-18 15:57:46 +00003820 } else {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003821 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
3822 << Step->getSourceRange();
3823 }
3824 continue;
3825 }
3826 NewStep = Step;
3827 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3828 !Step->isInstantiationDependent() &&
3829 !Step->containsUnexpandedParameterPack()) {
3830 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
3831 .get();
3832 if (NewStep)
3833 NewStep = VerifyIntegerConstantExpression(NewStep).get();
3834 }
3835 NewSteps.push_back(NewStep);
3836 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003837 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3838 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
Alexey Bataevd93d3762016-04-12 09:35:56 +00003839 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
Alexey Bataevecba70f2016-04-12 11:02:11 +00003840 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
3841 const_cast<Expr **>(Linears.data()), Linears.size(),
3842 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
3843 NewSteps.data(), NewSteps.size(), SR);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003844 ADecl->addAttr(NewAttr);
3845 return ConvertDeclToDeclGroup(ADecl);
3846}
3847
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003848StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3849 Stmt *AStmt,
3850 SourceLocation StartLoc,
3851 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003852 if (!AStmt)
3853 return StmtError();
3854
Alexey Bataeve3727102018-04-18 15:57:46 +00003855 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev9959db52014-05-06 10:08:46 +00003856 // 1.2.2 OpenMP Language Terminology
3857 // Structured block - An executable statement with a single entry at the
3858 // top and a single exit at the bottom.
3859 // The point of exit cannot be a branch out of the structured block.
3860 // longjmp() and throw() must not violate the entry/exit criteria.
3861 CS->getCapturedDecl()->setNothrow();
3862
Reid Kleckner87a31802018-03-12 21:43:02 +00003863 setFunctionHasBranchProtectedScope();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003864
Alexey Bataev25e5b442015-09-15 12:52:43 +00003865 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3866 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003867}
3868
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003869namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003870/// Helper class for checking canonical form of the OpenMP loops and
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003871/// extracting iteration space of each loop in the loop nest, that will be used
3872/// for IR generation.
3873class OpenMPIterationSpaceChecker {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003874 /// Reference to Sema.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003875 Sema &SemaRef;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003876 /// A location for diagnostics (when there is no some better location).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003877 SourceLocation DefaultLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003878 /// A location for diagnostics (when increment is not compatible).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003879 SourceLocation ConditionLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003880 /// A source location for referring to loop init later.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003881 SourceRange InitSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003882 /// A source location for referring to condition later.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003883 SourceRange ConditionSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003884 /// A source location for referring to increment later.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003885 SourceRange IncrementSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003886 /// Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003887 ValueDecl *LCDecl = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003888 /// Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003889 Expr *LCRef = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003890 /// Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003891 Expr *LB = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003892 /// Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003893 Expr *UB = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003894 /// Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003895 Expr *Step = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003896 /// This flag is true when condition is one of:
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003897 /// Var < UB
3898 /// Var <= UB
3899 /// UB > Var
3900 /// UB >= Var
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003901 bool TestIsLessOp = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003902 /// This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003903 bool TestIsStrictOp = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003904 /// This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003905 bool SubtractStep = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003906
3907public:
3908 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003909 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003910 /// Check init-expr for canonical loop form and save loop counter
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003911 /// variable - #Var and its initialization value - #LB.
Alexey Bataeve3727102018-04-18 15:57:46 +00003912 bool checkAndSetInit(Stmt *S, bool EmitDiags = true);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003913 /// Check test-expr for canonical form, save upper-bound (#UB), flags
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003914 /// for less/greater and for strict/non-strict comparison.
Alexey Bataeve3727102018-04-18 15:57:46 +00003915 bool checkAndSetCond(Expr *S);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003916 /// Check incr-expr for canonical loop form and return true if it
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003917 /// does not conform, otherwise save loop step (#Step).
Alexey Bataeve3727102018-04-18 15:57:46 +00003918 bool checkAndSetInc(Expr *S);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003919 /// Return the loop counter variable.
Alexey Bataeve3727102018-04-18 15:57:46 +00003920 ValueDecl *getLoopDecl() const { return LCDecl; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003921 /// Return the reference expression to loop counter variable.
Alexey Bataeve3727102018-04-18 15:57:46 +00003922 Expr *getLoopDeclRefExpr() const { return LCRef; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003923 /// Source range of the loop init.
Alexey Bataeve3727102018-04-18 15:57:46 +00003924 SourceRange getInitSrcRange() const { return InitSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003925 /// Source range of the loop condition.
Alexey Bataeve3727102018-04-18 15:57:46 +00003926 SourceRange getConditionSrcRange() const { return ConditionSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003927 /// Source range of the loop increment.
Alexey Bataeve3727102018-04-18 15:57:46 +00003928 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003929 /// True if the step should be subtracted.
Alexey Bataeve3727102018-04-18 15:57:46 +00003930 bool shouldSubtractStep() const { return SubtractStep; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003931 /// Build the expression to calculate the number of iterations.
Alexey Bataeve3727102018-04-18 15:57:46 +00003932 Expr *buildNumIterations(
3933 Scope *S, const bool LimitedType,
3934 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003935 /// Build the precondition expression for the loops.
Alexey Bataeve3727102018-04-18 15:57:46 +00003936 Expr *
3937 buildPreCond(Scope *S, Expr *Cond,
3938 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003939 /// Build reference expression to the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00003940 DeclRefExpr *
3941 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
3942 DSAStackTy &DSA) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003943 /// Build reference expression to the private counter be used for
Alexey Bataeva8899172015-08-06 12:30:57 +00003944 /// codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00003945 Expr *buildPrivateCounterVar() const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003946 /// Build initialization of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00003947 Expr *buildCounterInit() const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003948 /// Build step of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00003949 Expr *buildCounterStep() const;
Alexey Bataevf138fda2018-08-13 19:04:24 +00003950 /// Build loop data with counter value for depend clauses in ordered
3951 /// directives.
3952 Expr *
3953 buildOrderedLoopData(Scope *S, Expr *Counter,
3954 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
3955 SourceLocation Loc, Expr *Inc = nullptr,
3956 OverloadedOperatorKind OOK = OO_Amp);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003957 /// Return true if any expression is dependent.
Alexey Bataeve3727102018-04-18 15:57:46 +00003958 bool dependent() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003959
3960private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003961 /// Check the right-hand side of an assignment in the increment
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003962 /// expression.
Alexey Bataeve3727102018-04-18 15:57:46 +00003963 bool checkAndSetIncRHS(Expr *RHS);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003964 /// Helper to set loop counter variable and its initializer.
Alexey Bataeve3727102018-04-18 15:57:46 +00003965 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003966 /// Helper to set upper bound.
Alexey Bataeve3727102018-04-18 15:57:46 +00003967 bool setUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003968 SourceLocation SL);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003969 /// Helper to set loop increment.
Alexey Bataeve3727102018-04-18 15:57:46 +00003970 bool setStep(Expr *NewStep, bool Subtract);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003971};
3972
Alexey Bataeve3727102018-04-18 15:57:46 +00003973bool OpenMPIterationSpaceChecker::dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003974 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003975 assert(!LB && !UB && !Step);
3976 return false;
3977 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003978 return LCDecl->getType()->isDependentType() ||
3979 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
3980 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003981}
3982
Alexey Bataeve3727102018-04-18 15:57:46 +00003983bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl,
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003984 Expr *NewLCRefExpr,
3985 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003986 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003987 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003988 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003989 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003990 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003991 LCDecl = getCanonicalDecl(NewLCDecl);
3992 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003993 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3994 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003995 if ((Ctor->isCopyOrMoveConstructor() ||
3996 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3997 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003998 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003999 LB = NewLB;
4000 return false;
4001}
4002
Alexey Bataeve3727102018-04-18 15:57:46 +00004003bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00004004 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004005 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004006 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
4007 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004008 if (!NewUB)
4009 return true;
4010 UB = NewUB;
4011 TestIsLessOp = LessOp;
4012 TestIsStrictOp = StrictOp;
4013 ConditionSrcRange = SR;
4014 ConditionLoc = SL;
4015 return false;
4016}
4017
Alexey Bataeve3727102018-04-18 15:57:46 +00004018bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004019 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004020 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004021 if (!NewStep)
4022 return true;
4023 if (!NewStep->isValueDependent()) {
4024 // Check that the step is integer expression.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004025 SourceLocation StepLoc = NewStep->getBeginLoc();
Alexey Bataev5372fb82017-08-31 23:06:52 +00004026 ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion(
4027 StepLoc, getExprAsWritten(NewStep));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004028 if (Val.isInvalid())
4029 return true;
4030 NewStep = Val.get();
4031
4032 // OpenMP [2.6, Canonical Loop Form, Restrictions]
4033 // If test-expr is of form var relational-op b and relational-op is < or
4034 // <= then incr-expr must cause var to increase on each iteration of the
4035 // loop. If test-expr is of form var relational-op b and relational-op is
4036 // > or >= then incr-expr must cause var to decrease on each iteration of
4037 // the loop.
4038 // If test-expr is of form b relational-op var and relational-op is < or
4039 // <= then incr-expr must cause var to decrease on each iteration of the
4040 // loop. If test-expr is of form b relational-op var and relational-op is
4041 // > or >= then incr-expr must cause var to increase on each iteration of
4042 // the loop.
4043 llvm::APSInt Result;
4044 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
4045 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
4046 bool IsConstNeg =
4047 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004048 bool IsConstPos =
4049 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004050 bool IsConstZero = IsConstant && !Result.getBoolValue();
4051 if (UB && (IsConstZero ||
4052 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00004053 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004054 SemaRef.Diag(NewStep->getExprLoc(),
4055 diag::err_omp_loop_incr_not_compatible)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004056 << LCDecl << TestIsLessOp << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004057 SemaRef.Diag(ConditionLoc,
4058 diag::note_omp_loop_cond_requres_compatible_incr)
4059 << TestIsLessOp << ConditionSrcRange;
4060 return true;
4061 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004062 if (TestIsLessOp == Subtract) {
David Majnemer9d168222016-08-05 17:44:54 +00004063 NewStep =
4064 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep)
4065 .get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004066 Subtract = !Subtract;
4067 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004068 }
4069
4070 Step = NewStep;
4071 SubtractStep = Subtract;
4072 return false;
4073}
4074
Alexey Bataeve3727102018-04-18 15:57:46 +00004075bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004076 // Check init-expr for canonical loop form and save loop counter
4077 // variable - #Var and its initialization value - #LB.
4078 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
4079 // var = lb
4080 // integer-type var = lb
4081 // random-access-iterator-type var = lb
4082 // pointer-type var = lb
4083 //
4084 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00004085 if (EmitDiags) {
4086 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
4087 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004088 return true;
4089 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004090 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4091 if (!ExprTemp->cleanupsHaveSideEffects())
4092 S = ExprTemp->getSubExpr();
4093
Alexander Musmana5f070a2014-10-01 06:03:56 +00004094 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004095 if (Expr *E = dyn_cast<Expr>(S))
4096 S = E->IgnoreParens();
David Majnemer9d168222016-08-05 17:44:54 +00004097 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004098 if (BO->getOpcode() == BO_Assign) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004099 Expr *LHS = BO->getLHS()->IgnoreParens();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004100 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
4101 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4102 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataeve3727102018-04-18 15:57:46 +00004103 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4104 return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004105 }
4106 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4107 if (ME->isArrow() &&
4108 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
Alexey Bataeve3727102018-04-18 15:57:46 +00004109 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004110 }
4111 }
David Majnemer9d168222016-08-05 17:44:54 +00004112 } else if (auto *DS = dyn_cast<DeclStmt>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004113 if (DS->isSingleDecl()) {
David Majnemer9d168222016-08-05 17:44:54 +00004114 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00004115 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004116 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00004117 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004118 SemaRef.Diag(S->getBeginLoc(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004119 diag::ext_omp_loop_not_canonical_init)
4120 << S->getSourceRange();
Alexey Bataevf138fda2018-08-13 19:04:24 +00004121 return setLCDeclAndLB(
4122 Var,
4123 buildDeclRefExpr(SemaRef, Var,
4124 Var->getType().getNonReferenceType(),
4125 DS->getBeginLoc()),
4126 Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004127 }
4128 }
4129 }
David Majnemer9d168222016-08-05 17:44:54 +00004130 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004131 if (CE->getOperator() == OO_Equal) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004132 Expr *LHS = CE->getArg(0);
David Majnemer9d168222016-08-05 17:44:54 +00004133 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004134 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4135 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataeve3727102018-04-18 15:57:46 +00004136 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4137 return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004138 }
4139 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4140 if (ME->isArrow() &&
4141 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
Alexey Bataeve3727102018-04-18 15:57:46 +00004142 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004143 }
4144 }
4145 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004146
Alexey Bataeve3727102018-04-18 15:57:46 +00004147 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004148 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00004149 if (EmitDiags) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004150 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init)
Alexey Bataev9c821032015-04-30 04:23:23 +00004151 << S->getSourceRange();
4152 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004153 return true;
4154}
4155
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004156/// Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004157/// variable (which may be the loop variable) if possible.
Alexey Bataeve3727102018-04-18 15:57:46 +00004158static const ValueDecl *getInitLCDecl(const Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004159 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00004160 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004161 E = getExprAsWritten(E);
Alexey Bataeve3727102018-04-18 15:57:46 +00004162 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004163 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00004164 if ((Ctor->isCopyOrMoveConstructor() ||
4165 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4166 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004167 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00004168 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
4169 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004170 return getCanonicalDecl(VD);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004171 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004172 if (const auto *ME = dyn_cast_or_null<MemberExpr>(E))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004173 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4174 return getCanonicalDecl(ME->getMemberDecl());
4175 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004176}
4177
Alexey Bataeve3727102018-04-18 15:57:46 +00004178bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004179 // Check test-expr for canonical form, save upper-bound UB, flags for
4180 // less/greater and for strict/non-strict comparison.
4181 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4182 // var relational-op b
4183 // b relational-op var
4184 //
4185 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004186 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004187 return true;
4188 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004189 S = getExprAsWritten(S);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004190 SourceLocation CondLoc = S->getBeginLoc();
David Majnemer9d168222016-08-05 17:44:54 +00004191 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004192 if (BO->isRelationalOp()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004193 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4194 return setUB(BO->getRHS(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004195 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
4196 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4197 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +00004198 if (getInitLCDecl(BO->getRHS()) == LCDecl)
4199 return setUB(BO->getLHS(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004200 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
4201 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4202 BO->getSourceRange(), BO->getOperatorLoc());
4203 }
David Majnemer9d168222016-08-05 17:44:54 +00004204 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004205 if (CE->getNumArgs() == 2) {
4206 auto Op = CE->getOperator();
4207 switch (Op) {
4208 case OO_Greater:
4209 case OO_GreaterEqual:
4210 case OO_Less:
4211 case OO_LessEqual:
Alexey Bataeve3727102018-04-18 15:57:46 +00004212 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4213 return setUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004214 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4215 CE->getOperatorLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +00004216 if (getInitLCDecl(CE->getArg(1)) == LCDecl)
4217 return setUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004218 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4219 CE->getOperatorLoc());
4220 break;
4221 default:
4222 break;
4223 }
4224 }
4225 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004226 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004227 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004228 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004229 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004230 return true;
4231}
4232
Alexey Bataeve3727102018-04-18 15:57:46 +00004233bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004234 // RHS of canonical loop form increment can be:
4235 // var + incr
4236 // incr + var
4237 // var - incr
4238 //
4239 RHS = RHS->IgnoreParenImpCasts();
David Majnemer9d168222016-08-05 17:44:54 +00004240 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004241 if (BO->isAdditiveOp()) {
4242 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataeve3727102018-04-18 15:57:46 +00004243 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4244 return setStep(BO->getRHS(), !IsAdd);
4245 if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl)
4246 return setStep(BO->getLHS(), /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004247 }
David Majnemer9d168222016-08-05 17:44:54 +00004248 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004249 bool IsAdd = CE->getOperator() == OO_Plus;
4250 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004251 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4252 return setStep(CE->getArg(1), !IsAdd);
4253 if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl)
4254 return setStep(CE->getArg(0), /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004255 }
4256 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004257 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004258 return false;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004259 SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004260 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004261 return true;
4262}
4263
Alexey Bataeve3727102018-04-18 15:57:46 +00004264bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004265 // Check incr-expr for canonical loop form and return true if it
4266 // does not conform.
4267 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4268 // ++var
4269 // var++
4270 // --var
4271 // var--
4272 // var += incr
4273 // var -= incr
4274 // var = var + incr
4275 // var = incr + var
4276 // var = var - incr
4277 //
4278 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004279 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004280 return true;
4281 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004282 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4283 if (!ExprTemp->cleanupsHaveSideEffects())
4284 S = ExprTemp->getSubExpr();
4285
Alexander Musmana5f070a2014-10-01 06:03:56 +00004286 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004287 S = S->IgnoreParens();
David Majnemer9d168222016-08-05 17:44:54 +00004288 if (auto *UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004289 if (UO->isIncrementDecrementOp() &&
Alexey Bataeve3727102018-04-18 15:57:46 +00004290 getInitLCDecl(UO->getSubExpr()) == LCDecl)
4291 return setStep(SemaRef
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004292 .ActOnIntegerConstant(UO->getBeginLoc(),
David Majnemer9d168222016-08-05 17:44:54 +00004293 (UO->isDecrementOp() ? -1 : 1))
4294 .get(),
Alexey Bataeve3727102018-04-18 15:57:46 +00004295 /*Subtract=*/false);
David Majnemer9d168222016-08-05 17:44:54 +00004296 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004297 switch (BO->getOpcode()) {
4298 case BO_AddAssign:
4299 case BO_SubAssign:
Alexey Bataeve3727102018-04-18 15:57:46 +00004300 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4301 return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004302 break;
4303 case BO_Assign:
Alexey Bataeve3727102018-04-18 15:57:46 +00004304 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4305 return checkAndSetIncRHS(BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004306 break;
4307 default:
4308 break;
4309 }
David Majnemer9d168222016-08-05 17:44:54 +00004310 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004311 switch (CE->getOperator()) {
4312 case OO_PlusPlus:
4313 case OO_MinusMinus:
Alexey Bataeve3727102018-04-18 15:57:46 +00004314 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4315 return setStep(SemaRef
David Majnemer9d168222016-08-05 17:44:54 +00004316 .ActOnIntegerConstant(
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004317 CE->getBeginLoc(),
David Majnemer9d168222016-08-05 17:44:54 +00004318 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
4319 .get(),
Alexey Bataeve3727102018-04-18 15:57:46 +00004320 /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004321 break;
4322 case OO_PlusEqual:
4323 case OO_MinusEqual:
Alexey Bataeve3727102018-04-18 15:57:46 +00004324 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4325 return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004326 break;
4327 case OO_Equal:
Alexey Bataeve3727102018-04-18 15:57:46 +00004328 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4329 return checkAndSetIncRHS(CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004330 break;
4331 default:
4332 break;
4333 }
4334 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004335 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004336 return false;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004337 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004338 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004339 return true;
4340}
Alexander Musmana5f070a2014-10-01 06:03:56 +00004341
Alexey Bataev5a3af132016-03-29 08:58:54 +00004342static ExprResult
4343tryBuildCapture(Sema &SemaRef, Expr *Capture,
Alexey Bataeve3727102018-04-18 15:57:46 +00004344 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00004345 if (SemaRef.CurContext->isDependentContext())
4346 return ExprResult(Capture);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004347 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
4348 return SemaRef.PerformImplicitConversion(
4349 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
4350 /*AllowExplicit=*/true);
4351 auto I = Captures.find(Capture);
4352 if (I != Captures.end())
4353 return buildCapture(SemaRef, Capture, I->second);
4354 DeclRefExpr *Ref = nullptr;
4355 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
4356 Captures[Capture] = Ref;
4357 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004358}
4359
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004360/// Build the expression to calculate the number of iterations.
Alexey Bataeve3727102018-04-18 15:57:46 +00004361Expr *OpenMPIterationSpaceChecker::buildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004362 Scope *S, const bool LimitedType,
Alexey Bataeve3727102018-04-18 15:57:46 +00004363 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004364 ExprResult Diff;
Alexey Bataeve3727102018-04-18 15:57:46 +00004365 QualType VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004366 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004367 SemaRef.getLangOpts().CPlusPlus) {
4368 // Upper - Lower
Alexey Bataeve3727102018-04-18 15:57:46 +00004369 Expr *UBExpr = TestIsLessOp ? UB : LB;
4370 Expr *LBExpr = TestIsLessOp ? LB : UB;
Alexey Bataev5a3af132016-03-29 08:58:54 +00004371 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
4372 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004373 if (!Upper || !Lower)
4374 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004375
4376 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4377
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004378 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004379 // BuildBinOp already emitted error, this one is to point user to upper
4380 // and lower bound, and to tell what is passed to 'operator-'.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004381 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004382 << Upper->getSourceRange() << Lower->getSourceRange();
4383 return nullptr;
4384 }
4385 }
4386
4387 if (!Diff.isUsable())
4388 return nullptr;
4389
4390 // Upper - Lower [- 1]
4391 if (TestIsStrictOp)
4392 Diff = SemaRef.BuildBinOp(
4393 S, DefaultLoc, BO_Sub, Diff.get(),
4394 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4395 if (!Diff.isUsable())
4396 return nullptr;
4397
4398 // Upper - Lower [- 1] + Step
Alexey Bataeve3727102018-04-18 15:57:46 +00004399 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004400 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004401 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004402 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004403 if (!Diff.isUsable())
4404 return nullptr;
4405
4406 // Parentheses (for dumping/debugging purposes only).
4407 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4408 if (!Diff.isUsable())
4409 return nullptr;
4410
4411 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004412 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004413 if (!Diff.isUsable())
4414 return nullptr;
4415
Alexander Musman174b3ca2014-10-06 11:16:29 +00004416 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004417 QualType Type = Diff.get()->getType();
Alexey Bataeve3727102018-04-18 15:57:46 +00004418 ASTContext &C = SemaRef.Context;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004419 bool UseVarType = VarType->hasIntegerRepresentation() &&
4420 C.getTypeSize(Type) > C.getTypeSize(VarType);
4421 if (!Type->isIntegerType() || UseVarType) {
4422 unsigned NewSize =
4423 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4424 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4425 : Type->hasSignedIntegerRepresentation();
4426 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00004427 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4428 Diff = SemaRef.PerformImplicitConversion(
4429 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4430 if (!Diff.isUsable())
4431 return nullptr;
4432 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004433 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004434 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00004435 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4436 if (NewSize != C.getTypeSize(Type)) {
4437 if (NewSize < C.getTypeSize(Type)) {
4438 assert(NewSize == 64 && "incorrect loop var size");
4439 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4440 << InitSrcRange << ConditionSrcRange;
4441 }
4442 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004443 NewSize, Type->hasSignedIntegerRepresentation() ||
4444 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00004445 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4446 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4447 Sema::AA_Converting, true);
4448 if (!Diff.isUsable())
4449 return nullptr;
4450 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004451 }
4452 }
4453
Alexander Musmana5f070a2014-10-01 06:03:56 +00004454 return Diff.get();
4455}
4456
Alexey Bataeve3727102018-04-18 15:57:46 +00004457Expr *OpenMPIterationSpaceChecker::buildPreCond(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004458 Scope *S, Expr *Cond,
Alexey Bataeve3727102018-04-18 15:57:46 +00004459 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004460 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4461 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4462 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004463
Alexey Bataeve3727102018-04-18 15:57:46 +00004464 ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures);
4465 ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004466 if (!NewLB.isUsable() || !NewUB.isUsable())
4467 return nullptr;
4468
Alexey Bataeve3727102018-04-18 15:57:46 +00004469 ExprResult CondExpr =
4470 SemaRef.BuildBinOp(S, DefaultLoc,
4471 TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
4472 : (TestIsStrictOp ? BO_GT : BO_GE),
4473 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004474 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004475 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4476 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00004477 CondExpr = SemaRef.PerformImplicitConversion(
4478 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4479 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004480 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00004481 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4482 // Otherwise use original loop conditon and evaluate it in runtime.
4483 return CondExpr.isUsable() ? CondExpr.get() : Cond;
4484}
4485
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004486/// Build reference expression to the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004487DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar(
Alexey Bataevf138fda2018-08-13 19:04:24 +00004488 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
4489 DSAStackTy &DSA) const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004490 auto *VD = dyn_cast<VarDecl>(LCDecl);
4491 if (!VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004492 VD = SemaRef.isOpenMPCapturedDecl(LCDecl);
4493 DeclRefExpr *Ref = buildDeclRefExpr(
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004494 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
Alexey Bataeve3727102018-04-18 15:57:46 +00004495 const DSAStackTy::DSAVarData Data =
4496 DSA.getTopDSA(LCDecl, /*FromParent=*/false);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004497 // If the loop control decl is explicitly marked as private, do not mark it
4498 // as captured again.
4499 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4500 Captures.insert(std::make_pair(LCRef, Ref));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004501 return Ref;
4502 }
4503 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
Alexey Bataeva8899172015-08-06 12:30:57 +00004504 DefaultLoc);
4505}
4506
Alexey Bataeve3727102018-04-18 15:57:46 +00004507Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004508 if (LCDecl && !LCDecl->isInvalidDecl()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004509 QualType Type = LCDecl->getType().getNonReferenceType();
4510 VarDecl *PrivateVar = buildVarDecl(
Alexey Bataev63cc8e92018-03-20 14:45:59 +00004511 SemaRef, DefaultLoc, Type, LCDecl->getName(),
4512 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr,
4513 isa<VarDecl>(LCDecl)
4514 ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc)
4515 : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00004516 if (PrivateVar->isInvalidDecl())
4517 return nullptr;
4518 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4519 }
4520 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004521}
4522
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004523/// Build initialization of the counter to be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004524Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004525
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004526/// Build step of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004527Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004528
Alexey Bataevf138fda2018-08-13 19:04:24 +00004529Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData(
4530 Scope *S, Expr *Counter,
4531 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc,
4532 Expr *Inc, OverloadedOperatorKind OOK) {
4533 Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get();
4534 if (!Cnt)
4535 return nullptr;
4536 if (Inc) {
4537 assert((OOK == OO_Plus || OOK == OO_Minus) &&
4538 "Expected only + or - operations for depend clauses.");
4539 BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub;
4540 Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get();
4541 if (!Cnt)
4542 return nullptr;
4543 }
4544 ExprResult Diff;
4545 QualType VarType = LCDecl->getType().getNonReferenceType();
4546 if (VarType->isIntegerType() || VarType->isPointerType() ||
4547 SemaRef.getLangOpts().CPlusPlus) {
4548 // Upper - Lower
4549 Expr *Upper =
4550 TestIsLessOp ? Cnt : tryBuildCapture(SemaRef, UB, Captures).get();
4551 Expr *Lower =
4552 TestIsLessOp ? tryBuildCapture(SemaRef, LB, Captures).get() : Cnt;
4553 if (!Upper || !Lower)
4554 return nullptr;
4555
4556 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4557
4558 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
4559 // BuildBinOp already emitted error, this one is to point user to upper
4560 // and lower bound, and to tell what is passed to 'operator-'.
4561 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
4562 << Upper->getSourceRange() << Lower->getSourceRange();
4563 return nullptr;
4564 }
4565 }
4566
4567 if (!Diff.isUsable())
4568 return nullptr;
4569
4570 // Parentheses (for dumping/debugging purposes only).
4571 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4572 if (!Diff.isUsable())
4573 return nullptr;
4574
4575 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
4576 if (!NewStep.isUsable())
4577 return nullptr;
4578 // (Upper - Lower) / Step
4579 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
4580 if (!Diff.isUsable())
4581 return nullptr;
4582
4583 return Diff.get();
4584}
4585
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004586/// Iteration space of a single for loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004587struct LoopIterationSpace final {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004588 /// Condition of the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004589 Expr *PreCond = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004590 /// This expression calculates the number of iterations in the loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004591 /// It is always possible to calculate it before starting the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004592 Expr *NumIterations = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004593 /// The loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004594 Expr *CounterVar = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004595 /// Private loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004596 Expr *PrivateCounterVar = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004597 /// This is initializer for the initial value of #CounterVar.
Alexey Bataev8b427062016-05-25 12:36:08 +00004598 Expr *CounterInit = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004599 /// This is step for the #CounterVar used to generate its update:
Alexander Musmana5f070a2014-10-01 06:03:56 +00004600 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
Alexey Bataev8b427062016-05-25 12:36:08 +00004601 Expr *CounterStep = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004602 /// Should step be subtracted?
Alexey Bataev8b427062016-05-25 12:36:08 +00004603 bool Subtract = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004604 /// Source range of the loop init.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004605 SourceRange InitSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004606 /// Source range of the loop condition.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004607 SourceRange CondSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004608 /// Source range of the loop increment.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004609 SourceRange IncSrcRange;
4610};
4611
Alexey Bataev23b69422014-06-18 07:08:49 +00004612} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004613
Alexey Bataev9c821032015-04-30 04:23:23 +00004614void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4615 assert(getLangOpts().OpenMP && "OpenMP is not active.");
4616 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004617 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4618 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00004619 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4620 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataeve3727102018-04-18 15:57:46 +00004621 if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) {
4622 if (ValueDecl *D = ISC.getLoopDecl()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004623 auto *VD = dyn_cast<VarDecl>(D);
4624 if (!VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004625 if (VarDecl *Private = isOpenMPCapturedDecl(D)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004626 VD = Private;
Alexey Bataeve3727102018-04-18 15:57:46 +00004627 } else {
4628 DeclRefExpr *Ref = buildCapture(*this, D, ISC.getLoopDeclRefExpr(),
4629 /*WithInit=*/false);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004630 VD = cast<VarDecl>(Ref->getDecl());
4631 }
4632 }
4633 DSAStack->addLoopControlVariable(D, VD);
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00004634 const Decl *LD = DSAStack->getPossiblyLoopCunter();
4635 if (LD != D->getCanonicalDecl()) {
4636 DSAStack->resetPossibleLoopCounter();
4637 if (auto *Var = dyn_cast_or_null<VarDecl>(LD))
4638 MarkDeclarationsReferencedInExpr(
4639 buildDeclRefExpr(*this, const_cast<VarDecl *>(Var),
4640 Var->getType().getNonLValueExprType(Context),
4641 ForLoc, /*RefersToCapture=*/true));
4642 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004643 }
4644 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004645 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00004646 }
4647}
4648
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004649/// Called on a for stmt to check and extract its iteration space
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004650/// for further processing (such as collapsing).
Alexey Bataeve3727102018-04-18 15:57:46 +00004651static bool checkOpenMPIterationSpace(
Alexey Bataev4acb8592014-07-07 13:01:15 +00004652 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4653 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataevf138fda2018-08-13 19:04:24 +00004654 unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr,
4655 Expr *OrderedLoopCountExpr,
Alexey Bataeve3727102018-04-18 15:57:46 +00004656 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004657 LoopIterationSpace &ResultIterSpace,
Alexey Bataeve3727102018-04-18 15:57:46 +00004658 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004659 // OpenMP [2.6, Canonical Loop Form]
4660 // for (init-expr; test-expr; incr-expr) structured-block
David Majnemer9d168222016-08-05 17:44:54 +00004661 auto *For = dyn_cast_or_null<ForStmt>(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004662 if (!For) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004663 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004664 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
Alexey Bataevf138fda2018-08-13 19:04:24 +00004665 << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount
Alexey Bataev10e775f2015-07-30 11:36:16 +00004666 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
Alexey Bataevf138fda2018-08-13 19:04:24 +00004667 if (TotalNestedLoopCount > 1) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00004668 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4669 SemaRef.Diag(DSA.getConstructLoc(),
4670 diag::note_omp_collapse_ordered_expr)
4671 << 2 << CollapseLoopCountExpr->getSourceRange()
4672 << OrderedLoopCountExpr->getSourceRange();
4673 else if (CollapseLoopCountExpr)
4674 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4675 diag::note_omp_collapse_ordered_expr)
4676 << 0 << CollapseLoopCountExpr->getSourceRange();
4677 else
4678 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4679 diag::note_omp_collapse_ordered_expr)
4680 << 1 << OrderedLoopCountExpr->getSourceRange();
4681 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004682 return true;
4683 }
4684 assert(For->getBody());
4685
4686 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4687
4688 // Check init.
Alexey Bataeve3727102018-04-18 15:57:46 +00004689 Stmt *Init = For->getInit();
4690 if (ISC.checkAndSetInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004691 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004692
4693 bool HasErrors = false;
4694
4695 // Check loop variable's type.
Alexey Bataeve3727102018-04-18 15:57:46 +00004696 if (ValueDecl *LCDecl = ISC.getLoopDecl()) {
4697 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004698
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004699 // OpenMP [2.6, Canonical Loop Form]
4700 // Var is one of the following:
4701 // A variable of signed or unsigned integer type.
4702 // For C++, a variable of a random access iterator type.
4703 // For C, a variable of a pointer type.
Alexey Bataeve3727102018-04-18 15:57:46 +00004704 QualType VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004705 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4706 !VarType->isPointerType() &&
4707 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004708 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004709 << SemaRef.getLangOpts().CPlusPlus;
4710 HasErrors = true;
4711 }
4712
4713 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4714 // a Construct
4715 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4716 // parallel for construct is (are) private.
4717 // The loop iteration variable in the associated for-loop of a simd
4718 // construct with just one associated for-loop is linear with a
4719 // constant-linear-step that is the increment of the associated for-loop.
4720 // Exclude loop var from the list of variables with implicitly defined data
4721 // sharing attributes.
4722 VarsWithImplicitDSA.erase(LCDecl);
4723
4724 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4725 // in a Construct, C/C++].
4726 // The loop iteration variable in the associated for-loop of a simd
4727 // construct with just one associated for-loop may be listed in a linear
4728 // clause with a constant-linear-step that is the increment of the
4729 // associated for-loop.
4730 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4731 // parallel for construct may be listed in a private or lastprivate clause.
4732 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4733 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4734 // declared in the loop and it is predetermined as a private.
Alexey Bataeve3727102018-04-18 15:57:46 +00004735 OpenMPClauseKind PredeterminedCKind =
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004736 isOpenMPSimdDirective(DKind)
4737 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4738 : OMPC_private;
4739 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4740 DVar.CKind != PredeterminedCKind) ||
4741 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4742 isOpenMPDistributeDirective(DKind)) &&
4743 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4744 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4745 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004746 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004747 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4748 << getOpenMPClauseName(PredeterminedCKind);
4749 if (DVar.RefExpr == nullptr)
4750 DVar.CKind = PredeterminedCKind;
Alexey Bataeve3727102018-04-18 15:57:46 +00004751 reportOriginalDsa(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004752 HasErrors = true;
4753 } else if (LoopDeclRefExpr != nullptr) {
4754 // Make the loop iteration variable private (for worksharing constructs),
4755 // linear (for simd directives with the only one associated loop) or
4756 // lastprivate (for simd directives with several collapsed or ordered
4757 // loops).
4758 if (DVar.CKind == OMPC_unknown)
Alexey Bataev7ace49d2016-05-17 08:55:33 +00004759 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate,
4760 [](OpenMPDirectiveKind) -> bool { return true; },
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004761 /*FromParent=*/false);
4762 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4763 }
4764
4765 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4766
4767 // Check test-expr.
Alexey Bataeve3727102018-04-18 15:57:46 +00004768 HasErrors |= ISC.checkAndSetCond(For->getCond());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004769
4770 // Check incr-expr.
Alexey Bataeve3727102018-04-18 15:57:46 +00004771 HasErrors |= ISC.checkAndSetInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004772 }
4773
Alexey Bataeve3727102018-04-18 15:57:46 +00004774 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004775 return HasErrors;
4776
Alexander Musmana5f070a2014-10-01 06:03:56 +00004777 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004778 ResultIterSpace.PreCond =
Alexey Bataeve3727102018-04-18 15:57:46 +00004779 ISC.buildPreCond(DSA.getCurScope(), For->getCond(), Captures);
4780 ResultIterSpace.NumIterations = ISC.buildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004781 DSA.getCurScope(),
4782 (isOpenMPWorksharingDirective(DKind) ||
4783 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4784 Captures);
Alexey Bataeve3727102018-04-18 15:57:46 +00004785 ResultIterSpace.CounterVar = ISC.buildCounterVar(Captures, DSA);
4786 ResultIterSpace.PrivateCounterVar = ISC.buildPrivateCounterVar();
4787 ResultIterSpace.CounterInit = ISC.buildCounterInit();
4788 ResultIterSpace.CounterStep = ISC.buildCounterStep();
4789 ResultIterSpace.InitSrcRange = ISC.getInitSrcRange();
4790 ResultIterSpace.CondSrcRange = ISC.getConditionSrcRange();
4791 ResultIterSpace.IncSrcRange = ISC.getIncrementSrcRange();
4792 ResultIterSpace.Subtract = ISC.shouldSubtractStep();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004793
Alexey Bataev62dbb972015-04-22 11:59:37 +00004794 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4795 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004796 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004797 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004798 ResultIterSpace.CounterInit == nullptr ||
4799 ResultIterSpace.CounterStep == nullptr);
Alexey Bataevf138fda2018-08-13 19:04:24 +00004800 if (!HasErrors && DSA.isOrderedRegion()) {
4801 if (DSA.getOrderedRegionParam().second->getNumForLoops()) {
4802 if (CurrentNestedLoopCount <
4803 DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) {
4804 DSA.getOrderedRegionParam().second->setLoopNumIterations(
4805 CurrentNestedLoopCount, ResultIterSpace.NumIterations);
4806 DSA.getOrderedRegionParam().second->setLoopCounter(
4807 CurrentNestedLoopCount, ResultIterSpace.CounterVar);
4808 }
4809 }
4810 for (auto &Pair : DSA.getDoacrossDependClauses()) {
4811 if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) {
4812 // Erroneous case - clause has some problems.
4813 continue;
4814 }
4815 if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink &&
4816 Pair.second.size() <= CurrentNestedLoopCount) {
4817 // Erroneous case - clause has some problems.
4818 Pair.first->setLoopData(CurrentNestedLoopCount, nullptr);
4819 continue;
4820 }
4821 Expr *CntValue;
4822 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
4823 CntValue = ISC.buildOrderedLoopData(
4824 DSA.getCurScope(), ResultIterSpace.CounterVar, Captures,
4825 Pair.first->getDependencyLoc());
4826 else
4827 CntValue = ISC.buildOrderedLoopData(
4828 DSA.getCurScope(), ResultIterSpace.CounterVar, Captures,
4829 Pair.first->getDependencyLoc(),
4830 Pair.second[CurrentNestedLoopCount].first,
4831 Pair.second[CurrentNestedLoopCount].second);
4832 Pair.first->setLoopData(CurrentNestedLoopCount, CntValue);
4833 }
4834 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004835
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004836 return HasErrors;
4837}
4838
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004839/// Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004840static ExprResult
Alexey Bataeve3727102018-04-18 15:57:46 +00004841buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004842 ExprResult Start,
Alexey Bataeve3727102018-04-18 15:57:46 +00004843 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004844 // Build 'VarRef = Start.
Alexey Bataeve3727102018-04-18 15:57:46 +00004845 ExprResult NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004846 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004847 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004848 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00004849 VarRef.get()->getType())) {
4850 NewStart = SemaRef.PerformImplicitConversion(
4851 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4852 /*AllowExplicit=*/true);
4853 if (!NewStart.isUsable())
4854 return ExprError();
4855 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004856
Alexey Bataeve3727102018-04-18 15:57:46 +00004857 ExprResult Init =
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004858 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4859 return Init;
4860}
4861
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004862/// Build 'VarRef = Start + Iter * Step'.
Alexey Bataeve3727102018-04-18 15:57:46 +00004863static ExprResult buildCounterUpdate(
4864 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4865 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract,
4866 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004867 // Add parentheses (for debugging purposes only).
4868 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4869 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4870 !Step.isUsable())
4871 return ExprError();
4872
Alexey Bataev5a3af132016-03-29 08:58:54 +00004873 ExprResult NewStep = Step;
4874 if (Captures)
4875 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004876 if (NewStep.isInvalid())
4877 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004878 ExprResult Update =
4879 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004880 if (!Update.isUsable())
4881 return ExprError();
4882
Alexey Bataevc0214e02016-02-16 12:13:49 +00004883 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4884 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004885 ExprResult NewStart = Start;
4886 if (Captures)
4887 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004888 if (NewStart.isInvalid())
4889 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004890
Alexey Bataevc0214e02016-02-16 12:13:49 +00004891 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4892 ExprResult SavedUpdate = Update;
4893 ExprResult UpdateVal;
4894 if (VarRef.get()->getType()->isOverloadableType() ||
4895 NewStart.get()->getType()->isOverloadableType() ||
4896 Update.get()->getType()->isOverloadableType()) {
4897 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4898 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4899 Update =
4900 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4901 if (Update.isUsable()) {
4902 UpdateVal =
4903 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4904 VarRef.get(), SavedUpdate.get());
4905 if (UpdateVal.isUsable()) {
4906 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4907 UpdateVal.get());
4908 }
4909 }
4910 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4911 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004912
Alexey Bataevc0214e02016-02-16 12:13:49 +00004913 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4914 if (!Update.isUsable() || !UpdateVal.isUsable()) {
4915 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4916 NewStart.get(), SavedUpdate.get());
4917 if (!Update.isUsable())
4918 return ExprError();
4919
Alexey Bataev11481f52016-02-17 10:29:05 +00004920 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4921 VarRef.get()->getType())) {
4922 Update = SemaRef.PerformImplicitConversion(
4923 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4924 if (!Update.isUsable())
4925 return ExprError();
4926 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00004927
4928 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4929 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004930 return Update;
4931}
4932
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004933/// Convert integer expression \a E to make it have at least \a Bits
Alexander Musmana5f070a2014-10-01 06:03:56 +00004934/// bits.
Alexey Bataeve3727102018-04-18 15:57:46 +00004935static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004936 if (E == nullptr)
4937 return ExprError();
Alexey Bataeve3727102018-04-18 15:57:46 +00004938 ASTContext &C = SemaRef.Context;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004939 QualType OldType = E->getType();
4940 unsigned HasBits = C.getTypeSize(OldType);
4941 if (HasBits >= Bits)
4942 return ExprResult(E);
4943 // OK to convert to signed, because new type has more bits than old.
4944 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4945 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4946 true);
4947}
4948
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004949/// Check if the given expression \a E is a constant integer that fits
Alexander Musmana5f070a2014-10-01 06:03:56 +00004950/// into \a Bits bits.
Alexey Bataeve3727102018-04-18 15:57:46 +00004951static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004952 if (E == nullptr)
4953 return false;
4954 llvm::APSInt Result;
4955 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4956 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4957 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004958}
4959
Alexey Bataev5a3af132016-03-29 08:58:54 +00004960/// Build preinits statement for the given declarations.
4961static Stmt *buildPreInits(ASTContext &Context,
Alexey Bataevc5514062017-10-25 15:44:52 +00004962 MutableArrayRef<Decl *> PreInits) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004963 if (!PreInits.empty()) {
4964 return new (Context) DeclStmt(
4965 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
4966 SourceLocation(), SourceLocation());
4967 }
4968 return nullptr;
4969}
4970
4971/// Build preinits statement for the given declarations.
Alexey Bataevc5514062017-10-25 15:44:52 +00004972static Stmt *
4973buildPreInits(ASTContext &Context,
Alexey Bataeve3727102018-04-18 15:57:46 +00004974 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004975 if (!Captures.empty()) {
4976 SmallVector<Decl *, 16> PreInits;
Alexey Bataeve3727102018-04-18 15:57:46 +00004977 for (const auto &Pair : Captures)
Alexey Bataev5a3af132016-03-29 08:58:54 +00004978 PreInits.push_back(Pair.second->getDecl());
4979 return buildPreInits(Context, PreInits);
4980 }
4981 return nullptr;
4982}
4983
4984/// Build postupdate expression for the given list of postupdates expressions.
4985static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
4986 Expr *PostUpdate = nullptr;
4987 if (!PostUpdates.empty()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004988 for (Expr *E : PostUpdates) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004989 Expr *ConvE = S.BuildCStyleCastExpr(
4990 E->getExprLoc(),
4991 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
4992 E->getExprLoc(), E)
4993 .get();
4994 PostUpdate = PostUpdate
4995 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
4996 PostUpdate, ConvE)
4997 .get()
4998 : ConvE;
4999 }
5000 }
5001 return PostUpdate;
5002}
5003
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005004/// Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00005005/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
5006/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005007static unsigned
Alexey Bataeve3727102018-04-18 15:57:46 +00005008checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
Alexey Bataev10e775f2015-07-30 11:36:16 +00005009 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
5010 DSAStackTy &DSA,
Alexey Bataeve3727102018-04-18 15:57:46 +00005011 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00005012 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005013 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005014 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005015 // Found 'collapse' clause - calculate collapse number.
5016 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005017 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005018 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005019 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00005020 unsigned OrderedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005021 if (OrderedLoopCountExpr) {
5022 // Found 'ordered' clause - calculate collapse number.
5023 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005024 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
5025 if (Result.getLimitedValue() < NestedLoopCount) {
5026 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
5027 diag::err_omp_wrong_ordered_loop_count)
5028 << OrderedLoopCountExpr->getSourceRange();
5029 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
5030 diag::note_collapse_loop_count)
5031 << CollapseLoopCountExpr->getSourceRange();
5032 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00005033 OrderedLoopCount = Result.getLimitedValue();
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005034 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005035 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005036 // This is helper routine for loop directives (e.g., 'for', 'simd',
5037 // 'for simd', etc.).
Alexey Bataeve3727102018-04-18 15:57:46 +00005038 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005039 SmallVector<LoopIterationSpace, 4> IterSpaces;
Alexey Bataevf138fda2018-08-13 19:04:24 +00005040 IterSpaces.resize(std::max(OrderedLoopCount, NestedLoopCount));
Alexander Musmana5f070a2014-10-01 06:03:56 +00005041 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005042 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataevf138fda2018-08-13 19:04:24 +00005043 if (checkOpenMPIterationSpace(
5044 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
5045 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
5046 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt],
5047 Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00005048 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005049 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005050 // OpenMP [2.8.1, simd construct, Restrictions]
5051 // All loops associated with the construct must be perfectly nested; that
5052 // is, there must be no intervening code nor any OpenMP directive between
5053 // any two loops.
5054 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005055 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00005056 for (unsigned Cnt = NestedLoopCount; Cnt < OrderedLoopCount; ++Cnt) {
5057 if (checkOpenMPIterationSpace(
5058 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
5059 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
5060 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt],
5061 Captures))
5062 return 0;
5063 if (Cnt > 0 && IterSpaces[Cnt].CounterVar) {
5064 // Handle initialization of captured loop iterator variables.
5065 auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar);
5066 if (isa<OMPCapturedExprDecl>(DRE->getDecl())) {
5067 Captures[DRE] = DRE;
5068 }
5069 }
5070 // Move on to the next nested for loop, or to the loop body.
5071 // OpenMP [2.8.1, simd construct, Restrictions]
5072 // All loops associated with the construct must be perfectly nested; that
5073 // is, there must be no intervening code nor any OpenMP directive between
5074 // any two loops.
5075 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
5076 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005077
Alexander Musmana5f070a2014-10-01 06:03:56 +00005078 Built.clear(/* size */ NestedLoopCount);
5079
5080 if (SemaRef.CurContext->isDependentContext())
5081 return NestedLoopCount;
5082
5083 // An example of what is generated for the following code:
5084 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00005085 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00005086 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00005087 // for (k = 0; k < NK; ++k)
5088 // for (j = J0; j < NJ; j+=2) {
5089 // <loop body>
5090 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005091 //
5092 // We generate the code below.
5093 // Note: the loop body may be outlined in CodeGen.
5094 // Note: some counters may be C++ classes, operator- is used to find number of
5095 // iterations and operator+= to calculate counter value.
5096 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
5097 // or i64 is currently supported).
5098 //
5099 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
5100 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
5101 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
5102 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
5103 // // similar updates for vars in clauses (e.g. 'linear')
5104 // <loop body (using local i and j)>
5105 // }
5106 // i = NI; // assign final values of counters
5107 // j = NJ;
5108 //
5109
5110 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
5111 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00005112 // Precondition tests if there is at least one iteration (all conditions are
5113 // true).
5114 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexey Bataeve3727102018-04-18 15:57:46 +00005115 Expr *N0 = IterSpaces[0].NumIterations;
5116 ExprResult LastIteration32 =
5117 widenIterationCount(/*Bits=*/32,
5118 SemaRef
5119 .PerformImplicitConversion(
5120 N0->IgnoreImpCasts(), N0->getType(),
5121 Sema::AA_Converting, /*AllowExplicit=*/true)
5122 .get(),
5123 SemaRef);
5124 ExprResult LastIteration64 = widenIterationCount(
5125 /*Bits=*/64,
5126 SemaRef
5127 .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(),
5128 Sema::AA_Converting,
5129 /*AllowExplicit=*/true)
5130 .get(),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005131 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005132
5133 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
5134 return NestedLoopCount;
5135
Alexey Bataeve3727102018-04-18 15:57:46 +00005136 ASTContext &C = SemaRef.Context;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005137 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
5138
5139 Scope *CurScope = DSA.getCurScope();
5140 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00005141 if (PreCond.isUsable()) {
Alexey Bataeva7206b92016-12-20 16:51:02 +00005142 PreCond =
5143 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd,
5144 PreCond.get(), IterSpaces[Cnt].PreCond);
Alexey Bataev62dbb972015-04-22 11:59:37 +00005145 }
Alexey Bataeve3727102018-04-18 15:57:46 +00005146 Expr *N = IterSpaces[Cnt].NumIterations;
Alexey Bataeva7206b92016-12-20 16:51:02 +00005147 SourceLocation Loc = N->getExprLoc();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005148 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
5149 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005150 LastIteration32 = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005151 CurScope, Loc, BO_Mul, LastIteration32.get(),
David Majnemer9d168222016-08-05 17:44:54 +00005152 SemaRef
5153 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5154 Sema::AA_Converting,
5155 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005156 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005157 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005158 LastIteration64 = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005159 CurScope, Loc, BO_Mul, LastIteration64.get(),
David Majnemer9d168222016-08-05 17:44:54 +00005160 SemaRef
5161 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5162 Sema::AA_Converting,
5163 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005164 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005165 }
5166
5167 // Choose either the 32-bit or 64-bit version.
5168 ExprResult LastIteration = LastIteration64;
5169 if (LastIteration32.isUsable() &&
5170 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
5171 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
Alexey Bataeve3727102018-04-18 15:57:46 +00005172 fitsInto(
5173 /*Bits=*/32,
Alexander Musmana5f070a2014-10-01 06:03:56 +00005174 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
5175 LastIteration64.get(), SemaRef)))
5176 LastIteration = LastIteration32;
Alexey Bataev7292c292016-04-25 12:22:29 +00005177 QualType VType = LastIteration.get()->getType();
5178 QualType RealVType = VType;
5179 QualType StrideVType = VType;
5180 if (isOpenMPTaskLoopDirective(DKind)) {
5181 VType =
5182 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
5183 StrideVType =
5184 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
5185 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005186
5187 if (!LastIteration.isUsable())
5188 return 0;
5189
5190 // Save the number of iterations.
5191 ExprResult NumIterations = LastIteration;
5192 {
5193 LastIteration = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005194 CurScope, LastIteration.get()->getExprLoc(), BO_Sub,
5195 LastIteration.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00005196 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5197 if (!LastIteration.isUsable())
5198 return 0;
5199 }
5200
5201 // Calculate the last iteration number beforehand instead of doing this on
5202 // each iteration. Do not do this if the number of iterations may be kfold-ed.
5203 llvm::APSInt Result;
5204 bool IsConstant =
5205 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
5206 ExprResult CalcLastIteration;
5207 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005208 ExprResult SaveRef =
5209 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005210 LastIteration = SaveRef;
5211
5212 // Prepare SaveRef + 1.
5213 NumIterations = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005214 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00005215 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5216 if (!NumIterations.isUsable())
5217 return 0;
5218 }
5219
5220 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
5221
David Majnemer9d168222016-08-05 17:44:54 +00005222 // Build variables passed into runtime, necessary for worksharing directives.
Carlo Bertolliffafe102017-04-20 00:39:39 +00005223 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005224 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5225 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005226 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005227 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
5228 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005229 SemaRef.AddInitializerToDecl(LBDecl,
5230 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5231 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005232
5233 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005234 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
5235 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005236 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
Richard Smith3beb7c62017-01-12 02:27:38 +00005237 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005238
5239 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
5240 // This will be used to implement clause 'lastprivate'.
5241 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005242 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
5243 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005244 SemaRef.AddInitializerToDecl(ILDecl,
5245 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5246 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005247
5248 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev7292c292016-04-25 12:22:29 +00005249 VarDecl *STDecl =
5250 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
5251 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005252 SemaRef.AddInitializerToDecl(STDecl,
5253 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
5254 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005255
5256 // Build expression: UB = min(UB, LastIteration)
David Majnemer9d168222016-08-05 17:44:54 +00005257 // It is necessary for CodeGen of directives with static scheduling.
Alexander Musmanc6388682014-12-15 07:07:06 +00005258 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
5259 UB.get(), LastIteration.get());
5260 ExprResult CondOp = SemaRef.ActOnConditionalOp(
Alexey Bataev86ec3fe2018-07-25 14:40:26 +00005261 LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(),
5262 LastIteration.get(), UB.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00005263 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
5264 CondOp.get());
5265 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
Carlo Bertolli9925f152016-06-27 14:55:37 +00005266
5267 // If we have a combined directive that combines 'distribute', 'for' or
5268 // 'simd' we need to be able to access the bounds of the schedule of the
5269 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
5270 // by scheduling 'distribute' have to be passed to the schedule of 'for'.
5271 if (isOpenMPLoopBoundSharingDirective(DKind)) {
Carlo Bertolliffafe102017-04-20 00:39:39 +00005272 // Lower bound variable, initialized with zero.
5273 VarDecl *CombLBDecl =
5274 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb");
5275 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc);
5276 SemaRef.AddInitializerToDecl(
5277 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5278 /*DirectInit*/ false);
5279
5280 // Upper bound variable, initialized with last iteration number.
5281 VarDecl *CombUBDecl =
5282 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub");
5283 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc);
5284 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(),
5285 /*DirectInit*/ false);
5286
5287 ExprResult CombIsUBGreater = SemaRef.BuildBinOp(
5288 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get());
5289 ExprResult CombCondOp =
5290 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(),
5291 LastIteration.get(), CombUB.get());
5292 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(),
5293 CombCondOp.get());
5294 CombEUB = SemaRef.ActOnFinishFullExpr(CombEUB.get());
5295
Alexey Bataeve3727102018-04-18 15:57:46 +00005296 const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005297 // We expect to have at least 2 more parameters than the 'parallel'
5298 // directive does - the lower and upper bounds of the previous schedule.
5299 assert(CD->getNumParams() >= 4 &&
5300 "Unexpected number of parameters in loop combined directive");
5301
5302 // Set the proper type for the bounds given what we learned from the
5303 // enclosed loops.
Alexey Bataeve3727102018-04-18 15:57:46 +00005304 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
5305 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
Carlo Bertolli9925f152016-06-27 14:55:37 +00005306
5307 // Previous lower and upper bounds are obtained from the region
5308 // parameters.
5309 PrevLB =
5310 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
5311 PrevUB =
5312 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
5313 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005314 }
5315
5316 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005317 ExprResult IV;
Carlo Bertolliffafe102017-04-20 00:39:39 +00005318 ExprResult Init, CombInit;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005319 {
Alexey Bataev7292c292016-04-25 12:22:29 +00005320 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
5321 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
David Majnemer9d168222016-08-05 17:44:54 +00005322 Expr *RHS =
5323 (isOpenMPWorksharingDirective(DKind) ||
5324 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
5325 ? LB.get()
5326 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005327 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
5328 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Carlo Bertolliffafe102017-04-20 00:39:39 +00005329
5330 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5331 Expr *CombRHS =
5332 (isOpenMPWorksharingDirective(DKind) ||
5333 isOpenMPTaskLoopDirective(DKind) ||
5334 isOpenMPDistributeDirective(DKind))
5335 ? CombLB.get()
5336 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
5337 CombInit =
5338 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS);
5339 CombInit = SemaRef.ActOnFinishFullExpr(CombInit.get());
5340 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005341 }
5342
Alexander Musmanc6388682014-12-15 07:07:06 +00005343 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005344 SourceLocation CondLoc = AStmt->getBeginLoc();
Alexander Musmanc6388682014-12-15 07:07:06 +00005345 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005346 (isOpenMPWorksharingDirective(DKind) ||
5347 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00005348 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
5349 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
5350 NumIterations.get());
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005351 ExprResult CombDistCond;
5352 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5353 CombDistCond =
Gheorghe-Teodor Bercea9d6341f2018-10-29 19:44:25 +00005354 SemaRef.BuildBinOp(
5355 CurScope, CondLoc, BO_LT, IV.get(), NumIterations.get());
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005356 }
5357
Carlo Bertolliffafe102017-04-20 00:39:39 +00005358 ExprResult CombCond;
5359 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5360 CombCond =
5361 SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), CombUB.get());
5362 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005363 // Loop increment (IV = IV + 1)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005364 SourceLocation IncLoc = AStmt->getBeginLoc();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005365 ExprResult Inc =
5366 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
5367 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
5368 if (!Inc.isUsable())
5369 return 0;
5370 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00005371 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
5372 if (!Inc.isUsable())
5373 return 0;
5374
5375 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
5376 // Used for directives with static scheduling.
Carlo Bertolliffafe102017-04-20 00:39:39 +00005377 // In combined construct, add combined version that use CombLB and CombUB
5378 // base variables for the update
5379 ExprResult NextLB, NextUB, CombNextLB, CombNextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005380 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5381 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005382 // LB + ST
5383 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
5384 if (!NextLB.isUsable())
5385 return 0;
5386 // LB = LB + ST
5387 NextLB =
5388 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
5389 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
5390 if (!NextLB.isUsable())
5391 return 0;
5392 // UB + ST
5393 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
5394 if (!NextUB.isUsable())
5395 return 0;
5396 // UB = UB + ST
5397 NextUB =
5398 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
5399 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
5400 if (!NextUB.isUsable())
5401 return 0;
Carlo Bertolliffafe102017-04-20 00:39:39 +00005402 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5403 CombNextLB =
5404 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get());
5405 if (!NextLB.isUsable())
5406 return 0;
5407 // LB = LB + ST
5408 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(),
5409 CombNextLB.get());
5410 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get());
5411 if (!CombNextLB.isUsable())
5412 return 0;
5413 // UB + ST
5414 CombNextUB =
5415 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get());
5416 if (!CombNextUB.isUsable())
5417 return 0;
5418 // UB = UB + ST
5419 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(),
5420 CombNextUB.get());
5421 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get());
5422 if (!CombNextUB.isUsable())
5423 return 0;
5424 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005425 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005426
Carlo Bertolliffafe102017-04-20 00:39:39 +00005427 // Create increment expression for distribute loop when combined in a same
Carlo Bertolli8429d812017-02-17 21:29:13 +00005428 // directive with for as IV = IV + ST; ensure upper bound expression based
5429 // on PrevUB instead of NumIterations - used to implement 'for' when found
5430 // in combination with 'distribute', like in 'distribute parallel for'
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005431 SourceLocation DistIncLoc = AStmt->getBeginLoc();
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005432 ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond;
Carlo Bertolli8429d812017-02-17 21:29:13 +00005433 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5434 DistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get());
5435 assert(DistCond.isUsable() && "distribute cond expr was not built");
5436
5437 DistInc =
5438 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get());
5439 assert(DistInc.isUsable() && "distribute inc expr was not built");
5440 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(),
5441 DistInc.get());
5442 DistInc = SemaRef.ActOnFinishFullExpr(DistInc.get());
5443 assert(DistInc.isUsable() && "distribute inc expr was not built");
5444
5445 // Build expression: UB = min(UB, prevUB) for #for in composite or combined
5446 // construct
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005447 SourceLocation DistEUBLoc = AStmt->getBeginLoc();
Carlo Bertolli8429d812017-02-17 21:29:13 +00005448 ExprResult IsUBGreater =
5449 SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get());
5450 ExprResult CondOp = SemaRef.ActOnConditionalOp(
5451 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get());
5452 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(),
5453 CondOp.get());
5454 PrevEUB = SemaRef.ActOnFinishFullExpr(PrevEUB.get());
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005455
5456 // Build IV <= PrevUB to be used in parallel for is in combination with
5457 // a distribute directive with schedule(static, 1)
5458 ParForInDistCond =
5459 SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), PrevUB.get());
Carlo Bertolli8429d812017-02-17 21:29:13 +00005460 }
5461
Alexander Musmana5f070a2014-10-01 06:03:56 +00005462 // Build updates and final values of the loop counters.
5463 bool HasErrors = false;
5464 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005465 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005466 Built.Updates.resize(NestedLoopCount);
5467 Built.Finals.resize(NestedLoopCount);
5468 {
5469 ExprResult Div;
5470 // Go from inner nested loop to outer.
5471 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5472 LoopIterationSpace &IS = IterSpaces[Cnt];
5473 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
5474 // Build: Iter = (IV / Div) % IS.NumIters
5475 // where Div is product of previous iterations' IS.NumIters.
5476 ExprResult Iter;
5477 if (Div.isUsable()) {
5478 Iter =
5479 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
5480 } else {
5481 Iter = IV;
5482 assert((Cnt == (int)NestedLoopCount - 1) &&
5483 "unusable div expected on first iteration only");
5484 }
5485
5486 if (Cnt != 0 && Iter.isUsable())
5487 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
5488 IS.NumIterations);
5489 if (!Iter.isUsable()) {
5490 HasErrors = true;
5491 break;
5492 }
5493
Alexey Bataev39f915b82015-05-08 10:41:21 +00005494 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005495 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00005496 DeclRefExpr *CounterVar = buildDeclRefExpr(
5497 SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
5498 /*RefersToCapture=*/true);
5499 ExprResult Init = buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005500 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005501 if (!Init.isUsable()) {
5502 HasErrors = true;
5503 break;
5504 }
Alexey Bataeve3727102018-04-18 15:57:46 +00005505 ExprResult Update = buildCounterUpdate(
Alexey Bataev5a3af132016-03-29 08:58:54 +00005506 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
5507 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005508 if (!Update.isUsable()) {
5509 HasErrors = true;
5510 break;
5511 }
5512
5513 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
Alexey Bataeve3727102018-04-18 15:57:46 +00005514 ExprResult Final = buildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00005515 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005516 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005517 if (!Final.isUsable()) {
5518 HasErrors = true;
5519 break;
5520 }
5521
5522 // Build Div for the next iteration: Div <- Div * IS.NumIters
5523 if (Cnt != 0) {
5524 if (Div.isUnset())
5525 Div = IS.NumIterations;
5526 else
5527 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
5528 IS.NumIterations);
5529
5530 // Add parentheses (for debugging purposes only).
5531 if (Div.isUsable())
Alexey Bataev8b427062016-05-25 12:36:08 +00005532 Div = tryBuildCapture(SemaRef, Div.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005533 if (!Div.isUsable()) {
5534 HasErrors = true;
5535 break;
5536 }
5537 }
5538 if (!Update.isUsable() || !Final.isUsable()) {
5539 HasErrors = true;
5540 break;
5541 }
5542 // Save results
5543 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00005544 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005545 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005546 Built.Updates[Cnt] = Update.get();
5547 Built.Finals[Cnt] = Final.get();
5548 }
5549 }
5550
5551 if (HasErrors)
5552 return 0;
5553
5554 // Save results
5555 Built.IterationVarRef = IV.get();
5556 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00005557 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00005558 Built.CalcLastIteration =
5559 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005560 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00005561 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005562 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005563 Built.Init = Init.get();
5564 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005565 Built.LB = LB.get();
5566 Built.UB = UB.get();
5567 Built.IL = IL.get();
5568 Built.ST = ST.get();
5569 Built.EUB = EUB.get();
5570 Built.NLB = NextLB.get();
5571 Built.NUB = NextUB.get();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005572 Built.PrevLB = PrevLB.get();
5573 Built.PrevUB = PrevUB.get();
Carlo Bertolli8429d812017-02-17 21:29:13 +00005574 Built.DistInc = DistInc.get();
5575 Built.PrevEUB = PrevEUB.get();
Carlo Bertolliffafe102017-04-20 00:39:39 +00005576 Built.DistCombinedFields.LB = CombLB.get();
5577 Built.DistCombinedFields.UB = CombUB.get();
5578 Built.DistCombinedFields.EUB = CombEUB.get();
5579 Built.DistCombinedFields.Init = CombInit.get();
5580 Built.DistCombinedFields.Cond = CombCond.get();
5581 Built.DistCombinedFields.NLB = CombNextLB.get();
5582 Built.DistCombinedFields.NUB = CombNextUB.get();
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005583 Built.DistCombinedFields.DistCond = CombDistCond.get();
5584 Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005585
Alexey Bataevabfc0692014-06-25 06:52:00 +00005586 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005587}
5588
Alexey Bataev10e775f2015-07-30 11:36:16 +00005589static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005590 auto CollapseClauses =
5591 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
5592 if (CollapseClauses.begin() != CollapseClauses.end())
5593 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005594 return nullptr;
5595}
5596
Alexey Bataev10e775f2015-07-30 11:36:16 +00005597static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005598 auto OrderedClauses =
5599 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
5600 if (OrderedClauses.begin() != OrderedClauses.end())
5601 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005602 return nullptr;
5603}
5604
Kelvin Lic5609492016-07-15 04:39:07 +00005605static bool checkSimdlenSafelenSpecified(Sema &S,
5606 const ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00005607 const OMPSafelenClause *Safelen = nullptr;
5608 const OMPSimdlenClause *Simdlen = nullptr;
Kelvin Lic5609492016-07-15 04:39:07 +00005609
Alexey Bataeve3727102018-04-18 15:57:46 +00005610 for (const OMPClause *Clause : Clauses) {
Kelvin Lic5609492016-07-15 04:39:07 +00005611 if (Clause->getClauseKind() == OMPC_safelen)
5612 Safelen = cast<OMPSafelenClause>(Clause);
5613 else if (Clause->getClauseKind() == OMPC_simdlen)
5614 Simdlen = cast<OMPSimdlenClause>(Clause);
5615 if (Safelen && Simdlen)
5616 break;
5617 }
5618
5619 if (Simdlen && Safelen) {
5620 llvm::APSInt SimdlenRes, SafelenRes;
Alexey Bataeve3727102018-04-18 15:57:46 +00005621 const Expr *SimdlenLength = Simdlen->getSimdlen();
5622 const Expr *SafelenLength = Safelen->getSafelen();
Kelvin Lic5609492016-07-15 04:39:07 +00005623 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
5624 SimdlenLength->isInstantiationDependent() ||
5625 SimdlenLength->containsUnexpandedParameterPack())
5626 return false;
5627 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
5628 SafelenLength->isInstantiationDependent() ||
5629 SafelenLength->containsUnexpandedParameterPack())
5630 return false;
5631 SimdlenLength->EvaluateAsInt(SimdlenRes, S.Context);
5632 SafelenLength->EvaluateAsInt(SafelenRes, S.Context);
5633 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
5634 // If both simdlen and safelen clauses are specified, the value of the
5635 // simdlen parameter must be less than or equal to the value of the safelen
5636 // parameter.
5637 if (SimdlenRes > SafelenRes) {
5638 S.Diag(SimdlenLength->getExprLoc(),
5639 diag::err_omp_wrong_simdlen_safelen_values)
5640 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
5641 return true;
5642 }
Alexey Bataev66b15b52015-08-21 11:14:16 +00005643 }
5644 return false;
5645}
5646
Alexey Bataeve3727102018-04-18 15:57:46 +00005647StmtResult
5648Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
5649 SourceLocation StartLoc, SourceLocation EndLoc,
5650 VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005651 if (!AStmt)
5652 return StmtError();
5653
5654 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005655 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005656 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5657 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00005658 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataev10e775f2015-07-30 11:36:16 +00005659 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5660 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005661 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005662 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005663
Alexander Musmana5f070a2014-10-01 06:03:56 +00005664 assert((CurContext->isDependentContext() || B.builtAll()) &&
5665 "omp simd loop exprs were not built");
5666
Alexander Musman3276a272015-03-21 10:12:56 +00005667 if (!CurContext->isDependentContext()) {
5668 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005669 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005670 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexander Musman3276a272015-03-21 10:12:56 +00005671 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005672 B.NumIterations, *this, CurScope,
5673 DSAStack))
Alexander Musman3276a272015-03-21 10:12:56 +00005674 return StmtError();
5675 }
5676 }
5677
Kelvin Lic5609492016-07-15 04:39:07 +00005678 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005679 return StmtError();
5680
Reid Kleckner87a31802018-03-12 21:43:02 +00005681 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005682 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5683 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005684}
5685
Alexey Bataeve3727102018-04-18 15:57:46 +00005686StmtResult
5687Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
5688 SourceLocation StartLoc, SourceLocation EndLoc,
5689 VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005690 if (!AStmt)
5691 return StmtError();
5692
5693 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005694 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005695 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5696 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00005697 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataev10e775f2015-07-30 11:36:16 +00005698 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5699 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005700 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005701 return StmtError();
5702
Alexander Musmana5f070a2014-10-01 06:03:56 +00005703 assert((CurContext->isDependentContext() || B.builtAll()) &&
5704 "omp for loop exprs were not built");
5705
Alexey Bataev54acd402015-08-04 11:18:19 +00005706 if (!CurContext->isDependentContext()) {
5707 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005708 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005709 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00005710 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005711 B.NumIterations, *this, CurScope,
5712 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005713 return StmtError();
5714 }
5715 }
5716
Reid Kleckner87a31802018-03-12 21:43:02 +00005717 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005718 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005719 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005720}
5721
Alexander Musmanf82886e2014-09-18 05:12:34 +00005722StmtResult Sema::ActOnOpenMPForSimdDirective(
5723 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00005724 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005725 if (!AStmt)
5726 return StmtError();
5727
5728 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005729 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005730 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5731 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00005732 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00005733 checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00005734 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5735 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005736 if (NestedLoopCount == 0)
5737 return StmtError();
5738
Alexander Musmanc6388682014-12-15 07:07:06 +00005739 assert((CurContext->isDependentContext() || B.builtAll()) &&
5740 "omp for simd loop exprs were not built");
5741
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005742 if (!CurContext->isDependentContext()) {
5743 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005744 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005745 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005746 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005747 B.NumIterations, *this, CurScope,
5748 DSAStack))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005749 return StmtError();
5750 }
5751 }
5752
Kelvin Lic5609492016-07-15 04:39:07 +00005753 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005754 return StmtError();
5755
Reid Kleckner87a31802018-03-12 21:43:02 +00005756 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005757 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5758 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005759}
5760
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005761StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5762 Stmt *AStmt,
5763 SourceLocation StartLoc,
5764 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005765 if (!AStmt)
5766 return StmtError();
5767
5768 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005769 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00005770 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005771 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00005772 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005773 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005774 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005775 return StmtError();
5776 // All associated statements must be '#pragma omp section' except for
5777 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005778 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005779 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5780 if (SectionStmt)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005781 Diag(SectionStmt->getBeginLoc(),
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005782 diag::err_omp_sections_substmt_not_section);
5783 return StmtError();
5784 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005785 cast<OMPSectionDirective>(SectionStmt)
5786 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005787 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005788 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005789 Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005790 return StmtError();
5791 }
5792
Reid Kleckner87a31802018-03-12 21:43:02 +00005793 setFunctionHasBranchProtectedScope();
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005794
Alexey Bataev25e5b442015-09-15 12:52:43 +00005795 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5796 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005797}
5798
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005799StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5800 SourceLocation StartLoc,
5801 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005802 if (!AStmt)
5803 return StmtError();
5804
5805 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005806
Reid Kleckner87a31802018-03-12 21:43:02 +00005807 setFunctionHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00005808 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005809
Alexey Bataev25e5b442015-09-15 12:52:43 +00005810 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5811 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005812}
5813
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005814StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5815 Stmt *AStmt,
5816 SourceLocation StartLoc,
5817 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005818 if (!AStmt)
5819 return StmtError();
5820
5821 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00005822
Reid Kleckner87a31802018-03-12 21:43:02 +00005823 setFunctionHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00005824
Alexey Bataev3255bf32015-01-19 05:20:46 +00005825 // OpenMP [2.7.3, single Construct, Restrictions]
5826 // The copyprivate clause must not be used with the nowait clause.
Alexey Bataeve3727102018-04-18 15:57:46 +00005827 const OMPClause *Nowait = nullptr;
5828 const OMPClause *Copyprivate = nullptr;
5829 for (const OMPClause *Clause : Clauses) {
Alexey Bataev3255bf32015-01-19 05:20:46 +00005830 if (Clause->getClauseKind() == OMPC_nowait)
5831 Nowait = Clause;
5832 else if (Clause->getClauseKind() == OMPC_copyprivate)
5833 Copyprivate = Clause;
5834 if (Copyprivate && Nowait) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005835 Diag(Copyprivate->getBeginLoc(),
Alexey Bataev3255bf32015-01-19 05:20:46 +00005836 diag::err_omp_single_copyprivate_with_nowait);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005837 Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here);
Alexey Bataev3255bf32015-01-19 05:20:46 +00005838 return StmtError();
5839 }
5840 }
5841
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005842 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5843}
5844
Alexander Musman80c22892014-07-17 08:54:58 +00005845StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5846 SourceLocation StartLoc,
5847 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005848 if (!AStmt)
5849 return StmtError();
5850
5851 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00005852
Reid Kleckner87a31802018-03-12 21:43:02 +00005853 setFunctionHasBranchProtectedScope();
Alexander Musman80c22892014-07-17 08:54:58 +00005854
5855 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5856}
5857
Alexey Bataev28c75412015-12-15 08:19:24 +00005858StmtResult Sema::ActOnOpenMPCriticalDirective(
5859 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5860 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005861 if (!AStmt)
5862 return StmtError();
5863
5864 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005865
Alexey Bataev28c75412015-12-15 08:19:24 +00005866 bool ErrorFound = false;
5867 llvm::APSInt Hint;
5868 SourceLocation HintLoc;
5869 bool DependentHint = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00005870 for (const OMPClause *C : Clauses) {
Alexey Bataev28c75412015-12-15 08:19:24 +00005871 if (C->getClauseKind() == OMPC_hint) {
5872 if (!DirName.getName()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005873 Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name);
Alexey Bataev28c75412015-12-15 08:19:24 +00005874 ErrorFound = true;
5875 }
5876 Expr *E = cast<OMPHintClause>(C)->getHint();
5877 if (E->isTypeDependent() || E->isValueDependent() ||
Alexey Bataeve3727102018-04-18 15:57:46 +00005878 E->isInstantiationDependent()) {
Alexey Bataev28c75412015-12-15 08:19:24 +00005879 DependentHint = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00005880 } else {
Alexey Bataev28c75412015-12-15 08:19:24 +00005881 Hint = E->EvaluateKnownConstInt(Context);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005882 HintLoc = C->getBeginLoc();
Alexey Bataev28c75412015-12-15 08:19:24 +00005883 }
5884 }
5885 }
5886 if (ErrorFound)
5887 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00005888 const auto Pair = DSAStack->getCriticalWithHint(DirName);
Alexey Bataev28c75412015-12-15 08:19:24 +00005889 if (Pair.first && DirName.getName() && !DependentHint) {
5890 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5891 Diag(StartLoc, diag::err_omp_critical_with_hint);
Alexey Bataeve3727102018-04-18 15:57:46 +00005892 if (HintLoc.isValid())
Alexey Bataev28c75412015-12-15 08:19:24 +00005893 Diag(HintLoc, diag::note_omp_critical_hint_here)
5894 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +00005895 else
Alexey Bataev28c75412015-12-15 08:19:24 +00005896 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
Alexey Bataeve3727102018-04-18 15:57:46 +00005897 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005898 Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here)
Alexey Bataev28c75412015-12-15 08:19:24 +00005899 << 1
5900 << C->getHint()->EvaluateKnownConstInt(Context).toString(
5901 /*Radix=*/10, /*Signed=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +00005902 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005903 Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1;
Alexey Bataeve3727102018-04-18 15:57:46 +00005904 }
Alexey Bataev28c75412015-12-15 08:19:24 +00005905 }
5906 }
5907
Reid Kleckner87a31802018-03-12 21:43:02 +00005908 setFunctionHasBranchProtectedScope();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005909
Alexey Bataev28c75412015-12-15 08:19:24 +00005910 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5911 Clauses, AStmt);
5912 if (!Pair.first && DirName.getName() && !DependentHint)
5913 DSAStack->addCriticalWithHint(Dir, Hint);
5914 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005915}
5916
Alexey Bataev4acb8592014-07-07 13:01:15 +00005917StmtResult Sema::ActOnOpenMPParallelForDirective(
5918 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00005919 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005920 if (!AStmt)
5921 return StmtError();
5922
Alexey Bataeve3727102018-04-18 15:57:46 +00005923 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005924 // 1.2.2 OpenMP Language Terminology
5925 // Structured block - An executable statement with a single entry at the
5926 // top and a single exit at the bottom.
5927 // The point of exit cannot be a branch out of the structured block.
5928 // longjmp() and throw() must not violate the entry/exit criteria.
5929 CS->getCapturedDecl()->setNothrow();
5930
Alexander Musmanc6388682014-12-15 07:07:06 +00005931 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005932 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5933 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005934 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00005935 checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00005936 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5937 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005938 if (NestedLoopCount == 0)
5939 return StmtError();
5940
Alexander Musmana5f070a2014-10-01 06:03:56 +00005941 assert((CurContext->isDependentContext() || B.builtAll()) &&
5942 "omp parallel for loop exprs were not built");
5943
Alexey Bataev54acd402015-08-04 11:18:19 +00005944 if (!CurContext->isDependentContext()) {
5945 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005946 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005947 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00005948 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005949 B.NumIterations, *this, CurScope,
5950 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005951 return StmtError();
5952 }
5953 }
5954
Reid Kleckner87a31802018-03-12 21:43:02 +00005955 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005956 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005957 NestedLoopCount, Clauses, AStmt, B,
5958 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00005959}
5960
Alexander Musmane4e893b2014-09-23 09:33:00 +00005961StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
5962 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00005963 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005964 if (!AStmt)
5965 return StmtError();
5966
Alexey Bataeve3727102018-04-18 15:57:46 +00005967 auto *CS = cast<CapturedStmt>(AStmt);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005968 // 1.2.2 OpenMP Language Terminology
5969 // Structured block - An executable statement with a single entry at the
5970 // top and a single exit at the bottom.
5971 // The point of exit cannot be a branch out of the structured block.
5972 // longjmp() and throw() must not violate the entry/exit criteria.
5973 CS->getCapturedDecl()->setNothrow();
5974
Alexander Musmanc6388682014-12-15 07:07:06 +00005975 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005976 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5977 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00005978 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00005979 checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00005980 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5981 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005982 if (NestedLoopCount == 0)
5983 return StmtError();
5984
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005985 if (!CurContext->isDependentContext()) {
5986 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005987 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005988 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005989 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005990 B.NumIterations, *this, CurScope,
5991 DSAStack))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005992 return StmtError();
5993 }
5994 }
5995
Kelvin Lic5609492016-07-15 04:39:07 +00005996 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005997 return StmtError();
5998
Reid Kleckner87a31802018-03-12 21:43:02 +00005999 setFunctionHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00006000 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00006001 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00006002}
6003
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006004StmtResult
6005Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
6006 Stmt *AStmt, SourceLocation StartLoc,
6007 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006008 if (!AStmt)
6009 return StmtError();
6010
6011 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006012 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00006013 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006014 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00006015 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006016 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00006017 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006018 return StmtError();
6019 // All associated statements must be '#pragma omp section' except for
6020 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00006021 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006022 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
6023 if (SectionStmt)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006024 Diag(SectionStmt->getBeginLoc(),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006025 diag::err_omp_parallel_sections_substmt_not_section);
6026 return StmtError();
6027 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006028 cast<OMPSectionDirective>(SectionStmt)
6029 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006030 }
6031 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006032 Diag(AStmt->getBeginLoc(),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006033 diag::err_omp_parallel_sections_not_compound_stmt);
6034 return StmtError();
6035 }
6036
Reid Kleckner87a31802018-03-12 21:43:02 +00006037 setFunctionHasBranchProtectedScope();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006038
Alexey Bataev25e5b442015-09-15 12:52:43 +00006039 return OMPParallelSectionsDirective::Create(
6040 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006041}
6042
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006043StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
6044 Stmt *AStmt, SourceLocation StartLoc,
6045 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006046 if (!AStmt)
6047 return StmtError();
6048
David Majnemer9d168222016-08-05 17:44:54 +00006049 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006050 // 1.2.2 OpenMP Language Terminology
6051 // Structured block - An executable statement with a single entry at the
6052 // top and a single exit at the bottom.
6053 // The point of exit cannot be a branch out of the structured block.
6054 // longjmp() and throw() must not violate the entry/exit criteria.
6055 CS->getCapturedDecl()->setNothrow();
6056
Reid Kleckner87a31802018-03-12 21:43:02 +00006057 setFunctionHasBranchProtectedScope();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006058
Alexey Bataev25e5b442015-09-15 12:52:43 +00006059 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
6060 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006061}
6062
Alexey Bataev68446b72014-07-18 07:47:19 +00006063StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
6064 SourceLocation EndLoc) {
6065 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
6066}
6067
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00006068StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
6069 SourceLocation EndLoc) {
6070 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
6071}
6072
Alexey Bataev2df347a2014-07-18 10:17:07 +00006073StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
6074 SourceLocation EndLoc) {
6075 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
6076}
6077
Alexey Bataev169d96a2017-07-18 20:17:46 +00006078StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses,
6079 Stmt *AStmt,
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006080 SourceLocation StartLoc,
6081 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006082 if (!AStmt)
6083 return StmtError();
6084
6085 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006086
Reid Kleckner87a31802018-03-12 21:43:02 +00006087 setFunctionHasBranchProtectedScope();
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006088
Alexey Bataev169d96a2017-07-18 20:17:46 +00006089 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses,
Alexey Bataev3b1b8952017-07-25 15:53:26 +00006090 AStmt,
6091 DSAStack->getTaskgroupReductionRef());
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006092}
6093
Alexey Bataev6125da92014-07-21 11:26:11 +00006094StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
6095 SourceLocation StartLoc,
6096 SourceLocation EndLoc) {
6097 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
6098 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
6099}
6100
Alexey Bataev346265e2015-09-25 10:37:12 +00006101StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
6102 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00006103 SourceLocation StartLoc,
6104 SourceLocation EndLoc) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006105 const OMPClause *DependFound = nullptr;
6106 const OMPClause *DependSourceClause = nullptr;
6107 const OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00006108 bool ErrorFound = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00006109 const OMPThreadsClause *TC = nullptr;
6110 const OMPSIMDClause *SC = nullptr;
6111 for (const OMPClause *C : Clauses) {
Alexey Bataeveb482352015-12-18 05:05:56 +00006112 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
6113 DependFound = C;
6114 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
6115 if (DependSourceClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006116 Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
Alexey Bataeveb482352015-12-18 05:05:56 +00006117 << getOpenMPDirectiveName(OMPD_ordered)
6118 << getOpenMPClauseName(OMPC_depend) << 2;
6119 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006120 } else {
Alexey Bataeveb482352015-12-18 05:05:56 +00006121 DependSourceClause = C;
Alexey Bataeve3727102018-04-18 15:57:46 +00006122 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006123 if (DependSinkClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006124 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006125 << 0;
6126 ErrorFound = true;
6127 }
6128 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
6129 if (DependSourceClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006130 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006131 << 1;
6132 ErrorFound = true;
6133 }
6134 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00006135 }
Alexey Bataeve3727102018-04-18 15:57:46 +00006136 } else if (C->getClauseKind() == OMPC_threads) {
Alexey Bataev346265e2015-09-25 10:37:12 +00006137 TC = cast<OMPThreadsClause>(C);
Alexey Bataeve3727102018-04-18 15:57:46 +00006138 } else if (C->getClauseKind() == OMPC_simd) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006139 SC = cast<OMPSIMDClause>(C);
Alexey Bataeve3727102018-04-18 15:57:46 +00006140 }
Alexey Bataev346265e2015-09-25 10:37:12 +00006141 }
Alexey Bataeveb482352015-12-18 05:05:56 +00006142 if (!ErrorFound && !SC &&
6143 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006144 // OpenMP [2.8.1,simd Construct, Restrictions]
6145 // An ordered construct with the simd clause is the only OpenMP construct
6146 // that can appear in the simd region.
6147 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00006148 ErrorFound = true;
6149 } else if (DependFound && (TC || SC)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006150 Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd)
Alexey Bataeveb482352015-12-18 05:05:56 +00006151 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
6152 ErrorFound = true;
Alexey Bataevf138fda2018-08-13 19:04:24 +00006153 } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006154 Diag(DependFound->getBeginLoc(),
Alexey Bataeveb482352015-12-18 05:05:56 +00006155 diag::err_omp_ordered_directive_without_param);
6156 ErrorFound = true;
6157 } else if (TC || Clauses.empty()) {
Alexey Bataevf138fda2018-08-13 19:04:24 +00006158 if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006159 SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc;
Alexey Bataeveb482352015-12-18 05:05:56 +00006160 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
6161 << (TC != nullptr);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006162 Diag(Param->getBeginLoc(), diag::note_omp_ordered_param);
Alexey Bataeveb482352015-12-18 05:05:56 +00006163 ErrorFound = true;
6164 }
6165 }
6166 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006167 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00006168
6169 if (AStmt) {
6170 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6171
Reid Kleckner87a31802018-03-12 21:43:02 +00006172 setFunctionHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006173 }
Alexey Bataev346265e2015-09-25 10:37:12 +00006174
6175 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00006176}
6177
Alexey Bataev1d160b12015-03-13 12:27:31 +00006178namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006179/// Helper class for checking expression in 'omp atomic [update]'
Alexey Bataev1d160b12015-03-13 12:27:31 +00006180/// construct.
6181class OpenMPAtomicUpdateChecker {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006182 /// Error results for atomic update expressions.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006183 enum ExprAnalysisErrorCode {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006184 /// A statement is not an expression statement.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006185 NotAnExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006186 /// Expression is not builtin binary or unary operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006187 NotABinaryOrUnaryExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006188 /// Unary operation is not post-/pre- increment/decrement operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006189 NotAnUnaryIncDecExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006190 /// An expression is not of scalar type.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006191 NotAScalarType,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006192 /// A binary operation is not an assignment operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006193 NotAnAssignmentOp,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006194 /// RHS part of the binary operation is not a binary expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006195 NotABinaryExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006196 /// RHS part is not additive/multiplicative/shift/biwise binary
Alexey Bataev1d160b12015-03-13 12:27:31 +00006197 /// expression.
6198 NotABinaryOperator,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006199 /// RHS binary operation does not have reference to the updated LHS
Alexey Bataev1d160b12015-03-13 12:27:31 +00006200 /// part.
6201 NotAnUpdateExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006202 /// No errors is found.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006203 NoError
6204 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006205 /// Reference to Sema.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006206 Sema &SemaRef;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006207 /// A location for note diagnostics (when error is found).
Alexey Bataev1d160b12015-03-13 12:27:31 +00006208 SourceLocation NoteLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006209 /// 'x' lvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006210 Expr *X;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006211 /// 'expr' rvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006212 Expr *E;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006213 /// Helper expression of the form
Alexey Bataevb4505a72015-03-30 05:20:59 +00006214 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6215 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6216 Expr *UpdateExpr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006217 /// Is 'x' a LHS in a RHS part of full update expression. It is
Alexey Bataevb4505a72015-03-30 05:20:59 +00006218 /// important for non-associative operations.
6219 bool IsXLHSInRHSPart;
6220 BinaryOperatorKind Op;
6221 SourceLocation OpLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006222 /// true if the source expression is a postfix unary operation, false
Alexey Bataevb78ca832015-04-01 03:33:17 +00006223 /// if it is a prefix unary operation.
6224 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006225
6226public:
6227 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00006228 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00006229 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006230 /// Check specified statement that it is suitable for 'atomic update'
Alexey Bataev1d160b12015-03-13 12:27:31 +00006231 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00006232 /// expression. If DiagId and NoteId == 0, then only check is performed
6233 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006234 /// \param DiagId Diagnostic which should be emitted if error is found.
6235 /// \param NoteId Diagnostic note for the main error message.
6236 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00006237 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006238 /// Return the 'x' lvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006239 Expr *getX() const { return X; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006240 /// Return the 'expr' rvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006241 Expr *getExpr() const { return E; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006242 /// Return the update expression used in calculation of the updated
Alexey Bataevb4505a72015-03-30 05:20:59 +00006243 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6244 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6245 Expr *getUpdateExpr() const { return UpdateExpr; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006246 /// Return true if 'x' is LHS in RHS part of full update expression,
Alexey Bataevb4505a72015-03-30 05:20:59 +00006247 /// false otherwise.
6248 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
6249
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006250 /// true if the source expression is a postfix unary operation, false
Alexey Bataevb78ca832015-04-01 03:33:17 +00006251 /// if it is a prefix unary operation.
6252 bool isPostfixUpdate() const { return IsPostfixUpdate; }
6253
Alexey Bataev1d160b12015-03-13 12:27:31 +00006254private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00006255 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
6256 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00006257};
6258} // namespace
6259
6260bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
6261 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
6262 ExprAnalysisErrorCode ErrorFound = NoError;
6263 SourceLocation ErrorLoc, NoteLoc;
6264 SourceRange ErrorRange, NoteRange;
6265 // Allowed constructs are:
6266 // x = x binop expr;
6267 // x = expr binop x;
6268 if (AtomicBinOp->getOpcode() == BO_Assign) {
6269 X = AtomicBinOp->getLHS();
Alexey Bataeve3727102018-04-18 15:57:46 +00006270 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006271 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
6272 if (AtomicInnerBinOp->isMultiplicativeOp() ||
6273 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
6274 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006275 Op = AtomicInnerBinOp->getOpcode();
6276 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataeve3727102018-04-18 15:57:46 +00006277 Expr *LHS = AtomicInnerBinOp->getLHS();
6278 Expr *RHS = AtomicInnerBinOp->getRHS();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006279 llvm::FoldingSetNodeID XId, LHSId, RHSId;
6280 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
6281 /*Canonical=*/true);
6282 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
6283 /*Canonical=*/true);
6284 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
6285 /*Canonical=*/true);
6286 if (XId == LHSId) {
6287 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006288 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006289 } else if (XId == RHSId) {
6290 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006291 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006292 } else {
6293 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6294 ErrorRange = AtomicInnerBinOp->getSourceRange();
6295 NoteLoc = X->getExprLoc();
6296 NoteRange = X->getSourceRange();
6297 ErrorFound = NotAnUpdateExpression;
6298 }
6299 } else {
6300 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6301 ErrorRange = AtomicInnerBinOp->getSourceRange();
6302 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
6303 NoteRange = SourceRange(NoteLoc, NoteLoc);
6304 ErrorFound = NotABinaryOperator;
6305 }
6306 } else {
6307 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
6308 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
6309 ErrorFound = NotABinaryExpression;
6310 }
6311 } else {
6312 ErrorLoc = AtomicBinOp->getExprLoc();
6313 ErrorRange = AtomicBinOp->getSourceRange();
6314 NoteLoc = AtomicBinOp->getOperatorLoc();
6315 NoteRange = SourceRange(NoteLoc, NoteLoc);
6316 ErrorFound = NotAnAssignmentOp;
6317 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006318 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006319 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6320 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6321 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006322 }
6323 if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006324 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006325 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006326}
6327
6328bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
6329 unsigned NoteId) {
6330 ExprAnalysisErrorCode ErrorFound = NoError;
6331 SourceLocation ErrorLoc, NoteLoc;
6332 SourceRange ErrorRange, NoteRange;
6333 // Allowed constructs are:
6334 // x++;
6335 // x--;
6336 // ++x;
6337 // --x;
6338 // x binop= expr;
6339 // x = x binop expr;
6340 // x = expr binop x;
6341 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
6342 AtomicBody = AtomicBody->IgnoreParenImpCasts();
6343 if (AtomicBody->getType()->isScalarType() ||
6344 AtomicBody->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006345 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006346 AtomicBody->IgnoreParenImpCasts())) {
6347 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00006348 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006349 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00006350 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006351 E = AtomicCompAssignOp->getRHS();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006352 X = AtomicCompAssignOp->getLHS()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006353 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006354 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
6355 AtomicBody->IgnoreParenImpCasts())) {
6356 // Check for Binary Operation
David Majnemer9d168222016-08-05 17:44:54 +00006357 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
Alexey Bataevb4505a72015-03-30 05:20:59 +00006358 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006359 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
David Majnemer9d168222016-08-05 17:44:54 +00006360 AtomicBody->IgnoreParenImpCasts())) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006361 // Check for Unary Operation
6362 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006363 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006364 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
6365 OpLoc = AtomicUnaryOp->getOperatorLoc();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006366 X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006367 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
6368 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006369 } else {
6370 ErrorFound = NotAnUnaryIncDecExpression;
6371 ErrorLoc = AtomicUnaryOp->getExprLoc();
6372 ErrorRange = AtomicUnaryOp->getSourceRange();
6373 NoteLoc = AtomicUnaryOp->getOperatorLoc();
6374 NoteRange = SourceRange(NoteLoc, NoteLoc);
6375 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006376 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006377 ErrorFound = NotABinaryOrUnaryExpression;
6378 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
6379 NoteRange = ErrorRange = AtomicBody->getSourceRange();
6380 }
6381 } else {
6382 ErrorFound = NotAScalarType;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006383 NoteLoc = ErrorLoc = AtomicBody->getBeginLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006384 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6385 }
6386 } else {
6387 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006388 NoteLoc = ErrorLoc = S->getBeginLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006389 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6390 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006391 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006392 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6393 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6394 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006395 }
6396 if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006397 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006398 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006399 // Build an update expression of form 'OpaqueValueExpr(x) binop
6400 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
6401 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
6402 auto *OVEX = new (SemaRef.getASTContext())
6403 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
6404 auto *OVEExpr = new (SemaRef.getASTContext())
6405 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
Alexey Bataeve3727102018-04-18 15:57:46 +00006406 ExprResult Update =
Alexey Bataevb4505a72015-03-30 05:20:59 +00006407 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
6408 IsXLHSInRHSPart ? OVEExpr : OVEX);
6409 if (Update.isInvalid())
6410 return true;
6411 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
6412 Sema::AA_Casting);
6413 if (Update.isInvalid())
6414 return true;
6415 UpdateExpr = Update.get();
6416 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00006417 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006418}
6419
Alexey Bataev0162e452014-07-22 10:10:35 +00006420StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
6421 Stmt *AStmt,
6422 SourceLocation StartLoc,
6423 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006424 if (!AStmt)
6425 return StmtError();
6426
David Majnemer9d168222016-08-05 17:44:54 +00006427 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00006428 // 1.2.2 OpenMP Language Terminology
6429 // Structured block - An executable statement with a single entry at the
6430 // top and a single exit at the bottom.
6431 // The point of exit cannot be a branch out of the structured block.
6432 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00006433 OpenMPClauseKind AtomicKind = OMPC_unknown;
6434 SourceLocation AtomicKindLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00006435 for (const OMPClause *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00006436 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00006437 C->getClauseKind() == OMPC_update ||
6438 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00006439 if (AtomicKind != OMPC_unknown) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006440 Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00006441 << SourceRange(C->getBeginLoc(), C->getEndLoc());
Alexey Bataevdea47612014-07-23 07:46:59 +00006442 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
6443 << getOpenMPClauseName(AtomicKind);
6444 } else {
6445 AtomicKind = C->getClauseKind();
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006446 AtomicKindLoc = C->getBeginLoc();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006447 }
6448 }
6449 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006450
Alexey Bataeve3727102018-04-18 15:57:46 +00006451 Stmt *Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00006452 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
6453 Body = EWC->getSubExpr();
6454
Alexey Bataev62cec442014-11-18 10:14:22 +00006455 Expr *X = nullptr;
6456 Expr *V = nullptr;
6457 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006458 Expr *UE = nullptr;
6459 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006460 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00006461 // OpenMP [2.12.6, atomic Construct]
6462 // In the next expressions:
6463 // * x and v (as applicable) are both l-value expressions with scalar type.
6464 // * During the execution of an atomic region, multiple syntactic
6465 // occurrences of x must designate the same storage location.
6466 // * Neither of v and expr (as applicable) may access the storage location
6467 // designated by x.
6468 // * Neither of x and expr (as applicable) may access the storage location
6469 // designated by v.
6470 // * expr is an expression with scalar type.
6471 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
6472 // * binop, binop=, ++, and -- are not overloaded operators.
6473 // * The expression x binop expr must be numerically equivalent to x binop
6474 // (expr). This requirement is satisfied if the operators in expr have
6475 // precedence greater than binop, or by using parentheses around expr or
6476 // subexpressions of expr.
6477 // * The expression expr binop x must be numerically equivalent to (expr)
6478 // binop x. This requirement is satisfied if the operators in expr have
6479 // precedence equal to or greater than binop, or by using parentheses around
6480 // expr or subexpressions of expr.
6481 // * For forms that allow multiple occurrences of x, the number of times
6482 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00006483 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006484 enum {
6485 NotAnExpression,
6486 NotAnAssignmentOp,
6487 NotAScalarType,
6488 NotAnLValue,
6489 NoError
6490 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00006491 SourceLocation ErrorLoc, NoteLoc;
6492 SourceRange ErrorRange, NoteRange;
6493 // If clause is read:
6494 // v = x;
Alexey Bataeve3727102018-04-18 15:57:46 +00006495 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
6496 const auto *AtomicBinOp =
Alexey Bataev62cec442014-11-18 10:14:22 +00006497 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6498 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6499 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6500 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
6501 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6502 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
6503 if (!X->isLValue() || !V->isLValue()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006504 const Expr *NotLValueExpr = X->isLValue() ? V : X;
Alexey Bataev62cec442014-11-18 10:14:22 +00006505 ErrorFound = NotAnLValue;
6506 ErrorLoc = AtomicBinOp->getExprLoc();
6507 ErrorRange = AtomicBinOp->getSourceRange();
6508 NoteLoc = NotLValueExpr->getExprLoc();
6509 NoteRange = NotLValueExpr->getSourceRange();
6510 }
6511 } else if (!X->isInstantiationDependent() ||
6512 !V->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006513 const Expr *NotScalarExpr =
Alexey Bataev62cec442014-11-18 10:14:22 +00006514 (X->isInstantiationDependent() || X->getType()->isScalarType())
6515 ? V
6516 : X;
6517 ErrorFound = NotAScalarType;
6518 ErrorLoc = AtomicBinOp->getExprLoc();
6519 ErrorRange = AtomicBinOp->getSourceRange();
6520 NoteLoc = NotScalarExpr->getExprLoc();
6521 NoteRange = NotScalarExpr->getSourceRange();
6522 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006523 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00006524 ErrorFound = NotAnAssignmentOp;
6525 ErrorLoc = AtomicBody->getExprLoc();
6526 ErrorRange = AtomicBody->getSourceRange();
6527 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6528 : AtomicBody->getExprLoc();
6529 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6530 : AtomicBody->getSourceRange();
6531 }
6532 } else {
6533 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006534 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataev62cec442014-11-18 10:14:22 +00006535 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006536 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006537 if (ErrorFound != NoError) {
6538 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
6539 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006540 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6541 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00006542 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00006543 }
6544 if (CurContext->isDependentContext())
Alexey Bataev62cec442014-11-18 10:14:22 +00006545 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00006546 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006547 enum {
6548 NotAnExpression,
6549 NotAnAssignmentOp,
6550 NotAScalarType,
6551 NotAnLValue,
6552 NoError
6553 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006554 SourceLocation ErrorLoc, NoteLoc;
6555 SourceRange ErrorRange, NoteRange;
6556 // If clause is write:
6557 // x = expr;
Alexey Bataeve3727102018-04-18 15:57:46 +00006558 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
6559 const auto *AtomicBinOp =
Alexey Bataevf33eba62014-11-28 07:21:40 +00006560 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6561 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00006562 X = AtomicBinOp->getLHS();
6563 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006564 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6565 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
6566 if (!X->isLValue()) {
6567 ErrorFound = NotAnLValue;
6568 ErrorLoc = AtomicBinOp->getExprLoc();
6569 ErrorRange = AtomicBinOp->getSourceRange();
6570 NoteLoc = X->getExprLoc();
6571 NoteRange = X->getSourceRange();
6572 }
6573 } else if (!X->isInstantiationDependent() ||
6574 !E->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006575 const Expr *NotScalarExpr =
Alexey Bataevf33eba62014-11-28 07:21:40 +00006576 (X->isInstantiationDependent() || X->getType()->isScalarType())
6577 ? E
6578 : X;
6579 ErrorFound = NotAScalarType;
6580 ErrorLoc = AtomicBinOp->getExprLoc();
6581 ErrorRange = AtomicBinOp->getSourceRange();
6582 NoteLoc = NotScalarExpr->getExprLoc();
6583 NoteRange = NotScalarExpr->getSourceRange();
6584 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006585 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00006586 ErrorFound = NotAnAssignmentOp;
6587 ErrorLoc = AtomicBody->getExprLoc();
6588 ErrorRange = AtomicBody->getSourceRange();
6589 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6590 : AtomicBody->getExprLoc();
6591 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6592 : AtomicBody->getSourceRange();
6593 }
6594 } else {
6595 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006596 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006597 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006598 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00006599 if (ErrorFound != NoError) {
6600 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
6601 << ErrorRange;
6602 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6603 << NoteRange;
6604 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00006605 }
6606 if (CurContext->isDependentContext())
Alexey Bataevf33eba62014-11-28 07:21:40 +00006607 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006608 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006609 // If clause is update:
6610 // x++;
6611 // x--;
6612 // ++x;
6613 // --x;
6614 // x binop= expr;
6615 // x = x binop expr;
6616 // x = expr binop x;
6617 OpenMPAtomicUpdateChecker Checker(*this);
6618 if (Checker.checkStatement(
6619 Body, (AtomicKind == OMPC_update)
6620 ? diag::err_omp_atomic_update_not_expression_statement
6621 : diag::err_omp_atomic_not_expression_statement,
6622 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00006623 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006624 if (!CurContext->isDependentContext()) {
6625 E = Checker.getExpr();
6626 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006627 UE = Checker.getUpdateExpr();
6628 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00006629 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006630 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006631 enum {
6632 NotAnAssignmentOp,
6633 NotACompoundStatement,
6634 NotTwoSubstatements,
6635 NotASpecificExpression,
6636 NoError
6637 } ErrorFound = NoError;
6638 SourceLocation ErrorLoc, NoteLoc;
6639 SourceRange ErrorRange, NoteRange;
Alexey Bataeve3727102018-04-18 15:57:46 +00006640 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006641 // If clause is a capture:
6642 // v = x++;
6643 // v = x--;
6644 // v = ++x;
6645 // v = --x;
6646 // v = x binop= expr;
6647 // v = x = x binop expr;
6648 // v = x = expr binop x;
Alexey Bataeve3727102018-04-18 15:57:46 +00006649 const auto *AtomicBinOp =
Alexey Bataevb78ca832015-04-01 03:33:17 +00006650 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6651 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6652 V = AtomicBinOp->getLHS();
6653 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6654 OpenMPAtomicUpdateChecker Checker(*this);
6655 if (Checker.checkStatement(
6656 Body, diag::err_omp_atomic_capture_not_expression_statement,
6657 diag::note_omp_atomic_update))
6658 return StmtError();
6659 E = Checker.getExpr();
6660 X = Checker.getX();
6661 UE = Checker.getUpdateExpr();
6662 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6663 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00006664 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006665 ErrorLoc = AtomicBody->getExprLoc();
6666 ErrorRange = AtomicBody->getSourceRange();
6667 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6668 : AtomicBody->getExprLoc();
6669 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6670 : AtomicBody->getSourceRange();
6671 ErrorFound = NotAnAssignmentOp;
6672 }
6673 if (ErrorFound != NoError) {
6674 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6675 << ErrorRange;
6676 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6677 return StmtError();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006678 }
Alexey Bataeve3727102018-04-18 15:57:46 +00006679 if (CurContext->isDependentContext())
6680 UE = V = E = X = nullptr;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006681 } else {
6682 // If clause is a capture:
6683 // { v = x; x = expr; }
6684 // { v = x; x++; }
6685 // { v = x; x--; }
6686 // { v = x; ++x; }
6687 // { v = x; --x; }
6688 // { v = x; x binop= expr; }
6689 // { v = x; x = x binop expr; }
6690 // { v = x; x = expr binop x; }
6691 // { x++; v = x; }
6692 // { x--; v = x; }
6693 // { ++x; v = x; }
6694 // { --x; v = x; }
6695 // { x binop= expr; v = x; }
6696 // { x = x binop expr; v = x; }
6697 // { x = expr binop x; v = x; }
6698 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6699 // Check that this is { expr1; expr2; }
6700 if (CS->size() == 2) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006701 Stmt *First = CS->body_front();
6702 Stmt *Second = CS->body_back();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006703 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6704 First = EWC->getSubExpr()->IgnoreParenImpCasts();
6705 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6706 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6707 // Need to find what subexpression is 'v' and what is 'x'.
6708 OpenMPAtomicUpdateChecker Checker(*this);
6709 bool IsUpdateExprFound = !Checker.checkStatement(Second);
6710 BinaryOperator *BinOp = nullptr;
6711 if (IsUpdateExprFound) {
6712 BinOp = dyn_cast<BinaryOperator>(First);
6713 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6714 }
6715 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6716 // { v = x; x++; }
6717 // { v = x; x--; }
6718 // { v = x; ++x; }
6719 // { v = x; --x; }
6720 // { v = x; x binop= expr; }
6721 // { v = x; x = x binop expr; }
6722 // { v = x; x = expr binop x; }
6723 // Check that the first expression has form v = x.
Alexey Bataeve3727102018-04-18 15:57:46 +00006724 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006725 llvm::FoldingSetNodeID XId, PossibleXId;
6726 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6727 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6728 IsUpdateExprFound = XId == PossibleXId;
6729 if (IsUpdateExprFound) {
6730 V = BinOp->getLHS();
6731 X = Checker.getX();
6732 E = Checker.getExpr();
6733 UE = Checker.getUpdateExpr();
6734 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006735 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006736 }
6737 }
6738 if (!IsUpdateExprFound) {
6739 IsUpdateExprFound = !Checker.checkStatement(First);
6740 BinOp = nullptr;
6741 if (IsUpdateExprFound) {
6742 BinOp = dyn_cast<BinaryOperator>(Second);
6743 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6744 }
6745 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6746 // { x++; v = x; }
6747 // { x--; v = x; }
6748 // { ++x; v = x; }
6749 // { --x; v = x; }
6750 // { x binop= expr; v = x; }
6751 // { x = x binop expr; v = x; }
6752 // { x = expr binop x; v = x; }
6753 // Check that the second expression has form v = x.
Alexey Bataeve3727102018-04-18 15:57:46 +00006754 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006755 llvm::FoldingSetNodeID XId, PossibleXId;
6756 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6757 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6758 IsUpdateExprFound = XId == PossibleXId;
6759 if (IsUpdateExprFound) {
6760 V = BinOp->getLHS();
6761 X = Checker.getX();
6762 E = Checker.getExpr();
6763 UE = Checker.getUpdateExpr();
6764 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006765 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006766 }
6767 }
6768 }
6769 if (!IsUpdateExprFound) {
6770 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00006771 auto *FirstExpr = dyn_cast<Expr>(First);
6772 auto *SecondExpr = dyn_cast<Expr>(Second);
6773 if (!FirstExpr || !SecondExpr ||
6774 !(FirstExpr->isInstantiationDependent() ||
6775 SecondExpr->isInstantiationDependent())) {
6776 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6777 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006778 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00006779 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006780 : First->getBeginLoc();
Alexey Bataev5a195472015-09-04 12:55:50 +00006781 NoteRange = ErrorRange = FirstBinOp
6782 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00006783 : SourceRange(ErrorLoc, ErrorLoc);
6784 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006785 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6786 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6787 ErrorFound = NotAnAssignmentOp;
6788 NoteLoc = ErrorLoc = SecondBinOp
6789 ? SecondBinOp->getOperatorLoc()
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006790 : Second->getBeginLoc();
Alexey Bataev5a195472015-09-04 12:55:50 +00006791 NoteRange = ErrorRange =
6792 SecondBinOp ? SecondBinOp->getSourceRange()
6793 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00006794 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +00006795 Expr *PossibleXRHSInFirst =
Alexey Bataev5a195472015-09-04 12:55:50 +00006796 FirstBinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00006797 Expr *PossibleXLHSInSecond =
Alexey Bataev5a195472015-09-04 12:55:50 +00006798 SecondBinOp->getLHS()->IgnoreParenImpCasts();
6799 llvm::FoldingSetNodeID X1Id, X2Id;
6800 PossibleXRHSInFirst->Profile(X1Id, Context,
6801 /*Canonical=*/true);
6802 PossibleXLHSInSecond->Profile(X2Id, Context,
6803 /*Canonical=*/true);
6804 IsUpdateExprFound = X1Id == X2Id;
6805 if (IsUpdateExprFound) {
6806 V = FirstBinOp->getLHS();
6807 X = SecondBinOp->getLHS();
6808 E = SecondBinOp->getRHS();
6809 UE = nullptr;
6810 IsXLHSInRHSPart = false;
6811 IsPostfixUpdate = true;
6812 } else {
6813 ErrorFound = NotASpecificExpression;
6814 ErrorLoc = FirstBinOp->getExprLoc();
6815 ErrorRange = FirstBinOp->getSourceRange();
6816 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6817 NoteRange = SecondBinOp->getRHS()->getSourceRange();
6818 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006819 }
6820 }
6821 }
6822 }
6823 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006824 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006825 NoteRange = ErrorRange =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006826 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
Alexey Bataevb78ca832015-04-01 03:33:17 +00006827 ErrorFound = NotTwoSubstatements;
6828 }
6829 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006830 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006831 NoteRange = ErrorRange =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006832 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
Alexey Bataevb78ca832015-04-01 03:33:17 +00006833 ErrorFound = NotACompoundStatement;
6834 }
6835 if (ErrorFound != NoError) {
6836 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6837 << ErrorRange;
6838 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6839 return StmtError();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006840 }
Alexey Bataeve3727102018-04-18 15:57:46 +00006841 if (CurContext->isDependentContext())
6842 UE = V = E = X = nullptr;
Alexey Bataev459dec02014-07-24 06:46:57 +00006843 }
Alexey Bataevdea47612014-07-23 07:46:59 +00006844 }
Alexey Bataev0162e452014-07-22 10:10:35 +00006845
Reid Kleckner87a31802018-03-12 21:43:02 +00006846 setFunctionHasBranchProtectedScope();
Alexey Bataev0162e452014-07-22 10:10:35 +00006847
Alexey Bataev62cec442014-11-18 10:14:22 +00006848 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00006849 X, V, E, UE, IsXLHSInRHSPart,
6850 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00006851}
6852
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006853StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6854 Stmt *AStmt,
6855 SourceLocation StartLoc,
6856 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006857 if (!AStmt)
6858 return StmtError();
6859
Alexey Bataeve3727102018-04-18 15:57:46 +00006860 auto *CS = cast<CapturedStmt>(AStmt);
Samuel Antao4af1b7b2015-12-02 17:44:43 +00006861 // 1.2.2 OpenMP Language Terminology
6862 // Structured block - An executable statement with a single entry at the
6863 // top and a single exit at the bottom.
6864 // The point of exit cannot be a branch out of the structured block.
6865 // longjmp() and throw() must not violate the entry/exit criteria.
6866 CS->getCapturedDecl()->setNothrow();
Alexey Bataev8451efa2018-01-15 19:06:12 +00006867 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target);
6868 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6869 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6870 // 1.2.2 OpenMP Language Terminology
6871 // Structured block - An executable statement with a single entry at the
6872 // top and a single exit at the bottom.
6873 // The point of exit cannot be a branch out of the structured block.
6874 // longjmp() and throw() must not violate the entry/exit criteria.
6875 CS->getCapturedDecl()->setNothrow();
6876 }
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006877
Alexey Bataev13314bf2014-10-09 04:18:56 +00006878 // OpenMP [2.16, Nesting of Regions]
6879 // If specified, a teams construct must be contained within a target
6880 // construct. That target construct must contain no statements or directives
6881 // outside of the teams construct.
6882 if (DSAStack->hasInnerTeamsRegion()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006883 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true);
Alexey Bataev13314bf2014-10-09 04:18:56 +00006884 bool OMPTeamsFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006885 if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00006886 auto I = CS->body_begin();
6887 while (I != CS->body_end()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006888 const auto *OED = dyn_cast<OMPExecutableDirective>(*I);
Alexey Bataev13314bf2014-10-09 04:18:56 +00006889 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6890 OMPTeamsFound = false;
6891 break;
6892 }
6893 ++I;
6894 }
6895 assert(I != CS->body_end() && "Not found statement");
6896 S = *I;
Kelvin Li3834dce2016-06-27 19:15:43 +00006897 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +00006898 const auto *OED = dyn_cast<OMPExecutableDirective>(S);
Kelvin Li3834dce2016-06-27 19:15:43 +00006899 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
Alexey Bataev13314bf2014-10-09 04:18:56 +00006900 }
6901 if (!OMPTeamsFound) {
6902 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
6903 Diag(DSAStack->getInnerTeamsRegionLoc(),
6904 diag::note_omp_nested_teams_construct_here);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006905 Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here)
Alexey Bataev13314bf2014-10-09 04:18:56 +00006906 << isa<OMPExecutableDirective>(S);
6907 return StmtError();
6908 }
6909 }
6910
Reid Kleckner87a31802018-03-12 21:43:02 +00006911 setFunctionHasBranchProtectedScope();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006912
6913 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6914}
6915
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006916StmtResult
6917Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
6918 Stmt *AStmt, SourceLocation StartLoc,
6919 SourceLocation EndLoc) {
6920 if (!AStmt)
6921 return StmtError();
6922
Alexey Bataeve3727102018-04-18 15:57:46 +00006923 auto *CS = cast<CapturedStmt>(AStmt);
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006924 // 1.2.2 OpenMP Language Terminology
6925 // Structured block - An executable statement with a single entry at the
6926 // top and a single exit at the bottom.
6927 // The point of exit cannot be a branch out of the structured block.
6928 // longjmp() and throw() must not violate the entry/exit criteria.
6929 CS->getCapturedDecl()->setNothrow();
Alexey Bataev8451efa2018-01-15 19:06:12 +00006930 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel);
6931 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6932 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6933 // 1.2.2 OpenMP Language Terminology
6934 // Structured block - An executable statement with a single entry at the
6935 // top and a single exit at the bottom.
6936 // The point of exit cannot be a branch out of the structured block.
6937 // longjmp() and throw() must not violate the entry/exit criteria.
6938 CS->getCapturedDecl()->setNothrow();
6939 }
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006940
Reid Kleckner87a31802018-03-12 21:43:02 +00006941 setFunctionHasBranchProtectedScope();
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006942
6943 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6944 AStmt);
6945}
6946
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006947StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
6948 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00006949 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006950 if (!AStmt)
6951 return StmtError();
6952
Alexey Bataeve3727102018-04-18 15:57:46 +00006953 auto *CS = cast<CapturedStmt>(AStmt);
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006954 // 1.2.2 OpenMP Language Terminology
6955 // Structured block - An executable statement with a single entry at the
6956 // top and a single exit at the bottom.
6957 // The point of exit cannot be a branch out of the structured block.
6958 // longjmp() and throw() must not violate the entry/exit criteria.
6959 CS->getCapturedDecl()->setNothrow();
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00006960 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
6961 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6962 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6963 // 1.2.2 OpenMP Language Terminology
6964 // Structured block - An executable statement with a single entry at the
6965 // top and a single exit at the bottom.
6966 // The point of exit cannot be a branch out of the structured block.
6967 // longjmp() and throw() must not violate the entry/exit criteria.
6968 CS->getCapturedDecl()->setNothrow();
6969 }
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006970
6971 OMPLoopDirective::HelperExprs B;
6972 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6973 // define the nested loops number.
6974 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00006975 checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00006976 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006977 VarsWithImplicitDSA, B);
6978 if (NestedLoopCount == 0)
6979 return StmtError();
6980
6981 assert((CurContext->isDependentContext() || B.builtAll()) &&
6982 "omp target parallel for loop exprs were not built");
6983
6984 if (!CurContext->isDependentContext()) {
6985 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00006986 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00006987 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006988 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006989 B.NumIterations, *this, CurScope,
6990 DSAStack))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006991 return StmtError();
6992 }
6993 }
6994
Reid Kleckner87a31802018-03-12 21:43:02 +00006995 setFunctionHasBranchProtectedScope();
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006996 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
6997 NestedLoopCount, Clauses, AStmt,
6998 B, DSAStack->isCancelRegion());
6999}
7000
Alexey Bataev95b64a92017-05-30 16:00:04 +00007001/// Check for existence of a map clause in the list of clauses.
7002static bool hasClauses(ArrayRef<OMPClause *> Clauses,
7003 const OpenMPClauseKind K) {
7004 return llvm::any_of(
7005 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; });
7006}
Samuel Antaodf67fc42016-01-19 19:15:56 +00007007
Alexey Bataev95b64a92017-05-30 16:00:04 +00007008template <typename... Params>
7009static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K,
7010 const Params... ClauseTypes) {
7011 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...);
Samuel Antaodf67fc42016-01-19 19:15:56 +00007012}
7013
Michael Wong65f367f2015-07-21 13:44:28 +00007014StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
7015 Stmt *AStmt,
7016 SourceLocation StartLoc,
7017 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007018 if (!AStmt)
7019 return StmtError();
7020
7021 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7022
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00007023 // OpenMP [2.10.1, Restrictions, p. 97]
7024 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00007025 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr)) {
7026 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
7027 << "'map' or 'use_device_ptr'"
David Majnemer9d168222016-08-05 17:44:54 +00007028 << getOpenMPDirectiveName(OMPD_target_data);
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00007029 return StmtError();
7030 }
7031
Reid Kleckner87a31802018-03-12 21:43:02 +00007032 setFunctionHasBranchProtectedScope();
Michael Wong65f367f2015-07-21 13:44:28 +00007033
7034 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
7035 AStmt);
7036}
7037
Samuel Antaodf67fc42016-01-19 19:15:56 +00007038StmtResult
7039Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
7040 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00007041 SourceLocation EndLoc, Stmt *AStmt) {
7042 if (!AStmt)
7043 return StmtError();
7044
Alexey Bataeve3727102018-04-18 15:57:46 +00007045 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00007046 // 1.2.2 OpenMP Language Terminology
7047 // Structured block - An executable statement with a single entry at the
7048 // top and a single exit at the bottom.
7049 // The point of exit cannot be a branch out of the structured block.
7050 // longjmp() and throw() must not violate the entry/exit criteria.
7051 CS->getCapturedDecl()->setNothrow();
7052 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data);
7053 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7054 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7055 // 1.2.2 OpenMP Language Terminology
7056 // Structured block - An executable statement with a single entry at the
7057 // top and a single exit at the bottom.
7058 // The point of exit cannot be a branch out of the structured block.
7059 // longjmp() and throw() must not violate the entry/exit criteria.
7060 CS->getCapturedDecl()->setNothrow();
7061 }
7062
Samuel Antaodf67fc42016-01-19 19:15:56 +00007063 // OpenMP [2.10.2, Restrictions, p. 99]
7064 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00007065 if (!hasClauses(Clauses, OMPC_map)) {
7066 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
7067 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data);
Samuel Antaodf67fc42016-01-19 19:15:56 +00007068 return StmtError();
7069 }
7070
Alexey Bataev7828b252017-11-21 17:08:48 +00007071 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
7072 AStmt);
Samuel Antaodf67fc42016-01-19 19:15:56 +00007073}
7074
Samuel Antao72590762016-01-19 20:04:50 +00007075StmtResult
7076Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
7077 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00007078 SourceLocation EndLoc, Stmt *AStmt) {
7079 if (!AStmt)
7080 return StmtError();
7081
Alexey Bataeve3727102018-04-18 15:57:46 +00007082 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00007083 // 1.2.2 OpenMP Language Terminology
7084 // Structured block - An executable statement with a single entry at the
7085 // top and a single exit at the bottom.
7086 // The point of exit cannot be a branch out of the structured block.
7087 // longjmp() and throw() must not violate the entry/exit criteria.
7088 CS->getCapturedDecl()->setNothrow();
7089 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data);
7090 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7091 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7092 // 1.2.2 OpenMP Language Terminology
7093 // Structured block - An executable statement with a single entry at the
7094 // top and a single exit at the bottom.
7095 // The point of exit cannot be a branch out of the structured block.
7096 // longjmp() and throw() must not violate the entry/exit criteria.
7097 CS->getCapturedDecl()->setNothrow();
7098 }
7099
Samuel Antao72590762016-01-19 20:04:50 +00007100 // OpenMP [2.10.3, Restrictions, p. 102]
7101 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00007102 if (!hasClauses(Clauses, OMPC_map)) {
7103 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
7104 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data);
Samuel Antao72590762016-01-19 20:04:50 +00007105 return StmtError();
7106 }
7107
Alexey Bataev7828b252017-11-21 17:08:48 +00007108 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
7109 AStmt);
Samuel Antao72590762016-01-19 20:04:50 +00007110}
7111
Samuel Antao686c70c2016-05-26 17:30:50 +00007112StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
7113 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00007114 SourceLocation EndLoc,
7115 Stmt *AStmt) {
7116 if (!AStmt)
7117 return StmtError();
7118
Alexey Bataeve3727102018-04-18 15:57:46 +00007119 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00007120 // 1.2.2 OpenMP Language Terminology
7121 // Structured block - An executable statement with a single entry at the
7122 // top and a single exit at the bottom.
7123 // The point of exit cannot be a branch out of the structured block.
7124 // longjmp() and throw() must not violate the entry/exit criteria.
7125 CS->getCapturedDecl()->setNothrow();
7126 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update);
7127 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7128 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7129 // 1.2.2 OpenMP Language Terminology
7130 // Structured block - An executable statement with a single entry at the
7131 // top and a single exit at the bottom.
7132 // The point of exit cannot be a branch out of the structured block.
7133 // longjmp() and throw() must not violate the entry/exit criteria.
7134 CS->getCapturedDecl()->setNothrow();
7135 }
7136
Alexey Bataev95b64a92017-05-30 16:00:04 +00007137 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) {
Samuel Antao686c70c2016-05-26 17:30:50 +00007138 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
7139 return StmtError();
7140 }
Alexey Bataev7828b252017-11-21 17:08:48 +00007141 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses,
7142 AStmt);
Samuel Antao686c70c2016-05-26 17:30:50 +00007143}
7144
Alexey Bataev13314bf2014-10-09 04:18:56 +00007145StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
7146 Stmt *AStmt, SourceLocation StartLoc,
7147 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007148 if (!AStmt)
7149 return StmtError();
7150
Alexey Bataeve3727102018-04-18 15:57:46 +00007151 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev13314bf2014-10-09 04:18:56 +00007152 // 1.2.2 OpenMP Language Terminology
7153 // Structured block - An executable statement with a single entry at the
7154 // top and a single exit at the bottom.
7155 // The point of exit cannot be a branch out of the structured block.
7156 // longjmp() and throw() must not violate the entry/exit criteria.
7157 CS->getCapturedDecl()->setNothrow();
7158
Reid Kleckner87a31802018-03-12 21:43:02 +00007159 setFunctionHasBranchProtectedScope();
Alexey Bataev13314bf2014-10-09 04:18:56 +00007160
Alexey Bataevceabd412017-11-30 18:01:54 +00007161 DSAStack->setParentTeamsRegionLoc(StartLoc);
7162
Alexey Bataev13314bf2014-10-09 04:18:56 +00007163 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
7164}
7165
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007166StmtResult
7167Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
7168 SourceLocation EndLoc,
7169 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007170 if (DSAStack->isParentNowaitRegion()) {
7171 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
7172 return StmtError();
7173 }
7174 if (DSAStack->isParentOrderedRegion()) {
7175 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
7176 return StmtError();
7177 }
7178 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
7179 CancelRegion);
7180}
7181
Alexey Bataev87933c72015-09-18 08:07:34 +00007182StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
7183 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00007184 SourceLocation EndLoc,
7185 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev80909872015-07-02 11:25:17 +00007186 if (DSAStack->isParentNowaitRegion()) {
7187 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
7188 return StmtError();
7189 }
7190 if (DSAStack->isParentOrderedRegion()) {
7191 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
7192 return StmtError();
7193 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00007194 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00007195 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
7196 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00007197}
7198
Alexey Bataev382967a2015-12-08 12:06:20 +00007199static bool checkGrainsizeNumTasksClauses(Sema &S,
7200 ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007201 const OMPClause *PrevClause = nullptr;
Alexey Bataev382967a2015-12-08 12:06:20 +00007202 bool ErrorFound = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00007203 for (const OMPClause *C : Clauses) {
Alexey Bataev382967a2015-12-08 12:06:20 +00007204 if (C->getClauseKind() == OMPC_grainsize ||
7205 C->getClauseKind() == OMPC_num_tasks) {
7206 if (!PrevClause)
7207 PrevClause = C;
7208 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007209 S.Diag(C->getBeginLoc(),
Alexey Bataev382967a2015-12-08 12:06:20 +00007210 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
7211 << getOpenMPClauseName(C->getClauseKind())
7212 << getOpenMPClauseName(PrevClause->getClauseKind());
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007213 S.Diag(PrevClause->getBeginLoc(),
Alexey Bataev382967a2015-12-08 12:06:20 +00007214 diag::note_omp_previous_grainsize_num_tasks)
7215 << getOpenMPClauseName(PrevClause->getClauseKind());
7216 ErrorFound = true;
7217 }
7218 }
7219 }
7220 return ErrorFound;
7221}
7222
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007223static bool checkReductionClauseWithNogroup(Sema &S,
7224 ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007225 const OMPClause *ReductionClause = nullptr;
7226 const OMPClause *NogroupClause = nullptr;
7227 for (const OMPClause *C : Clauses) {
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007228 if (C->getClauseKind() == OMPC_reduction) {
7229 ReductionClause = C;
7230 if (NogroupClause)
7231 break;
7232 continue;
7233 }
7234 if (C->getClauseKind() == OMPC_nogroup) {
7235 NogroupClause = C;
7236 if (ReductionClause)
7237 break;
7238 continue;
7239 }
7240 }
7241 if (ReductionClause && NogroupClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007242 S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup)
7243 << SourceRange(NogroupClause->getBeginLoc(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007244 NogroupClause->getEndLoc());
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007245 return true;
7246 }
7247 return false;
7248}
7249
Alexey Bataev49f6e782015-12-01 04:18:41 +00007250StmtResult Sema::ActOnOpenMPTaskLoopDirective(
7251 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007252 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00007253 if (!AStmt)
7254 return StmtError();
7255
7256 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7257 OMPLoopDirective::HelperExprs B;
7258 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7259 // define the nested loops number.
7260 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007261 checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007262 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00007263 VarsWithImplicitDSA, B);
7264 if (NestedLoopCount == 0)
7265 return StmtError();
7266
7267 assert((CurContext->isDependentContext() || B.builtAll()) &&
7268 "omp for loop exprs were not built");
7269
Alexey Bataev382967a2015-12-08 12:06:20 +00007270 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7271 // The grainsize clause and num_tasks clause are mutually exclusive and may
7272 // not appear on the same taskloop directive.
7273 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7274 return StmtError();
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007275 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7276 // If a reduction clause is present on the taskloop directive, the nogroup
7277 // clause must not be specified.
7278 if (checkReductionClauseWithNogroup(*this, Clauses))
7279 return StmtError();
Alexey Bataev382967a2015-12-08 12:06:20 +00007280
Reid Kleckner87a31802018-03-12 21:43:02 +00007281 setFunctionHasBranchProtectedScope();
Alexey Bataev49f6e782015-12-01 04:18:41 +00007282 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
7283 NestedLoopCount, Clauses, AStmt, B);
7284}
7285
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007286StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
7287 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007288 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007289 if (!AStmt)
7290 return StmtError();
7291
7292 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7293 OMPLoopDirective::HelperExprs B;
7294 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7295 // define the nested loops number.
7296 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007297 checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007298 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
7299 VarsWithImplicitDSA, B);
7300 if (NestedLoopCount == 0)
7301 return StmtError();
7302
7303 assert((CurContext->isDependentContext() || B.builtAll()) &&
7304 "omp for loop exprs were not built");
7305
Alexey Bataev5a3af132016-03-29 08:58:54 +00007306 if (!CurContext->isDependentContext()) {
7307 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007308 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007309 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007310 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007311 B.NumIterations, *this, CurScope,
7312 DSAStack))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007313 return StmtError();
7314 }
7315 }
7316
Alexey Bataev382967a2015-12-08 12:06:20 +00007317 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7318 // The grainsize clause and num_tasks clause are mutually exclusive and may
7319 // not appear on the same taskloop directive.
7320 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7321 return StmtError();
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007322 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7323 // If a reduction clause is present on the taskloop directive, the nogroup
7324 // clause must not be specified.
7325 if (checkReductionClauseWithNogroup(*this, Clauses))
7326 return StmtError();
Alexey Bataev438388c2017-11-22 18:34:02 +00007327 if (checkSimdlenSafelenSpecified(*this, Clauses))
7328 return StmtError();
Alexey Bataev382967a2015-12-08 12:06:20 +00007329
Reid Kleckner87a31802018-03-12 21:43:02 +00007330 setFunctionHasBranchProtectedScope();
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007331 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
7332 NestedLoopCount, Clauses, AStmt, B);
7333}
7334
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007335StmtResult Sema::ActOnOpenMPDistributeDirective(
7336 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007337 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007338 if (!AStmt)
7339 return StmtError();
7340
7341 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7342 OMPLoopDirective::HelperExprs B;
7343 // In presence of clause 'collapse' with number of loops, it will
7344 // define the nested loops number.
7345 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007346 checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007347 nullptr /*ordered not a clause on distribute*/, AStmt,
7348 *this, *DSAStack, VarsWithImplicitDSA, B);
7349 if (NestedLoopCount == 0)
7350 return StmtError();
7351
7352 assert((CurContext->isDependentContext() || B.builtAll()) &&
7353 "omp for loop exprs were not built");
7354
Reid Kleckner87a31802018-03-12 21:43:02 +00007355 setFunctionHasBranchProtectedScope();
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007356 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
7357 NestedLoopCount, Clauses, AStmt, B);
7358}
7359
Carlo Bertolli9925f152016-06-27 14:55:37 +00007360StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
7361 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007362 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Carlo Bertolli9925f152016-06-27 14:55:37 +00007363 if (!AStmt)
7364 return StmtError();
7365
Alexey Bataeve3727102018-04-18 15:57:46 +00007366 auto *CS = cast<CapturedStmt>(AStmt);
Carlo Bertolli9925f152016-06-27 14:55:37 +00007367 // 1.2.2 OpenMP Language Terminology
7368 // Structured block - An executable statement with a single entry at the
7369 // top and a single exit at the bottom.
7370 // The point of exit cannot be a branch out of the structured block.
7371 // longjmp() and throw() must not violate the entry/exit criteria.
7372 CS->getCapturedDecl()->setNothrow();
Alexey Bataev7f96c372017-11-22 17:19:31 +00007373 for (int ThisCaptureLevel =
7374 getOpenMPCaptureLevels(OMPD_distribute_parallel_for);
7375 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7376 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7377 // 1.2.2 OpenMP Language Terminology
7378 // Structured block - An executable statement with a single entry at the
7379 // top and a single exit at the bottom.
7380 // The point of exit cannot be a branch out of the structured block.
7381 // longjmp() and throw() must not violate the entry/exit criteria.
7382 CS->getCapturedDecl()->setNothrow();
7383 }
Carlo Bertolli9925f152016-06-27 14:55:37 +00007384
7385 OMPLoopDirective::HelperExprs B;
7386 // In presence of clause 'collapse' with number of loops, it will
7387 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007388 unsigned NestedLoopCount = checkOpenMPLoop(
Carlo Bertolli9925f152016-06-27 14:55:37 +00007389 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataev7f96c372017-11-22 17:19:31 +00007390 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Carlo Bertolli9925f152016-06-27 14:55:37 +00007391 VarsWithImplicitDSA, B);
7392 if (NestedLoopCount == 0)
7393 return StmtError();
7394
7395 assert((CurContext->isDependentContext() || B.builtAll()) &&
7396 "omp for loop exprs were not built");
7397
Reid Kleckner87a31802018-03-12 21:43:02 +00007398 setFunctionHasBranchProtectedScope();
Carlo Bertolli9925f152016-06-27 14:55:37 +00007399 return OMPDistributeParallelForDirective::Create(
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00007400 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7401 DSAStack->isCancelRegion());
Carlo Bertolli9925f152016-06-27 14:55:37 +00007402}
7403
Kelvin Li4a39add2016-07-05 05:00:15 +00007404StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
7405 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007406 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li4a39add2016-07-05 05:00:15 +00007407 if (!AStmt)
7408 return StmtError();
7409
Alexey Bataeve3727102018-04-18 15:57:46 +00007410 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li4a39add2016-07-05 05:00:15 +00007411 // 1.2.2 OpenMP Language Terminology
7412 // Structured block - An executable statement with a single entry at the
7413 // top and a single exit at the bottom.
7414 // The point of exit cannot be a branch out of the structured block.
7415 // longjmp() and throw() must not violate the entry/exit criteria.
7416 CS->getCapturedDecl()->setNothrow();
Alexey Bataev974acd62017-11-27 19:38:52 +00007417 for (int ThisCaptureLevel =
7418 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd);
7419 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7420 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7421 // 1.2.2 OpenMP Language Terminology
7422 // Structured block - An executable statement with a single entry at the
7423 // top and a single exit at the bottom.
7424 // The point of exit cannot be a branch out of the structured block.
7425 // longjmp() and throw() must not violate the entry/exit criteria.
7426 CS->getCapturedDecl()->setNothrow();
7427 }
Kelvin Li4a39add2016-07-05 05:00:15 +00007428
7429 OMPLoopDirective::HelperExprs B;
7430 // In presence of clause 'collapse' with number of loops, it will
7431 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007432 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li4a39add2016-07-05 05:00:15 +00007433 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev974acd62017-11-27 19:38:52 +00007434 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li4a39add2016-07-05 05:00:15 +00007435 VarsWithImplicitDSA, B);
7436 if (NestedLoopCount == 0)
7437 return StmtError();
7438
7439 assert((CurContext->isDependentContext() || B.builtAll()) &&
7440 "omp for loop exprs were not built");
7441
Alexey Bataev438388c2017-11-22 18:34:02 +00007442 if (!CurContext->isDependentContext()) {
7443 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007444 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00007445 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7446 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7447 B.NumIterations, *this, CurScope,
7448 DSAStack))
7449 return StmtError();
7450 }
7451 }
7452
Kelvin Lic5609492016-07-15 04:39:07 +00007453 if (checkSimdlenSafelenSpecified(*this, Clauses))
7454 return StmtError();
7455
Reid Kleckner87a31802018-03-12 21:43:02 +00007456 setFunctionHasBranchProtectedScope();
Kelvin Li4a39add2016-07-05 05:00:15 +00007457 return OMPDistributeParallelForSimdDirective::Create(
7458 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7459}
7460
Kelvin Li787f3fc2016-07-06 04:45:38 +00007461StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
7462 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007463 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li787f3fc2016-07-06 04:45:38 +00007464 if (!AStmt)
7465 return StmtError();
7466
Alexey Bataeve3727102018-04-18 15:57:46 +00007467 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li787f3fc2016-07-06 04:45:38 +00007468 // 1.2.2 OpenMP Language Terminology
7469 // Structured block - An executable statement with a single entry at the
7470 // top and a single exit at the bottom.
7471 // The point of exit cannot be a branch out of the structured block.
7472 // longjmp() and throw() must not violate the entry/exit criteria.
7473 CS->getCapturedDecl()->setNothrow();
Alexey Bataev617db5f2017-12-04 15:38:33 +00007474 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd);
7475 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7476 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7477 // 1.2.2 OpenMP Language Terminology
7478 // Structured block - An executable statement with a single entry at the
7479 // top and a single exit at the bottom.
7480 // The point of exit cannot be a branch out of the structured block.
7481 // longjmp() and throw() must not violate the entry/exit criteria.
7482 CS->getCapturedDecl()->setNothrow();
7483 }
Kelvin Li787f3fc2016-07-06 04:45:38 +00007484
7485 OMPLoopDirective::HelperExprs B;
7486 // In presence of clause 'collapse' with number of loops, it will
7487 // define the nested loops number.
7488 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007489 checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev617db5f2017-12-04 15:38:33 +00007490 nullptr /*ordered not a clause on distribute*/, CS, *this,
7491 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li787f3fc2016-07-06 04:45:38 +00007492 if (NestedLoopCount == 0)
7493 return StmtError();
7494
7495 assert((CurContext->isDependentContext() || B.builtAll()) &&
7496 "omp for loop exprs were not built");
7497
Alexey Bataev438388c2017-11-22 18:34:02 +00007498 if (!CurContext->isDependentContext()) {
7499 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007500 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00007501 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7502 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7503 B.NumIterations, *this, CurScope,
7504 DSAStack))
7505 return StmtError();
7506 }
7507 }
7508
Kelvin Lic5609492016-07-15 04:39:07 +00007509 if (checkSimdlenSafelenSpecified(*this, Clauses))
7510 return StmtError();
7511
Reid Kleckner87a31802018-03-12 21:43:02 +00007512 setFunctionHasBranchProtectedScope();
Kelvin Li787f3fc2016-07-06 04:45:38 +00007513 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
7514 NestedLoopCount, Clauses, AStmt, B);
7515}
7516
Kelvin Lia579b912016-07-14 02:54:56 +00007517StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
7518 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007519 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Lia579b912016-07-14 02:54:56 +00007520 if (!AStmt)
7521 return StmtError();
7522
Alexey Bataeve3727102018-04-18 15:57:46 +00007523 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Lia579b912016-07-14 02:54:56 +00007524 // 1.2.2 OpenMP Language Terminology
7525 // Structured block - An executable statement with a single entry at the
7526 // top and a single exit at the bottom.
7527 // The point of exit cannot be a branch out of the structured block.
7528 // longjmp() and throw() must not violate the entry/exit criteria.
7529 CS->getCapturedDecl()->setNothrow();
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007530 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
7531 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7532 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7533 // 1.2.2 OpenMP Language Terminology
7534 // Structured block - An executable statement with a single entry at the
7535 // top and a single exit at the bottom.
7536 // The point of exit cannot be a branch out of the structured block.
7537 // longjmp() and throw() must not violate the entry/exit criteria.
7538 CS->getCapturedDecl()->setNothrow();
7539 }
Kelvin Lia579b912016-07-14 02:54:56 +00007540
7541 OMPLoopDirective::HelperExprs B;
7542 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7543 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007544 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Lia579b912016-07-14 02:54:56 +00007545 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007546 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Kelvin Lia579b912016-07-14 02:54:56 +00007547 VarsWithImplicitDSA, B);
7548 if (NestedLoopCount == 0)
7549 return StmtError();
7550
7551 assert((CurContext->isDependentContext() || B.builtAll()) &&
7552 "omp target parallel for simd loop exprs were not built");
7553
7554 if (!CurContext->isDependentContext()) {
7555 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007556 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007557 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Lia579b912016-07-14 02:54:56 +00007558 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7559 B.NumIterations, *this, CurScope,
7560 DSAStack))
7561 return StmtError();
7562 }
7563 }
Kelvin Lic5609492016-07-15 04:39:07 +00007564 if (checkSimdlenSafelenSpecified(*this, Clauses))
Kelvin Lia579b912016-07-14 02:54:56 +00007565 return StmtError();
7566
Reid Kleckner87a31802018-03-12 21:43:02 +00007567 setFunctionHasBranchProtectedScope();
Kelvin Lia579b912016-07-14 02:54:56 +00007568 return OMPTargetParallelForSimdDirective::Create(
7569 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7570}
7571
Kelvin Li986330c2016-07-20 22:57:10 +00007572StmtResult Sema::ActOnOpenMPTargetSimdDirective(
7573 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007574 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li986330c2016-07-20 22:57:10 +00007575 if (!AStmt)
7576 return StmtError();
7577
Alexey Bataeve3727102018-04-18 15:57:46 +00007578 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li986330c2016-07-20 22:57:10 +00007579 // 1.2.2 OpenMP Language Terminology
7580 // Structured block - An executable statement with a single entry at the
7581 // top and a single exit at the bottom.
7582 // The point of exit cannot be a branch out of the structured block.
7583 // longjmp() and throw() must not violate the entry/exit criteria.
7584 CS->getCapturedDecl()->setNothrow();
Alexey Bataevf8365372017-11-17 17:57:25 +00007585 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd);
7586 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7587 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7588 // 1.2.2 OpenMP Language Terminology
7589 // Structured block - An executable statement with a single entry at the
7590 // top and a single exit at the bottom.
7591 // The point of exit cannot be a branch out of the structured block.
7592 // longjmp() and throw() must not violate the entry/exit criteria.
7593 CS->getCapturedDecl()->setNothrow();
7594 }
7595
Kelvin Li986330c2016-07-20 22:57:10 +00007596 OMPLoopDirective::HelperExprs B;
7597 // In presence of clause 'collapse' with number of loops, it will define the
7598 // nested loops number.
David Majnemer9d168222016-08-05 17:44:54 +00007599 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007600 checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
Alexey Bataevf8365372017-11-17 17:57:25 +00007601 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Kelvin Li986330c2016-07-20 22:57:10 +00007602 VarsWithImplicitDSA, B);
7603 if (NestedLoopCount == 0)
7604 return StmtError();
7605
7606 assert((CurContext->isDependentContext() || B.builtAll()) &&
7607 "omp target simd loop exprs were not built");
7608
7609 if (!CurContext->isDependentContext()) {
7610 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007611 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007612 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Li986330c2016-07-20 22:57:10 +00007613 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7614 B.NumIterations, *this, CurScope,
7615 DSAStack))
7616 return StmtError();
7617 }
7618 }
7619
7620 if (checkSimdlenSafelenSpecified(*this, Clauses))
7621 return StmtError();
7622
Reid Kleckner87a31802018-03-12 21:43:02 +00007623 setFunctionHasBranchProtectedScope();
Kelvin Li986330c2016-07-20 22:57:10 +00007624 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
7625 NestedLoopCount, Clauses, AStmt, B);
7626}
7627
Kelvin Li02532872016-08-05 14:37:37 +00007628StmtResult Sema::ActOnOpenMPTeamsDistributeDirective(
7629 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007630 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li02532872016-08-05 14:37:37 +00007631 if (!AStmt)
7632 return StmtError();
7633
Alexey Bataeve3727102018-04-18 15:57:46 +00007634 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li02532872016-08-05 14:37:37 +00007635 // 1.2.2 OpenMP Language Terminology
7636 // Structured block - An executable statement with a single entry at the
7637 // top and a single exit at the bottom.
7638 // The point of exit cannot be a branch out of the structured block.
7639 // longjmp() and throw() must not violate the entry/exit criteria.
7640 CS->getCapturedDecl()->setNothrow();
Alexey Bataev95c6dd42017-11-29 15:14:16 +00007641 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute);
7642 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7643 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7644 // 1.2.2 OpenMP Language Terminology
7645 // Structured block - An executable statement with a single entry at the
7646 // top and a single exit at the bottom.
7647 // The point of exit cannot be a branch out of the structured block.
7648 // longjmp() and throw() must not violate the entry/exit criteria.
7649 CS->getCapturedDecl()->setNothrow();
7650 }
Kelvin Li02532872016-08-05 14:37:37 +00007651
7652 OMPLoopDirective::HelperExprs B;
7653 // In presence of clause 'collapse' with number of loops, it will
7654 // define the nested loops number.
7655 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007656 checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses),
Alexey Bataev95c6dd42017-11-29 15:14:16 +00007657 nullptr /*ordered not a clause on distribute*/, CS, *this,
7658 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li02532872016-08-05 14:37:37 +00007659 if (NestedLoopCount == 0)
7660 return StmtError();
7661
7662 assert((CurContext->isDependentContext() || B.builtAll()) &&
7663 "omp teams distribute loop exprs were not built");
7664
Reid Kleckner87a31802018-03-12 21:43:02 +00007665 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007666
7667 DSAStack->setParentTeamsRegionLoc(StartLoc);
7668
David Majnemer9d168222016-08-05 17:44:54 +00007669 return OMPTeamsDistributeDirective::Create(
7670 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Kelvin Li02532872016-08-05 14:37:37 +00007671}
7672
Kelvin Li4e325f72016-10-25 12:50:55 +00007673StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective(
7674 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007675 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li4e325f72016-10-25 12:50:55 +00007676 if (!AStmt)
7677 return StmtError();
7678
Alexey Bataeve3727102018-04-18 15:57:46 +00007679 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li4e325f72016-10-25 12:50:55 +00007680 // 1.2.2 OpenMP Language Terminology
7681 // Structured block - An executable statement with a single entry at the
7682 // top and a single exit at the bottom.
7683 // The point of exit cannot be a branch out of the structured block.
7684 // longjmp() and throw() must not violate the entry/exit criteria.
7685 CS->getCapturedDecl()->setNothrow();
Alexey Bataev999277a2017-12-06 14:31:09 +00007686 for (int ThisCaptureLevel =
7687 getOpenMPCaptureLevels(OMPD_teams_distribute_simd);
7688 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7689 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7690 // 1.2.2 OpenMP Language Terminology
7691 // Structured block - An executable statement with a single entry at the
7692 // top and a single exit at the bottom.
7693 // The point of exit cannot be a branch out of the structured block.
7694 // longjmp() and throw() must not violate the entry/exit criteria.
7695 CS->getCapturedDecl()->setNothrow();
7696 }
7697
Kelvin Li4e325f72016-10-25 12:50:55 +00007698
7699 OMPLoopDirective::HelperExprs B;
7700 // In presence of clause 'collapse' with number of loops, it will
7701 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007702 unsigned NestedLoopCount = checkOpenMPLoop(
Samuel Antao4c8035b2016-12-12 18:00:20 +00007703 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev999277a2017-12-06 14:31:09 +00007704 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Samuel Antao4c8035b2016-12-12 18:00:20 +00007705 VarsWithImplicitDSA, B);
Kelvin Li4e325f72016-10-25 12:50:55 +00007706
7707 if (NestedLoopCount == 0)
7708 return StmtError();
7709
7710 assert((CurContext->isDependentContext() || B.builtAll()) &&
7711 "omp teams distribute simd loop exprs were not built");
7712
7713 if (!CurContext->isDependentContext()) {
7714 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007715 for (OMPClause *C : Clauses) {
Kelvin Li4e325f72016-10-25 12:50:55 +00007716 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7717 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7718 B.NumIterations, *this, CurScope,
7719 DSAStack))
7720 return StmtError();
7721 }
7722 }
7723
7724 if (checkSimdlenSafelenSpecified(*this, Clauses))
7725 return StmtError();
7726
Reid Kleckner87a31802018-03-12 21:43:02 +00007727 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007728
7729 DSAStack->setParentTeamsRegionLoc(StartLoc);
7730
Kelvin Li4e325f72016-10-25 12:50:55 +00007731 return OMPTeamsDistributeSimdDirective::Create(
7732 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7733}
7734
Kelvin Li579e41c2016-11-30 23:51:03 +00007735StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective(
7736 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007737 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li579e41c2016-11-30 23:51:03 +00007738 if (!AStmt)
7739 return StmtError();
7740
Alexey Bataeve3727102018-04-18 15:57:46 +00007741 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li579e41c2016-11-30 23:51:03 +00007742 // 1.2.2 OpenMP Language Terminology
7743 // Structured block - An executable statement with a single entry at the
7744 // top and a single exit at the bottom.
7745 // The point of exit cannot be a branch out of the structured block.
7746 // longjmp() and throw() must not violate the entry/exit criteria.
7747 CS->getCapturedDecl()->setNothrow();
7748
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00007749 for (int ThisCaptureLevel =
7750 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd);
7751 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7752 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7753 // 1.2.2 OpenMP Language Terminology
7754 // Structured block - An executable statement with a single entry at the
7755 // top and a single exit at the bottom.
7756 // The point of exit cannot be a branch out of the structured block.
7757 // longjmp() and throw() must not violate the entry/exit criteria.
7758 CS->getCapturedDecl()->setNothrow();
7759 }
7760
Kelvin Li579e41c2016-11-30 23:51:03 +00007761 OMPLoopDirective::HelperExprs B;
7762 // In presence of clause 'collapse' with number of loops, it will
7763 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007764 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li579e41c2016-11-30 23:51:03 +00007765 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00007766 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li579e41c2016-11-30 23:51:03 +00007767 VarsWithImplicitDSA, B);
7768
7769 if (NestedLoopCount == 0)
7770 return StmtError();
7771
7772 assert((CurContext->isDependentContext() || B.builtAll()) &&
7773 "omp for loop exprs were not built");
7774
7775 if (!CurContext->isDependentContext()) {
7776 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007777 for (OMPClause *C : Clauses) {
Kelvin Li579e41c2016-11-30 23:51:03 +00007778 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7779 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7780 B.NumIterations, *this, CurScope,
7781 DSAStack))
7782 return StmtError();
7783 }
7784 }
7785
7786 if (checkSimdlenSafelenSpecified(*this, Clauses))
7787 return StmtError();
7788
Reid Kleckner87a31802018-03-12 21:43:02 +00007789 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007790
7791 DSAStack->setParentTeamsRegionLoc(StartLoc);
7792
Kelvin Li579e41c2016-11-30 23:51:03 +00007793 return OMPTeamsDistributeParallelForSimdDirective::Create(
7794 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7795}
7796
Kelvin Li7ade93f2016-12-09 03:24:30 +00007797StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective(
7798 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007799 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li7ade93f2016-12-09 03:24:30 +00007800 if (!AStmt)
7801 return StmtError();
7802
Alexey Bataeve3727102018-04-18 15:57:46 +00007803 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li7ade93f2016-12-09 03:24:30 +00007804 // 1.2.2 OpenMP Language Terminology
7805 // Structured block - An executable statement with a single entry at the
7806 // top and a single exit at the bottom.
7807 // The point of exit cannot be a branch out of the structured block.
7808 // longjmp() and throw() must not violate the entry/exit criteria.
7809 CS->getCapturedDecl()->setNothrow();
7810
Carlo Bertolli62fae152017-11-20 20:46:39 +00007811 for (int ThisCaptureLevel =
7812 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for);
7813 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7814 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7815 // 1.2.2 OpenMP Language Terminology
7816 // Structured block - An executable statement with a single entry at the
7817 // top and a single exit at the bottom.
7818 // The point of exit cannot be a branch out of the structured block.
7819 // longjmp() and throw() must not violate the entry/exit criteria.
7820 CS->getCapturedDecl()->setNothrow();
7821 }
7822
Kelvin Li7ade93f2016-12-09 03:24:30 +00007823 OMPLoopDirective::HelperExprs B;
7824 // In presence of clause 'collapse' with number of loops, it will
7825 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007826 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li7ade93f2016-12-09 03:24:30 +00007827 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
Carlo Bertolli62fae152017-11-20 20:46:39 +00007828 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li7ade93f2016-12-09 03:24:30 +00007829 VarsWithImplicitDSA, B);
7830
7831 if (NestedLoopCount == 0)
7832 return StmtError();
7833
7834 assert((CurContext->isDependentContext() || B.builtAll()) &&
7835 "omp for loop exprs were not built");
7836
Reid Kleckner87a31802018-03-12 21:43:02 +00007837 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007838
7839 DSAStack->setParentTeamsRegionLoc(StartLoc);
7840
Kelvin Li7ade93f2016-12-09 03:24:30 +00007841 return OMPTeamsDistributeParallelForDirective::Create(
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00007842 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7843 DSAStack->isCancelRegion());
Kelvin Li7ade93f2016-12-09 03:24:30 +00007844}
7845
Kelvin Libf594a52016-12-17 05:48:59 +00007846StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses,
7847 Stmt *AStmt,
7848 SourceLocation StartLoc,
7849 SourceLocation EndLoc) {
7850 if (!AStmt)
7851 return StmtError();
7852
Alexey Bataeve3727102018-04-18 15:57:46 +00007853 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Libf594a52016-12-17 05:48:59 +00007854 // 1.2.2 OpenMP Language Terminology
7855 // Structured block - An executable statement with a single entry at the
7856 // top and a single exit at the bottom.
7857 // The point of exit cannot be a branch out of the structured block.
7858 // longjmp() and throw() must not violate the entry/exit criteria.
7859 CS->getCapturedDecl()->setNothrow();
7860
Alexey Bataevf9fc42e2017-11-22 14:25:55 +00007861 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams);
7862 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7863 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7864 // 1.2.2 OpenMP Language Terminology
7865 // Structured block - An executable statement with a single entry at the
7866 // top and a single exit at the bottom.
7867 // The point of exit cannot be a branch out of the structured block.
7868 // longjmp() and throw() must not violate the entry/exit criteria.
7869 CS->getCapturedDecl()->setNothrow();
7870 }
Reid Kleckner87a31802018-03-12 21:43:02 +00007871 setFunctionHasBranchProtectedScope();
Kelvin Libf594a52016-12-17 05:48:59 +00007872
7873 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses,
7874 AStmt);
7875}
7876
Kelvin Li83c451e2016-12-25 04:52:54 +00007877StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective(
7878 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007879 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li83c451e2016-12-25 04:52:54 +00007880 if (!AStmt)
7881 return StmtError();
7882
Alexey Bataeve3727102018-04-18 15:57:46 +00007883 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li83c451e2016-12-25 04:52:54 +00007884 // 1.2.2 OpenMP Language Terminology
7885 // Structured block - An executable statement with a single entry at the
7886 // top and a single exit at the bottom.
7887 // The point of exit cannot be a branch out of the structured block.
7888 // longjmp() and throw() must not violate the entry/exit criteria.
7889 CS->getCapturedDecl()->setNothrow();
Alexey Bataevdfa430f2017-12-08 15:03:50 +00007890 for (int ThisCaptureLevel =
7891 getOpenMPCaptureLevels(OMPD_target_teams_distribute);
7892 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7893 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7894 // 1.2.2 OpenMP Language Terminology
7895 // Structured block - An executable statement with a single entry at the
7896 // top and a single exit at the bottom.
7897 // The point of exit cannot be a branch out of the structured block.
7898 // longjmp() and throw() must not violate the entry/exit criteria.
7899 CS->getCapturedDecl()->setNothrow();
7900 }
Kelvin Li83c451e2016-12-25 04:52:54 +00007901
7902 OMPLoopDirective::HelperExprs B;
7903 // In presence of clause 'collapse' with number of loops, it will
7904 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007905 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataevdfa430f2017-12-08 15:03:50 +00007906 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses),
7907 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li83c451e2016-12-25 04:52:54 +00007908 VarsWithImplicitDSA, B);
7909 if (NestedLoopCount == 0)
7910 return StmtError();
7911
7912 assert((CurContext->isDependentContext() || B.builtAll()) &&
7913 "omp target teams distribute loop exprs were not built");
7914
Reid Kleckner87a31802018-03-12 21:43:02 +00007915 setFunctionHasBranchProtectedScope();
Kelvin Li83c451e2016-12-25 04:52:54 +00007916 return OMPTargetTeamsDistributeDirective::Create(
7917 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7918}
7919
Kelvin Li80e8f562016-12-29 22:16:30 +00007920StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective(
7921 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007922 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li80e8f562016-12-29 22:16:30 +00007923 if (!AStmt)
7924 return StmtError();
7925
Alexey Bataeve3727102018-04-18 15:57:46 +00007926 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li80e8f562016-12-29 22:16:30 +00007927 // 1.2.2 OpenMP Language Terminology
7928 // Structured block - An executable statement with a single entry at the
7929 // top and a single exit at the bottom.
7930 // The point of exit cannot be a branch out of the structured block.
7931 // longjmp() and throw() must not violate the entry/exit criteria.
7932 CS->getCapturedDecl()->setNothrow();
Carlo Bertolli52978c32018-01-03 21:12:44 +00007933 for (int ThisCaptureLevel =
7934 getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for);
7935 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7936 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7937 // 1.2.2 OpenMP Language Terminology
7938 // Structured block - An executable statement with a single entry at the
7939 // top and a single exit at the bottom.
7940 // The point of exit cannot be a branch out of the structured block.
7941 // longjmp() and throw() must not violate the entry/exit criteria.
7942 CS->getCapturedDecl()->setNothrow();
7943 }
7944
Kelvin Li80e8f562016-12-29 22:16:30 +00007945 OMPLoopDirective::HelperExprs B;
7946 // In presence of clause 'collapse' with number of loops, it will
7947 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007948 unsigned NestedLoopCount = checkOpenMPLoop(
Carlo Bertolli52978c32018-01-03 21:12:44 +00007949 OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
7950 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li80e8f562016-12-29 22:16:30 +00007951 VarsWithImplicitDSA, B);
7952 if (NestedLoopCount == 0)
7953 return StmtError();
7954
7955 assert((CurContext->isDependentContext() || B.builtAll()) &&
7956 "omp target teams distribute parallel for loop exprs were not built");
7957
Alexey Bataev647dd842018-01-15 20:59:40 +00007958 if (!CurContext->isDependentContext()) {
7959 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007960 for (OMPClause *C : Clauses) {
Alexey Bataev647dd842018-01-15 20:59:40 +00007961 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7962 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7963 B.NumIterations, *this, CurScope,
7964 DSAStack))
7965 return StmtError();
7966 }
7967 }
7968
Reid Kleckner87a31802018-03-12 21:43:02 +00007969 setFunctionHasBranchProtectedScope();
Kelvin Li80e8f562016-12-29 22:16:30 +00007970 return OMPTargetTeamsDistributeParallelForDirective::Create(
Alexey Bataev16e79882017-11-22 21:12:03 +00007971 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7972 DSAStack->isCancelRegion());
Kelvin Li80e8f562016-12-29 22:16:30 +00007973}
7974
Kelvin Li1851df52017-01-03 05:23:48 +00007975StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
7976 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007977 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li1851df52017-01-03 05:23:48 +00007978 if (!AStmt)
7979 return StmtError();
7980
Alexey Bataeve3727102018-04-18 15:57:46 +00007981 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li1851df52017-01-03 05:23:48 +00007982 // 1.2.2 OpenMP Language Terminology
7983 // Structured block - An executable statement with a single entry at the
7984 // top and a single exit at the bottom.
7985 // The point of exit cannot be a branch out of the structured block.
7986 // longjmp() and throw() must not violate the entry/exit criteria.
7987 CS->getCapturedDecl()->setNothrow();
Alexey Bataev647dd842018-01-15 20:59:40 +00007988 for (int ThisCaptureLevel = getOpenMPCaptureLevels(
7989 OMPD_target_teams_distribute_parallel_for_simd);
7990 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7991 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7992 // 1.2.2 OpenMP Language Terminology
7993 // Structured block - An executable statement with a single entry at the
7994 // top and a single exit at the bottom.
7995 // The point of exit cannot be a branch out of the structured block.
7996 // longjmp() and throw() must not violate the entry/exit criteria.
7997 CS->getCapturedDecl()->setNothrow();
7998 }
Kelvin Li1851df52017-01-03 05:23:48 +00007999
8000 OMPLoopDirective::HelperExprs B;
8001 // In presence of clause 'collapse' with number of loops, it will
8002 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00008003 unsigned NestedLoopCount =
8004 checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd,
Alexey Bataev647dd842018-01-15 20:59:40 +00008005 getCollapseNumberExpr(Clauses),
8006 nullptr /*ordered not a clause on distribute*/, CS, *this,
8007 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li1851df52017-01-03 05:23:48 +00008008 if (NestedLoopCount == 0)
8009 return StmtError();
8010
8011 assert((CurContext->isDependentContext() || B.builtAll()) &&
8012 "omp target teams distribute parallel for simd loop exprs were not "
8013 "built");
8014
8015 if (!CurContext->isDependentContext()) {
8016 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00008017 for (OMPClause *C : Clauses) {
Kelvin Li1851df52017-01-03 05:23:48 +00008018 if (auto *LC = dyn_cast<OMPLinearClause>(C))
8019 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8020 B.NumIterations, *this, CurScope,
8021 DSAStack))
8022 return StmtError();
8023 }
8024 }
8025
Alexey Bataev438388c2017-11-22 18:34:02 +00008026 if (checkSimdlenSafelenSpecified(*this, Clauses))
8027 return StmtError();
8028
Reid Kleckner87a31802018-03-12 21:43:02 +00008029 setFunctionHasBranchProtectedScope();
Kelvin Li1851df52017-01-03 05:23:48 +00008030 return OMPTargetTeamsDistributeParallelForSimdDirective::Create(
8031 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
8032}
8033
Kelvin Lida681182017-01-10 18:08:18 +00008034StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective(
8035 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00008036 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Lida681182017-01-10 18:08:18 +00008037 if (!AStmt)
8038 return StmtError();
8039
8040 auto *CS = cast<CapturedStmt>(AStmt);
8041 // 1.2.2 OpenMP Language Terminology
8042 // Structured block - An executable statement with a single entry at the
8043 // top and a single exit at the bottom.
8044 // The point of exit cannot be a branch out of the structured block.
8045 // longjmp() and throw() must not violate the entry/exit criteria.
8046 CS->getCapturedDecl()->setNothrow();
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00008047 for (int ThisCaptureLevel =
8048 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd);
8049 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8050 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8051 // 1.2.2 OpenMP Language Terminology
8052 // Structured block - An executable statement with a single entry at the
8053 // top and a single exit at the bottom.
8054 // The point of exit cannot be a branch out of the structured block.
8055 // longjmp() and throw() must not violate the entry/exit criteria.
8056 CS->getCapturedDecl()->setNothrow();
8057 }
Kelvin Lida681182017-01-10 18:08:18 +00008058
8059 OMPLoopDirective::HelperExprs B;
8060 // In presence of clause 'collapse' with number of loops, it will
8061 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00008062 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Lida681182017-01-10 18:08:18 +00008063 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00008064 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Lida681182017-01-10 18:08:18 +00008065 VarsWithImplicitDSA, B);
8066 if (NestedLoopCount == 0)
8067 return StmtError();
8068
8069 assert((CurContext->isDependentContext() || B.builtAll()) &&
8070 "omp target teams distribute simd loop exprs were not built");
8071
Alexey Bataev438388c2017-11-22 18:34:02 +00008072 if (!CurContext->isDependentContext()) {
8073 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00008074 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00008075 if (auto *LC = dyn_cast<OMPLinearClause>(C))
8076 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8077 B.NumIterations, *this, CurScope,
8078 DSAStack))
8079 return StmtError();
8080 }
8081 }
8082
8083 if (checkSimdlenSafelenSpecified(*this, Clauses))
8084 return StmtError();
8085
Reid Kleckner87a31802018-03-12 21:43:02 +00008086 setFunctionHasBranchProtectedScope();
Kelvin Lida681182017-01-10 18:08:18 +00008087 return OMPTargetTeamsDistributeSimdDirective::Create(
8088 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
8089}
8090
Alexey Bataeved09d242014-05-28 05:53:51 +00008091OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008092 SourceLocation StartLoc,
8093 SourceLocation LParenLoc,
8094 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008095 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008096 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00008097 case OMPC_final:
8098 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
8099 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00008100 case OMPC_num_threads:
8101 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
8102 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00008103 case OMPC_safelen:
8104 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
8105 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00008106 case OMPC_simdlen:
8107 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
8108 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00008109 case OMPC_collapse:
8110 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
8111 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00008112 case OMPC_ordered:
8113 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
8114 break;
Michael Wonge710d542015-08-07 16:16:36 +00008115 case OMPC_device:
8116 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
8117 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00008118 case OMPC_num_teams:
8119 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
8120 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008121 case OMPC_thread_limit:
8122 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
8123 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00008124 case OMPC_priority:
8125 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
8126 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008127 case OMPC_grainsize:
8128 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
8129 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00008130 case OMPC_num_tasks:
8131 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
8132 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00008133 case OMPC_hint:
8134 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
8135 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008136 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008137 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008138 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008139 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008140 case OMPC_private:
8141 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00008142 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008143 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00008144 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008145 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008146 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00008147 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008148 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008149 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008150 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00008151 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008152 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008153 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008154 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00008155 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008156 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008157 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008158 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008159 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008160 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008161 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00008162 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008163 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008164 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00008165 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008166 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008167 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008168 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008169 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00008170 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00008171 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00008172 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00008173 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00008174 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00008175 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00008176 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00008177 case OMPC_dynamic_allocators:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008178 llvm_unreachable("Clause is not allowed.");
8179 }
8180 return Res;
8181}
8182
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008183// An OpenMP directive such as 'target parallel' has two captured regions:
8184// for the 'target' and 'parallel' respectively. This function returns
8185// the region in which to capture expressions associated with a clause.
8186// A return value of OMPD_unknown signifies that the expression should not
8187// be captured.
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008188static OpenMPDirectiveKind getOpenMPCaptureRegionForClause(
8189 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
8190 OpenMPDirectiveKind NameModifier = OMPD_unknown) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008191 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008192 switch (CKind) {
8193 case OMPC_if:
8194 switch (DKind) {
8195 case OMPD_target_parallel:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008196 case OMPD_target_parallel_for:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00008197 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008198 // If this clause applies to the nested 'parallel' region, capture within
8199 // the 'target' region, otherwise do not capture.
8200 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
8201 CaptureRegion = OMPD_target;
8202 break;
Carlo Bertolli52978c32018-01-03 21:12:44 +00008203 case OMPD_target_teams_distribute_parallel_for:
8204 case OMPD_target_teams_distribute_parallel_for_simd:
8205 // If this clause applies to the nested 'parallel' region, capture within
8206 // the 'teams' region, otherwise do not capture.
8207 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
8208 CaptureRegion = OMPD_teams;
8209 break;
Carlo Bertolli62fae152017-11-20 20:46:39 +00008210 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008211 case OMPD_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008212 CaptureRegion = OMPD_teams;
8213 break;
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008214 case OMPD_target_update:
Alexey Bataevfab20e42017-12-27 18:49:38 +00008215 case OMPD_target_enter_data:
8216 case OMPD_target_exit_data:
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008217 CaptureRegion = OMPD_task;
8218 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008219 case OMPD_cancel:
8220 case OMPD_parallel:
8221 case OMPD_parallel_sections:
8222 case OMPD_parallel_for:
8223 case OMPD_parallel_for_simd:
8224 case OMPD_target:
8225 case OMPD_target_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008226 case OMPD_target_teams:
8227 case OMPD_target_teams_distribute:
8228 case OMPD_target_teams_distribute_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008229 case OMPD_distribute_parallel_for:
8230 case OMPD_distribute_parallel_for_simd:
8231 case OMPD_task:
8232 case OMPD_taskloop:
8233 case OMPD_taskloop_simd:
8234 case OMPD_target_data:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008235 // Do not capture if-clause expressions.
8236 break;
8237 case OMPD_threadprivate:
8238 case OMPD_taskyield:
8239 case OMPD_barrier:
8240 case OMPD_taskwait:
8241 case OMPD_cancellation_point:
8242 case OMPD_flush:
8243 case OMPD_declare_reduction:
8244 case OMPD_declare_simd:
8245 case OMPD_declare_target:
8246 case OMPD_end_declare_target:
8247 case OMPD_teams:
8248 case OMPD_simd:
8249 case OMPD_for:
8250 case OMPD_for_simd:
8251 case OMPD_sections:
8252 case OMPD_section:
8253 case OMPD_single:
8254 case OMPD_master:
8255 case OMPD_critical:
8256 case OMPD_taskgroup:
8257 case OMPD_distribute:
8258 case OMPD_ordered:
8259 case OMPD_atomic:
8260 case OMPD_distribute_simd:
8261 case OMPD_teams_distribute:
8262 case OMPD_teams_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008263 case OMPD_requires:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008264 llvm_unreachable("Unexpected OpenMP directive with if-clause");
8265 case OMPD_unknown:
8266 llvm_unreachable("Unknown OpenMP directive");
8267 }
8268 break;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008269 case OMPC_num_threads:
8270 switch (DKind) {
8271 case OMPD_target_parallel:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008272 case OMPD_target_parallel_for:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00008273 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008274 CaptureRegion = OMPD_target;
8275 break;
Carlo Bertolli62fae152017-11-20 20:46:39 +00008276 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008277 case OMPD_teams_distribute_parallel_for_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008278 case OMPD_target_teams_distribute_parallel_for:
8279 case OMPD_target_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008280 CaptureRegion = OMPD_teams;
8281 break;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008282 case OMPD_parallel:
8283 case OMPD_parallel_sections:
8284 case OMPD_parallel_for:
8285 case OMPD_parallel_for_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008286 case OMPD_distribute_parallel_for:
8287 case OMPD_distribute_parallel_for_simd:
8288 // Do not capture num_threads-clause expressions.
8289 break;
8290 case OMPD_target_data:
8291 case OMPD_target_enter_data:
8292 case OMPD_target_exit_data:
8293 case OMPD_target_update:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008294 case OMPD_target:
8295 case OMPD_target_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008296 case OMPD_target_teams:
8297 case OMPD_target_teams_distribute:
8298 case OMPD_target_teams_distribute_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008299 case OMPD_cancel:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008300 case OMPD_task:
8301 case OMPD_taskloop:
8302 case OMPD_taskloop_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008303 case OMPD_threadprivate:
8304 case OMPD_taskyield:
8305 case OMPD_barrier:
8306 case OMPD_taskwait:
8307 case OMPD_cancellation_point:
8308 case OMPD_flush:
8309 case OMPD_declare_reduction:
8310 case OMPD_declare_simd:
8311 case OMPD_declare_target:
8312 case OMPD_end_declare_target:
8313 case OMPD_teams:
8314 case OMPD_simd:
8315 case OMPD_for:
8316 case OMPD_for_simd:
8317 case OMPD_sections:
8318 case OMPD_section:
8319 case OMPD_single:
8320 case OMPD_master:
8321 case OMPD_critical:
8322 case OMPD_taskgroup:
8323 case OMPD_distribute:
8324 case OMPD_ordered:
8325 case OMPD_atomic:
8326 case OMPD_distribute_simd:
8327 case OMPD_teams_distribute:
8328 case OMPD_teams_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008329 case OMPD_requires:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008330 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause");
8331 case OMPD_unknown:
8332 llvm_unreachable("Unknown OpenMP directive");
8333 }
8334 break;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008335 case OMPC_num_teams:
8336 switch (DKind) {
8337 case OMPD_target_teams:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008338 case OMPD_target_teams_distribute:
8339 case OMPD_target_teams_distribute_simd:
8340 case OMPD_target_teams_distribute_parallel_for:
8341 case OMPD_target_teams_distribute_parallel_for_simd:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008342 CaptureRegion = OMPD_target;
8343 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008344 case OMPD_teams_distribute_parallel_for:
8345 case OMPD_teams_distribute_parallel_for_simd:
8346 case OMPD_teams:
8347 case OMPD_teams_distribute:
8348 case OMPD_teams_distribute_simd:
8349 // Do not capture num_teams-clause expressions.
8350 break;
8351 case OMPD_distribute_parallel_for:
8352 case OMPD_distribute_parallel_for_simd:
8353 case OMPD_task:
8354 case OMPD_taskloop:
8355 case OMPD_taskloop_simd:
8356 case OMPD_target_data:
8357 case OMPD_target_enter_data:
8358 case OMPD_target_exit_data:
8359 case OMPD_target_update:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008360 case OMPD_cancel:
8361 case OMPD_parallel:
8362 case OMPD_parallel_sections:
8363 case OMPD_parallel_for:
8364 case OMPD_parallel_for_simd:
8365 case OMPD_target:
8366 case OMPD_target_simd:
8367 case OMPD_target_parallel:
8368 case OMPD_target_parallel_for:
8369 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008370 case OMPD_threadprivate:
8371 case OMPD_taskyield:
8372 case OMPD_barrier:
8373 case OMPD_taskwait:
8374 case OMPD_cancellation_point:
8375 case OMPD_flush:
8376 case OMPD_declare_reduction:
8377 case OMPD_declare_simd:
8378 case OMPD_declare_target:
8379 case OMPD_end_declare_target:
8380 case OMPD_simd:
8381 case OMPD_for:
8382 case OMPD_for_simd:
8383 case OMPD_sections:
8384 case OMPD_section:
8385 case OMPD_single:
8386 case OMPD_master:
8387 case OMPD_critical:
8388 case OMPD_taskgroup:
8389 case OMPD_distribute:
8390 case OMPD_ordered:
8391 case OMPD_atomic:
8392 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008393 case OMPD_requires:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008394 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
8395 case OMPD_unknown:
8396 llvm_unreachable("Unknown OpenMP directive");
8397 }
8398 break;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008399 case OMPC_thread_limit:
8400 switch (DKind) {
8401 case OMPD_target_teams:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008402 case OMPD_target_teams_distribute:
8403 case OMPD_target_teams_distribute_simd:
8404 case OMPD_target_teams_distribute_parallel_for:
8405 case OMPD_target_teams_distribute_parallel_for_simd:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008406 CaptureRegion = OMPD_target;
8407 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008408 case OMPD_teams_distribute_parallel_for:
8409 case OMPD_teams_distribute_parallel_for_simd:
8410 case OMPD_teams:
8411 case OMPD_teams_distribute:
8412 case OMPD_teams_distribute_simd:
8413 // Do not capture thread_limit-clause expressions.
8414 break;
8415 case OMPD_distribute_parallel_for:
8416 case OMPD_distribute_parallel_for_simd:
8417 case OMPD_task:
8418 case OMPD_taskloop:
8419 case OMPD_taskloop_simd:
8420 case OMPD_target_data:
8421 case OMPD_target_enter_data:
8422 case OMPD_target_exit_data:
8423 case OMPD_target_update:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008424 case OMPD_cancel:
8425 case OMPD_parallel:
8426 case OMPD_parallel_sections:
8427 case OMPD_parallel_for:
8428 case OMPD_parallel_for_simd:
8429 case OMPD_target:
8430 case OMPD_target_simd:
8431 case OMPD_target_parallel:
8432 case OMPD_target_parallel_for:
8433 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008434 case OMPD_threadprivate:
8435 case OMPD_taskyield:
8436 case OMPD_barrier:
8437 case OMPD_taskwait:
8438 case OMPD_cancellation_point:
8439 case OMPD_flush:
8440 case OMPD_declare_reduction:
8441 case OMPD_declare_simd:
8442 case OMPD_declare_target:
8443 case OMPD_end_declare_target:
8444 case OMPD_simd:
8445 case OMPD_for:
8446 case OMPD_for_simd:
8447 case OMPD_sections:
8448 case OMPD_section:
8449 case OMPD_single:
8450 case OMPD_master:
8451 case OMPD_critical:
8452 case OMPD_taskgroup:
8453 case OMPD_distribute:
8454 case OMPD_ordered:
8455 case OMPD_atomic:
8456 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008457 case OMPD_requires:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008458 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause");
8459 case OMPD_unknown:
8460 llvm_unreachable("Unknown OpenMP directive");
8461 }
8462 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008463 case OMPC_schedule:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008464 switch (DKind) {
Alexey Bataev2ba67042017-11-28 21:11:44 +00008465 case OMPD_parallel_for:
8466 case OMPD_parallel_for_simd:
Alexey Bataev7f96c372017-11-22 17:19:31 +00008467 case OMPD_distribute_parallel_for:
Alexey Bataev974acd62017-11-27 19:38:52 +00008468 case OMPD_distribute_parallel_for_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008469 case OMPD_teams_distribute_parallel_for:
8470 case OMPD_teams_distribute_parallel_for_simd:
8471 case OMPD_target_parallel_for:
8472 case OMPD_target_parallel_for_simd:
8473 case OMPD_target_teams_distribute_parallel_for:
8474 case OMPD_target_teams_distribute_parallel_for_simd:
Alexey Bataev7f96c372017-11-22 17:19:31 +00008475 CaptureRegion = OMPD_parallel;
8476 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008477 case OMPD_for:
8478 case OMPD_for_simd:
8479 // Do not capture schedule-clause expressions.
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008480 break;
8481 case OMPD_task:
8482 case OMPD_taskloop:
8483 case OMPD_taskloop_simd:
8484 case OMPD_target_data:
8485 case OMPD_target_enter_data:
8486 case OMPD_target_exit_data:
8487 case OMPD_target_update:
8488 case OMPD_teams:
8489 case OMPD_teams_distribute:
8490 case OMPD_teams_distribute_simd:
8491 case OMPD_target_teams_distribute:
8492 case OMPD_target_teams_distribute_simd:
8493 case OMPD_target:
8494 case OMPD_target_simd:
8495 case OMPD_target_parallel:
8496 case OMPD_cancel:
8497 case OMPD_parallel:
8498 case OMPD_parallel_sections:
8499 case OMPD_threadprivate:
8500 case OMPD_taskyield:
8501 case OMPD_barrier:
8502 case OMPD_taskwait:
8503 case OMPD_cancellation_point:
8504 case OMPD_flush:
8505 case OMPD_declare_reduction:
8506 case OMPD_declare_simd:
8507 case OMPD_declare_target:
8508 case OMPD_end_declare_target:
8509 case OMPD_simd:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008510 case OMPD_sections:
8511 case OMPD_section:
8512 case OMPD_single:
8513 case OMPD_master:
8514 case OMPD_critical:
8515 case OMPD_taskgroup:
8516 case OMPD_distribute:
8517 case OMPD_ordered:
8518 case OMPD_atomic:
8519 case OMPD_distribute_simd:
8520 case OMPD_target_teams:
Kelvin Li1408f912018-09-26 04:28:39 +00008521 case OMPD_requires:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008522 llvm_unreachable("Unexpected OpenMP directive with schedule clause");
8523 case OMPD_unknown:
8524 llvm_unreachable("Unknown OpenMP directive");
8525 }
8526 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008527 case OMPC_dist_schedule:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008528 switch (DKind) {
8529 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008530 case OMPD_teams_distribute_parallel_for_simd:
8531 case OMPD_teams_distribute:
8532 case OMPD_teams_distribute_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008533 case OMPD_target_teams_distribute_parallel_for:
8534 case OMPD_target_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008535 case OMPD_target_teams_distribute:
8536 case OMPD_target_teams_distribute_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008537 CaptureRegion = OMPD_teams;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008538 break;
8539 case OMPD_distribute_parallel_for:
8540 case OMPD_distribute_parallel_for_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008541 case OMPD_distribute:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008542 case OMPD_distribute_simd:
8543 // Do not capture thread_limit-clause expressions.
8544 break;
8545 case OMPD_parallel_for:
8546 case OMPD_parallel_for_simd:
8547 case OMPD_target_parallel_for_simd:
8548 case OMPD_target_parallel_for:
8549 case OMPD_task:
8550 case OMPD_taskloop:
8551 case OMPD_taskloop_simd:
8552 case OMPD_target_data:
8553 case OMPD_target_enter_data:
8554 case OMPD_target_exit_data:
8555 case OMPD_target_update:
8556 case OMPD_teams:
8557 case OMPD_target:
8558 case OMPD_target_simd:
8559 case OMPD_target_parallel:
8560 case OMPD_cancel:
8561 case OMPD_parallel:
8562 case OMPD_parallel_sections:
8563 case OMPD_threadprivate:
8564 case OMPD_taskyield:
8565 case OMPD_barrier:
8566 case OMPD_taskwait:
8567 case OMPD_cancellation_point:
8568 case OMPD_flush:
8569 case OMPD_declare_reduction:
8570 case OMPD_declare_simd:
8571 case OMPD_declare_target:
8572 case OMPD_end_declare_target:
8573 case OMPD_simd:
8574 case OMPD_for:
8575 case OMPD_for_simd:
8576 case OMPD_sections:
8577 case OMPD_section:
8578 case OMPD_single:
8579 case OMPD_master:
8580 case OMPD_critical:
8581 case OMPD_taskgroup:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008582 case OMPD_ordered:
8583 case OMPD_atomic:
8584 case OMPD_target_teams:
Kelvin Li1408f912018-09-26 04:28:39 +00008585 case OMPD_requires:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008586 llvm_unreachable("Unexpected OpenMP directive with schedule clause");
8587 case OMPD_unknown:
8588 llvm_unreachable("Unknown OpenMP directive");
8589 }
8590 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008591 case OMPC_device:
8592 switch (DKind) {
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008593 case OMPD_target_update:
Alexey Bataevfab20e42017-12-27 18:49:38 +00008594 case OMPD_target_enter_data:
8595 case OMPD_target_exit_data:
Alexey Bataev8451efa2018-01-15 19:06:12 +00008596 case OMPD_target:
Alexey Bataevf41c88f2018-01-16 15:05:16 +00008597 case OMPD_target_simd:
Alexey Bataev0c869ef2018-01-16 15:57:07 +00008598 case OMPD_target_teams:
Alexey Bataev54d5c7d2018-01-16 16:27:49 +00008599 case OMPD_target_parallel:
Alexey Bataev79df7562018-01-16 16:46:46 +00008600 case OMPD_target_teams_distribute:
Alexey Bataev8d16a432018-01-16 17:22:50 +00008601 case OMPD_target_teams_distribute_simd:
Alexey Bataev8ed895512018-01-16 17:41:04 +00008602 case OMPD_target_parallel_for:
Alexey Bataevd60d1ba2018-01-16 17:55:15 +00008603 case OMPD_target_parallel_for_simd:
Alexey Bataev9f9fb0b2018-01-16 19:02:33 +00008604 case OMPD_target_teams_distribute_parallel_for:
Alexey Bataev9350fc32018-01-16 19:18:24 +00008605 case OMPD_target_teams_distribute_parallel_for_simd:
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008606 CaptureRegion = OMPD_task;
8607 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008608 case OMPD_target_data:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008609 // Do not capture device-clause expressions.
8610 break;
8611 case OMPD_teams_distribute_parallel_for:
8612 case OMPD_teams_distribute_parallel_for_simd:
8613 case OMPD_teams:
8614 case OMPD_teams_distribute:
8615 case OMPD_teams_distribute_simd:
8616 case OMPD_distribute_parallel_for:
8617 case OMPD_distribute_parallel_for_simd:
8618 case OMPD_task:
8619 case OMPD_taskloop:
8620 case OMPD_taskloop_simd:
8621 case OMPD_cancel:
8622 case OMPD_parallel:
8623 case OMPD_parallel_sections:
8624 case OMPD_parallel_for:
8625 case OMPD_parallel_for_simd:
8626 case OMPD_threadprivate:
8627 case OMPD_taskyield:
8628 case OMPD_barrier:
8629 case OMPD_taskwait:
8630 case OMPD_cancellation_point:
8631 case OMPD_flush:
8632 case OMPD_declare_reduction:
8633 case OMPD_declare_simd:
8634 case OMPD_declare_target:
8635 case OMPD_end_declare_target:
8636 case OMPD_simd:
8637 case OMPD_for:
8638 case OMPD_for_simd:
8639 case OMPD_sections:
8640 case OMPD_section:
8641 case OMPD_single:
8642 case OMPD_master:
8643 case OMPD_critical:
8644 case OMPD_taskgroup:
8645 case OMPD_distribute:
8646 case OMPD_ordered:
8647 case OMPD_atomic:
8648 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008649 case OMPD_requires:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008650 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
8651 case OMPD_unknown:
8652 llvm_unreachable("Unknown OpenMP directive");
8653 }
8654 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008655 case OMPC_firstprivate:
8656 case OMPC_lastprivate:
8657 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008658 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008659 case OMPC_in_reduction:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008660 case OMPC_linear:
8661 case OMPC_default:
8662 case OMPC_proc_bind:
8663 case OMPC_final:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008664 case OMPC_safelen:
8665 case OMPC_simdlen:
8666 case OMPC_collapse:
8667 case OMPC_private:
8668 case OMPC_shared:
8669 case OMPC_aligned:
8670 case OMPC_copyin:
8671 case OMPC_copyprivate:
8672 case OMPC_ordered:
8673 case OMPC_nowait:
8674 case OMPC_untied:
8675 case OMPC_mergeable:
8676 case OMPC_threadprivate:
8677 case OMPC_flush:
8678 case OMPC_read:
8679 case OMPC_write:
8680 case OMPC_update:
8681 case OMPC_capture:
8682 case OMPC_seq_cst:
8683 case OMPC_depend:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008684 case OMPC_threads:
8685 case OMPC_simd:
8686 case OMPC_map:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008687 case OMPC_priority:
8688 case OMPC_grainsize:
8689 case OMPC_nogroup:
8690 case OMPC_num_tasks:
8691 case OMPC_hint:
8692 case OMPC_defaultmap:
8693 case OMPC_unknown:
8694 case OMPC_uniform:
8695 case OMPC_to:
8696 case OMPC_from:
8697 case OMPC_use_device_ptr:
8698 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00008699 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00008700 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00008701 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00008702 case OMPC_dynamic_allocators:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008703 llvm_unreachable("Unexpected OpenMP clause.");
8704 }
8705 return CaptureRegion;
8706}
8707
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008708OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
8709 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008710 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008711 SourceLocation NameModifierLoc,
8712 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008713 SourceLocation EndLoc) {
8714 Expr *ValExpr = Condition;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008715 Stmt *HelperValStmt = nullptr;
8716 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008717 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
8718 !Condition->isInstantiationDependent() &&
8719 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00008720 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008721 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008722 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008723
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008724 ValExpr = Val.get();
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008725
8726 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
8727 CaptureRegion =
8728 getOpenMPCaptureRegionForClause(DKind, OMPC_if, NameModifier);
Alexey Bataev2ba67042017-11-28 21:11:44 +00008729 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008730 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00008731 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008732 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8733 HelperValStmt = buildPreInits(Context, Captures);
8734 }
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008735 }
8736
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008737 return new (Context)
8738 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
8739 LParenLoc, NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008740}
8741
Alexey Bataev3778b602014-07-17 07:32:53 +00008742OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
8743 SourceLocation StartLoc,
8744 SourceLocation LParenLoc,
8745 SourceLocation EndLoc) {
8746 Expr *ValExpr = Condition;
8747 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
8748 !Condition->isInstantiationDependent() &&
8749 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00008750 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataev3778b602014-07-17 07:32:53 +00008751 if (Val.isInvalid())
8752 return nullptr;
8753
Richard Smith03a4aa32016-06-23 19:02:52 +00008754 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataev3778b602014-07-17 07:32:53 +00008755 }
8756
8757 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8758}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00008759ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
8760 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00008761 if (!Op)
8762 return ExprError();
8763
8764 class IntConvertDiagnoser : public ICEConvertDiagnoser {
8765 public:
8766 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00008767 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00008768 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
8769 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008770 return S.Diag(Loc, diag::err_omp_not_integral) << T;
8771 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008772 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
8773 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008774 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
8775 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008776 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
8777 QualType T,
8778 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008779 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
8780 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008781 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
8782 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008783 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00008784 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00008785 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008786 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
8787 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008788 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
8789 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008790 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
8791 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008792 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00008793 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00008794 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008795 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
8796 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008797 llvm_unreachable("conversion functions are permitted");
8798 }
8799 } ConvertDiagnoser;
8800 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
8801}
8802
Alexey Bataeve3727102018-04-18 15:57:46 +00008803static bool isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00008804 OpenMPClauseKind CKind,
8805 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008806 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
8807 !ValExpr->isInstantiationDependent()) {
8808 SourceLocation Loc = ValExpr->getExprLoc();
8809 ExprResult Value =
8810 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
8811 if (Value.isInvalid())
8812 return false;
8813
8814 ValExpr = Value.get();
8815 // The expression must evaluate to a non-negative integer value.
8816 llvm::APSInt Result;
8817 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00008818 Result.isSigned() &&
8819 !((!StrictlyPositive && Result.isNonNegative()) ||
8820 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008821 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00008822 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
8823 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008824 return false;
8825 }
8826 }
8827 return true;
8828}
8829
Alexey Bataev568a8332014-03-06 06:15:19 +00008830OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
8831 SourceLocation StartLoc,
8832 SourceLocation LParenLoc,
8833 SourceLocation EndLoc) {
8834 Expr *ValExpr = NumThreads;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008835 Stmt *HelperValStmt = nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00008836
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008837 // OpenMP [2.5, Restrictions]
8838 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +00008839 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
Alexey Bataeva0569352015-12-01 10:17:31 +00008840 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008841 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00008842
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008843 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +00008844 OpenMPDirectiveKind CaptureRegion =
8845 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads);
8846 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008847 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00008848 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008849 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8850 HelperValStmt = buildPreInits(Context, Captures);
8851 }
8852
8853 return new (Context) OMPNumThreadsClause(
8854 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00008855}
8856
Alexey Bataev62c87d22014-03-21 04:51:18 +00008857ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008858 OpenMPClauseKind CKind,
8859 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00008860 if (!E)
8861 return ExprError();
8862 if (E->isValueDependent() || E->isTypeDependent() ||
8863 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00008864 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00008865 llvm::APSInt Result;
8866 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
8867 if (ICE.isInvalid())
8868 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008869 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
8870 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00008871 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008872 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
8873 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00008874 return ExprError();
8875 }
Alexander Musman09184fe2014-09-30 05:29:28 +00008876 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
8877 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
8878 << E->getSourceRange();
8879 return ExprError();
8880 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008881 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
8882 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00008883 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008884 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00008885 return ICE;
8886}
8887
8888OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
8889 SourceLocation LParenLoc,
8890 SourceLocation EndLoc) {
8891 // OpenMP [2.8.1, simd construct, Description]
8892 // The parameter of the safelen clause must be a constant
8893 // positive integer expression.
8894 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
8895 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008896 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00008897 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00008898 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00008899}
8900
Alexey Bataev66b15b52015-08-21 11:14:16 +00008901OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
8902 SourceLocation LParenLoc,
8903 SourceLocation EndLoc) {
8904 // OpenMP [2.8.1, simd construct, Description]
8905 // The parameter of the simdlen clause must be a constant
8906 // positive integer expression.
8907 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
8908 if (Simdlen.isInvalid())
8909 return nullptr;
8910 return new (Context)
8911 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
8912}
8913
Alexander Musman64d33f12014-06-04 07:53:32 +00008914OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
8915 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00008916 SourceLocation LParenLoc,
8917 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00008918 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00008919 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00008920 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00008921 // The parameter of the collapse clause must be a constant
8922 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00008923 ExprResult NumForLoopsResult =
8924 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
8925 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00008926 return nullptr;
8927 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00008928 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00008929}
8930
Alexey Bataev10e775f2015-07-30 11:36:16 +00008931OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
8932 SourceLocation EndLoc,
8933 SourceLocation LParenLoc,
8934 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00008935 // OpenMP [2.7.1, loop construct, Description]
8936 // OpenMP [2.8.1, simd construct, Description]
8937 // OpenMP [2.9.6, distribute construct, Description]
8938 // The parameter of the ordered clause must be a constant
8939 // positive integer expression if any.
8940 if (NumForLoops && LParenLoc.isValid()) {
8941 ExprResult NumForLoopsResult =
8942 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
8943 if (NumForLoopsResult.isInvalid())
8944 return nullptr;
8945 NumForLoops = NumForLoopsResult.get();
Alexey Bataeve3727102018-04-18 15:57:46 +00008946 } else {
Alexey Bataev346265e2015-09-25 10:37:12 +00008947 NumForLoops = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +00008948 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00008949 auto *Clause = OMPOrderedClause::Create(
8950 Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0,
8951 StartLoc, LParenLoc, EndLoc);
8952 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause);
8953 return Clause;
Alexey Bataev10e775f2015-07-30 11:36:16 +00008954}
8955
Alexey Bataeved09d242014-05-28 05:53:51 +00008956OMPClause *Sema::ActOnOpenMPSimpleClause(
8957 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
8958 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008959 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008960 switch (Kind) {
8961 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00008962 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00008963 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
8964 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008965 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008966 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00008967 Res = ActOnOpenMPProcBindClause(
8968 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
8969 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008970 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008971 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00008972 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00008973 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00008974 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00008975 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00008976 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008977 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008978 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008979 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00008980 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00008981 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00008982 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008983 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008984 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00008985 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008986 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008987 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008988 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008989 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00008990 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008991 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008992 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008993 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00008994 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008995 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008996 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008997 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008998 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008999 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009000 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00009001 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00009002 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009003 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00009004 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009005 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009006 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009007 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009008 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00009009 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00009010 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009011 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00009012 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009013 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009014 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009015 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00009016 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00009017 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00009018 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00009019 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00009020 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00009021 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009022 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009023 case OMPC_dynamic_allocators:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009024 llvm_unreachable("Clause is not allowed.");
9025 }
9026 return Res;
9027}
9028
Alexey Bataev6402bca2015-12-28 07:25:51 +00009029static std::string
9030getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
9031 ArrayRef<unsigned> Exclude = llvm::None) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009032 SmallString<256> Buffer;
9033 llvm::raw_svector_ostream Out(Buffer);
Alexey Bataev6402bca2015-12-28 07:25:51 +00009034 unsigned Bound = Last >= 2 ? Last - 2 : 0;
9035 unsigned Skipped = Exclude.size();
9036 auto S = Exclude.begin(), E = Exclude.end();
Alexey Bataeve3727102018-04-18 15:57:46 +00009037 for (unsigned I = First; I < Last; ++I) {
9038 if (std::find(S, E, I) != E) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00009039 --Skipped;
9040 continue;
9041 }
Alexey Bataeve3727102018-04-18 15:57:46 +00009042 Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'";
9043 if (I == Bound - Skipped)
9044 Out << " or ";
9045 else if (I != Bound + 1 - Skipped)
9046 Out << ", ";
Alexey Bataev6402bca2015-12-28 07:25:51 +00009047 }
Alexey Bataeve3727102018-04-18 15:57:46 +00009048 return Out.str();
Alexey Bataev6402bca2015-12-28 07:25:51 +00009049}
9050
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009051OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
9052 SourceLocation KindKwLoc,
9053 SourceLocation StartLoc,
9054 SourceLocation LParenLoc,
9055 SourceLocation EndLoc) {
9056 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00009057 static_assert(OMPC_DEFAULT_unknown > 0,
9058 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009059 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009060 << getListOfPossibleValues(OMPC_default, /*First=*/0,
9061 /*Last=*/OMPC_DEFAULT_unknown)
9062 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009063 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009064 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00009065 switch (Kind) {
9066 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009067 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009068 break;
9069 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009070 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009071 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009072 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009073 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00009074 break;
9075 }
Alexey Bataeved09d242014-05-28 05:53:51 +00009076 return new (Context)
9077 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009078}
9079
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009080OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
9081 SourceLocation KindKwLoc,
9082 SourceLocation StartLoc,
9083 SourceLocation LParenLoc,
9084 SourceLocation EndLoc) {
9085 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009086 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009087 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
9088 /*Last=*/OMPC_PROC_BIND_unknown)
9089 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009090 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009091 }
Alexey Bataeved09d242014-05-28 05:53:51 +00009092 return new (Context)
9093 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009094}
9095
Alexey Bataev56dafe82014-06-20 07:16:17 +00009096OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00009097 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00009098 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00009099 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00009100 SourceLocation EndLoc) {
9101 OMPClause *Res = nullptr;
9102 switch (Kind) {
9103 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00009104 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
9105 assert(Argument.size() == NumberOfElements &&
9106 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009107 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00009108 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
9109 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
9110 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
9111 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
9112 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009113 break;
9114 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00009115 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
9116 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
9117 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
9118 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009119 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00009120 case OMPC_dist_schedule:
9121 Res = ActOnOpenMPDistScheduleClause(
9122 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
9123 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
9124 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009125 case OMPC_defaultmap:
9126 enum { Modifier, DefaultmapKind };
9127 Res = ActOnOpenMPDefaultmapClause(
9128 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
9129 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
David Majnemer9d168222016-08-05 17:44:54 +00009130 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind],
9131 EndLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009132 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00009133 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009134 case OMPC_num_threads:
9135 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009136 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009137 case OMPC_collapse:
9138 case OMPC_default:
9139 case OMPC_proc_bind:
9140 case OMPC_private:
9141 case OMPC_firstprivate:
9142 case OMPC_lastprivate:
9143 case OMPC_shared:
9144 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00009145 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00009146 case OMPC_in_reduction:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009147 case OMPC_linear:
9148 case OMPC_aligned:
9149 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009150 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009151 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00009152 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009153 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009154 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009155 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00009156 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009157 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00009158 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00009159 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00009160 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009161 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009162 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00009163 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00009164 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009165 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00009166 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009167 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009168 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009169 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009170 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00009171 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00009172 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009173 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009174 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009175 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00009176 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00009177 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00009178 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00009179 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00009180 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00009181 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009182 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009183 case OMPC_dynamic_allocators:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009184 llvm_unreachable("Clause is not allowed.");
9185 }
9186 return Res;
9187}
9188
Alexey Bataev6402bca2015-12-28 07:25:51 +00009189static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
9190 OpenMPScheduleClauseModifier M2,
9191 SourceLocation M1Loc, SourceLocation M2Loc) {
9192 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
9193 SmallVector<unsigned, 2> Excluded;
9194 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
9195 Excluded.push_back(M2);
9196 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
9197 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
9198 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
9199 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
9200 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
9201 << getListOfPossibleValues(OMPC_schedule,
9202 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
9203 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
9204 Excluded)
9205 << getOpenMPClauseName(OMPC_schedule);
9206 return true;
9207 }
9208 return false;
9209}
9210
Alexey Bataev56dafe82014-06-20 07:16:17 +00009211OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00009212 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00009213 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00009214 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
9215 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
9216 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
9217 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
9218 return nullptr;
9219 // OpenMP, 2.7.1, Loop Construct, Restrictions
9220 // Either the monotonic modifier or the nonmonotonic modifier can be specified
9221 // but not both.
9222 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
9223 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
9224 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
9225 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
9226 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
9227 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
9228 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
9229 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
9230 return nullptr;
9231 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00009232 if (Kind == OMPC_SCHEDULE_unknown) {
9233 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00009234 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
9235 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
9236 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
9237 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
9238 Exclude);
9239 } else {
9240 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
9241 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009242 }
9243 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
9244 << Values << getOpenMPClauseName(OMPC_schedule);
9245 return nullptr;
9246 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00009247 // OpenMP, 2.7.1, Loop Construct, Restrictions
9248 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
9249 // schedule(guided).
9250 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
9251 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
9252 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
9253 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
9254 diag::err_omp_schedule_nonmonotonic_static);
9255 return nullptr;
9256 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00009257 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00009258 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00009259 if (ChunkSize) {
9260 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
9261 !ChunkSize->isInstantiationDependent() &&
9262 !ChunkSize->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00009263 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
Alexey Bataev56dafe82014-06-20 07:16:17 +00009264 ExprResult Val =
9265 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
9266 if (Val.isInvalid())
9267 return nullptr;
9268
9269 ValExpr = Val.get();
9270
9271 // OpenMP [2.7.1, Restrictions]
9272 // chunk_size must be a loop invariant integer expression with a positive
9273 // value.
9274 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00009275 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
9276 if (Result.isSigned() && !Result.isStrictlyPositive()) {
9277 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00009278 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00009279 return nullptr;
9280 }
Alexey Bataev2ba67042017-11-28 21:11:44 +00009281 } else if (getOpenMPCaptureRegionForClause(
9282 DSAStack->getCurrentDirective(), OMPC_schedule) !=
9283 OMPD_unknown &&
Alexey Bataevb46cdea2016-06-15 11:20:48 +00009284 !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00009285 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00009286 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev5a3af132016-03-29 08:58:54 +00009287 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
9288 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009289 }
9290 }
9291 }
9292
Alexey Bataev6402bca2015-12-28 07:25:51 +00009293 return new (Context)
9294 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00009295 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009296}
9297
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009298OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
9299 SourceLocation StartLoc,
9300 SourceLocation EndLoc) {
9301 OMPClause *Res = nullptr;
9302 switch (Kind) {
9303 case OMPC_ordered:
9304 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
9305 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00009306 case OMPC_nowait:
9307 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
9308 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009309 case OMPC_untied:
9310 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
9311 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009312 case OMPC_mergeable:
9313 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
9314 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009315 case OMPC_read:
9316 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
9317 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00009318 case OMPC_write:
9319 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
9320 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00009321 case OMPC_update:
9322 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
9323 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00009324 case OMPC_capture:
9325 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
9326 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009327 case OMPC_seq_cst:
9328 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
9329 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00009330 case OMPC_threads:
9331 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
9332 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009333 case OMPC_simd:
9334 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
9335 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00009336 case OMPC_nogroup:
9337 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
9338 break;
Kelvin Li1408f912018-09-26 04:28:39 +00009339 case OMPC_unified_address:
9340 Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc);
9341 break;
Patrick Lyster4a370b92018-10-01 13:47:43 +00009342 case OMPC_unified_shared_memory:
9343 Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
9344 break;
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009345 case OMPC_reverse_offload:
9346 Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc);
9347 break;
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009348 case OMPC_dynamic_allocators:
9349 Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc);
9350 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009351 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00009352 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009353 case OMPC_num_threads:
9354 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009355 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009356 case OMPC_collapse:
9357 case OMPC_schedule:
9358 case OMPC_private:
9359 case OMPC_firstprivate:
9360 case OMPC_lastprivate:
9361 case OMPC_shared:
9362 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00009363 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00009364 case OMPC_in_reduction:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009365 case OMPC_linear:
9366 case OMPC_aligned:
9367 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009368 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009369 case OMPC_default:
9370 case OMPC_proc_bind:
9371 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00009372 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009373 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00009374 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00009375 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009376 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009377 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009378 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009379 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00009380 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009381 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00009382 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009383 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009384 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009385 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00009386 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00009387 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00009388 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00009389 case OMPC_is_device_ptr:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009390 llvm_unreachable("Clause is not allowed.");
9391 }
9392 return Res;
9393}
9394
Alexey Bataev236070f2014-06-20 11:19:47 +00009395OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
9396 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00009397 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00009398 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
9399}
9400
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009401OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
9402 SourceLocation EndLoc) {
9403 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
9404}
9405
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009406OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
9407 SourceLocation EndLoc) {
9408 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
9409}
9410
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009411OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
9412 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009413 return new (Context) OMPReadClause(StartLoc, EndLoc);
9414}
9415
Alexey Bataevdea47612014-07-23 07:46:59 +00009416OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
9417 SourceLocation EndLoc) {
9418 return new (Context) OMPWriteClause(StartLoc, EndLoc);
9419}
9420
Alexey Bataev67a4f222014-07-23 10:25:33 +00009421OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
9422 SourceLocation EndLoc) {
9423 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
9424}
9425
Alexey Bataev459dec02014-07-24 06:46:57 +00009426OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
9427 SourceLocation EndLoc) {
9428 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
9429}
9430
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009431OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
9432 SourceLocation EndLoc) {
9433 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
9434}
9435
Alexey Bataev346265e2015-09-25 10:37:12 +00009436OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
9437 SourceLocation EndLoc) {
9438 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
9439}
9440
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009441OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
9442 SourceLocation EndLoc) {
9443 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
9444}
9445
Alexey Bataevb825de12015-12-07 10:51:44 +00009446OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
9447 SourceLocation EndLoc) {
9448 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
9449}
9450
Kelvin Li1408f912018-09-26 04:28:39 +00009451OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc,
9452 SourceLocation EndLoc) {
9453 return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc);
9454}
9455
Patrick Lyster4a370b92018-10-01 13:47:43 +00009456OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc,
9457 SourceLocation EndLoc) {
9458 return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
9459}
9460
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009461OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc,
9462 SourceLocation EndLoc) {
9463 return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc);
9464}
9465
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009466OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc,
9467 SourceLocation EndLoc) {
9468 return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc);
9469}
9470
Alexey Bataevc5e02582014-06-16 07:08:35 +00009471OMPClause *Sema::ActOnOpenMPVarListClause(
9472 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
9473 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
9474 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009475 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00009476 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
9477 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
9478 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009479 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009480 switch (Kind) {
9481 case OMPC_private:
9482 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9483 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009484 case OMPC_firstprivate:
9485 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9486 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00009487 case OMPC_lastprivate:
9488 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9489 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00009490 case OMPC_shared:
9491 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
9492 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009493 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00009494 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9495 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00009496 break;
Alexey Bataev169d96a2017-07-18 20:17:46 +00009497 case OMPC_task_reduction:
9498 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9499 EndLoc, ReductionIdScopeSpec,
9500 ReductionId);
9501 break;
Alexey Bataevfa312f32017-07-21 18:48:21 +00009502 case OMPC_in_reduction:
9503 Res =
9504 ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9505 EndLoc, ReductionIdScopeSpec, ReductionId);
9506 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00009507 case OMPC_linear:
9508 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00009509 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00009510 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009511 case OMPC_aligned:
9512 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
9513 ColonLoc, EndLoc);
9514 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009515 case OMPC_copyin:
9516 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
9517 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009518 case OMPC_copyprivate:
9519 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9520 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00009521 case OMPC_flush:
9522 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
9523 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009524 case OMPC_depend:
David Majnemer9d168222016-08-05 17:44:54 +00009525 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
Kelvin Li0bff7af2015-11-23 05:32:03 +00009526 StartLoc, LParenLoc, EndLoc);
9527 break;
9528 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00009529 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
9530 DepLinMapLoc, ColonLoc, VarList, StartLoc,
9531 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009532 break;
Samuel Antao661c0902016-05-26 17:39:58 +00009533 case OMPC_to:
9534 Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc);
9535 break;
Samuel Antaoec172c62016-05-26 17:49:04 +00009536 case OMPC_from:
9537 Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc);
9538 break;
Carlo Bertolli2404b172016-07-13 15:37:16 +00009539 case OMPC_use_device_ptr:
9540 Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
9541 break;
Carlo Bertolli70594e92016-07-13 17:16:49 +00009542 case OMPC_is_device_ptr:
9543 Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
9544 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009545 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00009546 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00009547 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00009548 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009549 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00009550 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009551 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009552 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009553 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009554 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00009555 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009556 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009557 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009558 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009559 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00009560 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00009561 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00009562 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009563 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00009564 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00009565 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009566 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009567 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009568 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009569 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009570 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00009571 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00009572 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009573 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00009574 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009575 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009576 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009577 case OMPC_uniform:
Kelvin Li1408f912018-09-26 04:28:39 +00009578 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00009579 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009580 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009581 case OMPC_dynamic_allocators:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009582 llvm_unreachable("Clause is not allowed.");
9583 }
9584 return Res;
9585}
9586
Alexey Bataev90c228f2016-02-08 09:29:13 +00009587ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00009588 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00009589 ExprResult Res = BuildDeclRefExpr(
9590 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
9591 if (!Res.isUsable())
9592 return ExprError();
9593 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
9594 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
9595 if (!Res.isUsable())
9596 return ExprError();
9597 }
9598 if (VK != VK_LValue && Res.get()->isGLValue()) {
9599 Res = DefaultLvalueConversion(Res.get());
9600 if (!Res.isUsable())
9601 return ExprError();
9602 }
9603 return Res;
9604}
9605
Alexey Bataev60da77e2016-02-29 05:54:20 +00009606static std::pair<ValueDecl *, bool>
9607getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
9608 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00009609 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
9610 RefExpr->containsUnexpandedParameterPack())
9611 return std::make_pair(nullptr, true);
9612
Alexey Bataevd985eda2016-02-10 11:29:16 +00009613 // OpenMP [3.1, C/C++]
9614 // A list item is a variable name.
9615 // OpenMP [2.9.3.3, Restrictions, p.1]
9616 // A variable that is part of another variable (as an array or
9617 // structure element) cannot appear in a private clause.
9618 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009619 enum {
9620 NoArrayExpr = -1,
9621 ArraySubscript = 0,
9622 OMPArraySection = 1
9623 } IsArrayExpr = NoArrayExpr;
9624 if (AllowArraySection) {
9625 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009626 Expr *Base = ASE->getBase()->IgnoreParenImpCasts();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009627 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
9628 Base = TempASE->getBase()->IgnoreParenImpCasts();
9629 RefExpr = Base;
9630 IsArrayExpr = ArraySubscript;
9631 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009632 Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009633 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
9634 Base = TempOASE->getBase()->IgnoreParenImpCasts();
9635 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
9636 Base = TempASE->getBase()->IgnoreParenImpCasts();
9637 RefExpr = Base;
9638 IsArrayExpr = OMPArraySection;
9639 }
9640 }
9641 ELoc = RefExpr->getExprLoc();
9642 ERange = RefExpr->getSourceRange();
9643 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00009644 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
9645 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
9646 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
9647 (S.getCurrentThisType().isNull() || !ME ||
9648 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
9649 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009650 if (IsArrayExpr != NoArrayExpr) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009651 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
9652 << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +00009653 } else {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009654 S.Diag(ELoc,
9655 AllowArraySection
9656 ? diag::err_omp_expected_var_name_member_expr_or_array_item
9657 : diag::err_omp_expected_var_name_member_expr)
9658 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
9659 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009660 return std::make_pair(nullptr, false);
9661 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +00009662 return std::make_pair(
9663 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009664}
9665
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009666OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
9667 SourceLocation StartLoc,
9668 SourceLocation LParenLoc,
9669 SourceLocation EndLoc) {
9670 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00009671 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeve3727102018-04-18 15:57:46 +00009672 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +00009673 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009674 SourceLocation ELoc;
9675 SourceRange ERange;
9676 Expr *SimpleRefExpr = RefExpr;
9677 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009678 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009679 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009680 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009681 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009682 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009683 ValueDecl *D = Res.first;
9684 if (!D)
9685 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009686
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009687 QualType Type = D->getType();
9688 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009689
9690 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9691 // A variable that appears in a private clause must not have an incomplete
9692 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009693 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009694 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009695 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009696
Alexey Bataev758e55e2013-09-06 18:03:48 +00009697 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9698 // in a Construct]
9699 // Variables with the predetermined data-sharing attributes may not be
9700 // listed in data-sharing attributes clauses, except for the cases
9701 // listed below. For these exceptions only, listing a predetermined
9702 // variable in a data-sharing attribute clause is allowed and overrides
9703 // the variable's predetermined data-sharing attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +00009704 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009705 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00009706 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9707 << getOpenMPClauseName(OMPC_private);
Alexey Bataeve3727102018-04-18 15:57:46 +00009708 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009709 continue;
9710 }
9711
Alexey Bataeve3727102018-04-18 15:57:46 +00009712 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009713 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00009714 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Kelvin Libf594a52016-12-17 05:48:59 +00009715 isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009716 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
9717 << getOpenMPClauseName(OMPC_private) << Type
Kelvin Libf594a52016-12-17 05:48:59 +00009718 << getOpenMPDirectiveName(CurrDir);
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009719 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009720 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009721 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009722 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009723 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009724 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009725 continue;
9726 }
9727
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009728 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
9729 // A list item cannot appear in both a map clause and a data-sharing
9730 // attribute clause on the same construct
Alexey Bataeve3727102018-04-18 15:57:46 +00009731 if (isOpenMPTargetExecutionDirective(CurrDir)) {
Samuel Antao6890b092016-07-28 14:25:09 +00009732 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +00009733 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +00009734 VD, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +00009735 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
9736 OpenMPClauseKind WhereFoundClauseKind) -> bool {
9737 ConflictKind = WhereFoundClauseKind;
9738 return true;
9739 })) {
9740 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009741 << getOpenMPClauseName(OMPC_private)
Samuel Antao6890b092016-07-28 14:25:09 +00009742 << getOpenMPClauseName(ConflictKind)
Kelvin Libf594a52016-12-17 05:48:59 +00009743 << getOpenMPDirectiveName(CurrDir);
Alexey Bataeve3727102018-04-18 15:57:46 +00009744 reportOriginalDsa(*this, DSAStack, D, DVar);
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009745 continue;
9746 }
9747 }
9748
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009749 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
9750 // A variable of class type (or array thereof) that appears in a private
9751 // clause requires an accessible, unambiguous default constructor for the
9752 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00009753 // Generate helper private variable and initialize it with the default
9754 // value. The address of the original variable is replaced by the address of
9755 // the new private variable in CodeGen. This new variable is not added to
9756 // IdResolver, so the code in the OpenMP region uses original variable for
9757 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009758 Type = Type.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +00009759 VarDecl *VDPrivate =
Alexey Bataev63cc8e92018-03-20 14:45:59 +00009760 buildVarDecl(*this, ELoc, Type, D->getName(),
9761 D->hasAttrs() ? &D->getAttrs() : nullptr,
9762 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Richard Smith3beb7c62017-01-12 02:27:38 +00009763 ActOnUninitializedDecl(VDPrivate);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009764 if (VDPrivate->isInvalidDecl())
9765 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00009766 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009767 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009768
Alexey Bataev90c228f2016-02-08 09:29:13 +00009769 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009770 if (!VD && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00009771 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00009772 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009773 Vars.push_back((VD || CurContext->isDependentContext())
9774 ? RefExpr->IgnoreParens()
9775 : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009776 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009777 }
9778
Alexey Bataeved09d242014-05-28 05:53:51 +00009779 if (Vars.empty())
9780 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009781
Alexey Bataev03b340a2014-10-21 03:16:40 +00009782 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
9783 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009784}
9785
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009786namespace {
9787class DiagsUninitializedSeveretyRAII {
9788private:
9789 DiagnosticsEngine &Diags;
9790 SourceLocation SavedLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00009791 bool IsIgnored = false;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009792
9793public:
9794 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
9795 bool IsIgnored)
9796 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
9797 if (!IsIgnored) {
9798 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
9799 /*Map*/ diag::Severity::Ignored, Loc);
9800 }
9801 }
9802 ~DiagsUninitializedSeveretyRAII() {
9803 if (!IsIgnored)
9804 Diags.popMappings(SavedLoc);
9805 }
9806};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00009807}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009808
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009809OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
9810 SourceLocation StartLoc,
9811 SourceLocation LParenLoc,
9812 SourceLocation EndLoc) {
9813 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009814 SmallVector<Expr *, 8> PrivateCopies;
9815 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00009816 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009817 bool IsImplicitClause =
9818 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
Alexey Bataeve3727102018-04-18 15:57:46 +00009819 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009820
Alexey Bataeve3727102018-04-18 15:57:46 +00009821 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +00009822 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009823 SourceLocation ELoc;
9824 SourceRange ERange;
9825 Expr *SimpleRefExpr = RefExpr;
9826 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009827 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009828 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009829 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009830 PrivateCopies.push_back(nullptr);
9831 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009832 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009833 ValueDecl *D = Res.first;
9834 if (!D)
9835 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009836
Alexey Bataev60da77e2016-02-29 05:54:20 +00009837 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00009838 QualType Type = D->getType();
9839 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009840
9841 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9842 // A variable that appears in a private clause must not have an incomplete
9843 // type or a reference type.
9844 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00009845 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009846 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009847 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009848
9849 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
9850 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00009851 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009852 // class type.
Alexey Bataeve3727102018-04-18 15:57:46 +00009853 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009854
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009855 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00009856 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009857 if (!IsImplicitClause) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009858 DSAStackTy::DSAVarData DVar =
9859 DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00009860 TopDVar = DVar;
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009861 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009862 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009863 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
9864 // A list item that specifies a given variable may not appear in more
9865 // than one clause on the same directive, except that a variable may be
9866 // specified in both firstprivate and lastprivate clauses.
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009867 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
9868 // A list item may appear in a firstprivate or lastprivate clause but not
9869 // both.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009870 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevb358f992017-12-01 17:40:15 +00009871 (isOpenMPDistributeDirective(CurrDir) ||
9872 DVar.CKind != OMPC_lastprivate) &&
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009873 DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009874 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00009875 << getOpenMPClauseName(DVar.CKind)
9876 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +00009877 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009878 continue;
9879 }
9880
9881 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9882 // in a Construct]
9883 // Variables with the predetermined data-sharing attributes may not be
9884 // listed in data-sharing attributes clauses, except for the cases
9885 // listed below. For these exceptions only, listing a predetermined
9886 // variable in a data-sharing attribute clause is allowed and overrides
9887 // the variable's predetermined data-sharing attributes.
9888 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9889 // in a Construct, C/C++, p.2]
9890 // Variables with const-qualified type having no mutable member may be
9891 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00009892 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009893 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
9894 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00009895 << getOpenMPClauseName(DVar.CKind)
9896 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +00009897 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009898 continue;
9899 }
9900
9901 // OpenMP [2.9.3.4, Restrictions, p.2]
9902 // A list item that is private within a parallel region must not appear
9903 // in a firstprivate clause on a worksharing construct if any of the
9904 // worksharing regions arising from the worksharing construct ever bind
9905 // to any of the parallel regions arising from the parallel construct.
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009906 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
9907 // A list item that is private within a teams region must not appear in a
9908 // firstprivate clause on a distribute construct if any of the distribute
9909 // regions arising from the distribute construct ever bind to any of the
9910 // teams regions arising from the teams construct.
9911 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
9912 // A list item that appears in a reduction clause of a teams construct
9913 // must not appear in a firstprivate clause on a distribute construct if
9914 // any of the distribute regions arising from the distribute construct
9915 // ever bind to any of the teams regions arising from the teams construct.
9916 if ((isOpenMPWorksharingDirective(CurrDir) ||
9917 isOpenMPDistributeDirective(CurrDir)) &&
Kelvin Li579e41c2016-11-30 23:51:03 +00009918 !isOpenMPParallelDirective(CurrDir) &&
9919 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00009920 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009921 if (DVar.CKind != OMPC_shared &&
9922 (isOpenMPParallelDirective(DVar.DKind) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009923 isOpenMPTeamsDirective(DVar.DKind) ||
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009924 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00009925 Diag(ELoc, diag::err_omp_required_access)
9926 << getOpenMPClauseName(OMPC_firstprivate)
9927 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +00009928 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00009929 continue;
9930 }
9931 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009932 // OpenMP [2.9.3.4, Restrictions, p.3]
9933 // A list item that appears in a reduction clause of a parallel construct
9934 // must not appear in a firstprivate clause on a worksharing or task
9935 // construct if any of the worksharing or task regions arising from the
9936 // worksharing or task construct ever bind to any of the parallel regions
9937 // arising from the parallel construct.
9938 // OpenMP [2.9.3.4, Restrictions, p.4]
9939 // A list item that appears in a reduction clause in worksharing
9940 // construct must not appear in a firstprivate clause in a task construct
9941 // encountered during execution of any of the worksharing regions arising
9942 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +00009943 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00009944 DVar = DSAStack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00009945 D, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
9946 [](OpenMPDirectiveKind K) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00009947 return isOpenMPParallelDirective(K) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009948 isOpenMPWorksharingDirective(K) ||
9949 isOpenMPTeamsDirective(K);
Alexey Bataev7ace49d2016-05-17 08:55:33 +00009950 },
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009951 /*FromParent=*/true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009952 if (DVar.CKind == OMPC_reduction &&
9953 (isOpenMPParallelDirective(DVar.DKind) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009954 isOpenMPWorksharingDirective(DVar.DKind) ||
9955 isOpenMPTeamsDirective(DVar.DKind))) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009956 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
9957 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataeve3727102018-04-18 15:57:46 +00009958 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009959 continue;
9960 }
9961 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00009962
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009963 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
9964 // A list item cannot appear in both a map clause and a data-sharing
9965 // attribute clause on the same construct
Alexey Bataevb358f992017-12-01 17:40:15 +00009966 if (isOpenMPTargetExecutionDirective(CurrDir)) {
Samuel Antao6890b092016-07-28 14:25:09 +00009967 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +00009968 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +00009969 VD, /*CurrentRegionOnly=*/true,
Alexey Bataeve3727102018-04-18 15:57:46 +00009970 [&ConflictKind](
9971 OMPClauseMappableExprCommon::MappableExprComponentListRef,
9972 OpenMPClauseKind WhereFoundClauseKind) {
Samuel Antao6890b092016-07-28 14:25:09 +00009973 ConflictKind = WhereFoundClauseKind;
9974 return true;
9975 })) {
9976 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009977 << getOpenMPClauseName(OMPC_firstprivate)
Samuel Antao6890b092016-07-28 14:25:09 +00009978 << getOpenMPClauseName(ConflictKind)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009979 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +00009980 reportOriginalDsa(*this, DSAStack, D, DVar);
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009981 continue;
9982 }
9983 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009984 }
9985
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009986 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00009987 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00009988 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009989 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
9990 << getOpenMPClauseName(OMPC_firstprivate) << Type
9991 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
9992 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00009993 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009994 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00009995 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009996 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00009997 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009998 continue;
9999 }
10000
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010001 Type = Type.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010002 VarDecl *VDPrivate =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000010003 buildVarDecl(*this, ELoc, Type, D->getName(),
10004 D->hasAttrs() ? &D->getAttrs() : nullptr,
10005 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010006 // Generate helper private variable and initialize it with the value of the
10007 // original variable. The address of the original variable is replaced by
10008 // the address of the new private variable in the CodeGen. This new variable
10009 // is not added to IdResolver, so the code in the OpenMP region uses
10010 // original variable for proper diagnostics and variable capturing.
10011 Expr *VDInitRefExpr = nullptr;
10012 // For arrays generate initializer for single element and replace it by the
10013 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010014 if (Type->isArrayType()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010015 VarDecl *VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +000010016 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010017 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataeve3727102018-04-18 15:57:46 +000010018 Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010019 ElemType = ElemType.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010020 VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
10021 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +000010022 InitializedEntity Entity =
10023 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010024 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
10025
10026 InitializationSequence InitSeq(*this, Entity, Kind, Init);
10027 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
10028 if (Result.isInvalid())
10029 VDPrivate->setInvalidDecl();
10030 else
10031 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010032 // Remove temp variable declaration.
10033 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010034 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +000010035 VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
10036 ".firstprivate.temp");
Alexey Bataevd985eda2016-02-10 11:29:16 +000010037 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
10038 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +000010039 AddInitializerToDecl(VDPrivate,
10040 DefaultLvalueConversion(VDInitRefExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000010041 /*DirectInit=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010042 }
10043 if (VDPrivate->isInvalidDecl()) {
10044 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +000010045 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010046 diag::note_omp_task_predetermined_firstprivate_here);
10047 }
10048 continue;
10049 }
10050 CurContext->addDecl(VDPrivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000010051 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +000010052 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
10053 RefExpr->getExprLoc());
10054 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010055 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010056 if (TopDVar.CKind == OMPC_lastprivate) {
Alexey Bataev005248a2016-02-25 05:25:57 +000010057 Ref = TopDVar.PrivateCopy;
Alexey Bataeve3727102018-04-18 15:57:46 +000010058 } else {
Alexey Bataev61205072016-03-02 04:57:40 +000010059 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataeve3727102018-04-18 15:57:46 +000010060 if (!isOpenMPCapturedDecl(D))
Alexey Bataev005248a2016-02-25 05:25:57 +000010061 ExprCaptures.push_back(Ref->getDecl());
10062 }
Alexey Bataev417089f2016-02-17 13:19:37 +000010063 }
Alexey Bataevd985eda2016-02-10 11:29:16 +000010064 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010065 Vars.push_back((VD || CurContext->isDependentContext())
10066 ? RefExpr->IgnoreParens()
10067 : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010068 PrivateCopies.push_back(VDPrivateRefExpr);
10069 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010070 }
10071
Alexey Bataeved09d242014-05-28 05:53:51 +000010072 if (Vars.empty())
10073 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010074
10075 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +000010076 Vars, PrivateCopies, Inits,
10077 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010078}
10079
Alexander Musman1bb328c2014-06-04 13:06:39 +000010080OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
10081 SourceLocation StartLoc,
10082 SourceLocation LParenLoc,
10083 SourceLocation EndLoc) {
10084 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +000010085 SmallVector<Expr *, 8> SrcExprs;
10086 SmallVector<Expr *, 8> DstExprs;
10087 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +000010088 SmallVector<Decl *, 4> ExprCaptures;
10089 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeve3727102018-04-18 15:57:46 +000010090 for (Expr *RefExpr : VarList) {
Alexander Musman1bb328c2014-06-04 13:06:39 +000010091 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +000010092 SourceLocation ELoc;
10093 SourceRange ERange;
10094 Expr *SimpleRefExpr = RefExpr;
10095 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +000010096 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +000010097 // It will be analyzed later.
10098 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +000010099 SrcExprs.push_back(nullptr);
10100 DstExprs.push_back(nullptr);
10101 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010102 }
Alexey Bataev74caaf22016-02-20 04:09:36 +000010103 ValueDecl *D = Res.first;
10104 if (!D)
10105 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +000010106
Alexey Bataev74caaf22016-02-20 04:09:36 +000010107 QualType Type = D->getType();
10108 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010109
10110 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
10111 // A variable that appears in a lastprivate clause must not have an
10112 // incomplete type or a reference type.
10113 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +000010114 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +000010115 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010116 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +000010117
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010118 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexander Musman1bb328c2014-06-04 13:06:39 +000010119 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
10120 // in a Construct]
10121 // Variables with the predetermined data-sharing attributes may not be
10122 // listed in data-sharing attributes clauses, except for the cases
10123 // listed below.
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010124 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
10125 // A list item may appear in a firstprivate or lastprivate clause but not
10126 // both.
Alexey Bataeve3727102018-04-18 15:57:46 +000010127 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010128 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
Alexey Bataevb358f992017-12-01 17:40:15 +000010129 (isOpenMPDistributeDirective(CurrDir) ||
10130 DVar.CKind != OMPC_firstprivate) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +000010131 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
10132 Diag(ELoc, diag::err_omp_wrong_dsa)
10133 << getOpenMPClauseName(DVar.CKind)
10134 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000010135 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010136 continue;
10137 }
10138
Alexey Bataevf29276e2014-06-18 04:14:57 +000010139 // OpenMP [2.14.3.5, Restrictions, p.2]
10140 // A list item that is private within a parallel region, or that appears in
10141 // the reduction clause of a parallel construct, must not appear in a
10142 // lastprivate clause on a worksharing construct if any of the corresponding
10143 // worksharing regions ever binds to any of the corresponding parallel
10144 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +000010145 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +000010146 if (isOpenMPWorksharingDirective(CurrDir) &&
Kelvin Li579e41c2016-11-30 23:51:03 +000010147 !isOpenMPParallelDirective(CurrDir) &&
10148 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +000010149 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +000010150 if (DVar.CKind != OMPC_shared) {
10151 Diag(ELoc, diag::err_omp_required_access)
10152 << getOpenMPClauseName(OMPC_lastprivate)
10153 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +000010154 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +000010155 continue;
10156 }
10157 }
Alexey Bataev74caaf22016-02-20 04:09:36 +000010158
Alexander Musman1bb328c2014-06-04 13:06:39 +000010159 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +000010160 // A variable of class type (or array thereof) that appears in a
10161 // lastprivate clause requires an accessible, unambiguous default
10162 // constructor for the class type, unless the list item is also specified
10163 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +000010164 // A variable of class type (or array thereof) that appears in a
10165 // lastprivate clause requires an accessible, unambiguous copy assignment
10166 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +000010167 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010168 VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(),
10169 Type.getUnqualifiedType(), ".lastprivate.src",
10170 D->hasAttrs() ? &D->getAttrs() : nullptr);
10171 DeclRefExpr *PseudoSrcExpr =
Alexey Bataev74caaf22016-02-20 04:09:36 +000010172 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataeve3727102018-04-18 15:57:46 +000010173 VarDecl *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +000010174 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +000010175 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000010176 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +000010177 // For arrays generate assignment operation for single element and replace
10178 // it by the original array element in CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +000010179 ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
10180 PseudoDstExpr, PseudoSrcExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +000010181 if (AssignmentOp.isInvalid())
10182 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +000010183 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +000010184 /*DiscardedValue=*/true);
10185 if (AssignmentOp.isInvalid())
10186 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +000010187
Alexey Bataev74caaf22016-02-20 04:09:36 +000010188 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010189 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010190 if (TopDVar.CKind == OMPC_firstprivate) {
Alexey Bataev005248a2016-02-25 05:25:57 +000010191 Ref = TopDVar.PrivateCopy;
Alexey Bataeve3727102018-04-18 15:57:46 +000010192 } else {
Alexey Bataev61205072016-03-02 04:57:40 +000010193 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000010194 if (!isOpenMPCapturedDecl(D))
Alexey Bataev005248a2016-02-25 05:25:57 +000010195 ExprCaptures.push_back(Ref->getDecl());
10196 }
10197 if (TopDVar.CKind == OMPC_firstprivate ||
Alexey Bataeve3727102018-04-18 15:57:46 +000010198 (!isOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010199 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +000010200 ExprResult RefRes = DefaultLvalueConversion(Ref);
10201 if (!RefRes.isUsable())
10202 continue;
10203 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +000010204 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
10205 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +000010206 if (!PostUpdateRes.isUsable())
10207 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +000010208 ExprPostUpdates.push_back(
10209 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +000010210 }
10211 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010212 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010213 Vars.push_back((VD || CurContext->isDependentContext())
10214 ? RefExpr->IgnoreParens()
10215 : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +000010216 SrcExprs.push_back(PseudoSrcExpr);
10217 DstExprs.push_back(PseudoDstExpr);
10218 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +000010219 }
10220
10221 if (Vars.empty())
10222 return nullptr;
10223
10224 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +000010225 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +000010226 buildPreInits(Context, ExprCaptures),
10227 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +000010228}
10229
Alexey Bataev758e55e2013-09-06 18:03:48 +000010230OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
10231 SourceLocation StartLoc,
10232 SourceLocation LParenLoc,
10233 SourceLocation EndLoc) {
10234 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +000010235 for (Expr *RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010236 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +000010237 SourceLocation ELoc;
10238 SourceRange ERange;
10239 Expr *SimpleRefExpr = RefExpr;
10240 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010241 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +000010242 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000010243 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010244 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010245 ValueDecl *D = Res.first;
10246 if (!D)
10247 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +000010248
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010249 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010250 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
10251 // in a Construct]
10252 // Variables with the predetermined data-sharing attributes may not be
10253 // listed in data-sharing attributes clauses, except for the cases
10254 // listed below. For these exceptions only, listing a predetermined
10255 // variable in a data-sharing attribute clause is allowed and overrides
10256 // the variable's predetermined data-sharing attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +000010257 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataeved09d242014-05-28 05:53:51 +000010258 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
10259 DVar.RefExpr) {
10260 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
10261 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +000010262 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010263 continue;
10264 }
10265
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010266 DeclRefExpr *Ref = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000010267 if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +000010268 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010269 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010270 Vars.push_back((VD || !Ref || CurContext->isDependentContext())
10271 ? RefExpr->IgnoreParens()
10272 : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010273 }
10274
Alexey Bataeved09d242014-05-28 05:53:51 +000010275 if (Vars.empty())
10276 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +000010277
10278 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
10279}
10280
Alexey Bataevc5e02582014-06-16 07:08:35 +000010281namespace {
10282class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
10283 DSAStackTy *Stack;
10284
10285public:
10286 bool VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010287 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
10288 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataevc5e02582014-06-16 07:08:35 +000010289 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
10290 return false;
10291 if (DVar.CKind != OMPC_unknown)
10292 return true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010293 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +000010294 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) { return true; },
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010295 /*FromParent=*/true);
Alexey Bataeve3727102018-04-18 15:57:46 +000010296 return DVarPrivate.CKind != OMPC_unknown;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010297 }
10298 return false;
10299 }
10300 bool VisitStmt(Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010301 for (Stmt *Child : S->children()) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010302 if (Child && Visit(Child))
10303 return true;
10304 }
10305 return false;
10306 }
Alexey Bataev23b69422014-06-18 07:08:49 +000010307 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +000010308};
Alexey Bataev23b69422014-06-18 07:08:49 +000010309} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +000010310
Alexey Bataev60da77e2016-02-29 05:54:20 +000010311namespace {
10312// Transform MemberExpression for specified FieldDecl of current class to
10313// DeclRefExpr to specified OMPCapturedExprDecl.
10314class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
10315 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
Alexey Bataeve3727102018-04-18 15:57:46 +000010316 ValueDecl *Field = nullptr;
10317 DeclRefExpr *CapturedExpr = nullptr;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010318
10319public:
10320 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
10321 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
10322
10323 ExprResult TransformMemberExpr(MemberExpr *E) {
10324 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
10325 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +000010326 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +000010327 return CapturedExpr;
10328 }
10329 return BaseTransform::TransformMemberExpr(E);
10330 }
10331 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
10332};
10333} // namespace
10334
Alexey Bataev97d18bf2018-04-11 19:21:00 +000010335template <typename T, typename U>
10336static T filterLookupForUDR(SmallVectorImpl<U> &Lookups,
10337 const llvm::function_ref<T(ValueDecl *)> Gen) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010338 for (U &Set : Lookups) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010339 for (auto *D : Set) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010340 if (T Res = Gen(cast<ValueDecl>(D)))
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010341 return Res;
10342 }
10343 }
10344 return T();
10345}
10346
Alexey Bataev43b90b72018-09-12 16:31:59 +000010347static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) {
10348 assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case");
10349
10350 for (auto RD : D->redecls()) {
10351 // Don't bother with extra checks if we already know this one isn't visible.
10352 if (RD == D)
10353 continue;
10354
10355 auto ND = cast<NamedDecl>(RD);
10356 if (LookupResult::isVisible(SemaRef, ND))
10357 return ND;
10358 }
10359
10360 return nullptr;
10361}
10362
10363static void
10364argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &ReductionId,
10365 SourceLocation Loc, QualType Ty,
10366 SmallVectorImpl<UnresolvedSet<8>> &Lookups) {
10367 // Find all of the associated namespaces and classes based on the
10368 // arguments we have.
10369 Sema::AssociatedNamespaceSet AssociatedNamespaces;
10370 Sema::AssociatedClassSet AssociatedClasses;
10371 OpaqueValueExpr OVE(Loc, Ty, VK_LValue);
10372 SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces,
10373 AssociatedClasses);
10374
10375 // C++ [basic.lookup.argdep]p3:
10376 // Let X be the lookup set produced by unqualified lookup (3.4.1)
10377 // and let Y be the lookup set produced by argument dependent
10378 // lookup (defined as follows). If X contains [...] then Y is
10379 // empty. Otherwise Y is the set of declarations found in the
10380 // namespaces associated with the argument types as described
10381 // below. The set of declarations found by the lookup of the name
10382 // is the union of X and Y.
10383 //
10384 // Here, we compute Y and add its members to the overloaded
10385 // candidate set.
10386 for (auto *NS : AssociatedNamespaces) {
10387 // When considering an associated namespace, the lookup is the
10388 // same as the lookup performed when the associated namespace is
10389 // used as a qualifier (3.4.3.2) except that:
10390 //
10391 // -- Any using-directives in the associated namespace are
10392 // ignored.
10393 //
10394 // -- Any namespace-scope friend functions declared in
10395 // associated classes are visible within their respective
10396 // namespaces even if they are not visible during an ordinary
10397 // lookup (11.4).
10398 DeclContext::lookup_result R = NS->lookup(ReductionId.getName());
10399 for (auto *D : R) {
10400 auto *Underlying = D;
10401 if (auto *USD = dyn_cast<UsingShadowDecl>(D))
10402 Underlying = USD->getTargetDecl();
10403
10404 if (!isa<OMPDeclareReductionDecl>(Underlying))
10405 continue;
10406
10407 if (!SemaRef.isVisible(D)) {
10408 D = findAcceptableDecl(SemaRef, D);
10409 if (!D)
10410 continue;
10411 if (auto *USD = dyn_cast<UsingShadowDecl>(D))
10412 Underlying = USD->getTargetDecl();
10413 }
10414 Lookups.emplace_back();
10415 Lookups.back().addDecl(Underlying);
10416 }
10417 }
10418}
10419
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010420static ExprResult
10421buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
10422 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
10423 const DeclarationNameInfo &ReductionId, QualType Ty,
10424 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
10425 if (ReductionIdScopeSpec.isInvalid())
10426 return ExprError();
10427 SmallVector<UnresolvedSet<8>, 4> Lookups;
10428 if (S) {
10429 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
10430 Lookup.suppressDiagnostics();
10431 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010432 NamedDecl *D = Lookup.getRepresentativeDecl();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010433 do {
10434 S = S->getParent();
10435 } while (S && !S->isDeclScope(D));
10436 if (S)
10437 S = S->getParent();
Alexey Bataev43b90b72018-09-12 16:31:59 +000010438 Lookups.emplace_back();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010439 Lookups.back().append(Lookup.begin(), Lookup.end());
10440 Lookup.clear();
10441 }
10442 } else if (auto *ULE =
10443 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
10444 Lookups.push_back(UnresolvedSet<8>());
10445 Decl *PrevD = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000010446 for (NamedDecl *D : ULE->decls()) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010447 if (D == PrevD)
10448 Lookups.push_back(UnresolvedSet<8>());
10449 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
10450 Lookups.back().addDecl(DRD);
10451 PrevD = D;
10452 }
10453 }
Alexey Bataevfdc20352017-08-25 15:43:55 +000010454 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() ||
10455 Ty->isInstantiationDependentType() ||
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010456 Ty->containsUnexpandedParameterPack() ||
Alexey Bataeve3727102018-04-18 15:57:46 +000010457 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010458 return !D->isInvalidDecl() &&
10459 (D->getType()->isDependentType() ||
10460 D->getType()->isInstantiationDependentType() ||
10461 D->getType()->containsUnexpandedParameterPack());
10462 })) {
10463 UnresolvedSet<8> ResSet;
Alexey Bataeve3727102018-04-18 15:57:46 +000010464 for (const UnresolvedSet<8> &Set : Lookups) {
Alexey Bataev43b90b72018-09-12 16:31:59 +000010465 if (Set.empty())
10466 continue;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010467 ResSet.append(Set.begin(), Set.end());
10468 // The last item marks the end of all declarations at the specified scope.
10469 ResSet.addDecl(Set[Set.size() - 1]);
10470 }
10471 return UnresolvedLookupExpr::Create(
10472 SemaRef.Context, /*NamingClass=*/nullptr,
10473 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
10474 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
10475 }
Alexey Bataev43b90b72018-09-12 16:31:59 +000010476 // Lookup inside the classes.
10477 // C++ [over.match.oper]p3:
10478 // For a unary operator @ with an operand of a type whose
10479 // cv-unqualified version is T1, and for a binary operator @ with
10480 // a left operand of a type whose cv-unqualified version is T1 and
10481 // a right operand of a type whose cv-unqualified version is T2,
10482 // three sets of candidate functions, designated member
10483 // candidates, non-member candidates and built-in candidates, are
10484 // constructed as follows:
10485 // -- If T1 is a complete class type or a class currently being
10486 // defined, the set of member candidates is the result of the
10487 // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise,
10488 // the set of member candidates is empty.
10489 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
10490 Lookup.suppressDiagnostics();
10491 if (const auto *TyRec = Ty->getAs<RecordType>()) {
10492 // Complete the type if it can be completed.
10493 // If the type is neither complete nor being defined, bail out now.
10494 if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() ||
10495 TyRec->getDecl()->getDefinition()) {
10496 Lookup.clear();
10497 SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl());
10498 if (Lookup.empty()) {
10499 Lookups.emplace_back();
10500 Lookups.back().append(Lookup.begin(), Lookup.end());
10501 }
10502 }
10503 }
10504 // Perform ADL.
10505 argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010506 if (auto *VD = filterLookupForUDR<ValueDecl *>(
10507 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
10508 if (!D->isInvalidDecl() &&
10509 SemaRef.Context.hasSameType(D->getType(), Ty))
10510 return D;
10511 return nullptr;
10512 }))
10513 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
10514 if (auto *VD = filterLookupForUDR<ValueDecl *>(
10515 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
10516 if (!D->isInvalidDecl() &&
10517 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
10518 !Ty.isMoreQualifiedThan(D->getType()))
10519 return D;
10520 return nullptr;
10521 })) {
10522 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
10523 /*DetectVirtual=*/false);
10524 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
10525 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
10526 VD->getType().getUnqualifiedType()))) {
10527 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
10528 /*DiagID=*/0) !=
10529 Sema::AR_inaccessible) {
10530 SemaRef.BuildBasePathArray(Paths, BasePath);
10531 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
10532 }
10533 }
10534 }
10535 }
10536 if (ReductionIdScopeSpec.isSet()) {
10537 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
10538 return ExprError();
10539 }
10540 return ExprEmpty();
10541}
10542
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010543namespace {
10544/// Data for the reduction-based clauses.
10545struct ReductionData {
10546 /// List of original reduction items.
10547 SmallVector<Expr *, 8> Vars;
10548 /// List of private copies of the reduction items.
10549 SmallVector<Expr *, 8> Privates;
10550 /// LHS expressions for the reduction_op expressions.
10551 SmallVector<Expr *, 8> LHSs;
10552 /// RHS expressions for the reduction_op expressions.
10553 SmallVector<Expr *, 8> RHSs;
10554 /// Reduction operation expression.
10555 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev88202be2017-07-27 13:20:36 +000010556 /// Taskgroup descriptors for the corresponding reduction items in
10557 /// in_reduction clauses.
10558 SmallVector<Expr *, 8> TaskgroupDescriptors;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010559 /// List of captures for clause.
10560 SmallVector<Decl *, 4> ExprCaptures;
10561 /// List of postupdate expressions.
10562 SmallVector<Expr *, 4> ExprPostUpdates;
10563 ReductionData() = delete;
10564 /// Reserves required memory for the reduction data.
10565 ReductionData(unsigned Size) {
10566 Vars.reserve(Size);
10567 Privates.reserve(Size);
10568 LHSs.reserve(Size);
10569 RHSs.reserve(Size);
10570 ReductionOps.reserve(Size);
Alexey Bataev88202be2017-07-27 13:20:36 +000010571 TaskgroupDescriptors.reserve(Size);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010572 ExprCaptures.reserve(Size);
10573 ExprPostUpdates.reserve(Size);
10574 }
10575 /// Stores reduction item and reduction operation only (required for dependent
10576 /// reduction item).
10577 void push(Expr *Item, Expr *ReductionOp) {
10578 Vars.emplace_back(Item);
10579 Privates.emplace_back(nullptr);
10580 LHSs.emplace_back(nullptr);
10581 RHSs.emplace_back(nullptr);
10582 ReductionOps.emplace_back(ReductionOp);
Alexey Bataev88202be2017-07-27 13:20:36 +000010583 TaskgroupDescriptors.emplace_back(nullptr);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010584 }
10585 /// Stores reduction data.
Alexey Bataev88202be2017-07-27 13:20:36 +000010586 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp,
10587 Expr *TaskgroupDescriptor) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010588 Vars.emplace_back(Item);
10589 Privates.emplace_back(Private);
10590 LHSs.emplace_back(LHS);
10591 RHSs.emplace_back(RHS);
10592 ReductionOps.emplace_back(ReductionOp);
Alexey Bataev88202be2017-07-27 13:20:36 +000010593 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010594 }
10595};
10596} // namespace
10597
Alexey Bataeve3727102018-04-18 15:57:46 +000010598static bool checkOMPArraySectionConstantForReduction(
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010599 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement,
10600 SmallVectorImpl<llvm::APSInt> &ArraySizes) {
10601 const Expr *Length = OASE->getLength();
10602 if (Length == nullptr) {
10603 // For array sections of the form [1:] or [:], we would need to analyze
10604 // the lower bound...
10605 if (OASE->getColonLoc().isValid())
10606 return false;
10607
10608 // This is an array subscript which has implicit length 1!
10609 SingleElement = true;
10610 ArraySizes.push_back(llvm::APSInt::get(1));
10611 } else {
10612 llvm::APSInt ConstantLengthValue;
10613 if (!Length->EvaluateAsInt(ConstantLengthValue, Context))
10614 return false;
10615
10616 SingleElement = (ConstantLengthValue.getSExtValue() == 1);
10617 ArraySizes.push_back(ConstantLengthValue);
10618 }
10619
10620 // Get the base of this array section and walk up from there.
10621 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
10622
10623 // We require length = 1 for all array sections except the right-most to
10624 // guarantee that the memory region is contiguous and has no holes in it.
10625 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) {
10626 Length = TempOASE->getLength();
10627 if (Length == nullptr) {
10628 // For array sections of the form [1:] or [:], we would need to analyze
10629 // the lower bound...
10630 if (OASE->getColonLoc().isValid())
10631 return false;
10632
10633 // This is an array subscript which has implicit length 1!
10634 ArraySizes.push_back(llvm::APSInt::get(1));
10635 } else {
10636 llvm::APSInt ConstantLengthValue;
10637 if (!Length->EvaluateAsInt(ConstantLengthValue, Context) ||
10638 ConstantLengthValue.getSExtValue() != 1)
10639 return false;
10640
10641 ArraySizes.push_back(ConstantLengthValue);
10642 }
10643 Base = TempOASE->getBase()->IgnoreParenImpCasts();
10644 }
10645
10646 // If we have a single element, we don't need to add the implicit lengths.
10647 if (!SingleElement) {
10648 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) {
10649 // Has implicit length 1!
10650 ArraySizes.push_back(llvm::APSInt::get(1));
10651 Base = TempASE->getBase()->IgnoreParenImpCasts();
10652 }
10653 }
10654
10655 // This array section can be privatized as a single value or as a constant
10656 // sized array.
10657 return true;
10658}
10659
Alexey Bataeve3727102018-04-18 15:57:46 +000010660static bool actOnOMPReductionKindClause(
Alexey Bataev169d96a2017-07-18 20:17:46 +000010661 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind,
10662 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
10663 SourceLocation ColonLoc, SourceLocation EndLoc,
10664 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010665 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010666 DeclarationName DN = ReductionId.getName();
10667 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator();
Alexey Bataevc5e02582014-06-16 07:08:35 +000010668 BinaryOperatorKind BOK = BO_Comma;
10669
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010670 ASTContext &Context = S.Context;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010671 // OpenMP [2.14.3.6, reduction clause]
10672 // C
10673 // reduction-identifier is either an identifier or one of the following
10674 // operators: +, -, *, &, |, ^, && and ||
10675 // C++
10676 // reduction-identifier is either an id-expression or one of the following
10677 // operators: +, -, *, &, |, ^, && and ||
Alexey Bataevc5e02582014-06-16 07:08:35 +000010678 switch (OOK) {
10679 case OO_Plus:
10680 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010681 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010682 break;
10683 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010684 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010685 break;
10686 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010687 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010688 break;
10689 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010690 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010691 break;
10692 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010693 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010694 break;
10695 case OO_AmpAmp:
10696 BOK = BO_LAnd;
10697 break;
10698 case OO_PipePipe:
10699 BOK = BO_LOr;
10700 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010701 case OO_New:
10702 case OO_Delete:
10703 case OO_Array_New:
10704 case OO_Array_Delete:
10705 case OO_Slash:
10706 case OO_Percent:
10707 case OO_Tilde:
10708 case OO_Exclaim:
10709 case OO_Equal:
10710 case OO_Less:
10711 case OO_Greater:
10712 case OO_LessEqual:
10713 case OO_GreaterEqual:
10714 case OO_PlusEqual:
10715 case OO_MinusEqual:
10716 case OO_StarEqual:
10717 case OO_SlashEqual:
10718 case OO_PercentEqual:
10719 case OO_CaretEqual:
10720 case OO_AmpEqual:
10721 case OO_PipeEqual:
10722 case OO_LessLess:
10723 case OO_GreaterGreater:
10724 case OO_LessLessEqual:
10725 case OO_GreaterGreaterEqual:
10726 case OO_EqualEqual:
10727 case OO_ExclaimEqual:
Richard Smithd30b23d2017-12-01 02:13:10 +000010728 case OO_Spaceship:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010729 case OO_PlusPlus:
10730 case OO_MinusMinus:
10731 case OO_Comma:
10732 case OO_ArrowStar:
10733 case OO_Arrow:
10734 case OO_Call:
10735 case OO_Subscript:
10736 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +000010737 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010738 case NUM_OVERLOADED_OPERATORS:
10739 llvm_unreachable("Unexpected reduction identifier");
10740 case OO_None:
Alexey Bataeve3727102018-04-18 15:57:46 +000010741 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010742 if (II->isStr("max"))
10743 BOK = BO_GT;
10744 else if (II->isStr("min"))
10745 BOK = BO_LT;
10746 }
10747 break;
10748 }
10749 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010750 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +000010751 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataev4d4624c2017-07-20 16:47:47 +000010752 else
10753 ReductionIdRange.setBegin(ReductionId.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +000010754 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +000010755
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010756 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
10757 bool FirstIter = true;
Alexey Bataeve3727102018-04-18 15:57:46 +000010758 for (Expr *RefExpr : VarList) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010759 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +000010760 // OpenMP [2.1, C/C++]
10761 // A list item is a variable or array section, subject to the restrictions
10762 // specified in Section 2.4 on page 42 and in each of the sections
10763 // describing clauses and directives for which a list appears.
10764 // OpenMP [2.14.3.3, Restrictions, p.1]
10765 // A variable that is part of another variable (as an array or
10766 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010767 if (!FirstIter && IR != ER)
10768 ++IR;
10769 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010770 SourceLocation ELoc;
10771 SourceRange ERange;
10772 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010773 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
Alexey Bataev60da77e2016-02-29 05:54:20 +000010774 /*AllowArraySection=*/true);
10775 if (Res.second) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010776 // Try to find 'declare reduction' corresponding construct before using
10777 // builtin/overloaded operators.
10778 QualType Type = Context.DependentTy;
10779 CXXCastPath BasePath;
10780 ExprResult DeclareReductionRef = buildDeclareReductionRef(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010781 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010782 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010783 Expr *ReductionOp = nullptr;
10784 if (S.CurContext->isDependentContext() &&
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010785 (DeclareReductionRef.isUnset() ||
10786 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010787 ReductionOp = DeclareReductionRef.get();
10788 // It will be analyzed later.
10789 RD.push(RefExpr, ReductionOp);
Alexey Bataevc5e02582014-06-16 07:08:35 +000010790 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000010791 ValueDecl *D = Res.first;
10792 if (!D)
10793 continue;
10794
Alexey Bataev88202be2017-07-27 13:20:36 +000010795 Expr *TaskgroupDescriptor = nullptr;
Alexey Bataeva1764212015-09-30 09:22:36 +000010796 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010797 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
10798 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
Alexey Bataeve3727102018-04-18 15:57:46 +000010799 if (ASE) {
Alexey Bataev31300ed2016-02-04 11:27:03 +000010800 Type = ASE->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010801 } else if (OASE) {
10802 QualType BaseType =
10803 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
10804 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
Alexey Bataeva1764212015-09-30 09:22:36 +000010805 Type = ATy->getElementType();
10806 else
10807 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +000010808 Type = Type.getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010809 } else {
Alexey Bataev60da77e2016-02-29 05:54:20 +000010810 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
Alexey Bataeve3727102018-04-18 15:57:46 +000010811 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000010812 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +000010813
Alexey Bataevc5e02582014-06-16 07:08:35 +000010814 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
10815 // A variable that appears in a private clause must not have an incomplete
10816 // type or a reference type.
Joel E. Denny3cabf732018-06-28 19:54:49 +000010817 if (S.RequireCompleteType(ELoc, D->getType(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010818 diag::err_omp_reduction_incomplete_type))
Alexey Bataevc5e02582014-06-16 07:08:35 +000010819 continue;
10820 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +000010821 // A list item that appears in a reduction clause must not be
10822 // const-qualified.
10823 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataev169d96a2017-07-18 20:17:46 +000010824 S.Diag(ELoc, diag::err_omp_const_reduction_list_item) << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010825 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010826 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10827 VarDecl::DeclarationOnly;
10828 S.Diag(D->getLocation(),
10829 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +000010830 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +000010831 }
Alexey Bataevc5e02582014-06-16 07:08:35 +000010832 continue;
10833 }
Alexey Bataevbc529672018-09-28 19:33:14 +000010834
10835 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective();
Alexey Bataevc5e02582014-06-16 07:08:35 +000010836 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
10837 // If a list-item is a reference type then it must bind to the same object
10838 // for all threads of the team.
Alexey Bataevbc529672018-09-28 19:33:14 +000010839 if (!ASE && !OASE) {
10840 if (VD) {
10841 VarDecl *VDDef = VD->getDefinition();
10842 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) {
10843 DSARefChecker Check(Stack);
10844 if (Check.Visit(VDDef->getInit())) {
10845 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg)
10846 << getOpenMPClauseName(ClauseKind) << ERange;
10847 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
10848 continue;
10849 }
Alexey Bataeva1764212015-09-30 09:22:36 +000010850 }
Alexey Bataevc5e02582014-06-16 07:08:35 +000010851 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010852
Alexey Bataevbc529672018-09-28 19:33:14 +000010853 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
10854 // in a Construct]
10855 // Variables with the predetermined data-sharing attributes may not be
10856 // listed in data-sharing attributes clauses, except for the cases
10857 // listed below. For these exceptions only, listing a predetermined
10858 // variable in a data-sharing attribute clause is allowed and overrides
10859 // the variable's predetermined data-sharing attributes.
10860 // OpenMP [2.14.3.6, Restrictions, p.3]
10861 // Any number of reduction clauses can be specified on the directive,
10862 // but a list item can appear only once in the reduction clauses for that
10863 // directive.
10864 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false);
10865 if (DVar.CKind == OMPC_reduction) {
10866 S.Diag(ELoc, diag::err_omp_once_referenced)
10867 << getOpenMPClauseName(ClauseKind);
10868 if (DVar.RefExpr)
10869 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
10870 continue;
10871 }
10872 if (DVar.CKind != OMPC_unknown) {
10873 S.Diag(ELoc, diag::err_omp_wrong_dsa)
10874 << getOpenMPClauseName(DVar.CKind)
10875 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataeve3727102018-04-18 15:57:46 +000010876 reportOriginalDsa(S, Stack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010877 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +000010878 }
Alexey Bataevbc529672018-09-28 19:33:14 +000010879
10880 // OpenMP [2.14.3.6, Restrictions, p.1]
10881 // A list item that appears in a reduction clause of a worksharing
10882 // construct must be shared in the parallel regions to which any of the
10883 // worksharing regions arising from the worksharing construct bind.
10884 if (isOpenMPWorksharingDirective(CurrDir) &&
10885 !isOpenMPParallelDirective(CurrDir) &&
10886 !isOpenMPTeamsDirective(CurrDir)) {
10887 DVar = Stack->getImplicitDSA(D, true);
10888 if (DVar.CKind != OMPC_shared) {
10889 S.Diag(ELoc, diag::err_omp_required_access)
10890 << getOpenMPClauseName(OMPC_reduction)
10891 << getOpenMPClauseName(OMPC_shared);
10892 reportOriginalDsa(S, Stack, D, DVar);
10893 continue;
10894 }
10895 }
Alexey Bataevf29276e2014-06-18 04:14:57 +000010896 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010897
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010898 // Try to find 'declare reduction' corresponding construct before using
10899 // builtin/overloaded operators.
10900 CXXCastPath BasePath;
10901 ExprResult DeclareReductionRef = buildDeclareReductionRef(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010902 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010903 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
10904 if (DeclareReductionRef.isInvalid())
10905 continue;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010906 if (S.CurContext->isDependentContext() &&
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010907 (DeclareReductionRef.isUnset() ||
10908 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010909 RD.push(RefExpr, DeclareReductionRef.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010910 continue;
10911 }
10912 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
10913 // Not allowed reduction identifier is found.
Stephen Kellyf2ceec42018-08-09 21:08:08 +000010914 S.Diag(ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010915 diag::err_omp_unknown_reduction_identifier)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010916 << Type << ReductionIdRange;
10917 continue;
10918 }
10919
10920 // OpenMP [2.14.3.6, reduction clause, Restrictions]
10921 // The type of a list item that appears in a reduction clause must be valid
10922 // for the reduction-identifier. For a max or min reduction in C, the type
10923 // of the list item must be an allowed arithmetic data type: char, int,
10924 // float, double, or _Bool, possibly modified with long, short, signed, or
10925 // unsigned. For a max or min reduction in C++, the type of the list item
10926 // must be an allowed arithmetic data type: char, wchar_t, int, float,
10927 // double, or bool, possibly modified with long, short, signed, or unsigned.
10928 if (DeclareReductionRef.isUnset()) {
10929 if ((BOK == BO_GT || BOK == BO_LT) &&
10930 !(Type->isScalarType() ||
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010931 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
10932 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
Alexey Bataev169d96a2017-07-18 20:17:46 +000010933 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010934 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010935 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10936 VarDecl::DeclarationOnly;
10937 S.Diag(D->getLocation(),
10938 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010939 << D;
10940 }
10941 continue;
10942 }
10943 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010944 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) {
Alexey Bataev169d96a2017-07-18 20:17:46 +000010945 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg)
10946 << getOpenMPClauseName(ClauseKind);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010947 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010948 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10949 VarDecl::DeclarationOnly;
10950 S.Diag(D->getLocation(),
10951 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010952 << D;
10953 }
10954 continue;
10955 }
10956 }
10957
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010958 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010959 VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs",
10960 D->hasAttrs() ? &D->getAttrs() : nullptr);
10961 VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(),
10962 D->hasAttrs() ? &D->getAttrs() : nullptr);
10963 QualType PrivateTy = Type;
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010964
10965 // Try if we can determine constant lengths for all array sections and avoid
10966 // the VLA.
10967 bool ConstantLengthOASE = false;
10968 if (OASE) {
10969 bool SingleElement;
10970 llvm::SmallVector<llvm::APSInt, 4> ArraySizes;
Alexey Bataeve3727102018-04-18 15:57:46 +000010971 ConstantLengthOASE = checkOMPArraySectionConstantForReduction(
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010972 Context, OASE, SingleElement, ArraySizes);
10973
10974 // If we don't have a single element, we must emit a constant array type.
10975 if (ConstantLengthOASE && !SingleElement) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010976 for (llvm::APSInt &Size : ArraySizes)
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010977 PrivateTy = Context.getConstantArrayType(
10978 PrivateTy, Size, ArrayType::Normal, /*IndexTypeQuals=*/0);
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010979 }
10980 }
10981
10982 if ((OASE && !ConstantLengthOASE) ||
Jonas Hahnfeld96087f32017-11-02 13:30:42 +000010983 (!OASE && !ASE &&
Alexey Bataev60da77e2016-02-29 05:54:20 +000010984 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Jonas Hahnfeld87d44262017-11-18 21:00:46 +000010985 if (!Context.getTargetInfo().isVLASupported() &&
10986 S.shouldDiagnoseTargetSupportFromOpenMP()) {
10987 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE;
10988 S.Diag(ELoc, diag::note_vla_unsupported);
10989 continue;
10990 }
David Majnemer9d168222016-08-05 17:44:54 +000010991 // For arrays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010992 // Create pseudo array type for private copy. The size for this array will
10993 // be generated during codegen.
10994 // For array subscripts or single variables Private Ty is the same as Type
10995 // (type of the variable or single array element).
10996 PrivateTy = Context.getVariableArrayType(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010997 Type,
Alexey Bataevd070a582017-10-25 15:54:04 +000010998 new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue),
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010999 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +000011000 } else if (!ASE && !OASE &&
Alexey Bataeve3727102018-04-18 15:57:46 +000011001 Context.getAsArrayType(D->getType().getNonReferenceType())) {
Alexey Bataev60da77e2016-02-29 05:54:20 +000011002 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000011003 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011004 // Private copy.
Alexey Bataeve3727102018-04-18 15:57:46 +000011005 VarDecl *PrivateVD =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000011006 buildVarDecl(S, ELoc, PrivateTy, D->getName(),
11007 D->hasAttrs() ? &D->getAttrs() : nullptr,
11008 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011009 // Add initializer for private variable.
11010 Expr *Init = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000011011 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc);
11012 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011013 if (DeclareReductionRef.isUsable()) {
11014 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
11015 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
11016 if (DRD->getInitializer()) {
11017 Init = DRDRef;
11018 RHSVD->setInit(DRDRef);
11019 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011020 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011021 } else {
11022 switch (BOK) {
11023 case BO_Add:
11024 case BO_Xor:
11025 case BO_Or:
11026 case BO_LOr:
11027 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
11028 if (Type->isScalarType() || Type->isAnyComplexType())
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011029 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011030 break;
11031 case BO_Mul:
11032 case BO_LAnd:
11033 if (Type->isScalarType() || Type->isAnyComplexType()) {
11034 // '*' and '&&' reduction ops - initializer is '1'.
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011035 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +000011036 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011037 break;
11038 case BO_And: {
11039 // '&' reduction op - initializer is '~0'.
11040 QualType OrigType = Type;
11041 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
11042 Type = ComplexTy->getElementType();
11043 if (Type->isRealFloatingType()) {
11044 llvm::APFloat InitValue =
11045 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
11046 /*isIEEE=*/true);
11047 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
11048 Type, ELoc);
11049 } else if (Type->isScalarType()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011050 uint64_t Size = Context.getTypeSize(Type);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011051 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
11052 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
11053 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
11054 }
11055 if (Init && OrigType->isAnyComplexType()) {
11056 // Init = 0xFFFF + 0xFFFFi;
11057 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011058 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011059 }
11060 Type = OrigType;
11061 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011062 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011063 case BO_LT:
11064 case BO_GT: {
11065 // 'min' reduction op - initializer is 'Largest representable number in
11066 // the reduction list item type'.
11067 // 'max' reduction op - initializer is 'Least representable number in
11068 // the reduction list item type'.
11069 if (Type->isIntegerType() || Type->isPointerType()) {
11070 bool IsSigned = Type->hasSignedIntegerRepresentation();
Alexey Bataeve3727102018-04-18 15:57:46 +000011071 uint64_t Size = Context.getTypeSize(Type);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011072 QualType IntTy =
11073 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
11074 llvm::APInt InitValue =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011075 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
11076 : llvm::APInt::getMinValue(Size)
11077 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
11078 : llvm::APInt::getMaxValue(Size);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011079 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
11080 if (Type->isPointerType()) {
11081 // Cast to pointer type.
Alexey Bataeve3727102018-04-18 15:57:46 +000011082 ExprResult CastExpr = S.BuildCStyleCastExpr(
Alexey Bataevd070a582017-10-25 15:54:04 +000011083 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011084 if (CastExpr.isInvalid())
11085 continue;
11086 Init = CastExpr.get();
11087 }
11088 } else if (Type->isRealFloatingType()) {
11089 llvm::APFloat InitValue = llvm::APFloat::getLargest(
11090 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
11091 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
11092 Type, ELoc);
11093 }
11094 break;
11095 }
11096 case BO_PtrMemD:
11097 case BO_PtrMemI:
11098 case BO_MulAssign:
11099 case BO_Div:
11100 case BO_Rem:
11101 case BO_Sub:
11102 case BO_Shl:
11103 case BO_Shr:
11104 case BO_LE:
11105 case BO_GE:
11106 case BO_EQ:
11107 case BO_NE:
Richard Smithc70f1d62017-12-14 15:16:18 +000011108 case BO_Cmp:
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011109 case BO_AndAssign:
11110 case BO_XorAssign:
11111 case BO_OrAssign:
11112 case BO_Assign:
11113 case BO_AddAssign:
11114 case BO_SubAssign:
11115 case BO_DivAssign:
11116 case BO_RemAssign:
11117 case BO_ShlAssign:
11118 case BO_ShrAssign:
11119 case BO_Comma:
11120 llvm_unreachable("Unexpected reduction operation");
11121 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011122 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011123 if (Init && DeclareReductionRef.isUnset())
11124 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false);
11125 else if (!Init)
11126 S.ActOnUninitializedDecl(RHSVD);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011127 if (RHSVD->isInvalidDecl())
11128 continue;
11129 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011130 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible)
11131 << Type << ReductionIdRange;
11132 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
11133 VarDecl::DeclarationOnly;
11134 S.Diag(D->getLocation(),
11135 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +000011136 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011137 continue;
11138 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011139 // Store initializer for single element in private copy. Will be used during
11140 // codegen.
11141 PrivateVD->setInit(RHSVD->getInit());
11142 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataeve3727102018-04-18 15:57:46 +000011143 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011144 ExprResult ReductionOp;
11145 if (DeclareReductionRef.isUsable()) {
11146 QualType RedTy = DeclareReductionRef.get()->getType();
11147 QualType PtrRedTy = Context.getPointerType(RedTy);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011148 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
11149 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011150 if (!BasePath.empty()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011151 LHS = S.DefaultLvalueConversion(LHS.get());
11152 RHS = S.DefaultLvalueConversion(RHS.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011153 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
11154 CK_UncheckedDerivedToBase, LHS.get(),
11155 &BasePath, LHS.get()->getValueKind());
11156 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
11157 CK_UncheckedDerivedToBase, RHS.get(),
11158 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011159 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011160 FunctionProtoType::ExtProtoInfo EPI;
11161 QualType Params[] = {PtrRedTy, PtrRedTy};
11162 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
11163 auto *OVE = new (Context) OpaqueValueExpr(
11164 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011165 S.DefaultLvalueConversion(DeclareReductionRef.get()).get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011166 Expr *Args[] = {LHS.get(), RHS.get()};
11167 ReductionOp = new (Context)
11168 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
11169 } else {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011170 ReductionOp = S.BuildBinOp(
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011171 Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, LHSDRE, RHSDRE);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011172 if (ReductionOp.isUsable()) {
11173 if (BOK != BO_LT && BOK != BO_GT) {
11174 ReductionOp =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011175 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011176 BO_Assign, LHSDRE, ReductionOp.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011177 } else {
Alexey Bataevd070a582017-10-25 15:54:04 +000011178 auto *ConditionalOp = new (Context)
11179 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE,
11180 Type, VK_LValue, OK_Ordinary);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011181 ReductionOp =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011182 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011183 BO_Assign, LHSDRE, ConditionalOp);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011184 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +000011185 if (ReductionOp.isUsable())
11186 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011187 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +000011188 if (!ReductionOp.isUsable())
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011189 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +000011190 }
11191
Alexey Bataevfa312f32017-07-21 18:48:21 +000011192 // OpenMP [2.15.4.6, Restrictions, p.2]
11193 // A list item that appears in an in_reduction clause of a task construct
11194 // must appear in a task_reduction clause of a construct associated with a
11195 // taskgroup region that includes the participating task in its taskgroup
11196 // set. The construct associated with the innermost region that meets this
11197 // condition must specify the same reduction-identifier as the in_reduction
11198 // clause.
11199 if (ClauseKind == OMPC_in_reduction) {
Alexey Bataevfa312f32017-07-21 18:48:21 +000011200 SourceRange ParentSR;
11201 BinaryOperatorKind ParentBOK;
11202 const Expr *ParentReductionOp;
Alexey Bataev88202be2017-07-27 13:20:36 +000011203 Expr *ParentBOKTD, *ParentReductionOpTD;
Alexey Bataevf189cb72017-07-24 14:52:13 +000011204 DSAStackTy::DSAVarData ParentBOKDSA =
Alexey Bataev88202be2017-07-27 13:20:36 +000011205 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK,
11206 ParentBOKTD);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011207 DSAStackTy::DSAVarData ParentReductionOpDSA =
Alexey Bataev88202be2017-07-27 13:20:36 +000011208 Stack->getTopMostTaskgroupReductionData(
11209 D, ParentSR, ParentReductionOp, ParentReductionOpTD);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011210 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown;
11211 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown;
11212 if (!IsParentBOK && !IsParentReductionOp) {
11213 S.Diag(ELoc, diag::err_omp_in_reduction_not_task_reduction);
11214 continue;
11215 }
Alexey Bataevfa312f32017-07-21 18:48:21 +000011216 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) ||
11217 (DeclareReductionRef.isUsable() && IsParentBOK) || BOK != ParentBOK ||
11218 IsParentReductionOp) {
11219 bool EmitError = true;
11220 if (IsParentReductionOp && DeclareReductionRef.isUsable()) {
11221 llvm::FoldingSetNodeID RedId, ParentRedId;
11222 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true);
11223 DeclareReductionRef.get()->Profile(RedId, Context,
11224 /*Canonical=*/true);
11225 EmitError = RedId != ParentRedId;
11226 }
11227 if (EmitError) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011228 S.Diag(ReductionId.getBeginLoc(),
Alexey Bataevfa312f32017-07-21 18:48:21 +000011229 diag::err_omp_reduction_identifier_mismatch)
11230 << ReductionIdRange << RefExpr->getSourceRange();
11231 S.Diag(ParentSR.getBegin(),
11232 diag::note_omp_previous_reduction_identifier)
Alexey Bataevf189cb72017-07-24 14:52:13 +000011233 << ParentSR
11234 << (IsParentBOK ? ParentBOKDSA.RefExpr
11235 : ParentReductionOpDSA.RefExpr)
11236 ->getSourceRange();
Alexey Bataevfa312f32017-07-21 18:48:21 +000011237 continue;
11238 }
11239 }
Alexey Bataev88202be2017-07-27 13:20:36 +000011240 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD;
11241 assert(TaskgroupDescriptor && "Taskgroup descriptor must be defined.");
Alexey Bataevfa312f32017-07-21 18:48:21 +000011242 }
11243
Alexey Bataev60da77e2016-02-29 05:54:20 +000011244 DeclRefExpr *Ref = nullptr;
11245 Expr *VarsExpr = RefExpr->IgnoreParens();
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011246 if (!VD && !S.CurContext->isDependentContext()) {
Alexey Bataev60da77e2016-02-29 05:54:20 +000011247 if (ASE || OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011248 TransformExprToCaptures RebuildToCapture(S, D);
Alexey Bataev60da77e2016-02-29 05:54:20 +000011249 VarsExpr =
11250 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
11251 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +000011252 } else {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011253 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +000011254 }
Alexey Bataeve3727102018-04-18 15:57:46 +000011255 if (!S.isOpenMPCapturedDecl(D)) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011256 RD.ExprCaptures.emplace_back(Ref->getDecl());
Alexey Bataev5a3af132016-03-29 08:58:54 +000011257 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011258 ExprResult RefRes = S.DefaultLvalueConversion(Ref);
Alexey Bataev5a3af132016-03-29 08:58:54 +000011259 if (!RefRes.isUsable())
11260 continue;
11261 ExprResult PostUpdateRes =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011262 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
11263 RefRes.get());
Alexey Bataev5a3af132016-03-29 08:58:54 +000011264 if (!PostUpdateRes.isUsable())
11265 continue;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011266 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
11267 Stack->getCurrentDirective() == OMPD_taskgroup) {
11268 S.Diag(RefExpr->getExprLoc(),
11269 diag::err_omp_reduction_non_addressable_expression)
Alexey Bataevbcd0ae02017-07-11 19:16:44 +000011270 << RefExpr->getSourceRange();
11271 continue;
11272 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011273 RD.ExprPostUpdates.emplace_back(
11274 S.IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +000011275 }
11276 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000011277 }
Alexey Bataev169d96a2017-07-18 20:17:46 +000011278 // All reduction items are still marked as reduction (to do not increase
11279 // code base size).
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011280 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011281 if (CurrDir == OMPD_taskgroup) {
11282 if (DeclareReductionRef.isUsable())
Alexey Bataev3b1b8952017-07-25 15:53:26 +000011283 Stack->addTaskgroupReductionData(D, ReductionIdRange,
11284 DeclareReductionRef.get());
Alexey Bataevf189cb72017-07-24 14:52:13 +000011285 else
Alexey Bataev3b1b8952017-07-25 15:53:26 +000011286 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011287 }
Alexey Bataev88202be2017-07-27 13:20:36 +000011288 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(),
11289 TaskgroupDescriptor);
Alexey Bataevc5e02582014-06-16 07:08:35 +000011290 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011291 return RD.Vars.empty();
11292}
Alexey Bataevc5e02582014-06-16 07:08:35 +000011293
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011294OMPClause *Sema::ActOnOpenMPReductionClause(
11295 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11296 SourceLocation ColonLoc, SourceLocation EndLoc,
11297 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11298 ArrayRef<Expr *> UnresolvedReductions) {
11299 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000011300 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList,
Alexey Bataev169d96a2017-07-18 20:17:46 +000011301 StartLoc, LParenLoc, ColonLoc, EndLoc,
11302 ReductionIdScopeSpec, ReductionId,
11303 UnresolvedReductions, RD))
Alexey Bataevc5e02582014-06-16 07:08:35 +000011304 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +000011305
Alexey Bataevc5e02582014-06-16 07:08:35 +000011306 return OMPReductionClause::Create(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011307 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11308 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
11309 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
11310 buildPreInits(Context, RD.ExprCaptures),
11311 buildPostUpdate(*this, RD.ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +000011312}
11313
Alexey Bataev169d96a2017-07-18 20:17:46 +000011314OMPClause *Sema::ActOnOpenMPTaskReductionClause(
11315 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11316 SourceLocation ColonLoc, SourceLocation EndLoc,
11317 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11318 ArrayRef<Expr *> UnresolvedReductions) {
11319 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000011320 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList,
11321 StartLoc, LParenLoc, ColonLoc, EndLoc,
11322 ReductionIdScopeSpec, ReductionId,
Alexey Bataev169d96a2017-07-18 20:17:46 +000011323 UnresolvedReductions, RD))
11324 return nullptr;
11325
11326 return OMPTaskReductionClause::Create(
11327 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11328 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
11329 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
11330 buildPreInits(Context, RD.ExprCaptures),
11331 buildPostUpdate(*this, RD.ExprPostUpdates));
11332}
11333
Alexey Bataevfa312f32017-07-21 18:48:21 +000011334OMPClause *Sema::ActOnOpenMPInReductionClause(
11335 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11336 SourceLocation ColonLoc, SourceLocation EndLoc,
11337 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11338 ArrayRef<Expr *> UnresolvedReductions) {
11339 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000011340 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList,
Alexey Bataevfa312f32017-07-21 18:48:21 +000011341 StartLoc, LParenLoc, ColonLoc, EndLoc,
11342 ReductionIdScopeSpec, ReductionId,
11343 UnresolvedReductions, RD))
11344 return nullptr;
11345
11346 return OMPInReductionClause::Create(
11347 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11348 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
Alexey Bataev88202be2017-07-27 13:20:36 +000011349 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors,
Alexey Bataevfa312f32017-07-21 18:48:21 +000011350 buildPreInits(Context, RD.ExprCaptures),
11351 buildPostUpdate(*this, RD.ExprPostUpdates));
11352}
11353
Alexey Bataevecba70f2016-04-12 11:02:11 +000011354bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
11355 SourceLocation LinLoc) {
11356 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
11357 LinKind == OMPC_LINEAR_unknown) {
11358 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
11359 return true;
11360 }
11361 return false;
11362}
11363
Alexey Bataeve3727102018-04-18 15:57:46 +000011364bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc,
Alexey Bataevecba70f2016-04-12 11:02:11 +000011365 OpenMPLinearClauseKind LinKind,
11366 QualType Type) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011367 const auto *VD = dyn_cast_or_null<VarDecl>(D);
Alexey Bataevecba70f2016-04-12 11:02:11 +000011368 // A variable must not have an incomplete type or a reference type.
11369 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
11370 return true;
11371 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
11372 !Type->isReferenceType()) {
11373 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
11374 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
11375 return true;
11376 }
11377 Type = Type.getNonReferenceType();
11378
11379 // A list item must not be const-qualified.
11380 if (Type.isConstant(Context)) {
11381 Diag(ELoc, diag::err_omp_const_variable)
11382 << getOpenMPClauseName(OMPC_linear);
11383 if (D) {
11384 bool IsDecl =
11385 !VD ||
11386 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
11387 Diag(D->getLocation(),
11388 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
11389 << D;
11390 }
11391 return true;
11392 }
11393
11394 // A list item must be of integral or pointer type.
11395 Type = Type.getUnqualifiedType().getCanonicalType();
11396 const auto *Ty = Type.getTypePtrOrNull();
11397 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
11398 !Ty->isPointerType())) {
11399 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
11400 if (D) {
11401 bool IsDecl =
11402 !VD ||
11403 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
11404 Diag(D->getLocation(),
11405 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
11406 << D;
11407 }
11408 return true;
11409 }
11410 return false;
11411}
11412
Alexey Bataev182227b2015-08-20 10:54:39 +000011413OMPClause *Sema::ActOnOpenMPLinearClause(
11414 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
11415 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
11416 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +000011417 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011418 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +000011419 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +000011420 SmallVector<Decl *, 4> ExprCaptures;
11421 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +000011422 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +000011423 LinKind = OMPC_LINEAR_val;
Alexey Bataeve3727102018-04-18 15:57:46 +000011424 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000011425 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011426 SourceLocation ELoc;
11427 SourceRange ERange;
11428 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011429 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011430 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +000011431 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000011432 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011433 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +000011434 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +000011435 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011436 ValueDecl *D = Res.first;
11437 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +000011438 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +000011439
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011440 QualType Type = D->getType();
11441 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +000011442
11443 // OpenMP [2.14.3.7, linear clause]
11444 // A list-item cannot appear in more than one linear clause.
11445 // A list-item that appears in a linear clause cannot appear in any
11446 // other data-sharing attribute clause.
Alexey Bataeve3727102018-04-18 15:57:46 +000011447 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexander Musman8dba6642014-04-22 13:09:42 +000011448 if (DVar.RefExpr) {
11449 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
11450 << getOpenMPClauseName(OMPC_linear);
Alexey Bataeve3727102018-04-18 15:57:46 +000011451 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +000011452 continue;
11453 }
11454
Alexey Bataevecba70f2016-04-12 11:02:11 +000011455 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +000011456 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +000011457 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +000011458
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011459 // Build private copy of original var.
Alexey Bataeve3727102018-04-18 15:57:46 +000011460 VarDecl *Private =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000011461 buildVarDecl(*this, ELoc, Type, D->getName(),
11462 D->hasAttrs() ? &D->getAttrs() : nullptr,
11463 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011464 DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +000011465 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011466 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011467 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011468 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000011469 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev78849fb2016-03-09 09:49:00 +000011470 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011471 if (!isOpenMPCapturedDecl(D)) {
Alexey Bataev78849fb2016-03-09 09:49:00 +000011472 ExprCaptures.push_back(Ref->getDecl());
11473 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
11474 ExprResult RefRes = DefaultLvalueConversion(Ref);
11475 if (!RefRes.isUsable())
11476 continue;
11477 ExprResult PostUpdateRes =
11478 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
11479 SimpleRefExpr, RefRes.get());
11480 if (!PostUpdateRes.isUsable())
11481 continue;
11482 ExprPostUpdates.push_back(
11483 IgnoredValueConversions(PostUpdateRes.get()).get());
11484 }
11485 }
11486 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011487 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011488 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011489 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011490 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011491 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000011492 /*DirectInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011493 DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011494
11495 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000011496 Vars.push_back((VD || CurContext->isDependentContext())
11497 ? RefExpr->IgnoreParens()
11498 : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011499 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +000011500 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +000011501 }
11502
11503 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000011504 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +000011505
11506 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +000011507 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +000011508 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
11509 !Step->isInstantiationDependent() &&
11510 !Step->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011511 SourceLocation StepLoc = Step->getBeginLoc();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +000011512 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +000011513 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000011514 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +000011515 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +000011516
Alexander Musman3276a272015-03-21 10:12:56 +000011517 // Build var to save the step value.
11518 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +000011519 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +000011520 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +000011521 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +000011522 ExprResult CalcStep =
11523 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +000011524 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +000011525
Alexander Musman8dba6642014-04-22 13:09:42 +000011526 // Warn about zero linear step (it would be probably better specified as
11527 // making corresponding variables 'const').
11528 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +000011529 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
11530 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +000011531 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
11532 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +000011533 if (!IsConstant && CalcStep.isUsable()) {
11534 // Calculate the step beforehand instead of doing this on each iteration.
11535 // (This is not used if the number of iterations may be kfold-ed).
11536 CalcStepExpr = CalcStep.get();
11537 }
Alexander Musman8dba6642014-04-22 13:09:42 +000011538 }
11539
Alexey Bataev182227b2015-08-20 10:54:39 +000011540 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
11541 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +000011542 StepExpr, CalcStepExpr,
11543 buildPreInits(Context, ExprCaptures),
11544 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +000011545}
11546
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011547static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
11548 Expr *NumIterations, Sema &SemaRef,
11549 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +000011550 // Walk the vars and build update/final expressions for the CodeGen.
11551 SmallVector<Expr *, 8> Updates;
11552 SmallVector<Expr *, 8> Finals;
11553 Expr *Step = Clause.getStep();
11554 Expr *CalcStep = Clause.getCalcStep();
11555 // OpenMP [2.14.3.7, linear clause]
11556 // If linear-step is not specified it is assumed to be 1.
Alexey Bataeve3727102018-04-18 15:57:46 +000011557 if (!Step)
Alexander Musman3276a272015-03-21 10:12:56 +000011558 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000011559 else if (CalcStep)
Alexander Musman3276a272015-03-21 10:12:56 +000011560 Step = cast<BinaryOperator>(CalcStep)->getLHS();
11561 bool HasErrors = false;
11562 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011563 auto CurPrivate = Clause.privates().begin();
Alexey Bataeve3727102018-04-18 15:57:46 +000011564 OpenMPLinearClauseKind LinKind = Clause.getModifier();
11565 for (Expr *RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011566 SourceLocation ELoc;
11567 SourceRange ERange;
11568 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011569 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange);
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011570 ValueDecl *D = Res.first;
11571 if (Res.second || !D) {
11572 Updates.push_back(nullptr);
11573 Finals.push_back(nullptr);
11574 HasErrors = true;
11575 continue;
11576 }
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011577 auto &&Info = Stack->isLoopControlVariable(D);
Alexey Bataev2b86f212017-11-29 21:31:48 +000011578 // OpenMP [2.15.11, distribute simd Construct]
11579 // A list item may not appear in a linear clause, unless it is the loop
11580 // iteration variable.
11581 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) &&
11582 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) {
11583 SemaRef.Diag(ELoc,
11584 diag::err_omp_linear_distribute_var_non_loop_iteration);
11585 Updates.push_back(nullptr);
11586 Finals.push_back(nullptr);
11587 HasErrors = true;
11588 continue;
11589 }
Alexander Musman3276a272015-03-21 10:12:56 +000011590 Expr *InitExpr = *CurInit;
11591
11592 // Build privatized reference to the current linear var.
David Majnemer9d168222016-08-05 17:44:54 +000011593 auto *DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011594 Expr *CapturedRef;
11595 if (LinKind == OMPC_LINEAR_uval)
11596 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
11597 else
11598 CapturedRef =
11599 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
11600 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
11601 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +000011602
11603 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011604 ExprResult Update;
Alexey Bataeve3727102018-04-18 15:57:46 +000011605 if (!Info.first)
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011606 Update =
Alexey Bataeve3727102018-04-18 15:57:46 +000011607 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011608 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011609 else
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011610 Update = *CurPrivate;
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011611 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +000011612 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +000011613
11614 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011615 ExprResult Final;
Alexey Bataeve3727102018-04-18 15:57:46 +000011616 if (!Info.first)
11617 Final =
11618 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
11619 InitExpr, NumIterations, Step, /*Subtract=*/false);
11620 else
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011621 Final = *CurPrivate;
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011622 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +000011623 /*DiscardedValue=*/true);
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011624
Alexander Musman3276a272015-03-21 10:12:56 +000011625 if (!Update.isUsable() || !Final.isUsable()) {
11626 Updates.push_back(nullptr);
11627 Finals.push_back(nullptr);
11628 HasErrors = true;
11629 } else {
11630 Updates.push_back(Update.get());
11631 Finals.push_back(Final.get());
11632 }
Richard Trieucc3949d2016-02-18 22:34:54 +000011633 ++CurInit;
11634 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +000011635 }
11636 Clause.setUpdates(Updates);
11637 Clause.setFinals(Finals);
11638 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +000011639}
11640
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011641OMPClause *Sema::ActOnOpenMPAlignedClause(
11642 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
11643 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011644 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +000011645 for (Expr *RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +000011646 assert(RefExpr && "NULL expr in OpenMP linear clause.");
11647 SourceLocation ELoc;
11648 SourceRange ERange;
11649 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011650 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev1efd1662016-03-29 10:59:56 +000011651 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011652 // It will be analyzed later.
11653 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011654 }
Alexey Bataev1efd1662016-03-29 10:59:56 +000011655 ValueDecl *D = Res.first;
11656 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011657 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011658
Alexey Bataev1efd1662016-03-29 10:59:56 +000011659 QualType QType = D->getType();
11660 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011661
11662 // OpenMP [2.8.1, simd construct, Restrictions]
11663 // The type of list items appearing in the aligned clause must be
11664 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011665 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011666 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +000011667 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011668 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +000011669 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011670 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +000011671 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011672 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +000011673 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011674 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +000011675 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011676 continue;
11677 }
11678
11679 // OpenMP [2.8.1, simd construct, Restrictions]
11680 // A list-item cannot appear in more than one aligned clause.
Alexey Bataeve3727102018-04-18 15:57:46 +000011681 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +000011682 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011683 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
11684 << getOpenMPClauseName(OMPC_aligned);
11685 continue;
11686 }
11687
Alexey Bataev1efd1662016-03-29 10:59:56 +000011688 DeclRefExpr *Ref = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000011689 if (!VD && isOpenMPCapturedDecl(D))
Alexey Bataev1efd1662016-03-29 10:59:56 +000011690 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
11691 Vars.push_back(DefaultFunctionArrayConversion(
11692 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
11693 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011694 }
11695
11696 // OpenMP [2.8.1, simd construct, Description]
11697 // The parameter of the aligned clause, alignment, must be a constant
11698 // positive integer expression.
11699 // If no optional parameter is specified, implementation-defined default
11700 // alignments for SIMD instructions on the target platforms are assumed.
11701 if (Alignment != nullptr) {
11702 ExprResult AlignResult =
11703 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
11704 if (AlignResult.isInvalid())
11705 return nullptr;
11706 Alignment = AlignResult.get();
11707 }
11708 if (Vars.empty())
11709 return nullptr;
11710
11711 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
11712 EndLoc, Vars, Alignment);
11713}
11714
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011715OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
11716 SourceLocation StartLoc,
11717 SourceLocation LParenLoc,
11718 SourceLocation EndLoc) {
11719 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011720 SmallVector<Expr *, 8> SrcExprs;
11721 SmallVector<Expr *, 8> DstExprs;
11722 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeve3727102018-04-18 15:57:46 +000011723 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000011724 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
11725 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011726 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000011727 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011728 SrcExprs.push_back(nullptr);
11729 DstExprs.push_back(nullptr);
11730 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011731 continue;
11732 }
11733
Alexey Bataeved09d242014-05-28 05:53:51 +000011734 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011735 // OpenMP [2.1, C/C++]
11736 // A list item is a variable name.
11737 // OpenMP [2.14.4.1, Restrictions, p.1]
11738 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeve3727102018-04-18 15:57:46 +000011739 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011740 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000011741 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
11742 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011743 continue;
11744 }
11745
11746 Decl *D = DE->getDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000011747 auto *VD = cast<VarDecl>(D);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011748
11749 QualType Type = VD->getType();
11750 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
11751 // It will be analyzed later.
11752 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011753 SrcExprs.push_back(nullptr);
11754 DstExprs.push_back(nullptr);
11755 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011756 continue;
11757 }
11758
11759 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
11760 // A list item that appears in a copyin clause must be threadprivate.
11761 if (!DSAStack->isThreadPrivate(VD)) {
11762 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +000011763 << getOpenMPClauseName(OMPC_copyin)
11764 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011765 continue;
11766 }
11767
11768 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
11769 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +000011770 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011771 // operator for the class type.
Alexey Bataeve3727102018-04-18 15:57:46 +000011772 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
11773 VarDecl *SrcVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011774 buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000011775 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011776 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011777 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +000011778 VarDecl *DstVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011779 buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst",
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000011780 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011781 DeclRefExpr *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011782 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011783 // For arrays generate assignment operation for single element and replace
11784 // it by the original array element in CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +000011785 ExprResult AssignmentOp =
11786 BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr,
11787 PseudoSrcExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011788 if (AssignmentOp.isInvalid())
11789 continue;
11790 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
11791 /*DiscardedValue=*/true);
11792 if (AssignmentOp.isInvalid())
11793 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011794
11795 DSAStack->addDSA(VD, DE, OMPC_copyin);
11796 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011797 SrcExprs.push_back(PseudoSrcExpr);
11798 DstExprs.push_back(PseudoDstExpr);
11799 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011800 }
11801
Alexey Bataeved09d242014-05-28 05:53:51 +000011802 if (Vars.empty())
11803 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011804
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011805 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
11806 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011807}
11808
Alexey Bataevbae9a792014-06-27 10:37:06 +000011809OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
11810 SourceLocation StartLoc,
11811 SourceLocation LParenLoc,
11812 SourceLocation EndLoc) {
11813 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +000011814 SmallVector<Expr *, 8> SrcExprs;
11815 SmallVector<Expr *, 8> DstExprs;
11816 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeve3727102018-04-18 15:57:46 +000011817 for (Expr *RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +000011818 assert(RefExpr && "NULL expr in OpenMP linear clause.");
11819 SourceLocation ELoc;
11820 SourceRange ERange;
11821 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011822 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataeve122da12016-03-17 10:50:17 +000011823 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000011824 // It will be analyzed later.
11825 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000011826 SrcExprs.push_back(nullptr);
11827 DstExprs.push_back(nullptr);
11828 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011829 }
Alexey Bataeve122da12016-03-17 10:50:17 +000011830 ValueDecl *D = Res.first;
11831 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +000011832 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000011833
Alexey Bataeve122da12016-03-17 10:50:17 +000011834 QualType Type = D->getType();
11835 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011836
11837 // OpenMP [2.14.4.2, Restrictions, p.2]
11838 // A list item that appears in a copyprivate clause may not appear in a
11839 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +000011840 if (!VD || !DSAStack->isThreadPrivate(VD)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011841 DSAStackTy::DSAVarData DVar =
11842 DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataeva63048e2015-03-23 06:18:07 +000011843 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
11844 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000011845 Diag(ELoc, diag::err_omp_wrong_dsa)
11846 << getOpenMPClauseName(DVar.CKind)
11847 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000011848 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011849 continue;
11850 }
11851
11852 // OpenMP [2.11.4.2, Restrictions, p.1]
11853 // All list items that appear in a copyprivate clause must be either
11854 // threadprivate or private in the enclosing context.
11855 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +000011856 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011857 if (DVar.CKind == OMPC_shared) {
11858 Diag(ELoc, diag::err_omp_required_access)
11859 << getOpenMPClauseName(OMPC_copyprivate)
11860 << "threadprivate or private in the enclosing context";
Alexey Bataeve3727102018-04-18 15:57:46 +000011861 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011862 continue;
11863 }
11864 }
11865 }
11866
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011867 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000011868 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011869 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +000011870 << getOpenMPClauseName(OMPC_copyprivate) << Type
11871 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011872 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +000011873 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011874 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +000011875 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011876 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +000011877 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011878 continue;
11879 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +000011880
Alexey Bataevbae9a792014-06-27 10:37:06 +000011881 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
11882 // A variable of class type (or array thereof) that appears in a
11883 // copyin clause requires an accessible, unambiguous copy assignment
11884 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011885 Type = Context.getBaseElementType(Type.getNonReferenceType())
11886 .getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000011887 VarDecl *SrcVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011888 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src",
Alexey Bataeve122da12016-03-17 10:50:17 +000011889 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011890 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
11891 VarDecl *DstVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011892 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst",
Alexey Bataeve122da12016-03-17 10:50:17 +000011893 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011894 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
11895 ExprResult AssignmentOp = BuildBinOp(
11896 DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000011897 if (AssignmentOp.isInvalid())
11898 continue;
Alexey Bataeve122da12016-03-17 10:50:17 +000011899 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataeva63048e2015-03-23 06:18:07 +000011900 /*DiscardedValue=*/true);
11901 if (AssignmentOp.isInvalid())
11902 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000011903
11904 // No need to mark vars as copyprivate, they are already threadprivate or
11905 // implicitly private.
Alexey Bataeve3727102018-04-18 15:57:46 +000011906 assert(VD || isOpenMPCapturedDecl(D));
Alexey Bataeve122da12016-03-17 10:50:17 +000011907 Vars.push_back(
11908 VD ? RefExpr->IgnoreParens()
11909 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +000011910 SrcExprs.push_back(PseudoSrcExpr);
11911 DstExprs.push_back(PseudoDstExpr);
11912 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +000011913 }
11914
11915 if (Vars.empty())
11916 return nullptr;
11917
Alexey Bataeva63048e2015-03-23 06:18:07 +000011918 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11919 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011920}
11921
Alexey Bataev6125da92014-07-21 11:26:11 +000011922OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
11923 SourceLocation StartLoc,
11924 SourceLocation LParenLoc,
11925 SourceLocation EndLoc) {
11926 if (VarList.empty())
11927 return nullptr;
11928
11929 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
11930}
Alexey Bataevdea47612014-07-23 07:46:59 +000011931
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011932OMPClause *
11933Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
11934 SourceLocation DepLoc, SourceLocation ColonLoc,
11935 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
11936 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +000011937 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011938 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +000011939 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000011940 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +000011941 return nullptr;
11942 }
11943 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011944 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
11945 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +000011946 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011947 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000011948 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
11949 /*Last=*/OMPC_DEPEND_unknown, Except)
11950 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011951 return nullptr;
11952 }
11953 SmallVector<Expr *, 8> Vars;
Alexey Bataev8b427062016-05-25 12:36:08 +000011954 DSAStackTy::OperatorOffsetTy OpsOffs;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011955 llvm::APSInt DepCounter(/*BitWidth=*/32);
11956 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
Alexey Bataevf138fda2018-08-13 19:04:24 +000011957 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) {
11958 if (const Expr *OrderedCountExpr =
11959 DSAStack->getParentOrderedRegionParam().first) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011960 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
11961 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011962 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011963 }
Alexey Bataeve3727102018-04-18 15:57:46 +000011964 for (Expr *RefExpr : VarList) {
Alexey Bataev17daedf2018-02-15 22:42:57 +000011965 assert(RefExpr && "NULL expr in OpenMP shared clause.");
11966 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
11967 // It will be analyzed later.
11968 Vars.push_back(RefExpr);
11969 continue;
11970 }
11971
11972 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataeve3727102018-04-18 15:57:46 +000011973 Expr *SimpleExpr = RefExpr->IgnoreParenCasts();
Alexey Bataev17daedf2018-02-15 22:42:57 +000011974 if (DepKind == OMPC_DEPEND_sink) {
Alexey Bataevf138fda2018-08-13 19:04:24 +000011975 if (DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000011976 DepCounter >= TotalDepCount) {
11977 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
11978 continue;
11979 }
11980 ++DepCounter;
11981 // OpenMP [2.13.9, Summary]
11982 // depend(dependence-type : vec), where dependence-type is:
11983 // 'sink' and where vec is the iteration vector, which has the form:
11984 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
11985 // where n is the value specified by the ordered clause in the loop
11986 // directive, xi denotes the loop iteration variable of the i-th nested
11987 // loop associated with the loop directive, and di is a constant
11988 // non-negative integer.
11989 if (CurContext->isDependentContext()) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011990 // It will be analyzed later.
11991 Vars.push_back(RefExpr);
11992 continue;
11993 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000011994 SimpleExpr = SimpleExpr->IgnoreImplicit();
11995 OverloadedOperatorKind OOK = OO_None;
11996 SourceLocation OOLoc;
11997 Expr *LHS = SimpleExpr;
11998 Expr *RHS = nullptr;
11999 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
12000 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
12001 OOLoc = BO->getOperatorLoc();
12002 LHS = BO->getLHS()->IgnoreParenImpCasts();
12003 RHS = BO->getRHS()->IgnoreParenImpCasts();
12004 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
12005 OOK = OCE->getOperator();
12006 OOLoc = OCE->getOperatorLoc();
12007 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
12008 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
12009 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
12010 OOK = MCE->getMethodDecl()
12011 ->getNameInfo()
12012 .getName()
12013 .getCXXOverloadedOperator();
12014 OOLoc = MCE->getCallee()->getExprLoc();
12015 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
12016 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012017 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000012018 SourceLocation ELoc;
12019 SourceRange ERange;
Alexey Bataevbc529672018-09-28 19:33:14 +000012020 auto Res = getPrivateItem(*this, LHS, ELoc, ERange);
Alexey Bataev17daedf2018-02-15 22:42:57 +000012021 if (Res.second) {
12022 // It will be analyzed later.
12023 Vars.push_back(RefExpr);
12024 }
12025 ValueDecl *D = Res.first;
12026 if (!D)
12027 continue;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012028
Alexey Bataev17daedf2018-02-15 22:42:57 +000012029 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
12030 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
12031 continue;
12032 }
12033 if (RHS) {
12034 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
12035 RHS, OMPC_depend, /*StrictlyPositive=*/false);
12036 if (RHSRes.isInvalid())
12037 continue;
12038 }
12039 if (!CurContext->isDependentContext() &&
Alexey Bataevf138fda2018-08-13 19:04:24 +000012040 DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000012041 DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012042 const ValueDecl *VD =
Alexey Bataev17daedf2018-02-15 22:42:57 +000012043 DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue());
Alexey Bataeve3727102018-04-18 15:57:46 +000012044 if (VD)
Alexey Bataev17daedf2018-02-15 22:42:57 +000012045 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
12046 << 1 << VD;
Alexey Bataeve3727102018-04-18 15:57:46 +000012047 else
Alexey Bataev17daedf2018-02-15 22:42:57 +000012048 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0;
Alexey Bataev17daedf2018-02-15 22:42:57 +000012049 continue;
12050 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012051 OpsOffs.emplace_back(RHS, OOK);
Alexey Bataev17daedf2018-02-15 22:42:57 +000012052 } else {
12053 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
12054 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
12055 (ASE &&
12056 !ASE->getBase()->getType().getNonReferenceType()->isPointerType() &&
12057 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
12058 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
12059 << RefExpr->getSourceRange();
12060 continue;
12061 }
12062 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
12063 getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
12064 ExprResult Res =
12065 CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RefExpr->IgnoreParenImpCasts());
12066 getDiagnostics().setSuppressAllDiagnostics(Suppress);
12067 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr)) {
12068 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
12069 << RefExpr->getSourceRange();
12070 continue;
12071 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012072 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000012073 Vars.push_back(RefExpr->IgnoreParenImpCasts());
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012074 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000012075
12076 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
12077 TotalDepCount > VarList.size() &&
Alexey Bataevf138fda2018-08-13 19:04:24 +000012078 DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000012079 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) {
12080 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
12081 << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
12082 }
12083 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
12084 Vars.empty())
12085 return nullptr;
12086
Alexey Bataev8b427062016-05-25 12:36:08 +000012087 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataevf138fda2018-08-13 19:04:24 +000012088 DepKind, DepLoc, ColonLoc, Vars,
12089 TotalDepCount.getZExtValue());
Alexey Bataev17daedf2018-02-15 22:42:57 +000012090 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) &&
12091 DSAStack->isParentOrderedRegion())
Alexey Bataev8b427062016-05-25 12:36:08 +000012092 DSAStack->addDoacrossDependClause(C, OpsOffs);
12093 return C;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012094}
Michael Wonge710d542015-08-07 16:16:36 +000012095
12096OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
12097 SourceLocation LParenLoc,
12098 SourceLocation EndLoc) {
12099 Expr *ValExpr = Device;
Alexey Bataev931e19b2017-10-02 16:32:39 +000012100 Stmt *HelperValStmt = nullptr;
Michael Wonge710d542015-08-07 16:16:36 +000012101
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012102 // OpenMP [2.9.1, Restrictions]
12103 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000012104 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
Alexey Bataeva0569352015-12-01 10:17:31 +000012105 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012106 return nullptr;
12107
Alexey Bataev931e19b2017-10-02 16:32:39 +000012108 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000012109 OpenMPDirectiveKind CaptureRegion =
12110 getOpenMPCaptureRegionForClause(DKind, OMPC_device);
12111 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000012112 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000012113 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev931e19b2017-10-02 16:32:39 +000012114 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
12115 HelperValStmt = buildPreInits(Context, Captures);
12116 }
12117
Alexey Bataev8451efa2018-01-15 19:06:12 +000012118 return new (Context) OMPDeviceClause(ValExpr, HelperValStmt, CaptureRegion,
12119 StartLoc, LParenLoc, EndLoc);
Michael Wonge710d542015-08-07 16:16:36 +000012120}
Kelvin Li0bff7af2015-11-23 05:32:03 +000012121
Alexey Bataeve3727102018-04-18 15:57:46 +000012122static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
Alexey Bataev95c23e72018-02-27 21:31:11 +000012123 DSAStackTy *Stack, QualType QTy,
12124 bool FullCheck = true) {
Kelvin Li0bff7af2015-11-23 05:32:03 +000012125 NamedDecl *ND;
12126 if (QTy->isIncompleteType(&ND)) {
12127 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
12128 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +000012129 }
Alexey Bataev95c23e72018-02-27 21:31:11 +000012130 if (FullCheck && !SemaRef.CurContext->isDependentContext() &&
12131 !QTy.isTrivialType(SemaRef.Context))
12132 SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR;
Kelvin Li0bff7af2015-11-23 05:32:03 +000012133 return true;
12134}
12135
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000012136/// Return true if it can be proven that the provided array expression
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012137/// (array section or array subscript) does NOT specify the whole size of the
12138/// array whose base type is \a BaseQTy.
Alexey Bataeve3727102018-04-18 15:57:46 +000012139static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012140 const Expr *E,
12141 QualType BaseQTy) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012142 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012143
12144 // If this is an array subscript, it refers to the whole size if the size of
12145 // the dimension is constant and equals 1. Also, an array section assumes the
12146 // format of an array subscript if no colon is used.
12147 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012148 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012149 return ATy->getSize().getSExtValue() != 1;
12150 // Size can't be evaluated statically.
12151 return false;
12152 }
12153
12154 assert(OASE && "Expecting array section if not an array subscript.");
Alexey Bataeve3727102018-04-18 15:57:46 +000012155 const Expr *LowerBound = OASE->getLowerBound();
12156 const Expr *Length = OASE->getLength();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012157
12158 // If there is a lower bound that does not evaluates to zero, we are not
David Majnemer9d168222016-08-05 17:44:54 +000012159 // covering the whole dimension.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012160 if (LowerBound) {
12161 llvm::APSInt ConstLowerBound;
12162 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
12163 return false; // Can't get the integer value as a constant.
12164 if (ConstLowerBound.getSExtValue())
12165 return true;
12166 }
12167
12168 // If we don't have a length we covering the whole dimension.
12169 if (!Length)
12170 return false;
12171
12172 // If the base is a pointer, we don't have a way to get the size of the
12173 // pointee.
12174 if (BaseQTy->isPointerType())
12175 return false;
12176
12177 // We can only check if the length is the same as the size of the dimension
12178 // if we have a constant array.
Alexey Bataeve3727102018-04-18 15:57:46 +000012179 const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012180 if (!CATy)
12181 return false;
12182
12183 llvm::APSInt ConstLength;
12184 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
12185 return false; // Can't get the integer value as a constant.
12186
12187 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
12188}
12189
12190// Return true if it can be proven that the provided array expression (array
12191// section or array subscript) does NOT specify a single element of the array
12192// whose base type is \a BaseQTy.
Alexey Bataeve3727102018-04-18 15:57:46 +000012193static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
David Majnemer9d168222016-08-05 17:44:54 +000012194 const Expr *E,
12195 QualType BaseQTy) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012196 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012197
12198 // An array subscript always refer to a single element. Also, an array section
12199 // assumes the format of an array subscript if no colon is used.
12200 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
12201 return false;
12202
12203 assert(OASE && "Expecting array section if not an array subscript.");
Alexey Bataeve3727102018-04-18 15:57:46 +000012204 const Expr *Length = OASE->getLength();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012205
12206 // If we don't have a length we have to check if the array has unitary size
12207 // for this dimension. Also, we should always expect a length if the base type
12208 // is pointer.
12209 if (!Length) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012210 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012211 return ATy->getSize().getSExtValue() != 1;
12212 // We cannot assume anything.
12213 return false;
12214 }
12215
12216 // Check if the length evaluates to 1.
12217 llvm::APSInt ConstLength;
12218 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
12219 return false; // Can't get the integer value as a constant.
12220
12221 return ConstLength.getSExtValue() != 1;
12222}
12223
Samuel Antao661c0902016-05-26 17:39:58 +000012224// Return the expression of the base of the mappable expression or null if it
12225// cannot be determined and do all the necessary checks to see if the expression
12226// is valid as a standalone mappable expression. In the process, record all the
Samuel Antao90927002016-04-26 14:54:23 +000012227// components of the expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000012228static const Expr *checkMapClauseExpressionBase(
Samuel Antao90927002016-04-26 14:54:23 +000012229 Sema &SemaRef, Expr *E,
Samuel Antao661c0902016-05-26 17:39:58 +000012230 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012231 OpenMPClauseKind CKind, bool NoDiagnose) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012232 SourceLocation ELoc = E->getExprLoc();
12233 SourceRange ERange = E->getSourceRange();
12234
12235 // The base of elements of list in a map clause have to be either:
12236 // - a reference to variable or field.
12237 // - a member expression.
12238 // - an array expression.
12239 //
12240 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
12241 // reference to 'r'.
12242 //
12243 // If we have:
12244 //
12245 // struct SS {
12246 // Bla S;
12247 // foo() {
12248 // #pragma omp target map (S.Arr[:12]);
12249 // }
12250 // }
12251 //
12252 // We want to retrieve the member expression 'this->S';
12253
Alexey Bataeve3727102018-04-18 15:57:46 +000012254 const Expr *RelevantExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012255
Samuel Antao5de996e2016-01-22 20:21:36 +000012256 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
12257 // If a list item is an array section, it must specify contiguous storage.
12258 //
12259 // For this restriction it is sufficient that we make sure only references
12260 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012261 // exist except in the rightmost expression (unless they cover the whole
12262 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +000012263 //
12264 // r.ArrS[3:5].Arr[6:7]
12265 //
12266 // r.ArrS[3:5].x
12267 //
12268 // but these would be valid:
12269 // r.ArrS[3].Arr[6:7]
12270 //
12271 // r.ArrS[3].x
12272
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012273 bool AllowUnitySizeArraySection = true;
12274 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +000012275
Dmitry Polukhin644a9252016-03-11 07:58:34 +000012276 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012277 E = E->IgnoreParenImpCasts();
12278
12279 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
12280 if (!isa<VarDecl>(CurE->getDecl()))
Alexey Bataev27041fa2017-12-05 15:22:49 +000012281 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012282
12283 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012284
12285 // If we got a reference to a declaration, we should not expect any array
12286 // section before that.
12287 AllowUnitySizeArraySection = false;
12288 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000012289
12290 // Record the component.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012291 CurComponents.emplace_back(CurE, CurE->getDecl());
12292 } else if (auto *CurE = dyn_cast<MemberExpr>(E)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012293 Expr *BaseE = CurE->getBase()->IgnoreParenImpCasts();
Samuel Antao5de996e2016-01-22 20:21:36 +000012294
12295 if (isa<CXXThisExpr>(BaseE))
12296 // We found a base expression: this->Val.
12297 RelevantExpr = CurE;
12298 else
12299 E = BaseE;
12300
12301 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012302 if (!NoDiagnose) {
12303 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
12304 << CurE->getSourceRange();
12305 return nullptr;
12306 }
12307 if (RelevantExpr)
12308 return nullptr;
12309 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000012310 }
12311
12312 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
12313
12314 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
12315 // A bit-field cannot appear in a map clause.
12316 //
12317 if (FD->isBitField()) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012318 if (!NoDiagnose) {
12319 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
12320 << CurE->getSourceRange() << getOpenMPClauseName(CKind);
12321 return nullptr;
12322 }
12323 if (RelevantExpr)
12324 return nullptr;
12325 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000012326 }
12327
12328 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12329 // If the type of a list item is a reference to a type T then the type
12330 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012331 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000012332
12333 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
12334 // A list item cannot be a variable that is a member of a structure with
12335 // a union type.
12336 //
Alexey Bataeve3727102018-04-18 15:57:46 +000012337 if (CurType->isUnionType()) {
12338 if (!NoDiagnose) {
12339 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
12340 << CurE->getSourceRange();
12341 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012342 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012343 continue;
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012344 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012345
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012346 // If we got a member expression, we should not expect any array section
12347 // before that:
12348 //
12349 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
12350 // If a list item is an element of a structure, only the rightmost symbol
12351 // of the variable reference can be an array section.
12352 //
12353 AllowUnitySizeArraySection = false;
12354 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000012355
12356 // Record the component.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012357 CurComponents.emplace_back(CurE, FD);
12358 } else if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012359 E = CurE->getBase()->IgnoreParenImpCasts();
12360
12361 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012362 if (!NoDiagnose) {
12363 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
12364 << 0 << CurE->getSourceRange();
12365 return nullptr;
12366 }
12367 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000012368 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012369
12370 // If we got an array subscript that express the whole dimension we
12371 // can have any array expressions before. If it only expressing part of
12372 // the dimension, we can only have unitary-size array expressions.
Alexey Bataeve3727102018-04-18 15:57:46 +000012373 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012374 E->getType()))
12375 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000012376
12377 // Record the component - we don't have any declaration associated.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012378 CurComponents.emplace_back(CurE, nullptr);
12379 } else if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012380 assert(!NoDiagnose && "Array sections cannot be implicitly mapped.");
Samuel Antao5de996e2016-01-22 20:21:36 +000012381 E = CurE->getBase()->IgnoreParenImpCasts();
12382
Alexey Bataev27041fa2017-12-05 15:22:49 +000012383 QualType CurType =
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012384 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
12385
Samuel Antao5de996e2016-01-22 20:21:36 +000012386 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12387 // If the type of a list item is a reference to a type T then the type
12388 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +000012389 if (CurType->isReferenceType())
12390 CurType = CurType->getPointeeType();
12391
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012392 bool IsPointer = CurType->isAnyPointerType();
12393
12394 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012395 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
12396 << 0 << CurE->getSourceRange();
Alexey Bataev27041fa2017-12-05 15:22:49 +000012397 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012398 }
12399
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012400 bool NotWhole =
Alexey Bataeve3727102018-04-18 15:57:46 +000012401 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012402 bool NotUnity =
Alexey Bataeve3727102018-04-18 15:57:46 +000012403 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012404
Samuel Antaodab51bb2016-07-18 23:22:11 +000012405 if (AllowWholeSizeArraySection) {
12406 // Any array section is currently allowed. Allowing a whole size array
12407 // section implies allowing a unity array section as well.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012408 //
12409 // If this array section refers to the whole dimension we can still
12410 // accept other array sections before this one, except if the base is a
12411 // pointer. Otherwise, only unitary sections are accepted.
12412 if (NotWhole || IsPointer)
12413 AllowWholeSizeArraySection = false;
Samuel Antaodab51bb2016-07-18 23:22:11 +000012414 } else if (AllowUnitySizeArraySection && NotUnity) {
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012415 // A unity or whole array section is not allowed and that is not
12416 // compatible with the properties of the current array section.
12417 SemaRef.Diag(
12418 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
12419 << CurE->getSourceRange();
Alexey Bataev27041fa2017-12-05 15:22:49 +000012420 return nullptr;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012421 }
Samuel Antao90927002016-04-26 14:54:23 +000012422
12423 // Record the component - we don't have any declaration associated.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012424 CurComponents.emplace_back(CurE, nullptr);
12425 } else {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012426 if (!NoDiagnose) {
12427 // If nothing else worked, this is not a valid map clause expression.
12428 SemaRef.Diag(
12429 ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
12430 << ERange;
12431 }
Alexey Bataev27041fa2017-12-05 15:22:49 +000012432 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012433 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012434 }
12435
12436 return RelevantExpr;
12437}
12438
12439// Return true if expression E associated with value VD has conflicts with other
12440// map information.
Alexey Bataeve3727102018-04-18 15:57:46 +000012441static bool checkMapConflicts(
12442 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E,
Samuel Antao90927002016-04-26 14:54:23 +000012443 bool CurrentRegionOnly,
Samuel Antao661c0902016-05-26 17:39:58 +000012444 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
12445 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012446 assert(VD && E);
Samuel Antao5de996e2016-01-22 20:21:36 +000012447 SourceLocation ELoc = E->getExprLoc();
12448 SourceRange ERange = E->getSourceRange();
12449
12450 // In order to easily check the conflicts we need to match each component of
12451 // the expression under test with the components of the expressions that are
12452 // already in the stack.
12453
Samuel Antao5de996e2016-01-22 20:21:36 +000012454 assert(!CurComponents.empty() && "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000012455 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000012456 "Map clause expression with unexpected base!");
12457
12458 // Variables to help detecting enclosing problems in data environment nests.
12459 bool IsEnclosedByDataEnvironmentExpr = false;
Samuel Antao90927002016-04-26 14:54:23 +000012460 const Expr *EnclosingExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012461
Samuel Antao90927002016-04-26 14:54:23 +000012462 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
12463 VD, CurrentRegionOnly,
Alexey Bataeve3727102018-04-18 15:57:46 +000012464 [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc,
12465 ERange, CKind, &EnclosingExpr,
12466 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef
12467 StackComponents,
12468 OpenMPClauseKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012469 assert(!StackComponents.empty() &&
12470 "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000012471 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000012472 "Map clause expression with unexpected base!");
Fangrui Song16fe49a2018-04-18 19:32:01 +000012473 (void)VD;
Samuel Antao5de996e2016-01-22 20:21:36 +000012474
Samuel Antao90927002016-04-26 14:54:23 +000012475 // The whole expression in the stack.
Alexey Bataeve3727102018-04-18 15:57:46 +000012476 const Expr *RE = StackComponents.front().getAssociatedExpression();
Samuel Antao90927002016-04-26 14:54:23 +000012477
Samuel Antao5de996e2016-01-22 20:21:36 +000012478 // Expressions must start from the same base. Here we detect at which
12479 // point both expressions diverge from each other and see if we can
12480 // detect if the memory referred to both expressions is contiguous and
12481 // do not overlap.
12482 auto CI = CurComponents.rbegin();
12483 auto CE = CurComponents.rend();
12484 auto SI = StackComponents.rbegin();
12485 auto SE = StackComponents.rend();
12486 for (; CI != CE && SI != SE; ++CI, ++SI) {
12487
12488 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
12489 // At most one list item can be an array item derived from a given
12490 // variable in map clauses of the same construct.
Samuel Antao90927002016-04-26 14:54:23 +000012491 if (CurrentRegionOnly &&
12492 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
12493 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
12494 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
12495 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
12496 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
Samuel Antao5de996e2016-01-22 20:21:36 +000012497 diag::err_omp_multiple_array_items_in_map_clause)
Samuel Antao90927002016-04-26 14:54:23 +000012498 << CI->getAssociatedExpression()->getSourceRange();
12499 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
12500 diag::note_used_here)
12501 << SI->getAssociatedExpression()->getSourceRange();
Samuel Antao5de996e2016-01-22 20:21:36 +000012502 return true;
12503 }
12504
12505 // Do both expressions have the same kind?
Samuel Antao90927002016-04-26 14:54:23 +000012506 if (CI->getAssociatedExpression()->getStmtClass() !=
12507 SI->getAssociatedExpression()->getStmtClass())
Samuel Antao5de996e2016-01-22 20:21:36 +000012508 break;
12509
12510 // Are we dealing with different variables/fields?
Samuel Antao90927002016-04-26 14:54:23 +000012511 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
Samuel Antao5de996e2016-01-22 20:21:36 +000012512 break;
12513 }
Kelvin Li9f645ae2016-07-18 22:49:16 +000012514 // Check if the extra components of the expressions in the enclosing
12515 // data environment are redundant for the current base declaration.
12516 // If they are, the maps completely overlap, which is legal.
12517 for (; SI != SE; ++SI) {
12518 QualType Type;
Alexey Bataeve3727102018-04-18 15:57:46 +000012519 if (const auto *ASE =
David Majnemer9d168222016-08-05 17:44:54 +000012520 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
Kelvin Li9f645ae2016-07-18 22:49:16 +000012521 Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
Alexey Bataeve3727102018-04-18 15:57:46 +000012522 } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(
David Majnemer9d168222016-08-05 17:44:54 +000012523 SI->getAssociatedExpression())) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012524 const Expr *E = OASE->getBase()->IgnoreParenImpCasts();
Kelvin Li9f645ae2016-07-18 22:49:16 +000012525 Type =
12526 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
12527 }
12528 if (Type.isNull() || Type->isAnyPointerType() ||
Alexey Bataeve3727102018-04-18 15:57:46 +000012529 checkArrayExpressionDoesNotReferToWholeSize(
Kelvin Li9f645ae2016-07-18 22:49:16 +000012530 SemaRef, SI->getAssociatedExpression(), Type))
12531 break;
12532 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012533
12534 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
12535 // List items of map clauses in the same construct must not share
12536 // original storage.
12537 //
12538 // If the expressions are exactly the same or one is a subset of the
12539 // other, it means they are sharing storage.
12540 if (CI == CE && SI == SE) {
12541 if (CurrentRegionOnly) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012542 if (CKind == OMPC_map) {
Samuel Antao661c0902016-05-26 17:39:58 +000012543 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +000012544 } else {
Samuel Antaoec172c62016-05-26 17:49:04 +000012545 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000012546 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
12547 << ERange;
12548 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012549 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12550 << RE->getSourceRange();
12551 return true;
Samuel Antao5de996e2016-01-22 20:21:36 +000012552 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012553 // If we find the same expression in the enclosing data environment,
12554 // that is legal.
12555 IsEnclosedByDataEnvironmentExpr = true;
12556 return false;
Samuel Antao5de996e2016-01-22 20:21:36 +000012557 }
12558
Samuel Antao90927002016-04-26 14:54:23 +000012559 QualType DerivedType =
12560 std::prev(CI)->getAssociatedDeclaration()->getType();
12561 SourceLocation DerivedLoc =
12562 std::prev(CI)->getAssociatedExpression()->getExprLoc();
Samuel Antao5de996e2016-01-22 20:21:36 +000012563
12564 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12565 // If the type of a list item is a reference to a type T then the type
12566 // will be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000012567 DerivedType = DerivedType.getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000012568
12569 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
12570 // A variable for which the type is pointer and an array section
12571 // derived from that variable must not appear as list items of map
12572 // clauses of the same construct.
12573 //
12574 // Also, cover one of the cases in:
12575 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
12576 // If any part of the original storage of a list item has corresponding
12577 // storage in the device data environment, all of the original storage
12578 // must have corresponding storage in the device data environment.
12579 //
12580 if (DerivedType->isAnyPointerType()) {
12581 if (CI == CE || SI == SE) {
12582 SemaRef.Diag(
12583 DerivedLoc,
12584 diag::err_omp_pointer_mapped_along_with_derived_section)
12585 << DerivedLoc;
Alexey Bataev2819260b2018-02-27 17:42:00 +000012586 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12587 << RE->getSourceRange();
12588 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +000012589 }
12590 if (CI->getAssociatedExpression()->getStmtClass() !=
Alexey Bataev2819260b2018-02-27 17:42:00 +000012591 SI->getAssociatedExpression()->getStmtClass() ||
12592 CI->getAssociatedDeclaration()->getCanonicalDecl() ==
12593 SI->getAssociatedDeclaration()->getCanonicalDecl()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012594 assert(CI != CE && SI != SE);
Alexey Bataev2819260b2018-02-27 17:42:00 +000012595 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced)
Samuel Antao5de996e2016-01-22 20:21:36 +000012596 << DerivedLoc;
Alexey Bataev2819260b2018-02-27 17:42:00 +000012597 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12598 << RE->getSourceRange();
12599 return true;
Samuel Antao5de996e2016-01-22 20:21:36 +000012600 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012601 }
12602
12603 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
12604 // List items of map clauses in the same construct must not share
12605 // original storage.
12606 //
12607 // An expression is a subset of the other.
12608 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012609 if (CKind == OMPC_map) {
Alexey Bataeve82445f2018-09-20 13:54:02 +000012610 if (CI != CE || SI != SE) {
12611 // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is
12612 // a pointer.
12613 auto Begin =
12614 CI != CE ? CurComponents.begin() : StackComponents.begin();
12615 auto End = CI != CE ? CurComponents.end() : StackComponents.end();
12616 auto It = Begin;
12617 while (It != End && !It->getAssociatedDeclaration())
12618 std::advance(It, 1);
12619 assert(It != End &&
12620 "Expected at least one component with the declaration.");
12621 if (It != Begin && It->getAssociatedDeclaration()
12622 ->getType()
12623 .getCanonicalType()
12624 ->isAnyPointerType()) {
12625 IsEnclosedByDataEnvironmentExpr = false;
12626 EnclosingExpr = nullptr;
12627 return false;
12628 }
12629 }
Samuel Antao661c0902016-05-26 17:39:58 +000012630 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +000012631 } else {
Samuel Antaoec172c62016-05-26 17:49:04 +000012632 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000012633 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
12634 << ERange;
12635 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012636 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12637 << RE->getSourceRange();
12638 return true;
12639 }
12640
12641 // The current expression uses the same base as other expression in the
Samuel Antao90927002016-04-26 14:54:23 +000012642 // data environment but does not contain it completely.
Samuel Antao5de996e2016-01-22 20:21:36 +000012643 if (!CurrentRegionOnly && SI != SE)
12644 EnclosingExpr = RE;
12645
12646 // The current expression is a subset of the expression in the data
12647 // environment.
12648 IsEnclosedByDataEnvironmentExpr |=
12649 (!CurrentRegionOnly && CI != CE && SI == SE);
12650
12651 return false;
12652 });
12653
12654 if (CurrentRegionOnly)
12655 return FoundError;
12656
12657 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
12658 // If any part of the original storage of a list item has corresponding
12659 // storage in the device data environment, all of the original storage must
12660 // have corresponding storage in the device data environment.
12661 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
12662 // If a list item is an element of a structure, and a different element of
12663 // the structure has a corresponding list item in the device data environment
12664 // prior to a task encountering the construct associated with the map clause,
Samuel Antao90927002016-04-26 14:54:23 +000012665 // then the list item must also have a corresponding list item in the device
Samuel Antao5de996e2016-01-22 20:21:36 +000012666 // data environment prior to the task encountering the construct.
12667 //
12668 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
12669 SemaRef.Diag(ELoc,
12670 diag::err_omp_original_storage_is_shared_and_does_not_contain)
12671 << ERange;
12672 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
12673 << EnclosingExpr->getSourceRange();
12674 return true;
12675 }
12676
12677 return FoundError;
12678}
12679
Samuel Antao661c0902016-05-26 17:39:58 +000012680namespace {
12681// Utility struct that gathers all the related lists associated with a mappable
12682// expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000012683struct MappableVarListInfo {
Samuel Antao661c0902016-05-26 17:39:58 +000012684 // The list of expressions.
12685 ArrayRef<Expr *> VarList;
12686 // The list of processed expressions.
12687 SmallVector<Expr *, 16> ProcessedVarList;
12688 // The mappble components for each expression.
12689 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
12690 // The base declaration of the variable.
12691 SmallVector<ValueDecl *, 16> VarBaseDeclarations;
12692
12693 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
12694 // We have a list of components and base declarations for each entry in the
12695 // variable list.
12696 VarComponents.reserve(VarList.size());
12697 VarBaseDeclarations.reserve(VarList.size());
12698 }
12699};
12700}
12701
12702// Check the validity of the provided variable list for the provided clause kind
12703// \a CKind. In the check process the valid expressions, and mappable expression
12704// components and variables are extracted and used to fill \a Vars,
12705// \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and
12706// \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'.
12707static void
12708checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS,
12709 OpenMPClauseKind CKind, MappableVarListInfo &MVLI,
12710 SourceLocation StartLoc,
12711 OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
12712 bool IsMapTypeImplicit = false) {
Samuel Antaoec172c62016-05-26 17:49:04 +000012713 // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
12714 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
Samuel Antao661c0902016-05-26 17:39:58 +000012715 "Unexpected clause kind with mappable expressions!");
Kelvin Li0bff7af2015-11-23 05:32:03 +000012716
Samuel Antao90927002016-04-26 14:54:23 +000012717 // Keep track of the mappable components and base declarations in this clause.
12718 // Each entry in the list is going to have a list of components associated. We
12719 // record each set of the components so that we can build the clause later on.
12720 // In the end we should have the same amount of declarations and component
12721 // lists.
Samuel Antao90927002016-04-26 14:54:23 +000012722
Alexey Bataeve3727102018-04-18 15:57:46 +000012723 for (Expr *RE : MVLI.VarList) {
Samuel Antaoec172c62016-05-26 17:49:04 +000012724 assert(RE && "Null expr in omp to/from/map clause");
Kelvin Li0bff7af2015-11-23 05:32:03 +000012725 SourceLocation ELoc = RE->getExprLoc();
12726
Alexey Bataeve3727102018-04-18 15:57:46 +000012727 const Expr *VE = RE->IgnoreParenLValueCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000012728
12729 if (VE->isValueDependent() || VE->isTypeDependent() ||
12730 VE->isInstantiationDependent() ||
12731 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012732 // We can only analyze this information once the missing information is
12733 // resolved.
Samuel Antao661c0902016-05-26 17:39:58 +000012734 MVLI.ProcessedVarList.push_back(RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012735 continue;
12736 }
12737
Alexey Bataeve3727102018-04-18 15:57:46 +000012738 Expr *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000012739
Samuel Antao5de996e2016-01-22 20:21:36 +000012740 if (!RE->IgnoreParenImpCasts()->isLValue()) {
Samuel Antao661c0902016-05-26 17:39:58 +000012741 SemaRef.Diag(ELoc,
12742 diag::err_omp_expected_named_var_member_or_array_expression)
Samuel Antao5de996e2016-01-22 20:21:36 +000012743 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000012744 continue;
12745 }
12746
Samuel Antao90927002016-04-26 14:54:23 +000012747 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
12748 ValueDecl *CurDeclaration = nullptr;
12749
12750 // Obtain the array or member expression bases if required. Also, fill the
12751 // components array with all the components identified in the process.
Alexey Bataeve3727102018-04-18 15:57:46 +000012752 const Expr *BE = checkMapClauseExpressionBase(
12753 SemaRef, SimpleExpr, CurComponents, CKind, /*NoDiagnose=*/false);
Samuel Antao5de996e2016-01-22 20:21:36 +000012754 if (!BE)
12755 continue;
12756
Samuel Antao90927002016-04-26 14:54:23 +000012757 assert(!CurComponents.empty() &&
12758 "Invalid mappable expression information.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000012759
Samuel Antao90927002016-04-26 14:54:23 +000012760 // For the following checks, we rely on the base declaration which is
12761 // expected to be associated with the last component. The declaration is
12762 // expected to be a variable or a field (if 'this' is being mapped).
12763 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
12764 assert(CurDeclaration && "Null decl on map clause.");
12765 assert(
12766 CurDeclaration->isCanonicalDecl() &&
12767 "Expecting components to have associated only canonical declarations.");
12768
12769 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
Alexey Bataeve3727102018-04-18 15:57:46 +000012770 const auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
Samuel Antao5de996e2016-01-22 20:21:36 +000012771
12772 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000012773 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000012774
12775 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
Samuel Antao661c0902016-05-26 17:39:58 +000012776 // threadprivate variables cannot appear in a map clause.
12777 // OpenMP 4.5 [2.10.5, target update Construct]
12778 // threadprivate variables cannot appear in a from clause.
12779 if (VD && DSAS->isThreadPrivate(VD)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012780 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
Samuel Antao661c0902016-05-26 17:39:58 +000012781 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
12782 << getOpenMPClauseName(CKind);
Alexey Bataeve3727102018-04-18 15:57:46 +000012783 reportOriginalDsa(SemaRef, DSAS, VD, DVar);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012784 continue;
12785 }
12786
Samuel Antao5de996e2016-01-22 20:21:36 +000012787 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
12788 // A list item cannot appear in both a map clause and a data-sharing
12789 // attribute clause on the same construct.
Kelvin Li0bff7af2015-11-23 05:32:03 +000012790
Samuel Antao5de996e2016-01-22 20:21:36 +000012791 // Check conflicts with other map clause expressions. We check the conflicts
12792 // with the current construct separately from the enclosing data
Samuel Antao661c0902016-05-26 17:39:58 +000012793 // environment, because the restrictions are different. We only have to
12794 // check conflicts across regions for the map clauses.
Alexey Bataeve3727102018-04-18 15:57:46 +000012795 if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
Samuel Antao661c0902016-05-26 17:39:58 +000012796 /*CurrentRegionOnly=*/true, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000012797 break;
Samuel Antao661c0902016-05-26 17:39:58 +000012798 if (CKind == OMPC_map &&
Alexey Bataeve3727102018-04-18 15:57:46 +000012799 checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
Samuel Antao661c0902016-05-26 17:39:58 +000012800 /*CurrentRegionOnly=*/false, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000012801 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000012802
Samuel Antao661c0902016-05-26 17:39:58 +000012803 // OpenMP 4.5 [2.10.5, target update Construct]
Samuel Antao5de996e2016-01-22 20:21:36 +000012804 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12805 // If the type of a list item is a reference to a type T then the type will
12806 // be considered to be T for all purposes of this clause.
Alexey Bataev354df2e2018-05-02 18:44:10 +000012807 auto I = llvm::find_if(
12808 CurComponents,
12809 [](const OMPClauseMappableExprCommon::MappableComponent &MC) {
12810 return MC.getAssociatedDeclaration();
12811 });
12812 assert(I != CurComponents.end() && "Null decl on map clause.");
12813 QualType Type =
12814 I->getAssociatedDeclaration()->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000012815
Samuel Antao661c0902016-05-26 17:39:58 +000012816 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
12817 // A list item in a to or from clause must have a mappable type.
Samuel Antao5de996e2016-01-22 20:21:36 +000012818 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000012819 // A list item must have a mappable type.
Alexey Bataeve3727102018-04-18 15:57:46 +000012820 if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
Samuel Antao661c0902016-05-26 17:39:58 +000012821 DSAS, Type))
Kelvin Li0bff7af2015-11-23 05:32:03 +000012822 continue;
12823
Samuel Antao661c0902016-05-26 17:39:58 +000012824 if (CKind == OMPC_map) {
12825 // target enter data
12826 // OpenMP [2.10.2, Restrictions, p. 99]
12827 // A map-type must be specified in all map clauses and must be either
12828 // to or alloc.
12829 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
12830 if (DKind == OMPD_target_enter_data &&
12831 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
12832 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
12833 << (IsMapTypeImplicit ? 1 : 0)
12834 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
12835 << getOpenMPDirectiveName(DKind);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000012836 continue;
12837 }
Samuel Antao661c0902016-05-26 17:39:58 +000012838
12839 // target exit_data
12840 // OpenMP [2.10.3, Restrictions, p. 102]
12841 // A map-type must be specified in all map clauses and must be either
12842 // from, release, or delete.
12843 if (DKind == OMPD_target_exit_data &&
12844 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
12845 MapType == OMPC_MAP_delete)) {
12846 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
12847 << (IsMapTypeImplicit ? 1 : 0)
12848 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
12849 << getOpenMPDirectiveName(DKind);
12850 continue;
12851 }
12852
12853 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
12854 // A list item cannot appear in both a map clause and a data-sharing
12855 // attribute clause on the same construct
Alexey Bataeve3727102018-04-18 15:57:46 +000012856 if (VD && isOpenMPTargetExecutionDirective(DKind)) {
12857 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
Samuel Antao661c0902016-05-26 17:39:58 +000012858 if (isOpenMPPrivate(DVar.CKind)) {
Samuel Antao6890b092016-07-28 14:25:09 +000012859 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Samuel Antao661c0902016-05-26 17:39:58 +000012860 << getOpenMPClauseName(DVar.CKind)
Samuel Antao6890b092016-07-28 14:25:09 +000012861 << getOpenMPClauseName(OMPC_map)
Samuel Antao661c0902016-05-26 17:39:58 +000012862 << getOpenMPDirectiveName(DSAS->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000012863 reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar);
Samuel Antao661c0902016-05-26 17:39:58 +000012864 continue;
12865 }
12866 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +000012867 }
12868
Samuel Antao90927002016-04-26 14:54:23 +000012869 // Save the current expression.
Samuel Antao661c0902016-05-26 17:39:58 +000012870 MVLI.ProcessedVarList.push_back(RE);
Samuel Antao90927002016-04-26 14:54:23 +000012871
12872 // Store the components in the stack so that they can be used to check
12873 // against other clauses later on.
Samuel Antao6890b092016-07-28 14:25:09 +000012874 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents,
12875 /*WhereFoundClauseKind=*/OMPC_map);
Samuel Antao90927002016-04-26 14:54:23 +000012876
12877 // Save the components and declaration to create the clause. For purposes of
12878 // the clause creation, any component list that has has base 'this' uses
Samuel Antao686c70c2016-05-26 17:30:50 +000012879 // null as base declaration.
Samuel Antao661c0902016-05-26 17:39:58 +000012880 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
12881 MVLI.VarComponents.back().append(CurComponents.begin(),
12882 CurComponents.end());
12883 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
12884 : CurDeclaration);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012885 }
Samuel Antao661c0902016-05-26 17:39:58 +000012886}
12887
12888OMPClause *
12889Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
12890 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
12891 SourceLocation MapLoc, SourceLocation ColonLoc,
12892 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
12893 SourceLocation LParenLoc, SourceLocation EndLoc) {
12894 MappableVarListInfo MVLI(VarList);
12895 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc,
12896 MapType, IsMapTypeImplicit);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012897
Samuel Antao5de996e2016-01-22 20:21:36 +000012898 // We need to produce a map clause even if we don't have variables so that
12899 // other diagnostics related with non-existing map clauses are accurate.
Samuel Antao661c0902016-05-26 17:39:58 +000012900 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc,
12901 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
12902 MVLI.VarComponents, MapTypeModifier, MapType,
12903 IsMapTypeImplicit, MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012904}
Kelvin Li099bb8c2015-11-24 20:50:12 +000012905
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012906QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
12907 TypeResult ParsedType) {
12908 assert(ParsedType.isUsable());
12909
12910 QualType ReductionType = GetTypeFromParser(ParsedType.get());
12911 if (ReductionType.isNull())
12912 return QualType();
12913
12914 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
12915 // A type name in a declare reduction directive cannot be a function type, an
12916 // array type, a reference type, or a type qualified with const, volatile or
12917 // restrict.
12918 if (ReductionType.hasQualifiers()) {
12919 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
12920 return QualType();
12921 }
12922
12923 if (ReductionType->isFunctionType()) {
12924 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
12925 return QualType();
12926 }
12927 if (ReductionType->isReferenceType()) {
12928 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
12929 return QualType();
12930 }
12931 if (ReductionType->isArrayType()) {
12932 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
12933 return QualType();
12934 }
12935 return ReductionType;
12936}
12937
12938Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
12939 Scope *S, DeclContext *DC, DeclarationName Name,
12940 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
12941 AccessSpecifier AS, Decl *PrevDeclInScope) {
12942 SmallVector<Decl *, 8> Decls;
12943 Decls.reserve(ReductionTypes.size());
12944
12945 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
Richard Smithbecb92d2017-10-10 22:33:17 +000012946 forRedeclarationInCurContext());
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012947 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
12948 // A reduction-identifier may not be re-declared in the current scope for the
12949 // same type or for a type that is compatible according to the base language
12950 // rules.
12951 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
12952 OMPDeclareReductionDecl *PrevDRD = nullptr;
12953 bool InCompoundScope = true;
12954 if (S != nullptr) {
12955 // Find previous declaration with the same name not referenced in other
12956 // declarations.
12957 FunctionScopeInfo *ParentFn = getEnclosingFunction();
12958 InCompoundScope =
12959 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
12960 LookupName(Lookup, S);
12961 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
12962 /*AllowInlineNamespace=*/false);
12963 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
Alexey Bataeve3727102018-04-18 15:57:46 +000012964 LookupResult::Filter Filter = Lookup.makeFilter();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012965 while (Filter.hasNext()) {
12966 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
12967 if (InCompoundScope) {
12968 auto I = UsedAsPrevious.find(PrevDecl);
12969 if (I == UsedAsPrevious.end())
12970 UsedAsPrevious[PrevDecl] = false;
Alexey Bataeve3727102018-04-18 15:57:46 +000012971 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope())
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012972 UsedAsPrevious[D] = true;
12973 }
12974 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
12975 PrevDecl->getLocation();
12976 }
12977 Filter.done();
12978 if (InCompoundScope) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012979 for (const auto &PrevData : UsedAsPrevious) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012980 if (!PrevData.second) {
12981 PrevDRD = PrevData.first;
12982 break;
12983 }
12984 }
12985 }
12986 } else if (PrevDeclInScope != nullptr) {
12987 auto *PrevDRDInScope = PrevDRD =
12988 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
12989 do {
12990 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
12991 PrevDRDInScope->getLocation();
12992 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
12993 } while (PrevDRDInScope != nullptr);
12994 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012995 for (const auto &TyData : ReductionTypes) {
12996 const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012997 bool Invalid = false;
12998 if (I != PreviousRedeclTypes.end()) {
12999 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
13000 << TyData.first;
13001 Diag(I->second, diag::note_previous_definition);
13002 Invalid = true;
13003 }
13004 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
13005 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
13006 Name, TyData.first, PrevDRD);
13007 DC->addDecl(DRD);
13008 DRD->setAccess(AS);
13009 Decls.push_back(DRD);
13010 if (Invalid)
13011 DRD->setInvalidDecl();
13012 else
13013 PrevDRD = DRD;
13014 }
13015
13016 return DeclGroupPtrTy::make(
13017 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
13018}
13019
13020void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
13021 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13022
13023 // Enter new function scope.
13024 PushFunctionScope();
Reid Kleckner87a31802018-03-12 21:43:02 +000013025 setFunctionHasBranchProtectedScope();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013026 getCurFunction()->setHasOMPDeclareReductionCombiner();
13027
13028 if (S != nullptr)
13029 PushDeclContext(S, DRD);
13030 else
13031 CurContext = DRD;
13032
Faisal Valid143a0c2017-04-01 21:30:49 +000013033 PushExpressionEvaluationContext(
13034 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013035
13036 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013037 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
13038 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
13039 // uses semantics of argument handles by value, but it should be passed by
13040 // reference. C lang does not support references, so pass all parameters as
13041 // pointers.
13042 // Create 'T omp_in;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000013043 VarDecl *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013044 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013045 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
13046 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
13047 // uses semantics of argument handles by value, but it should be passed by
13048 // reference. C lang does not support references, so pass all parameters as
13049 // pointers.
13050 // Create 'T omp_out;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000013051 VarDecl *OmpOutParm =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013052 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
13053 if (S != nullptr) {
13054 PushOnScopeChains(OmpInParm, S);
13055 PushOnScopeChains(OmpOutParm, S);
13056 } else {
13057 DRD->addDecl(OmpInParm);
13058 DRD->addDecl(OmpOutParm);
13059 }
Alexey Bataeve6aa4692018-09-13 16:54:05 +000013060 Expr *InE =
13061 ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation());
13062 Expr *OutE =
13063 ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation());
13064 DRD->setCombinerData(InE, OutE);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013065}
13066
13067void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
13068 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13069 DiscardCleanupsInEvaluationContext();
13070 PopExpressionEvaluationContext();
13071
13072 PopDeclContext();
13073 PopFunctionScopeInfo();
13074
13075 if (Combiner != nullptr)
13076 DRD->setCombiner(Combiner);
13077 else
13078 DRD->setInvalidDecl();
13079}
13080
Alexey Bataev070f43a2017-09-06 14:49:58 +000013081VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013082 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13083
13084 // Enter new function scope.
13085 PushFunctionScope();
Reid Kleckner87a31802018-03-12 21:43:02 +000013086 setFunctionHasBranchProtectedScope();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013087
13088 if (S != nullptr)
13089 PushDeclContext(S, DRD);
13090 else
13091 CurContext = DRD;
13092
Faisal Valid143a0c2017-04-01 21:30:49 +000013093 PushExpressionEvaluationContext(
13094 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013095
13096 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013097 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
13098 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
13099 // uses semantics of argument handles by value, but it should be passed by
13100 // reference. C lang does not support references, so pass all parameters as
13101 // pointers.
13102 // Create 'T omp_priv;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000013103 VarDecl *OmpPrivParm =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013104 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013105 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
13106 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
13107 // uses semantics of argument handles by value, but it should be passed by
13108 // reference. C lang does not support references, so pass all parameters as
13109 // pointers.
13110 // Create 'T omp_orig;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000013111 VarDecl *OmpOrigParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013112 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013113 if (S != nullptr) {
13114 PushOnScopeChains(OmpPrivParm, S);
13115 PushOnScopeChains(OmpOrigParm, S);
13116 } else {
13117 DRD->addDecl(OmpPrivParm);
13118 DRD->addDecl(OmpOrigParm);
13119 }
Alexey Bataeve6aa4692018-09-13 16:54:05 +000013120 Expr *OrigE =
13121 ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation());
13122 Expr *PrivE =
13123 ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation());
13124 DRD->setInitializerData(OrigE, PrivE);
Alexey Bataev070f43a2017-09-06 14:49:58 +000013125 return OmpPrivParm;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013126}
13127
Alexey Bataev070f43a2017-09-06 14:49:58 +000013128void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer,
13129 VarDecl *OmpPrivParm) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013130 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13131 DiscardCleanupsInEvaluationContext();
13132 PopExpressionEvaluationContext();
13133
13134 PopDeclContext();
13135 PopFunctionScopeInfo();
13136
Alexey Bataev070f43a2017-09-06 14:49:58 +000013137 if (Initializer != nullptr) {
13138 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit);
13139 } else if (OmpPrivParm->hasInit()) {
13140 DRD->setInitializer(OmpPrivParm->getInit(),
13141 OmpPrivParm->isDirectInit()
13142 ? OMPDeclareReductionDecl::DirectInit
13143 : OMPDeclareReductionDecl::CopyInit);
13144 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013145 DRD->setInvalidDecl();
Alexey Bataev070f43a2017-09-06 14:49:58 +000013146 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013147}
13148
13149Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
13150 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013151 for (Decl *D : DeclReductions.get()) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013152 if (IsValid) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013153 if (S)
13154 PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S,
13155 /*AddToContext=*/false);
13156 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013157 D->setInvalidDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000013158 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013159 }
13160 return DeclReductions;
13161}
13162
David Majnemer9d168222016-08-05 17:44:54 +000013163OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
Kelvin Li099bb8c2015-11-24 20:50:12 +000013164 SourceLocation StartLoc,
13165 SourceLocation LParenLoc,
13166 SourceLocation EndLoc) {
13167 Expr *ValExpr = NumTeams;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000013168 Stmt *HelperValStmt = nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000013169
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013170 // OpenMP [teams Constrcut, Restrictions]
13171 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000013172 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
Alexey Bataeva0569352015-12-01 10:17:31 +000013173 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013174 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000013175
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000013176 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000013177 OpenMPDirectiveKind CaptureRegion =
13178 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams);
13179 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000013180 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000013181 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000013182 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
13183 HelperValStmt = buildPreInits(Context, Captures);
13184 }
13185
13186 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion,
13187 StartLoc, LParenLoc, EndLoc);
Kelvin Li099bb8c2015-11-24 20:50:12 +000013188}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013189
13190OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
13191 SourceLocation StartLoc,
13192 SourceLocation LParenLoc,
13193 SourceLocation EndLoc) {
13194 Expr *ValExpr = ThreadLimit;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000013195 Stmt *HelperValStmt = nullptr;
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013196
13197 // OpenMP [teams Constrcut, Restrictions]
13198 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000013199 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
Alexey Bataeva0569352015-12-01 10:17:31 +000013200 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013201 return nullptr;
13202
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000013203 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000013204 OpenMPDirectiveKind CaptureRegion =
13205 getOpenMPCaptureRegionForClause(DKind, OMPC_thread_limit);
13206 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000013207 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000013208 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000013209 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
13210 HelperValStmt = buildPreInits(Context, Captures);
13211 }
13212
13213 return new (Context) OMPThreadLimitClause(
13214 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013215}
Alexey Bataeva0569352015-12-01 10:17:31 +000013216
13217OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
13218 SourceLocation StartLoc,
13219 SourceLocation LParenLoc,
13220 SourceLocation EndLoc) {
13221 Expr *ValExpr = Priority;
13222
13223 // OpenMP [2.9.1, task Constrcut]
13224 // The priority-value is a non-negative numerical scalar expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000013225 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
Alexey Bataeva0569352015-12-01 10:17:31 +000013226 /*StrictlyPositive=*/false))
13227 return nullptr;
13228
13229 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
13230}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000013231
13232OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
13233 SourceLocation StartLoc,
13234 SourceLocation LParenLoc,
13235 SourceLocation EndLoc) {
13236 Expr *ValExpr = Grainsize;
13237
13238 // OpenMP [2.9.2, taskloop Constrcut]
13239 // The parameter of the grainsize clause must be a positive integer
13240 // expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000013241 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000013242 /*StrictlyPositive=*/true))
13243 return nullptr;
13244
13245 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
13246}
Alexey Bataev382967a2015-12-08 12:06:20 +000013247
13248OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
13249 SourceLocation StartLoc,
13250 SourceLocation LParenLoc,
13251 SourceLocation EndLoc) {
13252 Expr *ValExpr = NumTasks;
13253
13254 // OpenMP [2.9.2, taskloop Constrcut]
13255 // The parameter of the num_tasks clause must be a positive integer
13256 // expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000013257 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
Alexey Bataev382967a2015-12-08 12:06:20 +000013258 /*StrictlyPositive=*/true))
13259 return nullptr;
13260
13261 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
13262}
13263
Alexey Bataev28c75412015-12-15 08:19:24 +000013264OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
13265 SourceLocation LParenLoc,
13266 SourceLocation EndLoc) {
13267 // OpenMP [2.13.2, critical construct, Description]
13268 // ... where hint-expression is an integer constant expression that evaluates
13269 // to a valid lock hint.
13270 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
13271 if (HintExpr.isInvalid())
13272 return nullptr;
13273 return new (Context)
13274 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
13275}
13276
Carlo Bertollib4adf552016-01-15 18:50:31 +000013277OMPClause *Sema::ActOnOpenMPDistScheduleClause(
13278 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
13279 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
13280 SourceLocation EndLoc) {
13281 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
13282 std::string Values;
13283 Values += "'";
13284 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
13285 Values += "'";
13286 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
13287 << Values << getOpenMPClauseName(OMPC_dist_schedule);
13288 return nullptr;
13289 }
13290 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000013291 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000013292 if (ChunkSize) {
13293 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
13294 !ChunkSize->isInstantiationDependent() &&
13295 !ChunkSize->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000013296 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
Carlo Bertollib4adf552016-01-15 18:50:31 +000013297 ExprResult Val =
13298 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
13299 if (Val.isInvalid())
13300 return nullptr;
13301
13302 ValExpr = Val.get();
13303
13304 // OpenMP [2.7.1, Restrictions]
13305 // chunk_size must be a loop invariant integer expression with a positive
13306 // value.
13307 llvm::APSInt Result;
13308 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
13309 if (Result.isSigned() && !Result.isStrictlyPositive()) {
13310 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
13311 << "dist_schedule" << ChunkSize->getSourceRange();
13312 return nullptr;
13313 }
Alexey Bataev2ba67042017-11-28 21:11:44 +000013314 } else if (getOpenMPCaptureRegionForClause(
13315 DSAStack->getCurrentDirective(), OMPC_dist_schedule) !=
13316 OMPD_unknown &&
Alexey Bataevb46cdea2016-06-15 11:20:48 +000013317 !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000013318 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000013319 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev5a3af132016-03-29 08:58:54 +000013320 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
13321 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000013322 }
13323 }
13324 }
13325
13326 return new (Context)
13327 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000013328 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000013329}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013330
13331OMPClause *Sema::ActOnOpenMPDefaultmapClause(
13332 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
13333 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
13334 SourceLocation KindLoc, SourceLocation EndLoc) {
13335 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
David Majnemer9d168222016-08-05 17:44:54 +000013336 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) {
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013337 std::string Value;
13338 SourceLocation Loc;
13339 Value += "'";
13340 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
13341 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000013342 OMPC_DEFAULTMAP_MODIFIER_tofrom);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013343 Loc = MLoc;
13344 } else {
13345 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000013346 OMPC_DEFAULTMAP_scalar);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013347 Loc = KindLoc;
13348 }
13349 Value += "'";
13350 Diag(Loc, diag::err_omp_unexpected_clause_value)
13351 << Value << getOpenMPClauseName(OMPC_defaultmap);
13352 return nullptr;
13353 }
Alexey Bataev2fd0cb22017-10-05 17:51:39 +000013354 DSAStack->setDefaultDMAToFromScalar(StartLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013355
13356 return new (Context)
13357 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
13358}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013359
13360bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
13361 DeclContext *CurLexicalContext = getCurLexicalContext();
13362 if (!CurLexicalContext->isFileContext() &&
13363 !CurLexicalContext->isExternCContext() &&
Alexey Bataev502ec492017-10-03 20:00:00 +000013364 !CurLexicalContext->isExternCXXContext() &&
13365 !isa<CXXRecordDecl>(CurLexicalContext) &&
13366 !isa<ClassTemplateDecl>(CurLexicalContext) &&
13367 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) &&
13368 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013369 Diag(Loc, diag::err_omp_region_not_file_context);
13370 return false;
13371 }
Kelvin Libc38e632018-09-10 02:07:09 +000013372 ++DeclareTargetNestingLevel;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013373 return true;
13374}
13375
13376void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
Kelvin Libc38e632018-09-10 02:07:09 +000013377 assert(DeclareTargetNestingLevel > 0 &&
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013378 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
Kelvin Libc38e632018-09-10 02:07:09 +000013379 --DeclareTargetNestingLevel;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013380}
13381
David Majnemer9d168222016-08-05 17:44:54 +000013382void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope,
13383 CXXScopeSpec &ScopeSpec,
13384 const DeclarationNameInfo &Id,
13385 OMPDeclareTargetDeclAttr::MapTypeTy MT,
13386 NamedDeclSetType &SameDirectiveDecls) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013387 LookupResult Lookup(*this, Id, LookupOrdinaryName);
13388 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
13389
13390 if (Lookup.isAmbiguous())
13391 return;
13392 Lookup.suppressDiagnostics();
13393
13394 if (!Lookup.isSingleResult()) {
13395 if (TypoCorrection Corrected =
13396 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
13397 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
13398 CTK_ErrorRecovery)) {
13399 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
13400 << Id.getName());
13401 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
13402 return;
13403 }
13404
13405 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
13406 return;
13407 }
13408
13409 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
Alexey Bataev30a78212018-09-11 13:59:10 +000013410 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND) ||
13411 isa<FunctionTemplateDecl>(ND)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013412 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
13413 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
Alexey Bataev30a78212018-09-11 13:59:10 +000013414 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
13415 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
13416 cast<ValueDecl>(ND));
13417 if (!Res) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013418 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013419 ND->addAttr(A);
13420 if (ASTMutationListener *ML = Context.getASTMutationListener())
13421 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
Kelvin Li1ce87c72017-12-12 20:08:12 +000013422 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Id.getLoc());
Alexey Bataev30a78212018-09-11 13:59:10 +000013423 } else if (*Res != MT) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013424 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
13425 << Id.getName();
13426 }
Alexey Bataeve3727102018-04-18 15:57:46 +000013427 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013428 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
Alexey Bataeve3727102018-04-18 15:57:46 +000013429 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013430}
13431
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013432static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
13433 Sema &SemaRef, Decl *D) {
Alexey Bataev30a78212018-09-11 13:59:10 +000013434 if (!D || !isa<VarDecl>(D))
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013435 return;
Alexey Bataev30a78212018-09-11 13:59:10 +000013436 auto *VD = cast<VarDecl>(D);
13437 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
13438 return;
13439 SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context);
13440 SemaRef.Diag(SL, diag::note_used_here) << SR;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013441}
13442
13443static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
13444 Sema &SemaRef, DSAStackTy *Stack,
13445 ValueDecl *VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013446 return VD->hasAttr<OMPDeclareTargetDeclAttr>() ||
13447 checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(),
13448 /*FullCheck=*/false);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013449}
13450
Kelvin Li1ce87c72017-12-12 20:08:12 +000013451void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D,
13452 SourceLocation IdLoc) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013453 if (!D || D->isInvalidDecl())
13454 return;
13455 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
Stephen Kellyf2ceec42018-08-09 21:08:08 +000013456 SourceLocation SL = E ? E->getBeginLoc() : D->getLocation();
Alexey Bataeve3727102018-04-18 15:57:46 +000013457 if (auto *VD = dyn_cast<VarDecl>(D)) {
Alexey Bataevc1943e72018-07-09 19:58:08 +000013458 // Only global variables can be marked as declare target.
Alexey Bataev30a78212018-09-11 13:59:10 +000013459 if (!VD->isFileVarDecl() && !VD->isStaticLocal() &&
13460 !VD->isStaticDataMember())
Alexey Bataevc1943e72018-07-09 19:58:08 +000013461 return;
13462 // 2.10.6: threadprivate variable cannot appear in a declare target
13463 // directive.
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013464 if (DSAStack->isThreadPrivate(VD)) {
13465 Diag(SL, diag::err_omp_threadprivate_in_target);
Alexey Bataeve3727102018-04-18 15:57:46 +000013466 reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013467 return;
13468 }
13469 }
Alexey Bataev97b72212018-08-14 18:31:20 +000013470 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
13471 D = FTD->getTemplatedDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000013472 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Alexey Bataev30a78212018-09-11 13:59:10 +000013473 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
13474 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD);
13475 if (Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) {
Kelvin Li1ce87c72017-12-12 20:08:12 +000013476 assert(IdLoc.isValid() && "Source location is expected");
13477 Diag(IdLoc, diag::err_omp_function_in_link_clause);
13478 Diag(FD->getLocation(), diag::note_defined_here) << FD;
13479 return;
13480 }
13481 }
Alexey Bataev30a78212018-09-11 13:59:10 +000013482 if (auto *VD = dyn_cast<ValueDecl>(D)) {
13483 // Problem if any with var declared with incomplete type will be reported
13484 // as normal, so no need to check it here.
13485 if ((E || !VD->getType()->isIncompleteType()) &&
13486 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD))
13487 return;
13488 if (!E && !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) {
13489 // Checking declaration inside declare target region.
13490 if (isa<VarDecl>(D) || isa<FunctionDecl>(D) ||
13491 isa<FunctionTemplateDecl>(D)) {
13492 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
13493 Context, OMPDeclareTargetDeclAttr::MT_To);
13494 D->addAttr(A);
13495 if (ASTMutationListener *ML = Context.getASTMutationListener())
13496 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
13497 }
13498 return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013499 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013500 }
Alexey Bataev30a78212018-09-11 13:59:10 +000013501 if (!E)
13502 return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013503 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
13504}
Samuel Antao661c0902016-05-26 17:39:58 +000013505
13506OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
13507 SourceLocation StartLoc,
13508 SourceLocation LParenLoc,
13509 SourceLocation EndLoc) {
13510 MappableVarListInfo MVLI(VarList);
13511 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc);
13512 if (MVLI.ProcessedVarList.empty())
13513 return nullptr;
13514
13515 return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13516 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
13517 MVLI.VarComponents);
13518}
Samuel Antaoec172c62016-05-26 17:49:04 +000013519
13520OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
13521 SourceLocation StartLoc,
13522 SourceLocation LParenLoc,
13523 SourceLocation EndLoc) {
13524 MappableVarListInfo MVLI(VarList);
13525 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc);
13526 if (MVLI.ProcessedVarList.empty())
13527 return nullptr;
13528
13529 return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13530 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
13531 MVLI.VarComponents);
13532}
Carlo Bertolli2404b172016-07-13 15:37:16 +000013533
13534OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
13535 SourceLocation StartLoc,
13536 SourceLocation LParenLoc,
13537 SourceLocation EndLoc) {
Samuel Antaocc10b852016-07-28 14:23:26 +000013538 MappableVarListInfo MVLI(VarList);
13539 SmallVector<Expr *, 8> PrivateCopies;
13540 SmallVector<Expr *, 8> Inits;
13541
Alexey Bataeve3727102018-04-18 15:57:46 +000013542 for (Expr *RefExpr : VarList) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000013543 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
13544 SourceLocation ELoc;
13545 SourceRange ERange;
13546 Expr *SimpleRefExpr = RefExpr;
13547 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
13548 if (Res.second) {
13549 // It will be analyzed later.
Samuel Antaocc10b852016-07-28 14:23:26 +000013550 MVLI.ProcessedVarList.push_back(RefExpr);
13551 PrivateCopies.push_back(nullptr);
13552 Inits.push_back(nullptr);
Carlo Bertolli2404b172016-07-13 15:37:16 +000013553 }
13554 ValueDecl *D = Res.first;
13555 if (!D)
13556 continue;
13557
13558 QualType Type = D->getType();
Samuel Antaocc10b852016-07-28 14:23:26 +000013559 Type = Type.getNonReferenceType().getUnqualifiedType();
13560
13561 auto *VD = dyn_cast<VarDecl>(D);
13562
13563 // Item should be a pointer or reference to pointer.
13564 if (!Type->isPointerType()) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000013565 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
13566 << 0 << RefExpr->getSourceRange();
13567 continue;
13568 }
Samuel Antaocc10b852016-07-28 14:23:26 +000013569
13570 // Build the private variable and the expression that refers to it.
Alexey Bataev63cc8e92018-03-20 14:45:59 +000013571 auto VDPrivate =
13572 buildVarDecl(*this, ELoc, Type, D->getName(),
13573 D->hasAttrs() ? &D->getAttrs() : nullptr,
13574 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Samuel Antaocc10b852016-07-28 14:23:26 +000013575 if (VDPrivate->isInvalidDecl())
13576 continue;
13577
13578 CurContext->addDecl(VDPrivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000013579 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Samuel Antaocc10b852016-07-28 14:23:26 +000013580 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
13581
13582 // Add temporary variable to initialize the private copy of the pointer.
Alexey Bataeve3727102018-04-18 15:57:46 +000013583 VarDecl *VDInit =
Samuel Antaocc10b852016-07-28 14:23:26 +000013584 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp");
Alexey Bataeve3727102018-04-18 15:57:46 +000013585 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr(
13586 *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc());
Samuel Antaocc10b852016-07-28 14:23:26 +000013587 AddInitializerToDecl(VDPrivate,
13588 DefaultLvalueConversion(VDInitRefExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000013589 /*DirectInit=*/false);
Samuel Antaocc10b852016-07-28 14:23:26 +000013590
13591 // If required, build a capture to implement the privatization initialized
13592 // with the current list item value.
13593 DeclRefExpr *Ref = nullptr;
13594 if (!VD)
13595 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
13596 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
13597 PrivateCopies.push_back(VDPrivateRefExpr);
13598 Inits.push_back(VDInitRefExpr);
13599
13600 // We need to add a data sharing attribute for this variable to make sure it
13601 // is correctly captured. A variable that shows up in a use_device_ptr has
13602 // similar properties of a first private variable.
13603 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
13604
13605 // Create a mappable component for the list item. List items in this clause
13606 // only need a component.
13607 MVLI.VarBaseDeclarations.push_back(D);
13608 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13609 MVLI.VarComponents.back().push_back(
13610 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D));
Carlo Bertolli2404b172016-07-13 15:37:16 +000013611 }
13612
Samuel Antaocc10b852016-07-28 14:23:26 +000013613 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli2404b172016-07-13 15:37:16 +000013614 return nullptr;
13615
Samuel Antaocc10b852016-07-28 14:23:26 +000013616 return OMPUseDevicePtrClause::Create(
13617 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
13618 PrivateCopies, Inits, MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli2404b172016-07-13 15:37:16 +000013619}
Carlo Bertolli70594e92016-07-13 17:16:49 +000013620
13621OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
13622 SourceLocation StartLoc,
13623 SourceLocation LParenLoc,
13624 SourceLocation EndLoc) {
Samuel Antao6890b092016-07-28 14:25:09 +000013625 MappableVarListInfo MVLI(VarList);
Alexey Bataeve3727102018-04-18 15:57:46 +000013626 for (Expr *RefExpr : VarList) {
Kelvin Li84376252016-12-14 15:39:58 +000013627 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause.");
Carlo Bertolli70594e92016-07-13 17:16:49 +000013628 SourceLocation ELoc;
13629 SourceRange ERange;
13630 Expr *SimpleRefExpr = RefExpr;
13631 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
13632 if (Res.second) {
13633 // It will be analyzed later.
Samuel Antao6890b092016-07-28 14:25:09 +000013634 MVLI.ProcessedVarList.push_back(RefExpr);
Carlo Bertolli70594e92016-07-13 17:16:49 +000013635 }
13636 ValueDecl *D = Res.first;
13637 if (!D)
13638 continue;
13639
13640 QualType Type = D->getType();
13641 // item should be a pointer or array or reference to pointer or array
13642 if (!Type.getNonReferenceType()->isPointerType() &&
13643 !Type.getNonReferenceType()->isArrayType()) {
13644 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
13645 << 0 << RefExpr->getSourceRange();
13646 continue;
13647 }
Samuel Antao6890b092016-07-28 14:25:09 +000013648
13649 // Check if the declaration in the clause does not show up in any data
13650 // sharing attribute.
Alexey Bataeve3727102018-04-18 15:57:46 +000013651 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Samuel Antao6890b092016-07-28 14:25:09 +000013652 if (isOpenMPPrivate(DVar.CKind)) {
13653 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
13654 << getOpenMPClauseName(DVar.CKind)
13655 << getOpenMPClauseName(OMPC_is_device_ptr)
13656 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000013657 reportOriginalDsa(*this, DSAStack, D, DVar);
Samuel Antao6890b092016-07-28 14:25:09 +000013658 continue;
13659 }
13660
Alexey Bataeve3727102018-04-18 15:57:46 +000013661 const Expr *ConflictExpr;
Samuel Antao6890b092016-07-28 14:25:09 +000013662 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +000013663 D, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +000013664 [&ConflictExpr](
13665 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
13666 OpenMPClauseKind) -> bool {
13667 ConflictExpr = R.front().getAssociatedExpression();
13668 return true;
13669 })) {
13670 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
13671 Diag(ConflictExpr->getExprLoc(), diag::note_used_here)
13672 << ConflictExpr->getSourceRange();
13673 continue;
13674 }
13675
13676 // Store the components in the stack so that they can be used to check
13677 // against other clauses later on.
13678 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D);
13679 DSAStack->addMappableExpressionComponents(
13680 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
13681
13682 // Record the expression we've just processed.
13683 MVLI.ProcessedVarList.push_back(SimpleRefExpr);
13684
13685 // Create a mappable component for the list item. List items in this clause
13686 // only need a component. We use a null declaration to signal fields in
13687 // 'this'.
13688 assert((isa<DeclRefExpr>(SimpleRefExpr) ||
13689 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
13690 "Unexpected device pointer expression!");
13691 MVLI.VarBaseDeclarations.push_back(
13692 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr);
13693 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13694 MVLI.VarComponents.back().push_back(MC);
Carlo Bertolli70594e92016-07-13 17:16:49 +000013695 }
13696
Samuel Antao6890b092016-07-28 14:25:09 +000013697 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli70594e92016-07-13 17:16:49 +000013698 return nullptr;
13699
Samuel Antao6890b092016-07-28 14:25:09 +000013700 return OMPIsDevicePtrClause::Create(
13701 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
13702 MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli70594e92016-07-13 17:16:49 +000013703}