blob: f084a0345be3e32e3e834407abfd157f98264296 [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Alexey Bataeva769e072013-03-22 06:34:35 +00006//
7//===----------------------------------------------------------------------===//
8/// \file
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00009/// This file implements semantic analysis for OpenMP directives and
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000010/// clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000011///
12//===----------------------------------------------------------------------===//
13
Alexey Bataevb08f89f2015-08-14 12:25:37 +000014#include "TreeTransform.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000015#include "clang/AST/ASTContext.h"
Alexey Bataev97720002014-11-11 04:05:39 +000016#include "clang/AST/ASTMutationListener.h"
Alexey Bataeva839ddd2016-03-17 10:19:46 +000017#include "clang/AST/CXXInheritance.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000018#include "clang/AST/Decl.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000019#include "clang/AST/DeclCXX.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000020#include "clang/AST/DeclOpenMP.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000021#include "clang/AST/StmtCXX.h"
22#include "clang/AST/StmtOpenMP.h"
23#include "clang/AST/StmtVisitor.h"
Patrick Lystere13b1e32019-01-02 19:28:48 +000024#include "clang/AST/TypeOrdering.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;
Patrick Lyster16471942019-02-06 18:18:02 +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;
Patrick Lystere13b1e32019-01-02 19:28:48 +0000149 llvm::DenseSet<QualType> MappedClassesQualTypes;
Alexey Bataeva495c642019-03-11 19:51:42 +0000150 /// List of globals marked as declare target link in this target region
151 /// (isOpenMPTargetExecutionDirective(Directive) == true).
152 llvm::SmallVector<DeclRefExpr *, 4> DeclareTargetLinkVarDecls;
Alexey Bataeved09d242014-05-28 05:53:51 +0000153 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000154 Scope *CurScope, SourceLocation Loc)
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000155 : Directive(DKind), DirectiveName(Name), CurScope(CurScope),
156 ConstructLoc(Loc) {}
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000157 SharingMapTy() = default;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000158 };
159
Alexey Bataeve3727102018-04-18 15:57:46 +0000160 using StackTy = SmallVector<SharingMapTy, 4>;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000161
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000162 /// Stack of used declaration and their data-sharing attributes.
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000163 DeclSAMapTy Threadprivates;
Alexey Bataev4b465392017-04-26 15:06:24 +0000164 const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr;
165 SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000166 /// true, if check for DSA must be from parent directive, false, if
Alexey Bataev39f915b82015-05-08 10:41:21 +0000167 /// from current directive.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000168 OpenMPClauseKind ClauseKindMode = OMPC_unknown;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000169 Sema &SemaRef;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000170 bool ForceCapturing = false;
Alexey Bataev60705422018-10-30 15:50:12 +0000171 /// true if all the vaiables in the target executable directives must be
172 /// captured by reference.
173 bool ForceCaptureByReferenceInTargetExecutable = false;
Alexey Bataev28c75412015-12-15 08:19:24 +0000174 CriticalsWithHintsTy Criticals;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000175
Alexey Bataeve3727102018-04-18 15:57:46 +0000176 using iterator = StackTy::const_reverse_iterator;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000177
Alexey Bataeve3727102018-04-18 15:57:46 +0000178 DSAVarData getDSA(iterator &Iter, ValueDecl *D) const;
Alexey Bataevec3da872014-01-31 05:15:34 +0000179
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000180 /// Checks if the variable is a local for OpenMP region.
Alexey Bataeve3727102018-04-18 15:57:46 +0000181 bool isOpenMPLocal(VarDecl *D, iterator Iter) const;
Alexey Bataeved09d242014-05-28 05:53:51 +0000182
Alexey Bataev4b465392017-04-26 15:06:24 +0000183 bool isStackEmpty() const {
184 return Stack.empty() ||
185 Stack.back().second != CurrentNonCapturingFunctionScope ||
186 Stack.back().first.empty();
187 }
188
Kelvin Li1408f912018-09-26 04:28:39 +0000189 /// Vector of previously declared requires directives
190 SmallVector<const OMPRequiresDecl *, 2> RequiresDecls;
191
Alexey Bataev758e55e2013-09-06 18:03:48 +0000192public:
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000193 explicit DSAStackTy(Sema &S) : SemaRef(S) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000194
Alexey Bataevaac108a2015-06-23 04:51:00 +0000195 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
Alexey Bataev3f82cfc2017-12-13 15:28:44 +0000196 OpenMPClauseKind getClauseParsingMode() const {
197 assert(isClauseParsingMode() && "Must be in clause parsing mode.");
198 return ClauseKindMode;
199 }
Alexey Bataevaac108a2015-06-23 04:51:00 +0000200 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000201
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000202 bool isForceVarCapturing() const { return ForceCapturing; }
203 void setForceVarCapturing(bool V) { ForceCapturing = V; }
204
Alexey Bataev60705422018-10-30 15:50:12 +0000205 void setForceCaptureByReferenceInTargetExecutable(bool V) {
206 ForceCaptureByReferenceInTargetExecutable = V;
207 }
208 bool isForceCaptureByReferenceInTargetExecutable() const {
209 return ForceCaptureByReferenceInTargetExecutable;
210 }
211
Alexey Bataev758e55e2013-09-06 18:03:48 +0000212 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000213 Scope *CurScope, SourceLocation Loc) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000214 if (Stack.empty() ||
215 Stack.back().second != CurrentNonCapturingFunctionScope)
216 Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope);
217 Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc);
218 Stack.back().first.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000219 }
220
221 void pop() {
Alexey Bataev4b465392017-04-26 15:06:24 +0000222 assert(!Stack.back().first.empty() &&
223 "Data-sharing attributes stack is empty!");
224 Stack.back().first.pop_back();
225 }
226
Alexey Bataev6ab5bb12018-10-29 15:01:58 +0000227 /// Marks that we're started loop parsing.
228 void loopInit() {
229 assert(isOpenMPLoopDirective(getCurrentDirective()) &&
230 "Expected loop-based directive.");
231 Stack.back().first.back().LoopStart = true;
232 }
233 /// Start capturing of the variables in the loop context.
234 void loopStart() {
235 assert(isOpenMPLoopDirective(getCurrentDirective()) &&
236 "Expected loop-based directive.");
237 Stack.back().first.back().LoopStart = false;
238 }
239 /// true, if variables are captured, false otherwise.
240 bool isLoopStarted() const {
241 assert(isOpenMPLoopDirective(getCurrentDirective()) &&
242 "Expected loop-based directive.");
243 return !Stack.back().first.back().LoopStart;
244 }
245 /// Marks (or clears) declaration as possibly loop counter.
246 void resetPossibleLoopCounter(const Decl *D = nullptr) {
247 Stack.back().first.back().PossiblyLoopCounter =
248 D ? D->getCanonicalDecl() : D;
249 }
250 /// Gets the possible loop counter decl.
251 const Decl *getPossiblyLoopCunter() const {
252 return Stack.back().first.back().PossiblyLoopCounter;
253 }
Alexey Bataev4b465392017-04-26 15:06:24 +0000254 /// Start new OpenMP region stack in new non-capturing function.
255 void pushFunction() {
256 const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction();
257 assert(!isa<CapturingScopeInfo>(CurFnScope));
258 CurrentNonCapturingFunctionScope = CurFnScope;
259 }
260 /// Pop region stack for non-capturing function.
261 void popFunction(const FunctionScopeInfo *OldFSI) {
262 if (!Stack.empty() && Stack.back().second == OldFSI) {
263 assert(Stack.back().first.empty());
264 Stack.pop_back();
265 }
266 CurrentNonCapturingFunctionScope = nullptr;
267 for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) {
268 if (!isa<CapturingScopeInfo>(FSI)) {
269 CurrentNonCapturingFunctionScope = FSI;
270 break;
271 }
272 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000273 }
274
Alexey Bataeve3727102018-04-18 15:57:46 +0000275 void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) {
Alexey Bataev43a919f2018-04-13 17:48:43 +0000276 Criticals.try_emplace(D->getDirectiveName().getAsString(), D, Hint);
Alexey Bataev28c75412015-12-15 08:19:24 +0000277 }
Alexey Bataeve3727102018-04-18 15:57:46 +0000278 const std::pair<const OMPCriticalDirective *, llvm::APSInt>
Alexey Bataev28c75412015-12-15 08:19:24 +0000279 getCriticalWithHint(const DeclarationNameInfo &Name) const {
280 auto I = Criticals.find(Name.getAsString());
281 if (I != Criticals.end())
282 return I->second;
283 return std::make_pair(nullptr, llvm::APSInt());
284 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000285 /// If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000286 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000287 /// for diagnostics.
Alexey Bataeve3727102018-04-18 15:57:46 +0000288 const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000289
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000290 /// Register specified variable as loop control variable.
Alexey Bataeve3727102018-04-18 15:57:46 +0000291 void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000292 /// Check if the specified variable is a loop control variable for
Alexey Bataev9c821032015-04-30 04:23:23 +0000293 /// current region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000294 /// \return The index of the loop control variable in the list of associated
295 /// for-loops (from outer to inner).
Alexey Bataeve3727102018-04-18 15:57:46 +0000296 const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000297 /// Check if the specified variable is a loop control variable for
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000298 /// parent region.
299 /// \return The index of the loop control variable in the list of associated
300 /// for-loops (from outer to inner).
Alexey Bataeve3727102018-04-18 15:57:46 +0000301 const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000302 /// Get the loop control variable for the I-th loop (or nullptr) in
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000303 /// parent directive.
Alexey Bataeve3727102018-04-18 15:57:46 +0000304 const ValueDecl *getParentLoopControlVariable(unsigned I) const;
Alexey Bataev9c821032015-04-30 04:23:23 +0000305
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000306 /// Adds explicit data sharing attribute to the specified declaration.
Alexey Bataeve3727102018-04-18 15:57:46 +0000307 void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
Alexey Bataev90c228f2016-02-08 09:29:13 +0000308 DeclRefExpr *PrivateCopy = nullptr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000309
Alexey Bataevfa312f32017-07-21 18:48:21 +0000310 /// Adds additional information for the reduction items with the reduction id
311 /// represented as an operator.
Alexey Bataeve3727102018-04-18 15:57:46 +0000312 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000313 BinaryOperatorKind BOK);
Alexey Bataevfa312f32017-07-21 18:48:21 +0000314 /// Adds additional information for the reduction items with the reduction id
315 /// represented as reduction identifier.
Alexey Bataeve3727102018-04-18 15:57:46 +0000316 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000317 const Expr *ReductionRef);
Alexey Bataevfa312f32017-07-21 18:48:21 +0000318 /// Returns the location and reduction operation from the innermost parent
319 /// region for the given \p D.
Alexey Bataeve3727102018-04-18 15:57:46 +0000320 const DSAVarData
321 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
322 BinaryOperatorKind &BOK,
323 Expr *&TaskgroupDescriptor) const;
Alexey Bataevfa312f32017-07-21 18:48:21 +0000324 /// Returns the location and reduction operation from the innermost parent
325 /// region for the given \p D.
Alexey Bataeve3727102018-04-18 15:57:46 +0000326 const DSAVarData
327 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
328 const Expr *&ReductionRef,
329 Expr *&TaskgroupDescriptor) const;
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000330 /// Return reduction reference expression for the current taskgroup.
331 Expr *getTaskgroupReductionRef() const {
332 assert(Stack.back().first.back().Directive == OMPD_taskgroup &&
333 "taskgroup reference expression requested for non taskgroup "
334 "directive.");
335 return Stack.back().first.back().TaskgroupReductionRef;
336 }
Alexey Bataev88202be2017-07-27 13:20:36 +0000337 /// Checks if the given \p VD declaration is actually a taskgroup reduction
338 /// descriptor variable at the \p Level of OpenMP regions.
Alexey Bataeve3727102018-04-18 15:57:46 +0000339 bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const {
Alexey Bataev88202be2017-07-27 13:20:36 +0000340 return Stack.back().first[Level].TaskgroupReductionRef &&
341 cast<DeclRefExpr>(Stack.back().first[Level].TaskgroupReductionRef)
342 ->getDecl() == VD;
343 }
Alexey Bataevfa312f32017-07-21 18:48:21 +0000344
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000345 /// Returns data sharing attributes from top of the stack for the
Alexey Bataev758e55e2013-09-06 18:03:48 +0000346 /// specified declaration.
Alexey Bataeve3727102018-04-18 15:57:46 +0000347 const DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000348 /// Returns data-sharing attributes for the specified declaration.
Alexey Bataeve3727102018-04-18 15:57:46 +0000349 const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000350 /// Checks if the specified variables has data-sharing attributes which
Alexey Bataevf29276e2014-06-18 04:14:57 +0000351 /// match specified \a CPred predicate in any directive which matches \a DPred
352 /// predicate.
Alexey Bataeve3727102018-04-18 15:57:46 +0000353 const DSAVarData
354 hasDSA(ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
355 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
356 bool FromParent) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000357 /// Checks if the specified variables has data-sharing attributes which
Alexey Bataevf29276e2014-06-18 04:14:57 +0000358 /// match specified \a CPred predicate in any innermost directive which
359 /// matches \a DPred predicate.
Alexey Bataeve3727102018-04-18 15:57:46 +0000360 const DSAVarData
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000361 hasInnermostDSA(ValueDecl *D,
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000362 const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
363 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +0000364 bool FromParent) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000365 /// Checks if the specified variables has explicit data-sharing
Alexey Bataevaac108a2015-06-23 04:51:00 +0000366 /// attributes which match specified \a CPred predicate at the specified
367 /// OpenMP region.
Alexey Bataeve3727102018-04-18 15:57:46 +0000368 bool hasExplicitDSA(const ValueDecl *D,
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000369 const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
Alexey Bataeve3727102018-04-18 15:57:46 +0000370 unsigned Level, bool NotLastprivate = false) const;
Samuel Antao4be30e92015-10-02 17:14:03 +0000371
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000372 /// Returns true if the directive at level \Level matches in the
Samuel Antao4be30e92015-10-02 17:14:03 +0000373 /// specified \a DPred predicate.
374 bool hasExplicitDirective(
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000375 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +0000376 unsigned Level) const;
Samuel Antao4be30e92015-10-02 17:14:03 +0000377
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000378 /// Finds a directive which matches specified \a DPred predicate.
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000379 bool hasDirective(
380 const llvm::function_ref<bool(
381 OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)>
382 DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +0000383 bool FromParent) const;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000384
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000385 /// Returns currently analyzed directive.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000386 OpenMPDirectiveKind getCurrentDirective() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000387 return isStackEmpty() ? OMPD_unknown : Stack.back().first.back().Directive;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000388 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000389 /// Returns directive kind at specified level.
Alexey Bataevdfa430f2017-12-08 15:03:50 +0000390 OpenMPDirectiveKind getDirective(unsigned Level) const {
391 assert(!isStackEmpty() && "No directive at specified level.");
392 return Stack.back().first[Level].Directive;
393 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000394 /// Returns parent directive.
Alexey Bataev549210e2014-06-24 04:39:47 +0000395 OpenMPDirectiveKind getParentDirective() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000396 if (isStackEmpty() || Stack.back().first.size() == 1)
397 return OMPD_unknown;
398 return std::next(Stack.back().first.rbegin())->Directive;
Alexey Bataev549210e2014-06-24 04:39:47 +0000399 }
Alexey Bataev6ab5bb12018-10-29 15:01:58 +0000400
Kelvin Li1408f912018-09-26 04:28:39 +0000401 /// Add requires decl to internal vector
402 void addRequiresDecl(OMPRequiresDecl *RD) {
403 RequiresDecls.push_back(RD);
404 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000405
Kelvin Li1408f912018-09-26 04:28:39 +0000406 /// Checks for a duplicate clause amongst previously declared requires
407 /// directives
408 bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const {
409 bool IsDuplicate = false;
410 for (OMPClause *CNew : ClauseList) {
411 for (const OMPRequiresDecl *D : RequiresDecls) {
412 for (const OMPClause *CPrev : D->clauselists()) {
413 if (CNew->getClauseKind() == CPrev->getClauseKind()) {
414 SemaRef.Diag(CNew->getBeginLoc(),
415 diag::err_omp_requires_clause_redeclaration)
416 << getOpenMPClauseName(CNew->getClauseKind());
417 SemaRef.Diag(CPrev->getBeginLoc(),
418 diag::note_omp_requires_previous_clause)
419 << getOpenMPClauseName(CPrev->getClauseKind());
420 IsDuplicate = true;
421 }
422 }
423 }
424 }
425 return IsDuplicate;
426 }
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +0000427
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000428 /// Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000429 void setDefaultDSANone(SourceLocation Loc) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000430 assert(!isStackEmpty());
431 Stack.back().first.back().DefaultAttr = DSA_none;
432 Stack.back().first.back().DefaultAttrLoc = Loc;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000433 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000434 /// Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000435 void setDefaultDSAShared(SourceLocation Loc) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000436 assert(!isStackEmpty());
437 Stack.back().first.back().DefaultAttr = DSA_shared;
438 Stack.back().first.back().DefaultAttrLoc = Loc;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000439 }
Alexey Bataev2fd0cb22017-10-05 17:51:39 +0000440 /// Set default data mapping attribute to 'tofrom:scalar'.
441 void setDefaultDMAToFromScalar(SourceLocation Loc) {
442 assert(!isStackEmpty());
443 Stack.back().first.back().DefaultMapAttr = DMA_tofrom_scalar;
444 Stack.back().first.back().DefaultMapAttrLoc = Loc;
445 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000446
447 DefaultDataSharingAttributes getDefaultDSA() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000448 return isStackEmpty() ? DSA_unspecified
449 : Stack.back().first.back().DefaultAttr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000450 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000451 SourceLocation getDefaultDSALocation() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000452 return isStackEmpty() ? SourceLocation()
453 : Stack.back().first.back().DefaultAttrLoc;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000454 }
Alexey Bataev2fd0cb22017-10-05 17:51:39 +0000455 DefaultMapAttributes getDefaultDMA() const {
456 return isStackEmpty() ? DMA_unspecified
457 : Stack.back().first.back().DefaultMapAttr;
458 }
459 DefaultMapAttributes getDefaultDMAAtLevel(unsigned Level) const {
460 return Stack.back().first[Level].DefaultMapAttr;
461 }
462 SourceLocation getDefaultDMALocation() const {
463 return isStackEmpty() ? SourceLocation()
464 : Stack.back().first.back().DefaultMapAttrLoc;
465 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000466
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000467 /// Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000468 bool isThreadPrivate(VarDecl *D) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000469 const DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000470 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000471 }
472
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000473 /// Marks current region as ordered (it has an 'ordered' clause).
Alexey Bataevf138fda2018-08-13 19:04:24 +0000474 void setOrderedRegion(bool IsOrdered, const Expr *Param,
475 OMPOrderedClause *Clause) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000476 assert(!isStackEmpty());
Alexey Bataevf138fda2018-08-13 19:04:24 +0000477 if (IsOrdered)
478 Stack.back().first.back().OrderedRegion.emplace(Param, Clause);
479 else
480 Stack.back().first.back().OrderedRegion.reset();
481 }
482 /// Returns true, if region is ordered (has associated 'ordered' clause),
483 /// false - otherwise.
484 bool isOrderedRegion() const {
485 if (isStackEmpty())
486 return false;
487 return Stack.back().first.rbegin()->OrderedRegion.hasValue();
488 }
489 /// Returns optional parameter for the ordered region.
490 std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const {
491 if (isStackEmpty() ||
492 !Stack.back().first.rbegin()->OrderedRegion.hasValue())
493 return std::make_pair(nullptr, nullptr);
494 return Stack.back().first.rbegin()->OrderedRegion.getValue();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000495 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000496 /// Returns true, if parent region is ordered (has associated
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000497 /// 'ordered' clause), false - otherwise.
498 bool isParentOrderedRegion() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000499 if (isStackEmpty() || Stack.back().first.size() == 1)
500 return false;
Alexey Bataevf138fda2018-08-13 19:04:24 +0000501 return std::next(Stack.back().first.rbegin())->OrderedRegion.hasValue();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000502 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000503 /// Returns optional parameter for the ordered region.
Alexey Bataevf138fda2018-08-13 19:04:24 +0000504 std::pair<const Expr *, OMPOrderedClause *>
505 getParentOrderedRegionParam() const {
506 if (isStackEmpty() || Stack.back().first.size() == 1 ||
507 !std::next(Stack.back().first.rbegin())->OrderedRegion.hasValue())
508 return std::make_pair(nullptr, nullptr);
509 return std::next(Stack.back().first.rbegin())->OrderedRegion.getValue();
Alexey Bataev346265e2015-09-25 10:37:12 +0000510 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000511 /// Marks current region as nowait (it has a 'nowait' clause).
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000512 void setNowaitRegion(bool IsNowait = true) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000513 assert(!isStackEmpty());
514 Stack.back().first.back().NowaitRegion = IsNowait;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000515 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000516 /// Returns true, if parent region is nowait (has associated
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000517 /// 'nowait' clause), false - otherwise.
518 bool isParentNowaitRegion() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000519 if (isStackEmpty() || Stack.back().first.size() == 1)
520 return false;
521 return std::next(Stack.back().first.rbegin())->NowaitRegion;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000522 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000523 /// Marks parent region as cancel region.
Alexey Bataev25e5b442015-09-15 12:52:43 +0000524 void setParentCancelRegion(bool Cancel = true) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000525 if (!isStackEmpty() && Stack.back().first.size() > 1) {
526 auto &StackElemRef = *std::next(Stack.back().first.rbegin());
527 StackElemRef.CancelRegion |= StackElemRef.CancelRegion || Cancel;
528 }
Alexey Bataev25e5b442015-09-15 12:52:43 +0000529 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000530 /// Return true if current region has inner cancel construct.
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000531 bool isCancelRegion() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000532 return isStackEmpty() ? false : Stack.back().first.back().CancelRegion;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000533 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000534
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000535 /// Set collapse value for the region.
Alexey Bataev4b465392017-04-26 15:06:24 +0000536 void setAssociatedLoops(unsigned Val) {
537 assert(!isStackEmpty());
538 Stack.back().first.back().AssociatedLoops = Val;
539 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000540 /// Return collapse value for region.
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000541 unsigned getAssociatedLoops() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000542 return isStackEmpty() ? 0 : Stack.back().first.back().AssociatedLoops;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000543 }
Alexey Bataev9c821032015-04-30 04:23:23 +0000544
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000545 /// Marks current target region as one with closely nested teams
Alexey Bataev13314bf2014-10-09 04:18:56 +0000546 /// region.
547 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000548 if (!isStackEmpty() && Stack.back().first.size() > 1) {
549 std::next(Stack.back().first.rbegin())->InnerTeamsRegionLoc =
550 TeamsRegionLoc;
551 }
Alexey Bataev13314bf2014-10-09 04:18:56 +0000552 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000553 /// Returns true, if current region has closely nested teams region.
Alexey Bataev13314bf2014-10-09 04:18:56 +0000554 bool hasInnerTeamsRegion() const {
555 return getInnerTeamsRegionLoc().isValid();
556 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000557 /// Returns location of the nested teams region (if any).
Alexey Bataev13314bf2014-10-09 04:18:56 +0000558 SourceLocation getInnerTeamsRegionLoc() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000559 return isStackEmpty() ? SourceLocation()
560 : Stack.back().first.back().InnerTeamsRegionLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000561 }
562
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000563 Scope *getCurScope() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000564 return isStackEmpty() ? nullptr : Stack.back().first.back().CurScope;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000565 }
Alexey Bataeve3727102018-04-18 15:57:46 +0000566 SourceLocation getConstructLoc() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000567 return isStackEmpty() ? SourceLocation()
568 : Stack.back().first.back().ConstructLoc;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000569 }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000570
Samuel Antao4c8035b2016-12-12 18:00:20 +0000571 /// Do the check specified in \a Check to all component lists and return true
572 /// if any issue is found.
Samuel Antao90927002016-04-26 14:54:23 +0000573 bool checkMappableExprComponentListsForDecl(
Alexey Bataeve3727102018-04-18 15:57:46 +0000574 const ValueDecl *VD, bool CurrentRegionOnly,
Samuel Antao6890b092016-07-28 14:25:09 +0000575 const llvm::function_ref<
576 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000577 OpenMPClauseKind)>
Alexey Bataeve3727102018-04-18 15:57:46 +0000578 Check) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000579 if (isStackEmpty())
580 return false;
581 auto SI = Stack.back().first.rbegin();
582 auto SE = Stack.back().first.rend();
Samuel Antao5de996e2016-01-22 20:21:36 +0000583
584 if (SI == SE)
585 return false;
586
Alexey Bataeve3727102018-04-18 15:57:46 +0000587 if (CurrentRegionOnly)
Samuel Antao5de996e2016-01-22 20:21:36 +0000588 SE = std::next(SI);
Alexey Bataeve3727102018-04-18 15:57:46 +0000589 else
590 std::advance(SI, 1);
Samuel Antao5de996e2016-01-22 20:21:36 +0000591
592 for (; SI != SE; ++SI) {
Samuel Antao90927002016-04-26 14:54:23 +0000593 auto MI = SI->MappedExprComponents.find(VD);
594 if (MI != SI->MappedExprComponents.end())
Alexey Bataeve3727102018-04-18 15:57:46 +0000595 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
596 MI->second.Components)
Samuel Antao6890b092016-07-28 14:25:09 +0000597 if (Check(L, MI->second.Kind))
Samuel Antao5de996e2016-01-22 20:21:36 +0000598 return true;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000599 }
Samuel Antao5de996e2016-01-22 20:21:36 +0000600 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000601 }
602
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +0000603 /// Do the check specified in \a Check to all component lists at a given level
604 /// and return true if any issue is found.
605 bool checkMappableExprComponentListsForDeclAtLevel(
Alexey Bataeve3727102018-04-18 15:57:46 +0000606 const ValueDecl *VD, unsigned Level,
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +0000607 const llvm::function_ref<
608 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000609 OpenMPClauseKind)>
Alexey Bataeve3727102018-04-18 15:57:46 +0000610 Check) const {
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +0000611 if (isStackEmpty())
612 return false;
613
614 auto StartI = Stack.back().first.begin();
615 auto EndI = Stack.back().first.end();
616 if (std::distance(StartI, EndI) <= (int)Level)
617 return false;
618 std::advance(StartI, Level);
619
620 auto MI = StartI->MappedExprComponents.find(VD);
621 if (MI != StartI->MappedExprComponents.end())
Alexey Bataeve3727102018-04-18 15:57:46 +0000622 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
623 MI->second.Components)
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +0000624 if (Check(L, MI->second.Kind))
625 return true;
626 return false;
627 }
628
Samuel Antao4c8035b2016-12-12 18:00:20 +0000629 /// Create a new mappable expression component list associated with a given
630 /// declaration and initialize it with the provided list of components.
Samuel Antao90927002016-04-26 14:54:23 +0000631 void addMappableExpressionComponents(
Alexey Bataeve3727102018-04-18 15:57:46 +0000632 const ValueDecl *VD,
Samuel Antao6890b092016-07-28 14:25:09 +0000633 OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
634 OpenMPClauseKind WhereFoundClauseKind) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000635 assert(!isStackEmpty() &&
Samuel Antao90927002016-04-26 14:54:23 +0000636 "Not expecting to retrieve components from a empty stack!");
Alexey Bataeve3727102018-04-18 15:57:46 +0000637 MappedExprComponentTy &MEC =
638 Stack.back().first.back().MappedExprComponents[VD];
Samuel Antao90927002016-04-26 14:54:23 +0000639 // Create new entry and append the new components there.
Samuel Antao6890b092016-07-28 14:25:09 +0000640 MEC.Components.resize(MEC.Components.size() + 1);
641 MEC.Components.back().append(Components.begin(), Components.end());
642 MEC.Kind = WhereFoundClauseKind;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000643 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000644
645 unsigned getNestingLevel() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000646 assert(!isStackEmpty());
647 return Stack.back().first.size() - 1;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000648 }
Alexey Bataeve3727102018-04-18 15:57:46 +0000649 void addDoacrossDependClause(OMPDependClause *C,
650 const OperatorOffsetTy &OpsOffs) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000651 assert(!isStackEmpty() && Stack.back().first.size() > 1);
Alexey Bataeve3727102018-04-18 15:57:46 +0000652 SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
Alexey Bataev4b465392017-04-26 15:06:24 +0000653 assert(isOpenMPWorksharingDirective(StackElem.Directive));
Alexey Bataeve3727102018-04-18 15:57:46 +0000654 StackElem.DoacrossDepends.try_emplace(C, OpsOffs);
Alexey Bataev8b427062016-05-25 12:36:08 +0000655 }
656 llvm::iterator_range<DoacrossDependMapTy::const_iterator>
657 getDoacrossDependClauses() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000658 assert(!isStackEmpty());
Alexey Bataeve3727102018-04-18 15:57:46 +0000659 const SharingMapTy &StackElem = Stack.back().first.back();
Alexey Bataev4b465392017-04-26 15:06:24 +0000660 if (isOpenMPWorksharingDirective(StackElem.Directive)) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000661 const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends;
Alexey Bataev8b427062016-05-25 12:36:08 +0000662 return llvm::make_range(Ref.begin(), Ref.end());
663 }
Alexey Bataev4b465392017-04-26 15:06:24 +0000664 return llvm::make_range(StackElem.DoacrossDepends.end(),
665 StackElem.DoacrossDepends.end());
Alexey Bataev8b427062016-05-25 12:36:08 +0000666 }
Patrick Lystere13b1e32019-01-02 19:28:48 +0000667
668 // Store types of classes which have been explicitly mapped
669 void addMappedClassesQualTypes(QualType QT) {
670 SharingMapTy &StackElem = Stack.back().first.back();
671 StackElem.MappedClassesQualTypes.insert(QT);
672 }
673
674 // Return set of mapped classes types
675 bool isClassPreviouslyMapped(QualType QT) const {
676 const SharingMapTy &StackElem = Stack.back().first.back();
677 return StackElem.MappedClassesQualTypes.count(QT) != 0;
678 }
679
Alexey Bataeva495c642019-03-11 19:51:42 +0000680 /// Adds global declare target to the parent target region.
681 void addToParentTargetRegionLinkGlobals(DeclRefExpr *E) {
682 assert(*OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
683 E->getDecl()) == OMPDeclareTargetDeclAttr::MT_Link &&
684 "Expected declare target link global.");
685 if (isStackEmpty())
686 return;
687 auto It = Stack.back().first.rbegin();
688 while (It != Stack.back().first.rend() &&
689 !isOpenMPTargetExecutionDirective(It->Directive))
690 ++It;
691 if (It != Stack.back().first.rend()) {
692 assert(isOpenMPTargetExecutionDirective(It->Directive) &&
693 "Expected target executable directive.");
694 It->DeclareTargetLinkVarDecls.push_back(E);
695 }
696 }
697
698 /// Returns the list of globals with declare target link if current directive
699 /// is target.
700 ArrayRef<DeclRefExpr *> getLinkGlobals() const {
701 assert(isOpenMPTargetExecutionDirective(getCurrentDirective()) &&
702 "Expected target executable directive.");
703 return Stack.back().first.back().DeclareTargetLinkVarDecls;
704 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000705};
Alexey Bataev7e6803e2019-01-09 15:58:05 +0000706
707bool isImplicitTaskingRegion(OpenMPDirectiveKind DKind) {
708 return isOpenMPParallelDirective(DKind) || isOpenMPTeamsDirective(DKind);
709}
710
711bool isImplicitOrExplicitTaskingRegion(OpenMPDirectiveKind DKind) {
712 return isImplicitTaskingRegion(DKind) || isOpenMPTaskingDirective(DKind) || DKind == OMPD_unknown;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000713}
Alexey Bataeve3727102018-04-18 15:57:46 +0000714
Alexey Bataeved09d242014-05-28 05:53:51 +0000715} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000716
Alexey Bataeve3727102018-04-18 15:57:46 +0000717static const Expr *getExprAsWritten(const Expr *E) {
Bill Wendling7c44da22018-10-31 03:48:47 +0000718 if (const auto *FE = dyn_cast<FullExpr>(E))
719 E = FE->getSubExpr();
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000720
Alexey Bataeve3727102018-04-18 15:57:46 +0000721 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000722 E = MTE->GetTemporaryExpr();
723
Alexey Bataeve3727102018-04-18 15:57:46 +0000724 while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000725 E = Binder->getSubExpr();
726
Alexey Bataeve3727102018-04-18 15:57:46 +0000727 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000728 E = ICE->getSubExprAsWritten();
729 return E->IgnoreParens();
730}
731
Alexey Bataeve3727102018-04-18 15:57:46 +0000732static Expr *getExprAsWritten(Expr *E) {
733 return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E)));
734}
735
736static const ValueDecl *getCanonicalDecl(const ValueDecl *D) {
737 if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D))
738 if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000739 D = ME->getMemberDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +0000740 const auto *VD = dyn_cast<VarDecl>(D);
741 const auto *FD = dyn_cast<FieldDecl>(D);
David Majnemer9d168222016-08-05 17:44:54 +0000742 if (VD != nullptr) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000743 VD = VD->getCanonicalDecl();
744 D = VD;
745 } else {
746 assert(FD);
747 FD = FD->getCanonicalDecl();
748 D = FD;
749 }
750 return D;
751}
752
Alexey Bataeve3727102018-04-18 15:57:46 +0000753static ValueDecl *getCanonicalDecl(ValueDecl *D) {
754 return const_cast<ValueDecl *>(
755 getCanonicalDecl(const_cast<const ValueDecl *>(D)));
756}
757
758DSAStackTy::DSAVarData DSAStackTy::getDSA(iterator &Iter,
759 ValueDecl *D) const {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000760 D = getCanonicalDecl(D);
761 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000762 const auto *FD = dyn_cast<FieldDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000763 DSAVarData DVar;
Alexey Bataev4b465392017-04-26 15:06:24 +0000764 if (isStackEmpty() || Iter == Stack.back().first.rend()) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000765 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
766 // in a region but not in construct]
767 // File-scope or namespace-scope variables referenced in called routines
768 // in the region are shared unless they appear in a threadprivate
769 // directive.
Alexey Bataeve3727102018-04-18 15:57:46 +0000770 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000771 DVar.CKind = OMPC_shared;
772
773 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
774 // in a region but not in construct]
775 // Variables with static storage duration that are declared in called
776 // routines in the region are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000777 if (VD && VD->hasGlobalStorage())
778 DVar.CKind = OMPC_shared;
779
780 // Non-static data members are shared by default.
781 if (FD)
Alexey Bataev750a58b2014-03-18 12:19:12 +0000782 DVar.CKind = OMPC_shared;
783
Alexey Bataev758e55e2013-09-06 18:03:48 +0000784 return DVar;
785 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000786
Alexey Bataevec3da872014-01-31 05:15:34 +0000787 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
788 // in a Construct, C/C++, predetermined, p.1]
789 // Variables with automatic storage duration that are declared in a scope
790 // inside the construct are private.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000791 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
792 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000793 DVar.CKind = OMPC_private;
794 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000795 }
796
Alexey Bataeveffbdf12017-07-21 17:24:30 +0000797 DVar.DKind = Iter->Directive;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000798 // Explicitly specified attributes and local variables with predetermined
799 // attributes.
800 if (Iter->SharingMap.count(D)) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000801 const DSAInfo &Data = Iter->SharingMap.lookup(D);
802 DVar.RefExpr = Data.RefExpr.getPointer();
803 DVar.PrivateCopy = Data.PrivateCopy;
804 DVar.CKind = Data.Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000805 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000806 return DVar;
807 }
808
809 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
810 // in a Construct, C/C++, implicitly determined, p.1]
811 // In a parallel or task construct, the data-sharing attributes of these
812 // variables are determined by the default clause, if present.
813 switch (Iter->DefaultAttr) {
814 case DSA_shared:
815 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000816 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000817 return DVar;
818 case DSA_none:
819 return DVar;
820 case DSA_unspecified:
821 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
822 // in a Construct, implicitly determined, p.2]
823 // In a parallel construct, if no default clause is present, these
824 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000825 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000826 if (isOpenMPParallelDirective(DVar.DKind) ||
827 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000828 DVar.CKind = OMPC_shared;
829 return DVar;
830 }
831
832 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
833 // in a Construct, implicitly determined, p.4]
834 // In a task construct, if no default clause is present, a variable that in
835 // the enclosing context is determined to be shared by all implicit tasks
836 // bound to the current team is shared.
Alexey Bataev35aaee62016-04-13 13:36:48 +0000837 if (isOpenMPTaskingDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000838 DSAVarData DVarTemp;
Alexey Bataeve3727102018-04-18 15:57:46 +0000839 iterator I = Iter, E = Stack.back().first.rend();
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000840 do {
841 ++I;
Alexey Bataeved09d242014-05-28 05:53:51 +0000842 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
Alexey Bataev35aaee62016-04-13 13:36:48 +0000843 // Referenced in a Construct, implicitly determined, p.6]
Alexey Bataev758e55e2013-09-06 18:03:48 +0000844 // In a task construct, if no default clause is present, a variable
845 // whose data-sharing attribute is not determined by the rules above is
846 // firstprivate.
847 DVarTemp = getDSA(I, D);
848 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000849 DVar.RefExpr = nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000850 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000851 return DVar;
852 }
Alexey Bataev7e6803e2019-01-09 15:58:05 +0000853 } while (I != E && !isImplicitTaskingRegion(I->Directive));
Alexey Bataev758e55e2013-09-06 18:03:48 +0000854 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000855 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000856 return DVar;
857 }
858 }
859 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
860 // in a Construct, implicitly determined, p.3]
861 // For constructs other than task, if no default clause is present, these
862 // variables inherit their data-sharing attributes from the enclosing
863 // context.
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000864 return getDSA(++Iter, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000865}
866
Alexey Bataeve3727102018-04-18 15:57:46 +0000867const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D,
868 const Expr *NewDE) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000869 assert(!isStackEmpty() && "Data sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000870 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000871 SharingMapTy &StackElem = Stack.back().first.back();
Alexey Bataev4b465392017-04-26 15:06:24 +0000872 auto It = StackElem.AlignedMap.find(D);
873 if (It == StackElem.AlignedMap.end()) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000874 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
Alexey Bataev4b465392017-04-26 15:06:24 +0000875 StackElem.AlignedMap[D] = NewDE;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000876 return nullptr;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000877 }
Alexey Bataeve3727102018-04-18 15:57:46 +0000878 assert(It->second && "Unexpected nullptr expr in the aligned map");
879 return It->second;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000880}
881
Alexey Bataeve3727102018-04-18 15:57:46 +0000882void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000883 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000884 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000885 SharingMapTy &StackElem = Stack.back().first.back();
886 StackElem.LCVMap.try_emplace(
887 D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture));
Alexey Bataev9c821032015-04-30 04:23:23 +0000888}
889
Alexey Bataeve3727102018-04-18 15:57:46 +0000890const DSAStackTy::LCDeclInfo
891DSAStackTy::isLoopControlVariable(const ValueDecl *D) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000892 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000893 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000894 const SharingMapTy &StackElem = Stack.back().first.back();
Alexey Bataev4b465392017-04-26 15:06:24 +0000895 auto It = StackElem.LCVMap.find(D);
896 if (It != StackElem.LCVMap.end())
897 return It->second;
898 return {0, nullptr};
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000899}
900
Alexey Bataeve3727102018-04-18 15:57:46 +0000901const DSAStackTy::LCDeclInfo
902DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000903 assert(!isStackEmpty() && Stack.back().first.size() > 1 &&
904 "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000905 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000906 const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
Alexey Bataev4b465392017-04-26 15:06:24 +0000907 auto It = StackElem.LCVMap.find(D);
908 if (It != StackElem.LCVMap.end())
909 return It->second;
910 return {0, nullptr};
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000911}
912
Alexey Bataeve3727102018-04-18 15:57:46 +0000913const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000914 assert(!isStackEmpty() && Stack.back().first.size() > 1 &&
915 "Data-sharing attributes stack is empty");
Alexey Bataeve3727102018-04-18 15:57:46 +0000916 const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
Alexey Bataev4b465392017-04-26 15:06:24 +0000917 if (StackElem.LCVMap.size() < I)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000918 return nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +0000919 for (const auto &Pair : StackElem.LCVMap)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000920 if (Pair.second.first == I)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000921 return Pair.first;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000922 return nullptr;
Alexey Bataev9c821032015-04-30 04:23:23 +0000923}
924
Alexey Bataeve3727102018-04-18 15:57:46 +0000925void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
Alexey Bataev90c228f2016-02-08 09:29:13 +0000926 DeclRefExpr *PrivateCopy) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000927 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000928 if (A == OMPC_threadprivate) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000929 DSAInfo &Data = Threadprivates[D];
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000930 Data.Attributes = A;
931 Data.RefExpr.setPointer(E);
932 Data.PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000933 } else {
Alexey Bataev4b465392017-04-26 15:06:24 +0000934 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataeve3727102018-04-18 15:57:46 +0000935 DSAInfo &Data = Stack.back().first.back().SharingMap[D];
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000936 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
937 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
938 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
939 (isLoopControlVariable(D).first && A == OMPC_private));
940 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
941 Data.RefExpr.setInt(/*IntVal=*/true);
942 return;
943 }
944 const bool IsLastprivate =
945 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
946 Data.Attributes = A;
947 Data.RefExpr.setPointerAndInt(E, IsLastprivate);
948 Data.PrivateCopy = PrivateCopy;
949 if (PrivateCopy) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000950 DSAInfo &Data =
951 Stack.back().first.back().SharingMap[PrivateCopy->getDecl()];
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000952 Data.Attributes = A;
953 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
954 Data.PrivateCopy = nullptr;
955 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000956 }
957}
958
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000959/// Build a variable declaration for OpenMP loop iteration variable.
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000960static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev63cc8e92018-03-20 14:45:59 +0000961 StringRef Name, const AttrVec *Attrs = nullptr,
962 DeclRefExpr *OrigRef = nullptr) {
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000963 DeclContext *DC = SemaRef.CurContext;
964 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
965 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
Alexey Bataeve3727102018-04-18 15:57:46 +0000966 auto *Decl =
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000967 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
968 if (Attrs) {
969 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
970 I != E; ++I)
971 Decl->addAttr(*I);
972 }
973 Decl->setImplicit();
Alexey Bataev63cc8e92018-03-20 14:45:59 +0000974 if (OrigRef) {
975 Decl->addAttr(
976 OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef));
977 }
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000978 return Decl;
979}
980
981static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
982 SourceLocation Loc,
983 bool RefersToCapture = false) {
984 D->setReferenced();
985 D->markUsed(S.Context);
986 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
987 SourceLocation(), D, RefersToCapture, Loc, Ty,
988 VK_LValue);
989}
990
Alexey Bataeve3727102018-04-18 15:57:46 +0000991void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000992 BinaryOperatorKind BOK) {
Alexey Bataevfa312f32017-07-21 18:48:21 +0000993 D = getCanonicalDecl(D);
994 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataevfa312f32017-07-21 18:48:21 +0000995 assert(
Richard Trieu09f14112017-07-21 21:29:35 +0000996 Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000997 "Additional reduction info may be specified only for reduction items.");
Alexey Bataeve3727102018-04-18 15:57:46 +0000998 ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D];
Alexey Bataevfa312f32017-07-21 18:48:21 +0000999 assert(ReductionData.ReductionRange.isInvalid() &&
Alexey Bataev3b1b8952017-07-25 15:53:26 +00001000 Stack.back().first.back().Directive == OMPD_taskgroup &&
Alexey Bataevfa312f32017-07-21 18:48:21 +00001001 "Additional reduction info may be specified only once for reduction "
1002 "items.");
1003 ReductionData.set(BOK, SR);
Alexey Bataev3b1b8952017-07-25 15:53:26 +00001004 Expr *&TaskgroupReductionRef =
1005 Stack.back().first.back().TaskgroupReductionRef;
1006 if (!TaskgroupReductionRef) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001007 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
1008 SemaRef.Context.VoidPtrTy, ".task_red.");
Alexey Bataevd070a582017-10-25 15:54:04 +00001009 TaskgroupReductionRef =
1010 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
Alexey Bataev3b1b8952017-07-25 15:53:26 +00001011 }
Alexey Bataevfa312f32017-07-21 18:48:21 +00001012}
1013
Alexey Bataeve3727102018-04-18 15:57:46 +00001014void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +00001015 const Expr *ReductionRef) {
Alexey Bataevfa312f32017-07-21 18:48:21 +00001016 D = getCanonicalDecl(D);
1017 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataevfa312f32017-07-21 18:48:21 +00001018 assert(
Richard Trieu09f14112017-07-21 21:29:35 +00001019 Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction &&
Alexey Bataevfa312f32017-07-21 18:48:21 +00001020 "Additional reduction info may be specified only for reduction items.");
Alexey Bataeve3727102018-04-18 15:57:46 +00001021 ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D];
Alexey Bataevfa312f32017-07-21 18:48:21 +00001022 assert(ReductionData.ReductionRange.isInvalid() &&
Alexey Bataev3b1b8952017-07-25 15:53:26 +00001023 Stack.back().first.back().Directive == OMPD_taskgroup &&
Alexey Bataevfa312f32017-07-21 18:48:21 +00001024 "Additional reduction info may be specified only once for reduction "
1025 "items.");
1026 ReductionData.set(ReductionRef, SR);
Alexey Bataev3b1b8952017-07-25 15:53:26 +00001027 Expr *&TaskgroupReductionRef =
1028 Stack.back().first.back().TaskgroupReductionRef;
1029 if (!TaskgroupReductionRef) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001030 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
1031 SemaRef.Context.VoidPtrTy, ".task_red.");
Alexey Bataevd070a582017-10-25 15:54:04 +00001032 TaskgroupReductionRef =
1033 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
Alexey Bataev3b1b8952017-07-25 15:53:26 +00001034 }
Alexey Bataevfa312f32017-07-21 18:48:21 +00001035}
1036
Alexey Bataeve3727102018-04-18 15:57:46 +00001037const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1038 const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK,
1039 Expr *&TaskgroupDescriptor) const {
Alexey Bataevfa312f32017-07-21 18:48:21 +00001040 D = getCanonicalDecl(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +00001041 assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1042 if (Stack.back().first.empty())
1043 return DSAVarData();
Alexey Bataeve3727102018-04-18 15:57:46 +00001044 for (iterator I = std::next(Stack.back().first.rbegin(), 1),
1045 E = Stack.back().first.rend();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001046 I != E; std::advance(I, 1)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001047 const DSAInfo &Data = I->SharingMap.lookup(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +00001048 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
Alexey Bataevfa312f32017-07-21 18:48:21 +00001049 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00001050 const ReductionData &ReductionData = I->ReductionMap.lookup(D);
Alexey Bataevfa312f32017-07-21 18:48:21 +00001051 if (!ReductionData.ReductionOp ||
1052 ReductionData.ReductionOp.is<const Expr *>())
Alexey Bataevf189cb72017-07-24 14:52:13 +00001053 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001054 SR = ReductionData.ReductionRange;
Alexey Bataevf87fa882017-07-21 19:26:22 +00001055 BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>();
Alexey Bataev88202be2017-07-27 13:20:36 +00001056 assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1057 "expression for the descriptor is not "
1058 "set.");
1059 TaskgroupDescriptor = I->TaskgroupReductionRef;
Alexey Bataevf189cb72017-07-24 14:52:13 +00001060 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
1061 Data.PrivateCopy, I->DefaultAttrLoc);
Alexey Bataevfa312f32017-07-21 18:48:21 +00001062 }
Alexey Bataevf189cb72017-07-24 14:52:13 +00001063 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001064}
1065
Alexey Bataeve3727102018-04-18 15:57:46 +00001066const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1067 const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef,
1068 Expr *&TaskgroupDescriptor) const {
Alexey Bataevfa312f32017-07-21 18:48:21 +00001069 D = getCanonicalDecl(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +00001070 assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1071 if (Stack.back().first.empty())
1072 return DSAVarData();
Alexey Bataeve3727102018-04-18 15:57:46 +00001073 for (iterator I = std::next(Stack.back().first.rbegin(), 1),
1074 E = Stack.back().first.rend();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001075 I != E; std::advance(I, 1)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001076 const DSAInfo &Data = I->SharingMap.lookup(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +00001077 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
Alexey Bataevfa312f32017-07-21 18:48:21 +00001078 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00001079 const ReductionData &ReductionData = I->ReductionMap.lookup(D);
Alexey Bataevfa312f32017-07-21 18:48:21 +00001080 if (!ReductionData.ReductionOp ||
1081 !ReductionData.ReductionOp.is<const Expr *>())
Alexey Bataevf189cb72017-07-24 14:52:13 +00001082 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001083 SR = ReductionData.ReductionRange;
1084 ReductionRef = ReductionData.ReductionOp.get<const Expr *>();
Alexey Bataev88202be2017-07-27 13:20:36 +00001085 assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1086 "expression for the descriptor is not "
1087 "set.");
1088 TaskgroupDescriptor = I->TaskgroupReductionRef;
Alexey Bataevf189cb72017-07-24 14:52:13 +00001089 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
1090 Data.PrivateCopy, I->DefaultAttrLoc);
Alexey Bataevfa312f32017-07-21 18:48:21 +00001091 }
Alexey Bataevf189cb72017-07-24 14:52:13 +00001092 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001093}
1094
Alexey Bataeve3727102018-04-18 15:57:46 +00001095bool DSAStackTy::isOpenMPLocal(VarDecl *D, iterator Iter) const {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001096 D = D->getCanonicalDecl();
Alexey Bataev852525d2018-03-02 17:17:12 +00001097 if (!isStackEmpty()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001098 iterator I = Iter, E = Stack.back().first.rend();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001099 Scope *TopScope = nullptr;
Alexey Bataev7e6803e2019-01-09 15:58:05 +00001100 while (I != E && !isImplicitOrExplicitTaskingRegion(I->Directive) &&
Alexey Bataev852525d2018-03-02 17:17:12 +00001101 !isOpenMPTargetExecutionDirective(I->Directive))
Alexey Bataevec3da872014-01-31 05:15:34 +00001102 ++I;
Alexey Bataeved09d242014-05-28 05:53:51 +00001103 if (I == E)
1104 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001105 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001106 Scope *CurScope = getCurScope();
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001107 while (CurScope != TopScope && !CurScope->isDeclScope(D))
Alexey Bataev758e55e2013-09-06 18:03:48 +00001108 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +00001109 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001110 }
Alexey Bataevec3da872014-01-31 05:15:34 +00001111 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001112}
1113
Joel E. Dennyd2649292019-01-04 22:11:56 +00001114static bool isConstNotMutableType(Sema &SemaRef, QualType Type,
1115 bool AcceptIfMutable = true,
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001116 bool *IsClassType = nullptr) {
1117 ASTContext &Context = SemaRef.getASTContext();
Joel E. Dennyd2649292019-01-04 22:11:56 +00001118 Type = Type.getNonReferenceType().getCanonicalType();
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001119 bool IsConstant = Type.isConstant(Context);
1120 Type = Context.getBaseElementType(Type);
Joel E. Dennyd2649292019-01-04 22:11:56 +00001121 const CXXRecordDecl *RD = AcceptIfMutable && SemaRef.getLangOpts().CPlusPlus
1122 ? Type->getAsCXXRecordDecl()
1123 : nullptr;
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001124 if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
1125 if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate())
1126 RD = CTD->getTemplatedDecl();
1127 if (IsClassType)
1128 *IsClassType = RD;
1129 return IsConstant && !(SemaRef.getLangOpts().CPlusPlus && RD &&
1130 RD->hasDefinition() && RD->hasMutableFields());
1131}
1132
Joel E. Dennyd2649292019-01-04 22:11:56 +00001133static bool rejectConstNotMutableType(Sema &SemaRef, const ValueDecl *D,
1134 QualType Type, OpenMPClauseKind CKind,
1135 SourceLocation ELoc,
1136 bool AcceptIfMutable = true,
1137 bool ListItemNotVar = false) {
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001138 ASTContext &Context = SemaRef.getASTContext();
1139 bool IsClassType;
Joel E. Dennyd2649292019-01-04 22:11:56 +00001140 if (isConstNotMutableType(SemaRef, Type, AcceptIfMutable, &IsClassType)) {
1141 unsigned Diag = ListItemNotVar
1142 ? diag::err_omp_const_list_item
1143 : IsClassType ? diag::err_omp_const_not_mutable_variable
1144 : diag::err_omp_const_variable;
1145 SemaRef.Diag(ELoc, Diag) << getOpenMPClauseName(CKind);
1146 if (!ListItemNotVar && D) {
1147 const VarDecl *VD = dyn_cast<VarDecl>(D);
1148 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
1149 VarDecl::DeclarationOnly;
1150 SemaRef.Diag(D->getLocation(),
1151 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1152 << D;
1153 }
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001154 return true;
1155 }
1156 return false;
1157}
1158
Alexey Bataeve3727102018-04-18 15:57:46 +00001159const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
1160 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001161 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001162 DSAVarData DVar;
1163
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001164 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001165 auto TI = Threadprivates.find(D);
1166 if (TI != Threadprivates.end()) {
1167 DVar.RefExpr = TI->getSecond().RefExpr.getPointer();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001168 DVar.CKind = OMPC_threadprivate;
1169 return DVar;
Alexey Bataeve3727102018-04-18 15:57:46 +00001170 }
1171 if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
Alexey Bataev817d7f32017-11-14 21:01:01 +00001172 DVar.RefExpr = buildDeclRefExpr(
1173 SemaRef, VD, D->getType().getNonReferenceType(),
1174 VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation());
1175 DVar.CKind = OMPC_threadprivate;
1176 addDSA(D, DVar.RefExpr, OMPC_threadprivate);
Alexey Bataev852525d2018-03-02 17:17:12 +00001177 return DVar;
1178 }
1179 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1180 // in a Construct, C/C++, predetermined, p.1]
1181 // Variables appearing in threadprivate directives are threadprivate.
1182 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
1183 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1184 SemaRef.getLangOpts().OpenMPUseTLS &&
1185 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
1186 (VD && VD->getStorageClass() == SC_Register &&
1187 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
1188 DVar.RefExpr = buildDeclRefExpr(
1189 SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation());
1190 DVar.CKind = OMPC_threadprivate;
1191 addDSA(D, DVar.RefExpr, OMPC_threadprivate);
1192 return DVar;
1193 }
1194 if (SemaRef.getLangOpts().OpenMPCUDAMode && VD &&
1195 VD->isLocalVarDeclOrParm() && !isStackEmpty() &&
1196 !isLoopControlVariable(D).first) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001197 iterator IterTarget =
Alexey Bataev852525d2018-03-02 17:17:12 +00001198 std::find_if(Stack.back().first.rbegin(), Stack.back().first.rend(),
1199 [](const SharingMapTy &Data) {
1200 return isOpenMPTargetExecutionDirective(Data.Directive);
1201 });
1202 if (IterTarget != Stack.back().first.rend()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001203 iterator ParentIterTarget = std::next(IterTarget, 1);
1204 for (iterator Iter = Stack.back().first.rbegin();
1205 Iter != ParentIterTarget; std::advance(Iter, 1)) {
Alexey Bataev852525d2018-03-02 17:17:12 +00001206 if (isOpenMPLocal(VD, Iter)) {
1207 DVar.RefExpr =
1208 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1209 D->getLocation());
1210 DVar.CKind = OMPC_threadprivate;
1211 return DVar;
1212 }
Alexey Bataev852525d2018-03-02 17:17:12 +00001213 }
1214 if (!isClauseParsingMode() || IterTarget != Stack.back().first.rbegin()) {
1215 auto DSAIter = IterTarget->SharingMap.find(D);
1216 if (DSAIter != IterTarget->SharingMap.end() &&
1217 isOpenMPPrivate(DSAIter->getSecond().Attributes)) {
1218 DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer();
1219 DVar.CKind = OMPC_threadprivate;
1220 return DVar;
Alexey Bataeve3727102018-04-18 15:57:46 +00001221 }
1222 iterator End = Stack.back().first.rend();
1223 if (!SemaRef.isOpenMPCapturedByRef(
1224 D, std::distance(ParentIterTarget, End))) {
Alexey Bataev852525d2018-03-02 17:17:12 +00001225 DVar.RefExpr =
1226 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1227 IterTarget->ConstructLoc);
1228 DVar.CKind = OMPC_threadprivate;
1229 return DVar;
1230 }
1231 }
1232 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001233 }
1234
Alexey Bataev4b465392017-04-26 15:06:24 +00001235 if (isStackEmpty())
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001236 // Not in OpenMP execution region and top scope was already checked.
1237 return DVar;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001238
Alexey Bataev758e55e2013-09-06 18:03:48 +00001239 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +00001240 // in a Construct, C/C++, predetermined, p.4]
1241 // Static data members are shared.
1242 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1243 // in a Construct, C/C++, predetermined, p.7]
1244 // Variables with static storage duration that are declared in a scope
1245 // inside the construct are shared.
Alexey Bataeve3727102018-04-18 15:57:46 +00001246 auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; };
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001247 if (VD && VD->isStaticDataMember()) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001248 DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent);
Alexey Bataevdffa93a2015-12-10 08:20:58 +00001249 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
Alexey Bataevec3da872014-01-31 05:15:34 +00001250 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001251
Alexey Bataevdffa93a2015-12-10 08:20:58 +00001252 DVar.CKind = OMPC_shared;
1253 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001254 }
1255
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001256 // The predetermined shared attribute for const-qualified types having no
1257 // mutable members was removed after OpenMP 3.1.
1258 if (SemaRef.LangOpts.OpenMP <= 31) {
1259 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1260 // in a Construct, C/C++, predetermined, p.6]
1261 // Variables with const qualified type having no mutable member are
1262 // shared.
Joel E. Dennyd2649292019-01-04 22:11:56 +00001263 if (isConstNotMutableType(SemaRef, D->getType())) {
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001264 // Variables with const-qualified type having no mutable member may be
1265 // listed in a firstprivate clause, even if they are static data members.
1266 DSAVarData DVarTemp = hasInnermostDSA(
1267 D,
1268 [](OpenMPClauseKind C) {
1269 return C == OMPC_firstprivate || C == OMPC_shared;
1270 },
1271 MatchesAlways, FromParent);
1272 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
1273 return DVarTemp;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001274
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001275 DVar.CKind = OMPC_shared;
1276 return DVar;
1277 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001278 }
1279
Alexey Bataev758e55e2013-09-06 18:03:48 +00001280 // Explicitly specified attributes and local variables with predetermined
1281 // attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +00001282 iterator I = Stack.back().first.rbegin();
1283 iterator EndI = Stack.back().first.rend();
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001284 if (FromParent && I != EndI)
1285 std::advance(I, 1);
Alexey Bataeve3727102018-04-18 15:57:46 +00001286 auto It = I->SharingMap.find(D);
1287 if (It != I->SharingMap.end()) {
1288 const DSAInfo &Data = It->getSecond();
1289 DVar.RefExpr = Data.RefExpr.getPointer();
1290 DVar.PrivateCopy = Data.PrivateCopy;
1291 DVar.CKind = Data.Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001292 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev4d4624c2017-07-20 16:47:47 +00001293 DVar.DKind = I->Directive;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001294 }
1295
1296 return DVar;
1297}
1298
Alexey Bataeve3727102018-04-18 15:57:46 +00001299const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1300 bool FromParent) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001301 if (isStackEmpty()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001302 iterator I;
Alexey Bataev4b465392017-04-26 15:06:24 +00001303 return getDSA(I, D);
1304 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001305 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +00001306 iterator StartI = Stack.back().first.rbegin();
1307 iterator EndI = Stack.back().first.rend();
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001308 if (FromParent && StartI != EndI)
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001309 std::advance(StartI, 1);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001310 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001311}
1312
Alexey Bataeve3727102018-04-18 15:57:46 +00001313const DSAStackTy::DSAVarData
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001314DSAStackTy::hasDSA(ValueDecl *D,
Alexey Bataev97d18bf2018-04-11 19:21:00 +00001315 const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1316 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +00001317 bool FromParent) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001318 if (isStackEmpty())
1319 return {};
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001320 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +00001321 iterator I = Stack.back().first.rbegin();
1322 iterator EndI = Stack.back().first.rend();
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001323 if (FromParent && I != EndI)
Alexey Bataev0e6fc1c2017-04-27 14:46:26 +00001324 std::advance(I, 1);
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001325 for (; I != EndI; std::advance(I, 1)) {
Alexey Bataev7e6803e2019-01-09 15:58:05 +00001326 if (!DPred(I->Directive) && !isImplicitOrExplicitTaskingRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +00001327 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00001328 iterator NewI = I;
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001329 DSAVarData DVar = getDSA(NewI, D);
1330 if (I == NewI && CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001331 return DVar;
Alexey Bataev60859c02017-04-27 15:10:33 +00001332 }
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001333 return {};
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001334}
1335
Alexey Bataeve3727102018-04-18 15:57:46 +00001336const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
Alexey Bataev97d18bf2018-04-11 19:21:00 +00001337 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1338 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +00001339 bool FromParent) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001340 if (isStackEmpty())
1341 return {};
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001342 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +00001343 iterator StartI = Stack.back().first.rbegin();
1344 iterator EndI = Stack.back().first.rend();
Alexey Bataeve3978122016-07-19 05:06:39 +00001345 if (FromParent && StartI != EndI)
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001346 std::advance(StartI, 1);
Alexey Bataeve3978122016-07-19 05:06:39 +00001347 if (StartI == EndI || !DPred(StartI->Directive))
Alexey Bataev4b465392017-04-26 15:06:24 +00001348 return {};
Alexey Bataeve3727102018-04-18 15:57:46 +00001349 iterator NewI = StartI;
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001350 DSAVarData DVar = getDSA(NewI, D);
1351 return (NewI == StartI && CPred(DVar.CKind)) ? DVar : DSAVarData();
Alexey Bataevc5e02582014-06-16 07:08:35 +00001352}
1353
Alexey Bataevaac108a2015-06-23 04:51:00 +00001354bool DSAStackTy::hasExplicitDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00001355 const ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1356 unsigned Level, bool NotLastprivate) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001357 if (isStackEmpty())
1358 return false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001359 D = getCanonicalDecl(D);
Alexey Bataev4b465392017-04-26 15:06:24 +00001360 auto StartI = Stack.back().first.begin();
1361 auto EndI = Stack.back().first.end();
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +00001362 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +00001363 return false;
1364 std::advance(StartI, Level);
Alexey Bataeve3727102018-04-18 15:57:46 +00001365 auto I = StartI->SharingMap.find(D);
Alexey Bataev92b33652018-11-21 19:41:10 +00001366 if ((I != StartI->SharingMap.end()) &&
Alexey Bataeve3727102018-04-18 15:57:46 +00001367 I->getSecond().RefExpr.getPointer() &&
1368 CPred(I->getSecond().Attributes) &&
Alexey Bataev92b33652018-11-21 19:41:10 +00001369 (!NotLastprivate || !I->getSecond().RefExpr.getInt()))
1370 return true;
1371 // Check predetermined rules for the loop control variables.
1372 auto LI = StartI->LCVMap.find(D);
1373 if (LI != StartI->LCVMap.end())
1374 return CPred(OMPC_private);
1375 return false;
Alexey Bataevaac108a2015-06-23 04:51:00 +00001376}
1377
Samuel Antao4be30e92015-10-02 17:14:03 +00001378bool DSAStackTy::hasExplicitDirective(
Alexey Bataeve3727102018-04-18 15:57:46 +00001379 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1380 unsigned Level) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001381 if (isStackEmpty())
1382 return false;
1383 auto StartI = Stack.back().first.begin();
1384 auto EndI = Stack.back().first.end();
Samuel Antao4be30e92015-10-02 17:14:03 +00001385 if (std::distance(StartI, EndI) <= (int)Level)
1386 return false;
1387 std::advance(StartI, Level);
1388 return DPred(StartI->Directive);
1389}
1390
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001391bool DSAStackTy::hasDirective(
1392 const llvm::function_ref<bool(OpenMPDirectiveKind,
1393 const DeclarationNameInfo &, SourceLocation)>
Alexey Bataev97d18bf2018-04-11 19:21:00 +00001394 DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +00001395 bool FromParent) const {
Samuel Antaof0d79752016-05-27 15:21:27 +00001396 // We look only in the enclosing region.
Alexey Bataev4b465392017-04-26 15:06:24 +00001397 if (isStackEmpty())
Samuel Antaof0d79752016-05-27 15:21:27 +00001398 return false;
Alexey Bataev4b465392017-04-26 15:06:24 +00001399 auto StartI = std::next(Stack.back().first.rbegin());
1400 auto EndI = Stack.back().first.rend();
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001401 if (FromParent && StartI != EndI)
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001402 StartI = std::next(StartI);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001403 for (auto I = StartI, EE = EndI; I != EE; ++I) {
1404 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
1405 return true;
1406 }
1407 return false;
1408}
1409
Alexey Bataev758e55e2013-09-06 18:03:48 +00001410void Sema::InitDataSharingAttributesStack() {
1411 VarDataSharingAttributesStack = new DSAStackTy(*this);
1412}
1413
1414#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
1415
Alexey Bataev4b465392017-04-26 15:06:24 +00001416void Sema::pushOpenMPFunctionRegion() {
1417 DSAStack->pushFunction();
1418}
1419
1420void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) {
1421 DSAStack->popFunction(OldFSI);
1422}
1423
Alexey Bataevc416e642019-02-08 18:02:25 +00001424static bool isOpenMPDeviceDelayedContext(Sema &S) {
1425 assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice &&
1426 "Expected OpenMP device compilation.");
1427 return !S.isInOpenMPTargetExecutionDirective() &&
1428 !S.isInOpenMPDeclareTargetContext();
1429}
1430
1431/// Do we know that we will eventually codegen the given function?
1432static bool isKnownEmitted(Sema &S, FunctionDecl *FD) {
1433 assert(S.LangOpts.OpenMP && S.LangOpts.OpenMPIsDevice &&
1434 "Expected OpenMP device compilation.");
1435 // Templates are emitted when they're instantiated.
1436 if (FD->isDependentContext())
1437 return false;
1438
1439 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
1440 FD->getCanonicalDecl()))
1441 return true;
1442
1443 // Otherwise, the function is known-emitted if it's in our set of
1444 // known-emitted functions.
1445 return S.DeviceKnownEmittedFns.count(FD) > 0;
1446}
1447
1448Sema::DeviceDiagBuilder Sema::diagIfOpenMPDeviceCode(SourceLocation Loc,
1449 unsigned DiagID) {
1450 assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice &&
1451 "Expected OpenMP device compilation.");
1452 return DeviceDiagBuilder((isOpenMPDeviceDelayedContext(*this) &&
1453 !isKnownEmitted(*this, getCurFunctionDecl()))
1454 ? DeviceDiagBuilder::K_Deferred
1455 : DeviceDiagBuilder::K_Immediate,
1456 Loc, DiagID, getCurFunctionDecl(), *this);
1457}
1458
1459void Sema::checkOpenMPDeviceFunction(SourceLocation Loc, FunctionDecl *Callee) {
1460 assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice &&
1461 "Expected OpenMP device compilation.");
1462 assert(Callee && "Callee may not be null.");
1463 FunctionDecl *Caller = getCurFunctionDecl();
1464
1465 // If the caller is known-emitted, mark the callee as known-emitted.
1466 // Otherwise, mark the call in our call graph so we can traverse it later.
1467 if (!isOpenMPDeviceDelayedContext(*this) ||
1468 (Caller && isKnownEmitted(*this, Caller)))
1469 markKnownEmitted(*this, Caller, Callee, Loc, isKnownEmitted);
1470 else if (Caller)
1471 DeviceCallGraph[Caller].insert({Callee, Loc});
1472}
1473
Alexey Bataev123ad192019-02-27 20:29:45 +00001474void Sema::checkOpenMPDeviceExpr(const Expr *E) {
1475 assert(getLangOpts().OpenMP && getLangOpts().OpenMPIsDevice &&
1476 "OpenMP device compilation mode is expected.");
1477 QualType Ty = E->getType();
1478 if ((Ty->isFloat16Type() && !Context.getTargetInfo().hasFloat16Type()) ||
1479 (Ty->isFloat128Type() && !Context.getTargetInfo().hasFloat128Type()) ||
1480 (Ty->isIntegerType() && Context.getTypeSize(Ty) == 128 &&
1481 !Context.getTargetInfo().hasInt128Type()))
1482 targetDiag(E->getExprLoc(), diag::err_type_unsupported)
1483 << Ty << E->getSourceRange();
1484}
1485
Alexey Bataeve3727102018-04-18 15:57:46 +00001486bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level) const {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001487 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1488
Alexey Bataeve3727102018-04-18 15:57:46 +00001489 ASTContext &Ctx = getASTContext();
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001490 bool IsByRef = true;
1491
1492 // Find the directive that is associated with the provided scope.
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00001493 D = cast<ValueDecl>(D->getCanonicalDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00001494 QualType Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001495
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001496 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001497 // This table summarizes how a given variable should be passed to the device
1498 // given its type and the clauses where it appears. This table is based on
1499 // the description in OpenMP 4.5 [2.10.4, target Construct] and
1500 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
1501 //
1502 // =========================================================================
1503 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
1504 // | |(tofrom:scalar)| | pvt | | | |
1505 // =========================================================================
1506 // | scl | | | | - | | bycopy|
1507 // | scl | | - | x | - | - | bycopy|
1508 // | scl | | x | - | - | - | null |
1509 // | scl | x | | | - | | byref |
1510 // | scl | x | - | x | - | - | bycopy|
1511 // | scl | x | x | - | - | - | null |
1512 // | scl | | - | - | - | x | byref |
1513 // | scl | x | - | - | - | x | byref |
1514 //
1515 // | agg | n.a. | | | - | | byref |
1516 // | agg | n.a. | - | x | - | - | byref |
1517 // | agg | n.a. | x | - | - | - | null |
1518 // | agg | n.a. | - | - | - | x | byref |
1519 // | agg | n.a. | - | - | - | x[] | byref |
1520 //
1521 // | ptr | n.a. | | | - | | bycopy|
1522 // | ptr | n.a. | - | x | - | - | bycopy|
1523 // | ptr | n.a. | x | - | - | - | null |
1524 // | ptr | n.a. | - | - | - | x | byref |
1525 // | ptr | n.a. | - | - | - | x[] | bycopy|
1526 // | ptr | n.a. | - | - | x | | bycopy|
1527 // | ptr | n.a. | - | - | x | x | bycopy|
1528 // | ptr | n.a. | - | - | x | x[] | bycopy|
1529 // =========================================================================
1530 // Legend:
1531 // scl - scalar
1532 // ptr - pointer
1533 // agg - aggregate
1534 // x - applies
1535 // - - invalid in this combination
1536 // [] - mapped with an array section
1537 // byref - should be mapped by reference
1538 // byval - should be mapped by value
1539 // null - initialize a local variable to null on the device
1540 //
1541 // Observations:
1542 // - All scalar declarations that show up in a map clause have to be passed
1543 // by reference, because they may have been mapped in the enclosing data
1544 // environment.
1545 // - If the scalar value does not fit the size of uintptr, it has to be
1546 // passed by reference, regardless the result in the table above.
1547 // - For pointers mapped by value that have either an implicit map or an
1548 // array section, the runtime library may pass the NULL value to the
1549 // device instead of the value passed to it by the compiler.
1550
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001551 if (Ty->isReferenceType())
1552 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
Samuel Antao86ace552016-04-27 22:40:57 +00001553
1554 // Locate map clauses and see if the variable being captured is referred to
1555 // in any of those clauses. Here we only care about variables, not fields,
1556 // because fields are part of aggregates.
1557 bool IsVariableUsedInMapClause = false;
1558 bool IsVariableAssociatedWithSection = false;
1559
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +00001560 DSAStack->checkMappableExprComponentListsForDeclAtLevel(
Alexey Bataeve3727102018-04-18 15:57:46 +00001561 D, Level,
1562 [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D](
1563 OMPClauseMappableExprCommon::MappableExprComponentListRef
Samuel Antao6890b092016-07-28 14:25:09 +00001564 MapExprComponents,
1565 OpenMPClauseKind WhereFoundClauseKind) {
1566 // Only the map clause information influences how a variable is
1567 // captured. E.g. is_device_ptr does not require changing the default
Samuel Antao4c8035b2016-12-12 18:00:20 +00001568 // behavior.
Samuel Antao6890b092016-07-28 14:25:09 +00001569 if (WhereFoundClauseKind != OMPC_map)
1570 return false;
Samuel Antao86ace552016-04-27 22:40:57 +00001571
1572 auto EI = MapExprComponents.rbegin();
1573 auto EE = MapExprComponents.rend();
1574
1575 assert(EI != EE && "Invalid map expression!");
1576
1577 if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
1578 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
1579
1580 ++EI;
1581 if (EI == EE)
1582 return false;
1583
1584 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
1585 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
1586 isa<MemberExpr>(EI->getAssociatedExpression())) {
1587 IsVariableAssociatedWithSection = true;
1588 // There is nothing more we need to know about this variable.
1589 return true;
1590 }
1591
1592 // Keep looking for more map info.
1593 return false;
1594 });
1595
1596 if (IsVariableUsedInMapClause) {
1597 // If variable is identified in a map clause it is always captured by
1598 // reference except if it is a pointer that is dereferenced somehow.
1599 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
1600 } else {
Alexey Bataev3f96fe62017-12-13 17:31:39 +00001601 // By default, all the data that has a scalar type is mapped by copy
1602 // (except for reduction variables).
1603 IsByRef =
Alexey Bataev60705422018-10-30 15:50:12 +00001604 (DSAStack->isForceCaptureByReferenceInTargetExecutable() &&
1605 !Ty->isAnyPointerType()) ||
Alexey Bataev3f96fe62017-12-13 17:31:39 +00001606 !Ty->isScalarType() ||
1607 DSAStack->getDefaultDMAAtLevel(Level) == DMA_tofrom_scalar ||
1608 DSAStack->hasExplicitDSA(
1609 D, [](OpenMPClauseKind K) { return K == OMPC_reduction; }, Level);
Samuel Antao86ace552016-04-27 22:40:57 +00001610 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001611 }
1612
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001613 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00001614 IsByRef =
Alexey Bataev60705422018-10-30 15:50:12 +00001615 ((DSAStack->isForceCaptureByReferenceInTargetExecutable() &&
1616 !Ty->isAnyPointerType()) ||
1617 !DSAStack->hasExplicitDSA(
1618 D,
1619 [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; },
1620 Level, /*NotLastprivate=*/true)) &&
Alexey Bataev8e769ee2017-12-22 21:01:52 +00001621 // If the variable is artificial and must be captured by value - try to
1622 // capture by value.
Alexey Bataevd2202ca2017-12-27 17:58:32 +00001623 !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() &&
1624 !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue());
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001625 }
1626
Samuel Antao86ace552016-04-27 22:40:57 +00001627 // When passing data by copy, we need to make sure it fits the uintptr size
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001628 // and alignment, because the runtime library only deals with uintptr types.
1629 // If it does not fit the uintptr size, we need to pass the data by reference
1630 // instead.
1631 if (!IsByRef &&
1632 (Ctx.getTypeSizeInChars(Ty) >
1633 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001634 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001635 IsByRef = true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001636 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001637
1638 return IsByRef;
1639}
1640
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001641unsigned Sema::getOpenMPNestingLevel() const {
1642 assert(getLangOpts().OpenMP);
1643 return DSAStack->getNestingLevel();
1644}
1645
Jonas Hahnfeld87d44262017-11-18 21:00:46 +00001646bool Sema::isInOpenMPTargetExecutionDirective() const {
1647 return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) &&
1648 !DSAStack->isClauseParsingMode()) ||
1649 DSAStack->hasDirective(
1650 [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
1651 SourceLocation) -> bool {
1652 return isOpenMPTargetExecutionDirective(K);
1653 },
1654 false);
1655}
1656
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001657VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D) {
Alexey Bataevf841bd92014-12-16 07:00:22 +00001658 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001659 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +00001660
1661 // If we are attempting to capture a global variable in a directive with
1662 // 'target' we return true so that this global is also mapped to the device.
1663 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001664 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001665 if (VD && !VD->hasLocalStorage()) {
1666 if (isInOpenMPDeclareTargetContext() &&
1667 (getCurCapturedRegion() || getCurBlock() || getCurLambda())) {
1668 // Try to mark variable as declare target if it is used in capturing
1669 // regions.
Alexey Bataev97b72212018-08-14 18:31:20 +00001670 if (!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001671 checkDeclIsAllowedInOpenMPTarget(nullptr, VD);
Alexey Bataev92327c52018-03-26 16:40:55 +00001672 return nullptr;
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001673 } else if (isInOpenMPTargetExecutionDirective()) {
1674 // If the declaration is enclosed in a 'declare target' directive,
1675 // then it should not be captured.
1676 //
Alexey Bataev97b72212018-08-14 18:31:20 +00001677 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001678 return nullptr;
1679 return VD;
1680 }
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001681 }
Alexey Bataev60705422018-10-30 15:50:12 +00001682 // Capture variables captured by reference in lambdas for target-based
1683 // directives.
1684 if (VD && !DSAStack->isClauseParsingMode()) {
1685 if (const auto *RD = VD->getType()
1686 .getCanonicalType()
1687 .getNonReferenceType()
1688 ->getAsCXXRecordDecl()) {
1689 bool SavedForceCaptureByReferenceInTargetExecutable =
1690 DSAStack->isForceCaptureByReferenceInTargetExecutable();
1691 DSAStack->setForceCaptureByReferenceInTargetExecutable(/*V=*/true);
Alexey Bataevd1840e52018-11-16 21:13:33 +00001692 if (RD->isLambda()) {
1693 llvm::DenseMap<const VarDecl *, FieldDecl *> Captures;
1694 FieldDecl *ThisCapture;
1695 RD->getCaptureFields(Captures, ThisCapture);
Alexey Bataev60705422018-10-30 15:50:12 +00001696 for (const LambdaCapture &LC : RD->captures()) {
1697 if (LC.getCaptureKind() == LCK_ByRef) {
1698 VarDecl *VD = LC.getCapturedVar();
1699 DeclContext *VDC = VD->getDeclContext();
1700 if (!VDC->Encloses(CurContext))
1701 continue;
1702 DSAStackTy::DSAVarData DVarPrivate =
1703 DSAStack->getTopDSA(VD, /*FromParent=*/false);
1704 // Do not capture already captured variables.
1705 if (!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) &&
1706 DVarPrivate.CKind == OMPC_unknown &&
1707 !DSAStack->checkMappableExprComponentListsForDecl(
1708 D, /*CurrentRegionOnly=*/true,
1709 [](OMPClauseMappableExprCommon::
1710 MappableExprComponentListRef,
1711 OpenMPClauseKind) { return true; }))
1712 MarkVariableReferenced(LC.getLocation(), LC.getCapturedVar());
1713 } else if (LC.getCaptureKind() == LCK_This) {
Alexey Bataevd1840e52018-11-16 21:13:33 +00001714 QualType ThisTy = getCurrentThisType();
1715 if (!ThisTy.isNull() &&
1716 Context.typesAreCompatible(ThisTy, ThisCapture->getType()))
1717 CheckCXXThisCapture(LC.getLocation());
Alexey Bataev60705422018-10-30 15:50:12 +00001718 }
1719 }
Alexey Bataevd1840e52018-11-16 21:13:33 +00001720 }
Alexey Bataev60705422018-10-30 15:50:12 +00001721 DSAStack->setForceCaptureByReferenceInTargetExecutable(
1722 SavedForceCaptureByReferenceInTargetExecutable);
1723 }
1724 }
Samuel Antao4be30e92015-10-02 17:14:03 +00001725
Alexey Bataev48977c32015-08-04 08:10:48 +00001726 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
1727 (!DSAStack->isClauseParsingMode() ||
1728 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001729 auto &&Info = DSAStack->isLoopControlVariable(D);
1730 if (Info.first ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001731 (VD && VD->hasLocalStorage() &&
Alexey Bataev7e6803e2019-01-09 15:58:05 +00001732 isImplicitOrExplicitTaskingRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001733 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001734 return VD ? VD : Info.second;
Alexey Bataeve3727102018-04-18 15:57:46 +00001735 DSAStackTy::DSAVarData DVarPrivate =
1736 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001737 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
Alexey Bataev90c228f2016-02-08 09:29:13 +00001738 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00001739 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate,
1740 [](OpenMPDirectiveKind) { return true; },
1741 DSAStack->isClauseParsingMode());
Alexey Bataev90c228f2016-02-08 09:29:13 +00001742 if (DVarPrivate.CKind != OMPC_unknown)
1743 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001744 }
Alexey Bataev90c228f2016-02-08 09:29:13 +00001745 return nullptr;
Alexey Bataevf841bd92014-12-16 07:00:22 +00001746}
1747
Alexey Bataevdfa430f2017-12-08 15:03:50 +00001748void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex,
1749 unsigned Level) const {
1750 SmallVector<OpenMPDirectiveKind, 4> Regions;
1751 getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level));
1752 FunctionScopesIndex -= Regions.size();
1753}
1754
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00001755void Sema::startOpenMPLoop() {
1756 assert(LangOpts.OpenMP && "OpenMP must be enabled.");
1757 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective()))
1758 DSAStack->loopInit();
1759}
1760
Alexey Bataeve3727102018-04-18 15:57:46 +00001761bool Sema::isOpenMPPrivateDecl(const ValueDecl *D, unsigned Level) const {
Alexey Bataevaac108a2015-06-23 04:51:00 +00001762 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00001763 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
1764 if (DSAStack->getAssociatedLoops() > 0 &&
1765 !DSAStack->isLoopStarted()) {
1766 DSAStack->resetPossibleLoopCounter(D);
1767 DSAStack->loopStart();
1768 return true;
1769 }
1770 if ((DSAStack->getPossiblyLoopCunter() == D->getCanonicalDecl() ||
1771 DSAStack->isLoopControlVariable(D).first) &&
1772 !DSAStack->hasExplicitDSA(
1773 D, [](OpenMPClauseKind K) { return K != OMPC_private; }, Level) &&
1774 !isOpenMPSimdDirective(DSAStack->getCurrentDirective()))
1775 return true;
1776 }
Alexey Bataevaac108a2015-06-23 04:51:00 +00001777 return DSAStack->hasExplicitDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00001778 D, [](OpenMPClauseKind K) { return K == OMPC_private; }, Level) ||
Alexey Bataev3f82cfc2017-12-13 15:28:44 +00001779 (DSAStack->isClauseParsingMode() &&
1780 DSAStack->getClauseParsingMode() == OMPC_private) ||
Alexey Bataev88202be2017-07-27 13:20:36 +00001781 // Consider taskgroup reduction descriptor variable a private to avoid
1782 // possible capture in the region.
1783 (DSAStack->hasExplicitDirective(
1784 [](OpenMPDirectiveKind K) { return K == OMPD_taskgroup; },
1785 Level) &&
1786 DSAStack->isTaskgroupReductionRef(D, Level));
Alexey Bataevaac108a2015-06-23 04:51:00 +00001787}
1788
Alexey Bataeve3727102018-04-18 15:57:46 +00001789void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D,
1790 unsigned Level) {
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001791 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1792 D = getCanonicalDecl(D);
1793 OpenMPClauseKind OMPC = OMPC_unknown;
1794 for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) {
1795 const unsigned NewLevel = I - 1;
1796 if (DSAStack->hasExplicitDSA(D,
1797 [&OMPC](const OpenMPClauseKind K) {
1798 if (isOpenMPPrivate(K)) {
1799 OMPC = K;
1800 return true;
1801 }
1802 return false;
1803 },
1804 NewLevel))
1805 break;
1806 if (DSAStack->checkMappableExprComponentListsForDeclAtLevel(
1807 D, NewLevel,
1808 [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
1809 OpenMPClauseKind) { return true; })) {
1810 OMPC = OMPC_map;
1811 break;
1812 }
1813 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1814 NewLevel)) {
Alexey Bataev63cc8e92018-03-20 14:45:59 +00001815 OMPC = OMPC_map;
1816 if (D->getType()->isScalarType() &&
1817 DSAStack->getDefaultDMAAtLevel(NewLevel) !=
1818 DefaultMapAttributes::DMA_tofrom_scalar)
1819 OMPC = OMPC_firstprivate;
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001820 break;
1821 }
1822 }
1823 if (OMPC != OMPC_unknown)
1824 FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, OMPC));
1825}
1826
Alexey Bataeve3727102018-04-18 15:57:46 +00001827bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D,
1828 unsigned Level) const {
Samuel Antao4be30e92015-10-02 17:14:03 +00001829 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1830 // Return true if the current level is no longer enclosed in a target region.
1831
Alexey Bataeve3727102018-04-18 15:57:46 +00001832 const auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001833 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00001834 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1835 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +00001836}
1837
Alexey Bataeved09d242014-05-28 05:53:51 +00001838void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001839
1840void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
1841 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001842 Scope *CurScope, SourceLocation Loc) {
1843 DSAStack->push(DKind, DirName, CurScope, Loc);
Faisal Valid143a0c2017-04-01 21:30:49 +00001844 PushExpressionEvaluationContext(
1845 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001846}
1847
Alexey Bataevaac108a2015-06-23 04:51:00 +00001848void Sema::StartOpenMPClause(OpenMPClauseKind K) {
1849 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001850}
1851
Alexey Bataevaac108a2015-06-23 04:51:00 +00001852void Sema::EndOpenMPClause() {
1853 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001854}
1855
Alexey Bataev758e55e2013-09-06 18:03:48 +00001856void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001857 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
1858 // A variable of class type (or array thereof) that appears in a lastprivate
1859 // clause requires an accessible, unambiguous default constructor for the
1860 // class type, unless the list item is also specified in a firstprivate
1861 // clause.
Alexey Bataeve3727102018-04-18 15:57:46 +00001862 if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
1863 for (OMPClause *C : D->clauses()) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001864 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
1865 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeve3727102018-04-18 15:57:46 +00001866 for (Expr *DE : Clause->varlists()) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001867 if (DE->isValueDependent() || DE->isTypeDependent()) {
1868 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001869 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +00001870 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00001871 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
Alexey Bataeve3727102018-04-18 15:57:46 +00001872 auto *VD = cast<VarDecl>(DRE->getDecl());
Alexey Bataev005248a2016-02-25 05:25:57 +00001873 QualType Type = VD->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +00001874 const DSAStackTy::DSAVarData DVar =
1875 DSAStack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001876 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001877 // Generate helper private variable and initialize it with the
1878 // default value. The address of the original variable is replaced
1879 // by the address of the new private variable in CodeGen. This new
1880 // variable is not added to IdResolver, so the code in the OpenMP
1881 // region uses original variable for proper diagnostics.
Alexey Bataeve3727102018-04-18 15:57:46 +00001882 VarDecl *VDPrivate = buildVarDecl(
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001883 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev63cc8e92018-03-20 14:45:59 +00001884 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE);
Richard Smith3beb7c62017-01-12 02:27:38 +00001885 ActOnUninitializedDecl(VDPrivate);
Alexey Bataev38e89532015-04-16 04:54:05 +00001886 if (VDPrivate->isInvalidDecl())
1887 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001888 PrivateCopies.push_back(buildDeclRefExpr(
1889 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001890 } else {
1891 // The variable is also a firstprivate, so initialization sequence
1892 // for private copy is generated already.
1893 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001894 }
1895 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001896 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001897 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001898 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001899 }
1900 }
1901 }
1902
Alexey Bataev758e55e2013-09-06 18:03:48 +00001903 DSAStack->pop();
1904 DiscardCleanupsInEvaluationContext();
1905 PopExpressionEvaluationContext();
1906}
1907
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001908static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1909 Expr *NumIterations, Sema &SemaRef,
1910 Scope *S, DSAStackTy *Stack);
Alexander Musman3276a272015-03-21 10:12:56 +00001911
Alexey Bataeva769e072013-03-22 06:34:35 +00001912namespace {
1913
Alexey Bataeve3727102018-04-18 15:57:46 +00001914class VarDeclFilterCCC final : public CorrectionCandidateCallback {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001915private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001916 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001917
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001918public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001919 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001920 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001921 NamedDecl *ND = Candidate.getCorrectionDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +00001922 if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001923 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001924 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1925 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001926 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001927 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001928 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001929};
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001930
Alexey Bataeve3727102018-04-18 15:57:46 +00001931class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001932private:
1933 Sema &SemaRef;
1934
1935public:
1936 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
1937 bool ValidateCandidate(const TypoCorrection &Candidate) override {
1938 NamedDecl *ND = Candidate.getCorrectionDecl();
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001939 if (ND && ((isa<VarDecl>(ND) && ND->getKind() == Decl::Var) ||
1940 isa<FunctionDecl>(ND))) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001941 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1942 SemaRef.getCurScope());
1943 }
1944 return false;
1945 }
1946};
1947
Alexey Bataeved09d242014-05-28 05:53:51 +00001948} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001949
1950ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1951 CXXScopeSpec &ScopeSpec,
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001952 const DeclarationNameInfo &Id,
1953 OpenMPDirectiveKind Kind) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001954 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1955 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1956
1957 if (Lookup.isAmbiguous())
1958 return ExprError();
1959
1960 VarDecl *VD;
1961 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001962 if (TypoCorrection Corrected = CorrectTypo(
1963 Id, LookupOrdinaryName, CurScope, nullptr,
1964 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001965 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001966 PDiag(Lookup.empty()
1967 ? diag::err_undeclared_var_use_suggest
1968 : diag::err_omp_expected_var_arg_suggest)
1969 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001970 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001971 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001972 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1973 : diag::err_omp_expected_var_arg)
1974 << Id.getName();
1975 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001976 }
Alexey Bataeve3727102018-04-18 15:57:46 +00001977 } else if (!(VD = Lookup.getAsSingle<VarDecl>())) {
1978 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
1979 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1980 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001981 }
1982 Lookup.suppressDiagnostics();
1983
1984 // OpenMP [2.9.2, Syntax, C/C++]
1985 // Variables must be file-scope, namespace-scope, or static block-scope.
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001986 if (Kind == OMPD_threadprivate && !VD->hasGlobalStorage()) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001987 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataev25ed0c02019-03-07 17:54:44 +00001988 << getOpenMPDirectiveName(Kind) << !VD->isStaticLocal();
Alexey Bataeved09d242014-05-28 05:53:51 +00001989 bool IsDecl =
1990 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001991 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001992 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1993 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001994 return ExprError();
1995 }
1996
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001997 VarDecl *CanonicalVD = VD->getCanonicalDecl();
George Burgess IV00f70bd2018-03-01 05:43:23 +00001998 NamedDecl *ND = CanonicalVD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001999 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
2000 // A threadprivate directive for file-scope variables must appear outside
2001 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00002002 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
2003 !getCurLexicalContext()->isTranslationUnit()) {
2004 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002005 << getOpenMPDirectiveName(Kind) << VD;
Alexey Bataeved09d242014-05-28 05:53:51 +00002006 bool IsDecl =
2007 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2008 Diag(VD->getLocation(),
2009 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2010 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00002011 return ExprError();
2012 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002013 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
2014 // A threadprivate directive for static class member variables must appear
2015 // in the class definition, in the same scope in which the member
2016 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00002017 if (CanonicalVD->isStaticDataMember() &&
2018 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
2019 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002020 << getOpenMPDirectiveName(Kind) << VD;
Alexey Bataeved09d242014-05-28 05:53:51 +00002021 bool IsDecl =
2022 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2023 Diag(VD->getLocation(),
2024 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2025 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00002026 return ExprError();
2027 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002028 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
2029 // A threadprivate directive for namespace-scope variables must appear
2030 // outside any definition or declaration other than the namespace
2031 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00002032 if (CanonicalVD->getDeclContext()->isNamespace() &&
2033 (!getCurLexicalContext()->isFileContext() ||
2034 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
2035 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002036 << getOpenMPDirectiveName(Kind) << VD;
Alexey Bataeved09d242014-05-28 05:53:51 +00002037 bool IsDecl =
2038 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2039 Diag(VD->getLocation(),
2040 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2041 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00002042 return ExprError();
2043 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002044 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
2045 // A threadprivate directive for static block-scope variables must appear
2046 // in the scope of the variable and not in a nested scope.
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002047 if (CanonicalVD->isLocalVarDecl() && CurScope &&
Alexey Bataev7d2960b2013-09-26 03:24:06 +00002048 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002049 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002050 << getOpenMPDirectiveName(Kind) << VD;
Alexey Bataeved09d242014-05-28 05:53:51 +00002051 bool IsDecl =
2052 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2053 Diag(VD->getLocation(),
2054 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2055 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002056 return ExprError();
2057 }
2058
2059 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
2060 // A threadprivate directive must lexically precede all references to any
2061 // of the variables in its list.
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002062 if (Kind == OMPD_threadprivate && VD->isUsed() &&
2063 !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002064 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002065 << getOpenMPDirectiveName(Kind) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002066 return ExprError();
2067 }
2068
2069 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev376b4a42016-02-09 09:41:09 +00002070 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
2071 SourceLocation(), VD,
2072 /*RefersToEnclosingVariableOrCapture=*/false,
2073 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002074}
2075
Alexey Bataeved09d242014-05-28 05:53:51 +00002076Sema::DeclGroupPtrTy
2077Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
2078 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002079 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00002080 CurContext->addDecl(D);
2081 return DeclGroupPtrTy::make(DeclGroupRef(D));
2082 }
David Blaikie0403cb12016-01-15 23:43:25 +00002083 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00002084}
2085
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002086namespace {
Alexey Bataeve3727102018-04-18 15:57:46 +00002087class LocalVarRefChecker final
2088 : public ConstStmtVisitor<LocalVarRefChecker, bool> {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002089 Sema &SemaRef;
2090
2091public:
2092 bool VisitDeclRefExpr(const DeclRefExpr *E) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002093 if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002094 if (VD->hasLocalStorage()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002095 SemaRef.Diag(E->getBeginLoc(),
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002096 diag::err_omp_local_var_in_threadprivate_init)
2097 << E->getSourceRange();
2098 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
2099 << VD << VD->getSourceRange();
2100 return true;
2101 }
2102 }
2103 return false;
2104 }
2105 bool VisitStmt(const Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002106 for (const Stmt *Child : S->children()) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002107 if (Child && Visit(Child))
2108 return true;
2109 }
2110 return false;
2111 }
Alexey Bataev23b69422014-06-18 07:08:49 +00002112 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002113};
2114} // namespace
2115
Alexey Bataeved09d242014-05-28 05:53:51 +00002116OMPThreadPrivateDecl *
2117Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002118 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +00002119 for (Expr *RefExpr : VarList) {
2120 auto *DE = cast<DeclRefExpr>(RefExpr);
2121 auto *VD = cast<VarDecl>(DE->getDecl());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002122 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00002123
Alexey Bataev376b4a42016-02-09 09:41:09 +00002124 // Mark variable as used.
2125 VD->setReferenced();
2126 VD->markUsed(Context);
2127
Alexey Bataevf56f98c2015-04-16 05:39:01 +00002128 QualType QType = VD->getType();
2129 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
2130 // It will be analyzed later.
2131 Vars.push_back(DE);
2132 continue;
2133 }
2134
Alexey Bataeva769e072013-03-22 06:34:35 +00002135 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
2136 // A threadprivate variable must not have an incomplete type.
2137 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002138 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00002139 continue;
2140 }
2141
2142 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
2143 // A threadprivate variable must not have a reference type.
2144 if (VD->getType()->isReferenceType()) {
2145 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00002146 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
2147 bool IsDecl =
2148 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2149 Diag(VD->getLocation(),
2150 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2151 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00002152 continue;
2153 }
2154
Samuel Antaof8b50122015-07-13 22:54:53 +00002155 // Check if this is a TLS variable. If TLS is not being supported, produce
2156 // the corresponding diagnostic.
2157 if ((VD->getTLSKind() != VarDecl::TLS_None &&
2158 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
2159 getLangOpts().OpenMPUseTLS &&
2160 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00002161 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
2162 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00002163 Diag(ILoc, diag::err_omp_var_thread_local)
2164 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00002165 bool IsDecl =
2166 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2167 Diag(VD->getLocation(),
2168 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2169 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00002170 continue;
2171 }
2172
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002173 // Check if initial value of threadprivate variable reference variable with
2174 // local storage (it is not supported by runtime).
Alexey Bataeve3727102018-04-18 15:57:46 +00002175 if (const Expr *Init = VD->getAnyInitializer()) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002176 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002177 if (Checker.Visit(Init))
2178 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002179 }
2180
Alexey Bataeved09d242014-05-28 05:53:51 +00002181 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00002182 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00002183 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
2184 Context, SourceRange(Loc, Loc)));
Alexey Bataeve3727102018-04-18 15:57:46 +00002185 if (ASTMutationListener *ML = Context.getASTMutationListener())
Alexey Bataev97720002014-11-11 04:05:39 +00002186 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00002187 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002188 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00002189 if (!Vars.empty()) {
2190 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
2191 Vars);
2192 D->setAccess(AS_public);
2193 }
2194 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00002195}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002196
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002197Sema::DeclGroupPtrTy Sema::ActOnOpenMPAllocateDirective(
2198 SourceLocation Loc, ArrayRef<Expr *> VarList,
2199 ArrayRef<OMPClause *> Clauses, DeclContext *Owner) {
2200 assert(Clauses.size() <= 1 && "Expected at most one clause.");
2201 Expr *Allocator = nullptr;
2202 if (!Clauses.empty())
2203 Allocator = cast<OMPAllocatorClause>(Clauses.back())->getAllocator();
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002204 SmallVector<Expr *, 8> Vars;
2205 for (Expr *RefExpr : VarList) {
2206 auto *DE = cast<DeclRefExpr>(RefExpr);
2207 auto *VD = cast<VarDecl>(DE->getDecl());
2208
2209 // Check if this is a TLS variable or global register.
2210 if (VD->getTLSKind() != VarDecl::TLS_None ||
2211 VD->hasAttr<OMPThreadPrivateDeclAttr>() ||
2212 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
2213 !VD->isLocalVarDecl()))
2214 continue;
2215 // Do not apply for parameters.
2216 if (isa<ParmVarDecl>(VD))
2217 continue;
2218
2219 Vars.push_back(RefExpr);
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002220 Attr *A = OMPAllocateDeclAttr::CreateImplicit(Context, Allocator,
2221 DE->getSourceRange());
2222 VD->addAttr(A);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002223 if (ASTMutationListener *ML = Context.getASTMutationListener())
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002224 ML->DeclarationMarkedOpenMPAllocate(VD, A);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002225 }
2226 if (Vars.empty())
2227 return nullptr;
2228 if (!Owner)
2229 Owner = getCurLexicalContext();
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00002230 OMPAllocateDecl *D =
2231 OMPAllocateDecl::Create(Context, Owner, Loc, Vars, Clauses);
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002232 D->setAccess(AS_public);
2233 Owner->addDecl(D);
2234 return DeclGroupPtrTy::make(DeclGroupRef(D));
2235}
2236
2237Sema::DeclGroupPtrTy
Kelvin Li1408f912018-09-26 04:28:39 +00002238Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc,
2239 ArrayRef<OMPClause *> ClauseList) {
2240 OMPRequiresDecl *D = nullptr;
2241 if (!CurContext->isFileContext()) {
2242 Diag(Loc, diag::err_omp_invalid_scope) << "requires";
2243 } else {
2244 D = CheckOMPRequiresDecl(Loc, ClauseList);
2245 if (D) {
2246 CurContext->addDecl(D);
2247 DSAStack->addRequiresDecl(D);
2248 }
2249 }
2250 return DeclGroupPtrTy::make(DeclGroupRef(D));
2251}
2252
2253OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc,
2254 ArrayRef<OMPClause *> ClauseList) {
2255 if (!DSAStack->hasDuplicateRequiresClause(ClauseList))
2256 return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc,
2257 ClauseList);
2258 return nullptr;
2259}
2260
Alexey Bataeve3727102018-04-18 15:57:46 +00002261static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
2262 const ValueDecl *D,
2263 const DSAStackTy::DSAVarData &DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00002264 bool IsLoopIterVar = false) {
2265 if (DVar.RefExpr) {
2266 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
2267 << getOpenMPClauseName(DVar.CKind);
2268 return;
2269 }
2270 enum {
2271 PDSA_StaticMemberShared,
2272 PDSA_StaticLocalVarShared,
2273 PDSA_LoopIterVarPrivate,
2274 PDSA_LoopIterVarLinear,
2275 PDSA_LoopIterVarLastprivate,
2276 PDSA_ConstVarShared,
2277 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002278 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00002279 PDSA_LocalVarPrivate,
2280 PDSA_Implicit
2281 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00002282 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002283 auto ReportLoc = D->getLocation();
2284 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002285 if (IsLoopIterVar) {
2286 if (DVar.CKind == OMPC_private)
2287 Reason = PDSA_LoopIterVarPrivate;
2288 else if (DVar.CKind == OMPC_lastprivate)
2289 Reason = PDSA_LoopIterVarLastprivate;
2290 else
2291 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev35aaee62016-04-13 13:36:48 +00002292 } else if (isOpenMPTaskingDirective(DVar.DKind) &&
2293 DVar.CKind == OMPC_firstprivate) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002294 Reason = PDSA_TaskVarFirstprivate;
2295 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002296 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00002297 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002298 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00002299 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002300 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00002301 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002302 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00002303 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002304 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00002305 ReportHint = true;
2306 Reason = PDSA_LocalVarPrivate;
2307 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00002308 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002309 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00002310 << Reason << ReportHint
2311 << getOpenMPDirectiveName(Stack->getCurrentDirective());
2312 } else if (DVar.ImplicitDSALoc.isValid()) {
2313 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
2314 << getOpenMPClauseName(DVar.CKind);
2315 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00002316}
2317
Alexey Bataev758e55e2013-09-06 18:03:48 +00002318namespace {
Alexey Bataeve3727102018-04-18 15:57:46 +00002319class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> {
Alexey Bataev758e55e2013-09-06 18:03:48 +00002320 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00002321 Sema &SemaRef;
Alexey Bataeve3727102018-04-18 15:57:46 +00002322 bool ErrorFound = false;
2323 CapturedStmt *CS = nullptr;
2324 llvm::SmallVector<Expr *, 4> ImplicitFirstprivate;
2325 llvm::SmallVector<Expr *, 4> ImplicitMap;
2326 Sema::VarsWithInheritedDSAType VarsWithInheritedDSA;
2327 llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations;
Alexey Bataeved09d242014-05-28 05:53:51 +00002328
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00002329 void VisitSubCaptures(OMPExecutableDirective *S) {
2330 // Check implicitly captured variables.
2331 if (!S->hasAssociatedStmt() || !S->getAssociatedStmt())
2332 return;
2333 for (const CapturedStmt::Capture &Cap :
2334 S->getInnermostCapturedStmt()->captures()) {
2335 if (!Cap.capturesVariable())
2336 continue;
2337 VarDecl *VD = Cap.getCapturedVar();
2338 // Do not try to map the variable if it or its sub-component was mapped
2339 // already.
2340 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) &&
2341 Stack->checkMappableExprComponentListsForDecl(
2342 VD, /*CurrentRegionOnly=*/true,
2343 [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
2344 OpenMPClauseKind) { return true; }))
2345 continue;
2346 DeclRefExpr *DRE = buildDeclRefExpr(
2347 SemaRef, VD, VD->getType().getNonLValueExprType(SemaRef.Context),
2348 Cap.getLocation(), /*RefersToCapture=*/true);
2349 Visit(DRE);
2350 }
2351 }
2352
Alexey Bataev758e55e2013-09-06 18:03:48 +00002353public:
2354 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00002355 if (E->isTypeDependent() || E->isValueDependent() ||
2356 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
2357 return;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002358 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00002359 VD = VD->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002360 // Skip internally declared variables.
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002361 if (VD->hasLocalStorage() && !CS->capturesVariable(VD))
Alexey Bataeved09d242014-05-28 05:53:51 +00002362 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002363
Alexey Bataeve3727102018-04-18 15:57:46 +00002364 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002365 // Check if the variable has explicit DSA set and stop analysis if it so.
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00002366 if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second)
David Majnemer9d168222016-08-05 17:44:54 +00002367 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002368
Alexey Bataevafe50572017-10-06 17:00:28 +00002369 // Skip internally declared static variables.
Alexey Bataev92327c52018-03-26 16:40:55 +00002370 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
Alexey Bataev97b72212018-08-14 18:31:20 +00002371 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
Alexey Bataev92327c52018-03-26 16:40:55 +00002372 if (VD->hasGlobalStorage() && !CS->capturesVariable(VD) &&
2373 (!Res || *Res != OMPDeclareTargetDeclAttr::MT_Link))
Alexey Bataevafe50572017-10-06 17:00:28 +00002374 return;
2375
Alexey Bataeve3727102018-04-18 15:57:46 +00002376 SourceLocation ELoc = E->getExprLoc();
2377 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002378 // The default(none) clause requires that each variable that is referenced
2379 // in the construct, and does not have a predetermined data-sharing
2380 // attribute, must have its data-sharing attribute explicitly determined
2381 // by being listed in a data-sharing attribute clause.
2382 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev7e6803e2019-01-09 15:58:05 +00002383 isImplicitOrExplicitTaskingRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00002384 VarsWithInheritedDSA.count(VD) == 0) {
2385 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002386 return;
2387 }
2388
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002389 if (isOpenMPTargetExecutionDirective(DKind) &&
2390 !Stack->isLoopControlVariable(VD).first) {
2391 if (!Stack->checkMappableExprComponentListsForDecl(
2392 VD, /*CurrentRegionOnly=*/true,
2393 [](OMPClauseMappableExprCommon::MappableExprComponentListRef
2394 StackComponents,
2395 OpenMPClauseKind) {
2396 // Variable is used if it has been marked as an array, array
2397 // section or the variable iself.
2398 return StackComponents.size() == 1 ||
2399 std::all_of(
2400 std::next(StackComponents.rbegin()),
2401 StackComponents.rend(),
2402 [](const OMPClauseMappableExprCommon::
2403 MappableComponent &MC) {
2404 return MC.getAssociatedDeclaration() ==
2405 nullptr &&
2406 (isa<OMPArraySectionExpr>(
2407 MC.getAssociatedExpression()) ||
2408 isa<ArraySubscriptExpr>(
2409 MC.getAssociatedExpression()));
2410 });
2411 })) {
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002412 bool IsFirstprivate = false;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002413 // By default lambdas are captured as firstprivates.
2414 if (const auto *RD =
2415 VD->getType().getNonReferenceType()->getAsCXXRecordDecl())
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002416 IsFirstprivate = RD->isLambda();
2417 IsFirstprivate =
2418 IsFirstprivate ||
2419 (VD->getType().getNonReferenceType()->isScalarType() &&
Alexey Bataev92327c52018-03-26 16:40:55 +00002420 Stack->getDefaultDMA() != DMA_tofrom_scalar && !Res);
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002421 if (IsFirstprivate)
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002422 ImplicitFirstprivate.emplace_back(E);
2423 else
2424 ImplicitMap.emplace_back(E);
2425 return;
2426 }
2427 }
2428
Alexey Bataev758e55e2013-09-06 18:03:48 +00002429 // OpenMP [2.9.3.6, Restrictions, p.2]
2430 // A list item that appears in a reduction clause of the innermost
2431 // enclosing worksharing or parallel construct may not be accessed in an
2432 // explicit task.
Alexey Bataev7ace49d2016-05-17 08:55:33 +00002433 DVar = Stack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00002434 VD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
2435 [](OpenMPDirectiveKind K) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00002436 return isOpenMPParallelDirective(K) ||
2437 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
2438 },
Alexey Bataeveffbdf12017-07-21 17:24:30 +00002439 /*FromParent=*/true);
Alexey Bataev35aaee62016-04-13 13:36:48 +00002440 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00002441 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00002442 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
Alexey Bataeve3727102018-04-18 15:57:46 +00002443 reportOriginalDsa(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002444 return;
2445 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002446
2447 // Define implicit data-sharing attributes for task.
Alexey Bataeve3727102018-04-18 15:57:46 +00002448 DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00002449 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
Alexey Bataeva495c642019-03-11 19:51:42 +00002450 !Stack->isLoopControlVariable(VD).first) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002451 ImplicitFirstprivate.push_back(E);
Alexey Bataeva495c642019-03-11 19:51:42 +00002452 return;
2453 }
2454
2455 // Store implicitly used globals with declare target link for parent
2456 // target.
2457 if (!isOpenMPTargetExecutionDirective(DKind) && Res &&
2458 *Res == OMPDeclareTargetDeclAttr::MT_Link) {
2459 Stack->addToParentTargetRegionLinkGlobals(E);
2460 return;
2461 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002462 }
2463 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002464 void VisitMemberExpr(MemberExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00002465 if (E->isTypeDependent() || E->isValueDependent() ||
2466 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
2467 return;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002468 auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002469 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
Patrick Lystere13b1e32019-01-02 19:28:48 +00002470 if (auto *TE = dyn_cast<CXXThisExpr>(E->getBase()->IgnoreParens())) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002471 if (!FD)
2472 return;
Alexey Bataeve3727102018-04-18 15:57:46 +00002473 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002474 // Check if the variable has explicit DSA set and stop analysis if it
2475 // so.
2476 if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second)
2477 return;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002478
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002479 if (isOpenMPTargetExecutionDirective(DKind) &&
2480 !Stack->isLoopControlVariable(FD).first &&
2481 !Stack->checkMappableExprComponentListsForDecl(
2482 FD, /*CurrentRegionOnly=*/true,
2483 [](OMPClauseMappableExprCommon::MappableExprComponentListRef
2484 StackComponents,
2485 OpenMPClauseKind) {
2486 return isa<CXXThisExpr>(
2487 cast<MemberExpr>(
2488 StackComponents.back().getAssociatedExpression())
2489 ->getBase()
2490 ->IgnoreParens());
2491 })) {
2492 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
2493 // A bit-field cannot appear in a map clause.
2494 //
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002495 if (FD->isBitField())
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002496 return;
Patrick Lystere13b1e32019-01-02 19:28:48 +00002497
2498 // Check to see if the member expression is referencing a class that
2499 // has already been explicitly mapped
2500 if (Stack->isClassPreviouslyMapped(TE->getType()))
2501 return;
2502
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002503 ImplicitMap.emplace_back(E);
2504 return;
2505 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002506
Alexey Bataeve3727102018-04-18 15:57:46 +00002507 SourceLocation ELoc = E->getExprLoc();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002508 // OpenMP [2.9.3.6, Restrictions, p.2]
2509 // A list item that appears in a reduction clause of the innermost
2510 // enclosing worksharing or parallel construct may not be accessed in
2511 // an explicit task.
2512 DVar = Stack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00002513 FD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
2514 [](OpenMPDirectiveKind K) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002515 return isOpenMPParallelDirective(K) ||
2516 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
2517 },
2518 /*FromParent=*/true);
2519 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
2520 ErrorFound = true;
2521 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
Alexey Bataeve3727102018-04-18 15:57:46 +00002522 reportOriginalDsa(SemaRef, Stack, FD, DVar);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002523 return;
2524 }
2525
2526 // Define implicit data-sharing attributes for task.
Alexey Bataeve3727102018-04-18 15:57:46 +00002527 DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002528 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
Alexey Bataevb40e05202018-10-24 18:53:12 +00002529 !Stack->isLoopControlVariable(FD).first) {
2530 // Check if there is a captured expression for the current field in the
2531 // region. Do not mark it as firstprivate unless there is no captured
2532 // expression.
2533 // TODO: try to make it firstprivate.
2534 if (DVar.CKind != OMPC_unknown)
2535 ImplicitFirstprivate.push_back(E);
2536 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002537 return;
2538 }
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002539 if (isOpenMPTargetExecutionDirective(DKind)) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002540 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
Alexey Bataeve3727102018-04-18 15:57:46 +00002541 if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map,
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002542 /*NoDiagnose=*/true))
Alexey Bataev27041fa2017-12-05 15:22:49 +00002543 return;
Alexey Bataeve3727102018-04-18 15:57:46 +00002544 const auto *VD = cast<ValueDecl>(
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002545 CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl());
2546 if (!Stack->checkMappableExprComponentListsForDecl(
2547 VD, /*CurrentRegionOnly=*/true,
2548 [&CurComponents](
2549 OMPClauseMappableExprCommon::MappableExprComponentListRef
2550 StackComponents,
2551 OpenMPClauseKind) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002552 auto CCI = CurComponents.rbegin();
Alexey Bataev5ec38932017-09-26 16:19:04 +00002553 auto CCE = CurComponents.rend();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002554 for (const auto &SC : llvm::reverse(StackComponents)) {
2555 // Do both expressions have the same kind?
2556 if (CCI->getAssociatedExpression()->getStmtClass() !=
2557 SC.getAssociatedExpression()->getStmtClass())
2558 if (!(isa<OMPArraySectionExpr>(
2559 SC.getAssociatedExpression()) &&
2560 isa<ArraySubscriptExpr>(
2561 CCI->getAssociatedExpression())))
2562 return false;
2563
Alexey Bataeve3727102018-04-18 15:57:46 +00002564 const Decl *CCD = CCI->getAssociatedDeclaration();
2565 const Decl *SCD = SC.getAssociatedDeclaration();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002566 CCD = CCD ? CCD->getCanonicalDecl() : nullptr;
2567 SCD = SCD ? SCD->getCanonicalDecl() : nullptr;
2568 if (SCD != CCD)
2569 return false;
2570 std::advance(CCI, 1);
Alexey Bataev5ec38932017-09-26 16:19:04 +00002571 if (CCI == CCE)
2572 break;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002573 }
2574 return true;
2575 })) {
2576 Visit(E->getBase());
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002577 }
Alexey Bataeve3727102018-04-18 15:57:46 +00002578 } else {
Alexey Bataev7fcacd82016-11-28 15:55:15 +00002579 Visit(E->getBase());
Alexey Bataeve3727102018-04-18 15:57:46 +00002580 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002581 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002582 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002583 for (OMPClause *C : S->clauses()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002584 // Skip analysis of arguments of implicitly defined firstprivate clause
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002585 // for task|target directives.
2586 // Skip analysis of arguments of implicitly defined map clause for target
2587 // directives.
2588 if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) &&
2589 C->isImplicit())) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002590 for (Stmt *CC : C->children()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002591 if (CC)
2592 Visit(CC);
2593 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002594 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002595 }
Alexey Bataevf07946e2018-10-29 20:17:42 +00002596 // Check implicitly captured variables.
2597 VisitSubCaptures(S);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002598 }
2599 void VisitStmt(Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002600 for (Stmt *C : S->children()) {
Alexey Bataev8fc7b5f2018-10-25 15:35:27 +00002601 if (C) {
Joel E. Denny0fdf5a92018-12-19 15:59:47 +00002602 // Check implicitly captured variables in the task-based directives to
2603 // check if they must be firstprivatized.
2604 Visit(C);
Alexey Bataev8fc7b5f2018-10-25 15:35:27 +00002605 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002606 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002607 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002608
Alexey Bataeve3727102018-04-18 15:57:46 +00002609 bool isErrorFound() const { return ErrorFound; }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002610 ArrayRef<Expr *> getImplicitFirstprivate() const {
2611 return ImplicitFirstprivate;
2612 }
2613 ArrayRef<Expr *> getImplicitMap() const { return ImplicitMap; }
Alexey Bataeve3727102018-04-18 15:57:46 +00002614 const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const {
Alexey Bataev4acb8592014-07-07 13:01:15 +00002615 return VarsWithInheritedDSA;
2616 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002617
Alexey Bataev7ff55242014-06-19 09:13:45 +00002618 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
Alexey Bataeva495c642019-03-11 19:51:42 +00002619 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {
2620 // Process declare target link variables for the target directives.
2621 if (isOpenMPTargetExecutionDirective(S->getCurrentDirective())) {
2622 for (DeclRefExpr *E : Stack->getLinkGlobals())
2623 Visit(E);
2624 }
2625 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002626};
Alexey Bataeved09d242014-05-28 05:53:51 +00002627} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00002628
Alexey Bataevbae9a792014-06-27 10:37:06 +00002629void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00002630 switch (DKind) {
Kelvin Li70a12c52016-07-13 21:51:49 +00002631 case OMPD_parallel:
2632 case OMPD_parallel_for:
2633 case OMPD_parallel_for_simd:
2634 case OMPD_parallel_sections:
Carlo Bertolliba1487b2017-10-04 14:12:09 +00002635 case OMPD_teams:
Alexey Bataev999277a2017-12-06 14:31:09 +00002636 case OMPD_teams_distribute:
2637 case OMPD_teams_distribute_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002638 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Alexey Bataev2377fe92015-09-10 08:12:02 +00002639 QualType KmpInt32PtrTy =
2640 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002641 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002642 std::make_pair(".global_tid.", KmpInt32PtrTy),
2643 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2644 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00002645 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00002646 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2647 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00002648 break;
2649 }
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002650 case OMPD_target_teams:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00002651 case OMPD_target_parallel:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00002652 case OMPD_target_parallel_for:
Alexey Bataevdfa430f2017-12-08 15:03:50 +00002653 case OMPD_target_parallel_for_simd:
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00002654 case OMPD_target_teams_distribute:
2655 case OMPD_target_teams_distribute_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002656 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2657 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2658 QualType KmpInt32PtrTy =
2659 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2660 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00002661 FunctionProtoType::ExtProtoInfo EPI;
2662 EPI.Variadic = true;
2663 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2664 Sema::CapturedParamNameType Params[] = {
2665 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002666 std::make_pair(".part_id.", KmpInt32PtrTy),
2667 std::make_pair(".privates.", VoidPtrTy),
2668 std::make_pair(
2669 ".copy_fn.",
2670 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00002671 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2672 std::make_pair(StringRef(), QualType()) // __context with shared vars
2673 };
2674 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2675 Params);
Alexey Bataev0c869ef2018-01-16 15:57:07 +00002676 // Mark this captured region as inlined, because we don't use outlined
2677 // function directly.
2678 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2679 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002680 Context, AlwaysInlineAttr::Keyword_forceinline));
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002681 Sema::CapturedParamNameType ParamsTarget[] = {
2682 std::make_pair(StringRef(), QualType()) // __context with shared vars
2683 };
2684 // Start a captured region for 'target' with no implicit parameters.
2685 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2686 ParamsTarget);
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002687 Sema::CapturedParamNameType ParamsTeamsOrParallel[] = {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002688 std::make_pair(".global_tid.", KmpInt32PtrTy),
2689 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2690 std::make_pair(StringRef(), QualType()) // __context with shared vars
2691 };
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002692 // Start a captured region for 'teams' or 'parallel'. Both regions have
2693 // the same implicit parameters.
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002694 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002695 ParamsTeamsOrParallel);
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002696 break;
2697 }
Alexey Bataev8451efa2018-01-15 19:06:12 +00002698 case OMPD_target:
2699 case OMPD_target_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002700 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2701 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2702 QualType KmpInt32PtrTy =
2703 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2704 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00002705 FunctionProtoType::ExtProtoInfo EPI;
2706 EPI.Variadic = true;
2707 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2708 Sema::CapturedParamNameType Params[] = {
2709 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002710 std::make_pair(".part_id.", KmpInt32PtrTy),
2711 std::make_pair(".privates.", VoidPtrTy),
2712 std::make_pair(
2713 ".copy_fn.",
2714 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00002715 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2716 std::make_pair(StringRef(), QualType()) // __context with shared vars
2717 };
2718 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2719 Params);
2720 // Mark this captured region as inlined, because we don't use outlined
2721 // function directly.
2722 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2723 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002724 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev8451efa2018-01-15 19:06:12 +00002725 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2726 std::make_pair(StringRef(), QualType()));
2727 break;
2728 }
Kelvin Li70a12c52016-07-13 21:51:49 +00002729 case OMPD_simd:
2730 case OMPD_for:
2731 case OMPD_for_simd:
2732 case OMPD_sections:
2733 case OMPD_section:
2734 case OMPD_single:
2735 case OMPD_master:
2736 case OMPD_critical:
Kelvin Lia579b912016-07-14 02:54:56 +00002737 case OMPD_taskgroup:
2738 case OMPD_distribute:
Alexey Bataev46506272017-12-05 17:41:34 +00002739 case OMPD_distribute_simd:
Kelvin Li70a12c52016-07-13 21:51:49 +00002740 case OMPD_ordered:
2741 case OMPD_atomic:
Alexey Bataev8451efa2018-01-15 19:06:12 +00002742 case OMPD_target_data: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002743 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002744 std::make_pair(StringRef(), QualType()) // __context with shared vars
2745 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00002746 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2747 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002748 break;
2749 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002750 case OMPD_task: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002751 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2752 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2753 QualType KmpInt32PtrTy =
2754 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2755 QualType Args[] = {VoidPtrTy};
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002756 FunctionProtoType::ExtProtoInfo EPI;
2757 EPI.Variadic = true;
2758 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002759 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00002760 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002761 std::make_pair(".part_id.", KmpInt32PtrTy),
2762 std::make_pair(".privates.", VoidPtrTy),
2763 std::make_pair(
2764 ".copy_fn.",
2765 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev48591dd2016-04-20 04:01:36 +00002766 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002767 std::make_pair(StringRef(), QualType()) // __context with shared vars
2768 };
2769 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2770 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00002771 // Mark this captured region as inlined, because we don't use outlined
2772 // function directly.
2773 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2774 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002775 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002776 break;
2777 }
Alexey Bataev1e73ef32016-04-28 12:14:51 +00002778 case OMPD_taskloop:
2779 case OMPD_taskloop_simd: {
Alexey Bataev7292c292016-04-25 12:22:29 +00002780 QualType KmpInt32Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002781 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1)
2782 .withConst();
Alexey Bataev7292c292016-04-25 12:22:29 +00002783 QualType KmpUInt64Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002784 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0)
2785 .withConst();
Alexey Bataev7292c292016-04-25 12:22:29 +00002786 QualType KmpInt64Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002787 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1)
2788 .withConst();
2789 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2790 QualType KmpInt32PtrTy =
2791 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2792 QualType Args[] = {VoidPtrTy};
Alexey Bataev7292c292016-04-25 12:22:29 +00002793 FunctionProtoType::ExtProtoInfo EPI;
2794 EPI.Variadic = true;
2795 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev49f6e782015-12-01 04:18:41 +00002796 Sema::CapturedParamNameType Params[] = {
Alexey Bataev7292c292016-04-25 12:22:29 +00002797 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002798 std::make_pair(".part_id.", KmpInt32PtrTy),
2799 std::make_pair(".privates.", VoidPtrTy),
Alexey Bataev7292c292016-04-25 12:22:29 +00002800 std::make_pair(
2801 ".copy_fn.",
2802 Context.getPointerType(CopyFnType).withConst().withRestrict()),
2803 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2804 std::make_pair(".lb.", KmpUInt64Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002805 std::make_pair(".ub.", KmpUInt64Ty),
2806 std::make_pair(".st.", KmpInt64Ty),
Alexey Bataev7292c292016-04-25 12:22:29 +00002807 std::make_pair(".liter.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002808 std::make_pair(".reductions.", VoidPtrTy),
Alexey Bataev49f6e782015-12-01 04:18:41 +00002809 std::make_pair(StringRef(), QualType()) // __context with shared vars
2810 };
2811 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2812 Params);
Alexey Bataev7292c292016-04-25 12:22:29 +00002813 // Mark this captured region as inlined, because we don't use outlined
2814 // function directly.
2815 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2816 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002817 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev49f6e782015-12-01 04:18:41 +00002818 break;
2819 }
Kelvin Li4a39add2016-07-05 05:00:15 +00002820 case OMPD_distribute_parallel_for_simd:
Alexey Bataev647dd842018-01-15 20:59:40 +00002821 case OMPD_distribute_parallel_for: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002822 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli9925f152016-06-27 14:55:37 +00002823 QualType KmpInt32PtrTy =
2824 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2825 Sema::CapturedParamNameType Params[] = {
2826 std::make_pair(".global_tid.", KmpInt32PtrTy),
2827 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002828 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2829 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli9925f152016-06-27 14:55:37 +00002830 std::make_pair(StringRef(), QualType()) // __context with shared vars
2831 };
2832 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2833 Params);
2834 break;
2835 }
Alexey Bataev647dd842018-01-15 20:59:40 +00002836 case OMPD_target_teams_distribute_parallel_for:
2837 case OMPD_target_teams_distribute_parallel_for_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002838 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli52978c32018-01-03 21:12:44 +00002839 QualType KmpInt32PtrTy =
2840 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002841 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
Carlo Bertolli52978c32018-01-03 21:12:44 +00002842
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002843 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00002844 FunctionProtoType::ExtProtoInfo EPI;
2845 EPI.Variadic = true;
2846 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2847 Sema::CapturedParamNameType Params[] = {
2848 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002849 std::make_pair(".part_id.", KmpInt32PtrTy),
2850 std::make_pair(".privates.", VoidPtrTy),
2851 std::make_pair(
2852 ".copy_fn.",
2853 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00002854 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2855 std::make_pair(StringRef(), QualType()) // __context with shared vars
2856 };
2857 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2858 Params);
Alexey Bataev9f9fb0b2018-01-16 19:02:33 +00002859 // Mark this captured region as inlined, because we don't use outlined
2860 // function directly.
2861 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2862 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002863 Context, AlwaysInlineAttr::Keyword_forceinline));
Carlo Bertolli52978c32018-01-03 21:12:44 +00002864 Sema::CapturedParamNameType ParamsTarget[] = {
2865 std::make_pair(StringRef(), QualType()) // __context with shared vars
2866 };
2867 // Start a captured region for 'target' with no implicit parameters.
2868 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2869 ParamsTarget);
2870
2871 Sema::CapturedParamNameType ParamsTeams[] = {
2872 std::make_pair(".global_tid.", KmpInt32PtrTy),
2873 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2874 std::make_pair(StringRef(), QualType()) // __context with shared vars
2875 };
2876 // Start a captured region for 'target' with no implicit parameters.
2877 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2878 ParamsTeams);
2879
2880 Sema::CapturedParamNameType ParamsParallel[] = {
2881 std::make_pair(".global_tid.", KmpInt32PtrTy),
2882 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002883 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2884 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli52978c32018-01-03 21:12:44 +00002885 std::make_pair(StringRef(), QualType()) // __context with shared vars
2886 };
2887 // Start a captured region for 'teams' or 'parallel'. Both regions have
2888 // the same implicit parameters.
2889 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2890 ParamsParallel);
2891 break;
2892 }
2893
Alexey Bataev46506272017-12-05 17:41:34 +00002894 case OMPD_teams_distribute_parallel_for:
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00002895 case OMPD_teams_distribute_parallel_for_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002896 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli62fae152017-11-20 20:46:39 +00002897 QualType KmpInt32PtrTy =
2898 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2899
2900 Sema::CapturedParamNameType ParamsTeams[] = {
2901 std::make_pair(".global_tid.", KmpInt32PtrTy),
2902 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2903 std::make_pair(StringRef(), QualType()) // __context with shared vars
2904 };
2905 // Start a captured region for 'target' with no implicit parameters.
2906 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2907 ParamsTeams);
2908
2909 Sema::CapturedParamNameType ParamsParallel[] = {
2910 std::make_pair(".global_tid.", KmpInt32PtrTy),
2911 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002912 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2913 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli62fae152017-11-20 20:46:39 +00002914 std::make_pair(StringRef(), QualType()) // __context with shared vars
2915 };
2916 // Start a captured region for 'teams' or 'parallel'. Both regions have
2917 // the same implicit parameters.
2918 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2919 ParamsParallel);
2920 break;
2921 }
Alexey Bataev7828b252017-11-21 17:08:48 +00002922 case OMPD_target_update:
2923 case OMPD_target_enter_data:
2924 case OMPD_target_exit_data: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002925 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2926 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2927 QualType KmpInt32PtrTy =
2928 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2929 QualType Args[] = {VoidPtrTy};
Alexey Bataev7828b252017-11-21 17:08:48 +00002930 FunctionProtoType::ExtProtoInfo EPI;
2931 EPI.Variadic = true;
2932 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2933 Sema::CapturedParamNameType Params[] = {
2934 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002935 std::make_pair(".part_id.", KmpInt32PtrTy),
2936 std::make_pair(".privates.", VoidPtrTy),
2937 std::make_pair(
2938 ".copy_fn.",
2939 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev7828b252017-11-21 17:08:48 +00002940 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2941 std::make_pair(StringRef(), QualType()) // __context with shared vars
2942 };
2943 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2944 Params);
2945 // Mark this captured region as inlined, because we don't use outlined
2946 // function directly.
2947 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2948 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002949 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev7828b252017-11-21 17:08:48 +00002950 break;
2951 }
Alexey Bataev9959db52014-05-06 10:08:46 +00002952 case OMPD_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +00002953 case OMPD_allocate:
Alexey Bataevee9af452014-11-21 11:33:46 +00002954 case OMPD_taskyield:
2955 case OMPD_barrier:
2956 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002957 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00002958 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00002959 case OMPD_flush:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002960 case OMPD_declare_reduction:
Michael Kruse251e1482019-02-01 20:25:04 +00002961 case OMPD_declare_mapper:
Alexey Bataev587e1de2016-03-30 10:43:55 +00002962 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00002963 case OMPD_declare_target:
2964 case OMPD_end_declare_target:
Kelvin Li1408f912018-09-26 04:28:39 +00002965 case OMPD_requires:
Alexey Bataev9959db52014-05-06 10:08:46 +00002966 llvm_unreachable("OpenMP Directive is not allowed");
2967 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00002968 llvm_unreachable("Unknown OpenMP directive");
2969 }
2970}
2971
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002972int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) {
2973 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2974 getOpenMPCaptureRegions(CaptureRegions, DKind);
2975 return CaptureRegions.size();
2976}
2977
Alexey Bataev3392d762016-02-16 11:18:12 +00002978static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00002979 Expr *CaptureExpr, bool WithInit,
2980 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00002981 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00002982 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00002983 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00002984 QualType Ty = Init->getType();
2985 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002986 if (S.getLangOpts().CPlusPlus) {
Alexey Bataev4244be22016-02-11 05:35:55 +00002987 Ty = C.getLValueReferenceType(Ty);
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002988 } else {
Alexey Bataev4244be22016-02-11 05:35:55 +00002989 Ty = C.getPointerType(Ty);
2990 ExprResult Res =
2991 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
2992 if (!Res.isUsable())
2993 return nullptr;
2994 Init = Res.get();
2995 }
Alexey Bataev61205072016-03-02 04:57:40 +00002996 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00002997 }
Alexey Bataeva7206b92016-12-20 16:51:02 +00002998 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty,
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002999 CaptureExpr->getBeginLoc());
Alexey Bataev2bbf7212016-03-03 03:52:24 +00003000 if (!WithInit)
Alexey Bataeve3727102018-04-18 15:57:46 +00003001 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C));
Alexey Bataev4244be22016-02-11 05:35:55 +00003002 S.CurContext->addHiddenDecl(CED);
Richard Smith3beb7c62017-01-12 02:27:38 +00003003 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00003004 return CED;
3005}
3006
Alexey Bataev61205072016-03-02 04:57:40 +00003007static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
3008 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00003009 OMPCapturedExprDecl *CD;
Alexey Bataeve3727102018-04-18 15:57:46 +00003010 if (VarDecl *VD = S.isOpenMPCapturedDecl(D))
Alexey Bataevb7a34b62016-02-25 03:59:29 +00003011 CD = cast<OMPCapturedExprDecl>(VD);
Alexey Bataeve3727102018-04-18 15:57:46 +00003012 else
Alexey Bataev5a3af132016-03-29 08:58:54 +00003013 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
3014 /*AsExpression=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00003015 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00003016 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00003017}
3018
Alexey Bataev5a3af132016-03-29 08:58:54 +00003019static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00003020 CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00003021 if (!Ref) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00003022 OMPCapturedExprDecl *CD = buildCaptureDecl(
3023 S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr,
3024 /*WithInit=*/true, /*AsExpression=*/true);
Alexey Bataev5a3af132016-03-29 08:58:54 +00003025 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
3026 CaptureExpr->getExprLoc());
3027 }
3028 ExprResult Res = Ref;
3029 if (!S.getLangOpts().CPlusPlus &&
3030 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
Alexey Bataev8e769ee2017-12-22 21:01:52 +00003031 Ref->getType()->isPointerType()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00003032 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
Alexey Bataev8e769ee2017-12-22 21:01:52 +00003033 if (!Res.isUsable())
3034 return ExprError();
3035 }
3036 return S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00003037}
3038
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003039namespace {
3040// OpenMP directives parsed in this section are represented as a
3041// CapturedStatement with an associated statement. If a syntax error
3042// is detected during the parsing of the associated statement, the
3043// compiler must abort processing and close the CapturedStatement.
3044//
3045// Combined directives such as 'target parallel' have more than one
3046// nested CapturedStatements. This RAII ensures that we unwind out
3047// of all the nested CapturedStatements when an error is found.
3048class CaptureRegionUnwinderRAII {
3049private:
3050 Sema &S;
3051 bool &ErrorFound;
Alexey Bataeve3727102018-04-18 15:57:46 +00003052 OpenMPDirectiveKind DKind = OMPD_unknown;
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003053
3054public:
3055 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound,
3056 OpenMPDirectiveKind DKind)
3057 : S(S), ErrorFound(ErrorFound), DKind(DKind) {}
3058 ~CaptureRegionUnwinderRAII() {
3059 if (ErrorFound) {
3060 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind);
3061 while (--ThisCaptureLevel >= 0)
3062 S.ActOnCapturedRegionError();
3063 }
3064 }
3065};
3066} // namespace
3067
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00003068StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
3069 ArrayRef<OMPClause *> Clauses) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003070 bool ErrorFound = false;
3071 CaptureRegionUnwinderRAII CaptureRegionUnwinder(
3072 *this, ErrorFound, DSAStack->getCurrentDirective());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00003073 if (!S.isUsable()) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003074 ErrorFound = true;
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00003075 return StmtError();
3076 }
Alexey Bataev993d2802015-12-28 06:23:08 +00003077
Alexey Bataev2ba67042017-11-28 21:11:44 +00003078 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
3079 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective());
Alexey Bataev993d2802015-12-28 06:23:08 +00003080 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00003081 OMPScheduleClause *SC = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +00003082 SmallVector<const OMPLinearClause *, 4> LCs;
3083 SmallVector<const OMPClauseWithPreInit *, 4> PICs;
Alexey Bataev040d5402015-05-12 08:35:28 +00003084 // This is required for proper codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00003085 for (OMPClause *Clause : Clauses) {
Alexey Bataev88202be2017-07-27 13:20:36 +00003086 if (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) &&
3087 Clause->getClauseKind() == OMPC_in_reduction) {
3088 // Capture taskgroup task_reduction descriptors inside the tasking regions
3089 // with the corresponding in_reduction items.
3090 auto *IRC = cast<OMPInReductionClause>(Clause);
Alexey Bataeve3727102018-04-18 15:57:46 +00003091 for (Expr *E : IRC->taskgroup_descriptors())
Alexey Bataev88202be2017-07-27 13:20:36 +00003092 if (E)
3093 MarkDeclarationsReferencedInExpr(E);
3094 }
Alexey Bataev16dc7b62015-05-20 03:46:04 +00003095 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00003096 Clause->getClauseKind() == OMPC_copyprivate ||
3097 (getLangOpts().OpenMPUseTLS &&
3098 getASTContext().getTargetInfo().isTLSSupported() &&
3099 Clause->getClauseKind() == OMPC_copyin)) {
3100 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00003101 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeve3727102018-04-18 15:57:46 +00003102 for (Stmt *VarRef : Clause->children()) {
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00003103 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00003104 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00003105 }
3106 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00003107 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev2ba67042017-11-28 21:11:44 +00003108 } else if (CaptureRegions.size() > 1 ||
3109 CaptureRegions.back() != OMPD_unknown) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00003110 if (auto *C = OMPClauseWithPreInit::get(Clause))
3111 PICs.push_back(C);
Alexey Bataev005248a2016-02-25 05:25:57 +00003112 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003113 if (Expr *E = C->getPostUpdateExpr())
Alexey Bataev005248a2016-02-25 05:25:57 +00003114 MarkDeclarationsReferencedInExpr(E);
3115 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00003116 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00003117 if (Clause->getClauseKind() == OMPC_schedule)
3118 SC = cast<OMPScheduleClause>(Clause);
3119 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00003120 OC = cast<OMPOrderedClause>(Clause);
3121 else if (Clause->getClauseKind() == OMPC_linear)
3122 LCs.push_back(cast<OMPLinearClause>(Clause));
3123 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00003124 // OpenMP, 2.7.1 Loop Construct, Restrictions
3125 // The nonmonotonic modifier cannot be specified if an ordered clause is
3126 // specified.
3127 if (SC &&
3128 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
3129 SC->getSecondScheduleModifier() ==
3130 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
3131 OC) {
3132 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
3133 ? SC->getFirstScheduleModifierLoc()
3134 : SC->getSecondScheduleModifierLoc(),
3135 diag::err_omp_schedule_nonmonotonic_ordered)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00003136 << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
Alexey Bataev6402bca2015-12-28 07:25:51 +00003137 ErrorFound = true;
3138 }
Alexey Bataev993d2802015-12-28 06:23:08 +00003139 if (!LCs.empty() && OC && OC->getNumForLoops()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003140 for (const OMPLinearClause *C : LCs) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003141 Diag(C->getBeginLoc(), diag::err_omp_linear_ordered)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00003142 << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
Alexey Bataev993d2802015-12-28 06:23:08 +00003143 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00003144 ErrorFound = true;
3145 }
Alexey Bataev113438c2015-12-30 12:06:23 +00003146 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
3147 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
3148 OC->getNumForLoops()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003149 Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd)
Alexey Bataev113438c2015-12-30 12:06:23 +00003150 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
3151 ErrorFound = true;
3152 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00003153 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00003154 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00003155 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003156 StmtResult SR = S;
Alexey Bataev2ba67042017-11-28 21:11:44 +00003157 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00003158 // Mark all variables in private list clauses as used in inner region.
3159 // Required for proper codegen of combined directives.
3160 // TODO: add processing for other clauses.
Alexey Bataev2ba67042017-11-28 21:11:44 +00003161 if (ThisCaptureRegion != OMPD_unknown) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003162 for (const clang::OMPClauseWithPreInit *C : PICs) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00003163 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion();
3164 // Find the particular capture region for the clause if the
3165 // directive is a combined one with multiple capture regions.
3166 // If the directive is not a combined one, the capture region
3167 // associated with the clause is OMPD_unknown and is generated
3168 // only once.
3169 if (CaptureRegion == ThisCaptureRegion ||
3170 CaptureRegion == OMPD_unknown) {
3171 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003172 for (Decl *D : DS->decls())
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00003173 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
3174 }
3175 }
3176 }
3177 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003178 SR = ActOnCapturedRegionEnd(SR.get());
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00003179 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003180 return SR;
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00003181}
3182
Jonas Hahnfeld64a9e3c2017-02-22 06:49:10 +00003183static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion,
3184 OpenMPDirectiveKind CancelRegion,
3185 SourceLocation StartLoc) {
3186 // CancelRegion is only needed for cancel and cancellation_point.
3187 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point)
3188 return false;
3189
3190 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for ||
3191 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup)
3192 return false;
3193
3194 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region)
3195 << getOpenMPDirectiveName(CancelRegion);
3196 return true;
3197}
3198
Alexey Bataeve3727102018-04-18 15:57:46 +00003199static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003200 OpenMPDirectiveKind CurrentRegion,
3201 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003202 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003203 SourceLocation StartLoc) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003204 if (Stack->getCurScope()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003205 OpenMPDirectiveKind ParentRegion = Stack->getParentDirective();
3206 OpenMPDirectiveKind OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003207 bool NestingProhibited = false;
3208 bool CloseNesting = true;
David Majnemer9d168222016-08-05 17:44:54 +00003209 bool OrphanSeen = false;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003210 enum {
3211 NoRecommend,
3212 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00003213 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003214 ShouldBeInTargetRegion,
3215 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003216 } Recommend = NoRecommend;
Kelvin Lifd8b5742016-07-01 14:30:25 +00003217 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003218 // OpenMP [2.16, Nesting of Regions]
3219 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003220 // OpenMP [2.8.1,simd Construct, Restrictions]
Kelvin Lifd8b5742016-07-01 14:30:25 +00003221 // An ordered construct with the simd clause is the only OpenMP
3222 // construct that can appear in the simd region.
David Majnemer9d168222016-08-05 17:44:54 +00003223 // Allowing a SIMD construct nested in another SIMD construct is an
Kelvin Lifd8b5742016-07-01 14:30:25 +00003224 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
3225 // message.
3226 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
3227 ? diag::err_omp_prohibited_region_simd
3228 : diag::warn_omp_nesting_simd);
3229 return CurrentRegion != OMPD_simd;
Alexey Bataev549210e2014-06-24 04:39:47 +00003230 }
Alexey Bataev0162e452014-07-22 10:10:35 +00003231 if (ParentRegion == OMPD_atomic) {
3232 // OpenMP [2.16, Nesting of Regions]
3233 // OpenMP constructs may not be nested inside an atomic region.
3234 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
3235 return true;
3236 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003237 if (CurrentRegion == OMPD_section) {
3238 // OpenMP [2.7.2, sections Construct, Restrictions]
3239 // Orphaned section directives are prohibited. That is, the section
3240 // directives must appear within the sections construct and must not be
3241 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003242 if (ParentRegion != OMPD_sections &&
3243 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003244 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
3245 << (ParentRegion != OMPD_unknown)
3246 << getOpenMPDirectiveName(ParentRegion);
3247 return true;
3248 }
3249 return false;
3250 }
Alexey Bataev185e88d2019-01-08 15:53:42 +00003251 // Allow some constructs (except teams and cancellation constructs) to be
3252 // orphaned (they could be used in functions, called from OpenMP regions
3253 // with the required preconditions).
Kelvin Libf594a52016-12-17 05:48:59 +00003254 if (ParentRegion == OMPD_unknown &&
Alexey Bataev185e88d2019-01-08 15:53:42 +00003255 !isOpenMPNestingTeamsDirective(CurrentRegion) &&
3256 CurrentRegion != OMPD_cancellation_point &&
3257 CurrentRegion != OMPD_cancel)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003258 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00003259 if (CurrentRegion == OMPD_cancellation_point ||
3260 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003261 // OpenMP [2.16, Nesting of Regions]
3262 // A cancellation point construct for which construct-type-clause is
3263 // taskgroup must be nested inside a task construct. A cancellation
3264 // point construct for which construct-type-clause is not taskgroup must
3265 // be closely nested inside an OpenMP construct that matches the type
3266 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00003267 // A cancel construct for which construct-type-clause is taskgroup must be
3268 // nested inside a task construct. A cancel construct for which
3269 // construct-type-clause is not taskgroup must be closely nested inside an
3270 // OpenMP construct that matches the type specified in
3271 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003272 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003273 !((CancelRegion == OMPD_parallel &&
3274 (ParentRegion == OMPD_parallel ||
3275 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00003276 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003277 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00003278 ParentRegion == OMPD_target_parallel_for ||
3279 ParentRegion == OMPD_distribute_parallel_for ||
Alexey Bataev16e79882017-11-22 21:12:03 +00003280 ParentRegion == OMPD_teams_distribute_parallel_for ||
3281 ParentRegion == OMPD_target_teams_distribute_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003282 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
3283 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00003284 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
3285 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev185e88d2019-01-08 15:53:42 +00003286 OrphanSeen = ParentRegion == OMPD_unknown;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003287 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00003288 // OpenMP [2.16, Nesting of Regions]
3289 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003290 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00003291 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003292 isOpenMPTaskingDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003293 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
3294 // OpenMP [2.16, Nesting of Regions]
3295 // A critical region may not be nested (closely or otherwise) inside a
3296 // critical region with the same name. Note that this restriction is not
3297 // sufficient to prevent deadlock.
3298 SourceLocation PreviousCriticalLoc;
David Majnemer9d168222016-08-05 17:44:54 +00003299 bool DeadLock = Stack->hasDirective(
3300 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
3301 const DeclarationNameInfo &DNI,
Alexey Bataeve3727102018-04-18 15:57:46 +00003302 SourceLocation Loc) {
David Majnemer9d168222016-08-05 17:44:54 +00003303 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
3304 PreviousCriticalLoc = Loc;
3305 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003306 }
3307 return false;
David Majnemer9d168222016-08-05 17:44:54 +00003308 },
3309 false /* skip top directive */);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003310 if (DeadLock) {
3311 SemaRef.Diag(StartLoc,
3312 diag::err_omp_prohibited_region_critical_same_name)
3313 << CurrentName.getName();
3314 if (PreviousCriticalLoc.isValid())
3315 SemaRef.Diag(PreviousCriticalLoc,
3316 diag::note_omp_previous_critical_region);
3317 return true;
3318 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003319 } else if (CurrentRegion == OMPD_barrier) {
3320 // OpenMP [2.16, Nesting of Regions]
3321 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003322 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003323 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3324 isOpenMPTaskingDirective(ParentRegion) ||
3325 ParentRegion == OMPD_master ||
3326 ParentRegion == OMPD_critical ||
3327 ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00003328 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Kelvin Li579e41c2016-11-30 23:51:03 +00003329 !isOpenMPParallelDirective(CurrentRegion) &&
3330 !isOpenMPTeamsDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003331 // OpenMP [2.16, Nesting of Regions]
3332 // A worksharing region may not be closely nested inside a worksharing,
3333 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003334 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3335 isOpenMPTaskingDirective(ParentRegion) ||
3336 ParentRegion == OMPD_master ||
3337 ParentRegion == OMPD_critical ||
3338 ParentRegion == OMPD_ordered;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003339 Recommend = ShouldBeInParallelRegion;
3340 } else if (CurrentRegion == OMPD_ordered) {
3341 // OpenMP [2.16, Nesting of Regions]
3342 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00003343 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003344 // An ordered region must be closely nested inside a loop region (or
3345 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003346 // OpenMP [2.8.1,simd Construct, Restrictions]
3347 // An ordered construct with the simd clause is the only OpenMP construct
3348 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003349 NestingProhibited = ParentRegion == OMPD_critical ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003350 isOpenMPTaskingDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003351 !(isOpenMPSimdDirective(ParentRegion) ||
3352 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003353 Recommend = ShouldBeInOrderedRegion;
Kelvin Libf594a52016-12-17 05:48:59 +00003354 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00003355 // OpenMP [2.16, Nesting of Regions]
3356 // If specified, a teams construct must be contained within a target
3357 // construct.
3358 NestingProhibited = ParentRegion != OMPD_target;
Kelvin Li2b51f722016-07-26 04:32:50 +00003359 OrphanSeen = ParentRegion == OMPD_unknown;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003360 Recommend = ShouldBeInTargetRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003361 }
Kelvin Libf594a52016-12-17 05:48:59 +00003362 if (!NestingProhibited &&
3363 !isOpenMPTargetExecutionDirective(CurrentRegion) &&
3364 !isOpenMPTargetDataManagementDirective(CurrentRegion) &&
3365 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00003366 // OpenMP [2.16, Nesting of Regions]
3367 // distribute, parallel, parallel sections, parallel workshare, and the
3368 // parallel loop and parallel loop SIMD constructs are the only OpenMP
3369 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003370 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
3371 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00003372 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003373 }
David Majnemer9d168222016-08-05 17:44:54 +00003374 if (!NestingProhibited &&
Kelvin Li02532872016-08-05 14:37:37 +00003375 isOpenMPNestingDistributeDirective(CurrentRegion)) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003376 // OpenMP 4.5 [2.17 Nesting of Regions]
3377 // The region associated with the distribute construct must be strictly
3378 // nested inside a teams region
Kelvin Libf594a52016-12-17 05:48:59 +00003379 NestingProhibited =
3380 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003381 Recommend = ShouldBeInTeamsRegion;
3382 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003383 if (!NestingProhibited &&
3384 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
3385 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
3386 // OpenMP 4.5 [2.17 Nesting of Regions]
3387 // If a target, target update, target data, target enter data, or
3388 // target exit data construct is encountered during execution of a
3389 // target region, the behavior is unspecified.
3390 NestingProhibited = Stack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +00003391 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
Alexey Bataeve3727102018-04-18 15:57:46 +00003392 SourceLocation) {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003393 if (isOpenMPTargetExecutionDirective(K)) {
3394 OffendingRegion = K;
3395 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003396 }
3397 return false;
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003398 },
3399 false /* don't skip top directive */);
3400 CloseNesting = false;
3401 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003402 if (NestingProhibited) {
Kelvin Li2b51f722016-07-26 04:32:50 +00003403 if (OrphanSeen) {
3404 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
3405 << getOpenMPDirectiveName(CurrentRegion) << Recommend;
3406 } else {
3407 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
3408 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
3409 << Recommend << getOpenMPDirectiveName(CurrentRegion);
3410 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003411 return true;
3412 }
3413 }
3414 return false;
3415}
3416
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003417static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
3418 ArrayRef<OMPClause *> Clauses,
3419 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
3420 bool ErrorFound = false;
3421 unsigned NamedModifiersNumber = 0;
3422 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
3423 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00003424 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00003425 for (const OMPClause *C : Clauses) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003426 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
3427 // At most one if clause without a directive-name-modifier can appear on
3428 // the directive.
3429 OpenMPDirectiveKind CurNM = IC->getNameModifier();
3430 if (FoundNameModifiers[CurNM]) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003431 S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003432 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
3433 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
3434 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003435 } else if (CurNM != OMPD_unknown) {
3436 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003437 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003438 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003439 FoundNameModifiers[CurNM] = IC;
3440 if (CurNM == OMPD_unknown)
3441 continue;
3442 // Check if the specified name modifier is allowed for the current
3443 // directive.
3444 // At most one if clause with the particular directive-name-modifier can
3445 // appear on the directive.
3446 bool MatchFound = false;
3447 for (auto NM : AllowedNameModifiers) {
3448 if (CurNM == NM) {
3449 MatchFound = true;
3450 break;
3451 }
3452 }
3453 if (!MatchFound) {
3454 S.Diag(IC->getNameModifierLoc(),
3455 diag::err_omp_wrong_if_directive_name_modifier)
3456 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
3457 ErrorFound = true;
3458 }
3459 }
3460 }
3461 // If any if clause on the directive includes a directive-name-modifier then
3462 // all if clauses on the directive must include a directive-name-modifier.
3463 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
3464 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003465 S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003466 diag::err_omp_no_more_if_clause);
3467 } else {
3468 std::string Values;
3469 std::string Sep(", ");
3470 unsigned AllowedCnt = 0;
3471 unsigned TotalAllowedNum =
3472 AllowedNameModifiers.size() - NamedModifiersNumber;
3473 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3474 ++Cnt) {
3475 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
3476 if (!FoundNameModifiers[NM]) {
3477 Values += "'";
3478 Values += getOpenMPDirectiveName(NM);
3479 Values += "'";
3480 if (AllowedCnt + 2 == TotalAllowedNum)
3481 Values += " or ";
3482 else if (AllowedCnt + 1 != TotalAllowedNum)
3483 Values += Sep;
3484 ++AllowedCnt;
3485 }
3486 }
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003487 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003488 diag::err_omp_unnamed_if_clause)
3489 << (TotalAllowedNum > 1) << Values;
3490 }
Alexey Bataeve3727102018-04-18 15:57:46 +00003491 for (SourceLocation Loc : NameModifierLoc) {
Alexey Bataevecb156a2015-09-15 17:23:56 +00003492 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3493 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003494 ErrorFound = true;
3495 }
3496 return ErrorFound;
3497}
3498
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003499StmtResult Sema::ActOnOpenMPExecutableDirective(
3500 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
3501 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3502 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003503 StmtResult Res = StmtError();
Jonas Hahnfeld64a9e3c2017-02-22 06:49:10 +00003504 // First check CancelRegion which is then used in checkNestingOfRegions.
3505 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) ||
3506 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003507 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00003508 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003509
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003510 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataeve3727102018-04-18 15:57:46 +00003511 VarsWithInheritedDSAType VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003512 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00003513 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00003514 if (AStmt && !CurContext->isDependentContext()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00003515 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
3516
3517 // Check default data sharing attributes for referenced variables.
3518 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
Arpith Chacko Jacob1f46b702017-01-23 15:38:49 +00003519 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind);
3520 Stmt *S = AStmt;
3521 while (--ThisCaptureLevel >= 0)
3522 S = cast<CapturedStmt>(S)->getCapturedStmt();
3523 DSAChecker.Visit(S);
Alexey Bataev68446b72014-07-18 07:47:19 +00003524 if (DSAChecker.isErrorFound())
3525 return StmtError();
3526 // Generate list of implicitly defined firstprivate variables.
3527 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00003528
Alexey Bataev88202be2017-07-27 13:20:36 +00003529 SmallVector<Expr *, 4> ImplicitFirstprivates(
3530 DSAChecker.getImplicitFirstprivate().begin(),
3531 DSAChecker.getImplicitFirstprivate().end());
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003532 SmallVector<Expr *, 4> ImplicitMaps(DSAChecker.getImplicitMap().begin(),
3533 DSAChecker.getImplicitMap().end());
Alexey Bataev88202be2017-07-27 13:20:36 +00003534 // Mark taskgroup task_reduction descriptors as implicitly firstprivate.
Alexey Bataeve3727102018-04-18 15:57:46 +00003535 for (OMPClause *C : Clauses) {
Alexey Bataev88202be2017-07-27 13:20:36 +00003536 if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003537 for (Expr *E : IRC->taskgroup_descriptors())
Alexey Bataev88202be2017-07-27 13:20:36 +00003538 if (E)
3539 ImplicitFirstprivates.emplace_back(E);
3540 }
3541 }
3542 if (!ImplicitFirstprivates.empty()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00003543 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
Alexey Bataev88202be2017-07-27 13:20:36 +00003544 ImplicitFirstprivates, SourceLocation(), SourceLocation(),
3545 SourceLocation())) {
Alexey Bataev68446b72014-07-18 07:47:19 +00003546 ClausesWithImplicit.push_back(Implicit);
3547 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
Alexey Bataev88202be2017-07-27 13:20:36 +00003548 ImplicitFirstprivates.size();
Alexey Bataeve3727102018-04-18 15:57:46 +00003549 } else {
Alexey Bataev68446b72014-07-18 07:47:19 +00003550 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003551 }
Alexey Bataev68446b72014-07-18 07:47:19 +00003552 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003553 if (!ImplicitMaps.empty()) {
Michael Kruse4304e9d2019-02-19 16:38:20 +00003554 CXXScopeSpec MapperIdScopeSpec;
3555 DeclarationNameInfo MapperId;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003556 if (OMPClause *Implicit = ActOnOpenMPMapClause(
Michael Kruse4304e9d2019-02-19 16:38:20 +00003557 llvm::None, llvm::None, MapperIdScopeSpec, MapperId,
3558 OMPC_MAP_tofrom, /*IsMapTypeImplicit=*/true, SourceLocation(),
3559 SourceLocation(), ImplicitMaps, OMPVarListLocTy())) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003560 ClausesWithImplicit.emplace_back(Implicit);
3561 ErrorFound |=
3562 cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMaps.size();
Alexey Bataeve3727102018-04-18 15:57:46 +00003563 } else {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003564 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003565 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003566 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003567 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003568
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003569 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003570 switch (Kind) {
3571 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00003572 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3573 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003574 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003575 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003576 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003577 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3578 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003579 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003580 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003581 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3582 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003583 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003584 case OMPD_for_simd:
3585 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3586 EndLoc, VarsWithInheritedDSA);
3587 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003588 case OMPD_sections:
3589 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3590 EndLoc);
3591 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003592 case OMPD_section:
3593 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003594 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003595 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3596 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003597 case OMPD_single:
3598 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3599 EndLoc);
3600 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003601 case OMPD_master:
3602 assert(ClausesWithImplicit.empty() &&
3603 "No clauses are allowed for 'omp master' directive");
3604 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3605 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003606 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003607 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3608 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003609 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003610 case OMPD_parallel_for:
3611 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3612 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003613 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003614 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003615 case OMPD_parallel_for_simd:
3616 Res = ActOnOpenMPParallelForSimdDirective(
3617 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003618 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003619 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003620 case OMPD_parallel_sections:
3621 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3622 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003623 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003624 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003625 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003626 Res =
3627 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003628 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003629 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003630 case OMPD_taskyield:
3631 assert(ClausesWithImplicit.empty() &&
3632 "No clauses are allowed for 'omp taskyield' directive");
3633 assert(AStmt == nullptr &&
3634 "No associated statement allowed for 'omp taskyield' directive");
3635 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3636 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003637 case OMPD_barrier:
3638 assert(ClausesWithImplicit.empty() &&
3639 "No clauses are allowed for 'omp barrier' directive");
3640 assert(AStmt == nullptr &&
3641 "No associated statement allowed for 'omp barrier' directive");
3642 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3643 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003644 case OMPD_taskwait:
3645 assert(ClausesWithImplicit.empty() &&
3646 "No clauses are allowed for 'omp taskwait' directive");
3647 assert(AStmt == nullptr &&
3648 "No associated statement allowed for 'omp taskwait' directive");
3649 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3650 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003651 case OMPD_taskgroup:
Alexey Bataev169d96a2017-07-18 20:17:46 +00003652 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc,
3653 EndLoc);
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003654 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003655 case OMPD_flush:
3656 assert(AStmt == nullptr &&
3657 "No associated statement allowed for 'omp flush' directive");
3658 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3659 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003660 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003661 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3662 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003663 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003664 case OMPD_atomic:
3665 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3666 EndLoc);
3667 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003668 case OMPD_teams:
3669 Res =
3670 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3671 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003672 case OMPD_target:
3673 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3674 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003675 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003676 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003677 case OMPD_target_parallel:
3678 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3679 StartLoc, EndLoc);
3680 AllowedNameModifiers.push_back(OMPD_target);
3681 AllowedNameModifiers.push_back(OMPD_parallel);
3682 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003683 case OMPD_target_parallel_for:
3684 Res = ActOnOpenMPTargetParallelForDirective(
3685 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3686 AllowedNameModifiers.push_back(OMPD_target);
3687 AllowedNameModifiers.push_back(OMPD_parallel);
3688 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003689 case OMPD_cancellation_point:
3690 assert(ClausesWithImplicit.empty() &&
3691 "No clauses are allowed for 'omp cancellation point' directive");
3692 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3693 "cancellation point' directive");
3694 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3695 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003696 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003697 assert(AStmt == nullptr &&
3698 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003699 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3700 CancelRegion);
3701 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003702 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003703 case OMPD_target_data:
3704 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3705 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003706 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003707 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003708 case OMPD_target_enter_data:
3709 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00003710 EndLoc, AStmt);
Samuel Antaodf67fc42016-01-19 19:15:56 +00003711 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3712 break;
Samuel Antao72590762016-01-19 20:04:50 +00003713 case OMPD_target_exit_data:
3714 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00003715 EndLoc, AStmt);
Samuel Antao72590762016-01-19 20:04:50 +00003716 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3717 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003718 case OMPD_taskloop:
3719 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3720 EndLoc, VarsWithInheritedDSA);
3721 AllowedNameModifiers.push_back(OMPD_taskloop);
3722 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003723 case OMPD_taskloop_simd:
3724 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3725 EndLoc, VarsWithInheritedDSA);
3726 AllowedNameModifiers.push_back(OMPD_taskloop);
3727 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003728 case OMPD_distribute:
3729 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3730 EndLoc, VarsWithInheritedDSA);
3731 break;
Samuel Antao686c70c2016-05-26 17:30:50 +00003732 case OMPD_target_update:
Alexey Bataev7828b252017-11-21 17:08:48 +00003733 Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc,
3734 EndLoc, AStmt);
Samuel Antao686c70c2016-05-26 17:30:50 +00003735 AllowedNameModifiers.push_back(OMPD_target_update);
3736 break;
Carlo Bertolli9925f152016-06-27 14:55:37 +00003737 case OMPD_distribute_parallel_for:
3738 Res = ActOnOpenMPDistributeParallelForDirective(
3739 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3740 AllowedNameModifiers.push_back(OMPD_parallel);
3741 break;
Kelvin Li4a39add2016-07-05 05:00:15 +00003742 case OMPD_distribute_parallel_for_simd:
3743 Res = ActOnOpenMPDistributeParallelForSimdDirective(
3744 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3745 AllowedNameModifiers.push_back(OMPD_parallel);
3746 break;
Kelvin Li787f3fc2016-07-06 04:45:38 +00003747 case OMPD_distribute_simd:
3748 Res = ActOnOpenMPDistributeSimdDirective(
3749 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3750 break;
Kelvin Lia579b912016-07-14 02:54:56 +00003751 case OMPD_target_parallel_for_simd:
3752 Res = ActOnOpenMPTargetParallelForSimdDirective(
3753 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3754 AllowedNameModifiers.push_back(OMPD_target);
3755 AllowedNameModifiers.push_back(OMPD_parallel);
3756 break;
Kelvin Li986330c2016-07-20 22:57:10 +00003757 case OMPD_target_simd:
3758 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3759 EndLoc, VarsWithInheritedDSA);
3760 AllowedNameModifiers.push_back(OMPD_target);
3761 break;
Kelvin Li02532872016-08-05 14:37:37 +00003762 case OMPD_teams_distribute:
David Majnemer9d168222016-08-05 17:44:54 +00003763 Res = ActOnOpenMPTeamsDistributeDirective(
3764 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Kelvin Li02532872016-08-05 14:37:37 +00003765 break;
Kelvin Li4e325f72016-10-25 12:50:55 +00003766 case OMPD_teams_distribute_simd:
3767 Res = ActOnOpenMPTeamsDistributeSimdDirective(
3768 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3769 break;
Kelvin Li579e41c2016-11-30 23:51:03 +00003770 case OMPD_teams_distribute_parallel_for_simd:
3771 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective(
3772 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3773 AllowedNameModifiers.push_back(OMPD_parallel);
3774 break;
Kelvin Li7ade93f2016-12-09 03:24:30 +00003775 case OMPD_teams_distribute_parallel_for:
3776 Res = ActOnOpenMPTeamsDistributeParallelForDirective(
3777 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3778 AllowedNameModifiers.push_back(OMPD_parallel);
3779 break;
Kelvin Libf594a52016-12-17 05:48:59 +00003780 case OMPD_target_teams:
3781 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc,
3782 EndLoc);
3783 AllowedNameModifiers.push_back(OMPD_target);
3784 break;
Kelvin Li83c451e2016-12-25 04:52:54 +00003785 case OMPD_target_teams_distribute:
3786 Res = ActOnOpenMPTargetTeamsDistributeDirective(
3787 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3788 AllowedNameModifiers.push_back(OMPD_target);
3789 break;
Kelvin Li80e8f562016-12-29 22:16:30 +00003790 case OMPD_target_teams_distribute_parallel_for:
3791 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective(
3792 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3793 AllowedNameModifiers.push_back(OMPD_target);
3794 AllowedNameModifiers.push_back(OMPD_parallel);
3795 break;
Kelvin Li1851df52017-01-03 05:23:48 +00003796 case OMPD_target_teams_distribute_parallel_for_simd:
3797 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
3798 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3799 AllowedNameModifiers.push_back(OMPD_target);
3800 AllowedNameModifiers.push_back(OMPD_parallel);
3801 break;
Kelvin Lida681182017-01-10 18:08:18 +00003802 case OMPD_target_teams_distribute_simd:
3803 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective(
3804 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3805 AllowedNameModifiers.push_back(OMPD_target);
3806 break;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00003807 case OMPD_declare_target:
3808 case OMPD_end_declare_target:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003809 case OMPD_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +00003810 case OMPD_allocate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003811 case OMPD_declare_reduction:
Michael Kruse251e1482019-02-01 20:25:04 +00003812 case OMPD_declare_mapper:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003813 case OMPD_declare_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00003814 case OMPD_requires:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003815 llvm_unreachable("OpenMP Directive is not allowed");
3816 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003817 llvm_unreachable("Unknown OpenMP directive");
3818 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003819
Alexey Bataeve3727102018-04-18 15:57:46 +00003820 for (const auto &P : VarsWithInheritedDSA) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003821 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3822 << P.first << P.second->getSourceRange();
3823 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003824 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3825
3826 if (!AllowedNameModifiers.empty())
3827 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3828 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003829
Alexey Bataeved09d242014-05-28 05:53:51 +00003830 if (ErrorFound)
3831 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003832 return Res;
3833}
3834
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003835Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3836 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
Alexey Bataevd93d3762016-04-12 09:35:56 +00003837 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
Alexey Bataevecba70f2016-04-12 11:02:11 +00003838 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3839 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003840 assert(Aligneds.size() == Alignments.size());
Alexey Bataevecba70f2016-04-12 11:02:11 +00003841 assert(Linears.size() == LinModifiers.size());
3842 assert(Linears.size() == Steps.size());
Alexey Bataev587e1de2016-03-30 10:43:55 +00003843 if (!DG || DG.get().isNull())
3844 return DeclGroupPtrTy();
3845
3846 if (!DG.get().isSingleDecl()) {
Alexey Bataev20dfd772016-04-04 10:12:15 +00003847 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003848 return DG;
3849 }
Alexey Bataeve3727102018-04-18 15:57:46 +00003850 Decl *ADecl = DG.get().getSingleDecl();
Alexey Bataev587e1de2016-03-30 10:43:55 +00003851 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3852 ADecl = FTD->getTemplatedDecl();
3853
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003854 auto *FD = dyn_cast<FunctionDecl>(ADecl);
3855 if (!FD) {
3856 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003857 return DeclGroupPtrTy();
3858 }
3859
Alexey Bataev2af33e32016-04-07 12:45:37 +00003860 // OpenMP [2.8.2, declare simd construct, Description]
3861 // The parameter of the simdlen clause must be a constant positive integer
3862 // expression.
3863 ExprResult SL;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003864 if (Simdlen)
Alexey Bataev2af33e32016-04-07 12:45:37 +00003865 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003866 // OpenMP [2.8.2, declare simd construct, Description]
3867 // The special this pointer can be used as if was one of the arguments to the
3868 // function in any of the linear, aligned, or uniform clauses.
3869 // The uniform clause declares one or more arguments to have an invariant
3870 // value for all concurrent invocations of the function in the execution of a
3871 // single SIMD loop.
Alexey Bataeve3727102018-04-18 15:57:46 +00003872 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs;
3873 const Expr *UniformedLinearThis = nullptr;
3874 for (const Expr *E : Uniforms) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003875 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00003876 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3877 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003878 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3879 FD->getParamDecl(PVD->getFunctionScopeIndex())
Alexey Bataevecba70f2016-04-12 11:02:11 +00003880 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
Alexey Bataev43a919f2018-04-13 17:48:43 +00003881 UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003882 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003883 }
3884 if (isa<CXXThisExpr>(E)) {
3885 UniformedLinearThis = E;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003886 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003887 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003888 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3889 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
Alexey Bataev2af33e32016-04-07 12:45:37 +00003890 }
Alexey Bataevd93d3762016-04-12 09:35:56 +00003891 // OpenMP [2.8.2, declare simd construct, Description]
3892 // The aligned clause declares that the object to which each list item points
3893 // is aligned to the number of bytes expressed in the optional parameter of
3894 // the aligned clause.
3895 // The special this pointer can be used as if was one of the arguments to the
3896 // function in any of the linear, aligned, or uniform clauses.
3897 // The type of list items appearing in the aligned clause must be array,
3898 // pointer, reference to array, or reference to pointer.
Alexey Bataeve3727102018-04-18 15:57:46 +00003899 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs;
3900 const Expr *AlignedThis = nullptr;
3901 for (const Expr *E : Aligneds) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003902 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00003903 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3904 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3905 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevd93d3762016-04-12 09:35:56 +00003906 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3907 FD->getParamDecl(PVD->getFunctionScopeIndex())
3908 ->getCanonicalDecl() == CanonPVD) {
3909 // OpenMP [2.8.1, simd construct, Restrictions]
3910 // A list-item cannot appear in more than one aligned clause.
3911 if (AlignedArgs.count(CanonPVD) > 0) {
3912 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3913 << 1 << E->getSourceRange();
3914 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3915 diag::note_omp_explicit_dsa)
3916 << getOpenMPClauseName(OMPC_aligned);
3917 continue;
3918 }
3919 AlignedArgs[CanonPVD] = E;
3920 QualType QTy = PVD->getType()
3921 .getNonReferenceType()
3922 .getUnqualifiedType()
3923 .getCanonicalType();
3924 const Type *Ty = QTy.getTypePtrOrNull();
3925 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3926 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3927 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3928 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3929 }
3930 continue;
3931 }
3932 }
3933 if (isa<CXXThisExpr>(E)) {
3934 if (AlignedThis) {
3935 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3936 << 2 << E->getSourceRange();
3937 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3938 << getOpenMPClauseName(OMPC_aligned);
3939 }
3940 AlignedThis = E;
3941 continue;
3942 }
3943 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3944 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3945 }
3946 // The optional parameter of the aligned clause, alignment, must be a constant
3947 // positive integer expression. If no optional parameter is specified,
3948 // implementation-defined default alignments for SIMD instructions on the
3949 // target platforms are assumed.
Alexey Bataeve3727102018-04-18 15:57:46 +00003950 SmallVector<const Expr *, 4> NewAligns;
3951 for (Expr *E : Alignments) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003952 ExprResult Align;
3953 if (E)
3954 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3955 NewAligns.push_back(Align.get());
3956 }
Alexey Bataevecba70f2016-04-12 11:02:11 +00003957 // OpenMP [2.8.2, declare simd construct, Description]
3958 // The linear clause declares one or more list items to be private to a SIMD
3959 // lane and to have a linear relationship with respect to the iteration space
3960 // of a loop.
3961 // The special this pointer can be used as if was one of the arguments to the
3962 // function in any of the linear, aligned, or uniform clauses.
3963 // When a linear-step expression is specified in a linear clause it must be
3964 // either a constant integer expression or an integer-typed parameter that is
3965 // specified in a uniform clause on the directive.
Alexey Bataeve3727102018-04-18 15:57:46 +00003966 llvm::DenseMap<const Decl *, const Expr *> LinearArgs;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003967 const bool IsUniformedThis = UniformedLinearThis != nullptr;
3968 auto MI = LinModifiers.begin();
Alexey Bataeve3727102018-04-18 15:57:46 +00003969 for (const Expr *E : Linears) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003970 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3971 ++MI;
3972 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00003973 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3974 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3975 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevecba70f2016-04-12 11:02:11 +00003976 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3977 FD->getParamDecl(PVD->getFunctionScopeIndex())
3978 ->getCanonicalDecl() == CanonPVD) {
3979 // OpenMP [2.15.3.7, linear Clause, Restrictions]
3980 // A list-item cannot appear in more than one linear clause.
3981 if (LinearArgs.count(CanonPVD) > 0) {
3982 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3983 << getOpenMPClauseName(OMPC_linear)
3984 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3985 Diag(LinearArgs[CanonPVD]->getExprLoc(),
3986 diag::note_omp_explicit_dsa)
3987 << getOpenMPClauseName(OMPC_linear);
3988 continue;
3989 }
3990 // Each argument can appear in at most one uniform or linear clause.
3991 if (UniformedArgs.count(CanonPVD) > 0) {
3992 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3993 << getOpenMPClauseName(OMPC_linear)
3994 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3995 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3996 diag::note_omp_explicit_dsa)
3997 << getOpenMPClauseName(OMPC_uniform);
3998 continue;
3999 }
4000 LinearArgs[CanonPVD] = E;
4001 if (E->isValueDependent() || E->isTypeDependent() ||
4002 E->isInstantiationDependent() ||
4003 E->containsUnexpandedParameterPack())
4004 continue;
4005 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
4006 PVD->getOriginalType());
4007 continue;
4008 }
4009 }
4010 if (isa<CXXThisExpr>(E)) {
4011 if (UniformedLinearThis) {
4012 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
4013 << getOpenMPClauseName(OMPC_linear)
4014 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
4015 << E->getSourceRange();
4016 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
4017 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
4018 : OMPC_linear);
4019 continue;
4020 }
4021 UniformedLinearThis = E;
4022 if (E->isValueDependent() || E->isTypeDependent() ||
4023 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
4024 continue;
4025 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
4026 E->getType());
4027 continue;
4028 }
4029 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
4030 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
4031 }
4032 Expr *Step = nullptr;
4033 Expr *NewStep = nullptr;
4034 SmallVector<Expr *, 4> NewSteps;
Alexey Bataeve3727102018-04-18 15:57:46 +00004035 for (Expr *E : Steps) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00004036 // Skip the same step expression, it was checked already.
4037 if (Step == E || !E) {
4038 NewSteps.push_back(E ? NewStep : nullptr);
4039 continue;
4040 }
4041 Step = E;
Alexey Bataeve3727102018-04-18 15:57:46 +00004042 if (const auto *DRE = dyn_cast<DeclRefExpr>(Step))
4043 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
4044 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevecba70f2016-04-12 11:02:11 +00004045 if (UniformedArgs.count(CanonPVD) == 0) {
4046 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
4047 << Step->getSourceRange();
4048 } else if (E->isValueDependent() || E->isTypeDependent() ||
4049 E->isInstantiationDependent() ||
4050 E->containsUnexpandedParameterPack() ||
Alexey Bataeve3727102018-04-18 15:57:46 +00004051 CanonPVD->getType()->hasIntegerRepresentation()) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00004052 NewSteps.push_back(Step);
Alexey Bataeve3727102018-04-18 15:57:46 +00004053 } else {
Alexey Bataevecba70f2016-04-12 11:02:11 +00004054 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
4055 << Step->getSourceRange();
4056 }
4057 continue;
4058 }
4059 NewStep = Step;
4060 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
4061 !Step->isInstantiationDependent() &&
4062 !Step->containsUnexpandedParameterPack()) {
4063 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
4064 .get();
4065 if (NewStep)
4066 NewStep = VerifyIntegerConstantExpression(NewStep).get();
4067 }
4068 NewSteps.push_back(NewStep);
4069 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00004070 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
4071 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
Alexey Bataevd93d3762016-04-12 09:35:56 +00004072 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
Alexey Bataevecba70f2016-04-12 11:02:11 +00004073 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
4074 const_cast<Expr **>(Linears.data()), Linears.size(),
4075 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
4076 NewSteps.data(), NewSteps.size(), SR);
Alexey Bataev587e1de2016-03-30 10:43:55 +00004077 ADecl->addAttr(NewAttr);
4078 return ConvertDeclToDeclGroup(ADecl);
4079}
4080
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004081StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
4082 Stmt *AStmt,
4083 SourceLocation StartLoc,
4084 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004085 if (!AStmt)
4086 return StmtError();
4087
Alexey Bataeve3727102018-04-18 15:57:46 +00004088 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev9959db52014-05-06 10:08:46 +00004089 // 1.2.2 OpenMP Language Terminology
4090 // Structured block - An executable statement with a single entry at the
4091 // top and a single exit at the bottom.
4092 // The point of exit cannot be a branch out of the structured block.
4093 // longjmp() and throw() must not violate the entry/exit criteria.
4094 CS->getCapturedDecl()->setNothrow();
4095
Reid Kleckner87a31802018-03-12 21:43:02 +00004096 setFunctionHasBranchProtectedScope();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004097
Alexey Bataev25e5b442015-09-15 12:52:43 +00004098 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4099 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004100}
4101
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004102namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004103/// Helper class for checking canonical form of the OpenMP loops and
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004104/// extracting iteration space of each loop in the loop nest, that will be used
4105/// for IR generation.
4106class OpenMPIterationSpaceChecker {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004107 /// Reference to Sema.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004108 Sema &SemaRef;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004109 /// A location for diagnostics (when there is no some better location).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004110 SourceLocation DefaultLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004111 /// A location for diagnostics (when increment is not compatible).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004112 SourceLocation ConditionLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004113 /// A source location for referring to loop init later.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004114 SourceRange InitSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004115 /// A source location for referring to condition later.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004116 SourceRange ConditionSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004117 /// A source location for referring to increment later.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004118 SourceRange IncrementSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004119 /// Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004120 ValueDecl *LCDecl = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004121 /// Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004122 Expr *LCRef = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004123 /// Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004124 Expr *LB = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004125 /// Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004126 Expr *UB = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004127 /// Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004128 Expr *Step = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004129 /// This flag is true when condition is one of:
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004130 /// Var < UB
4131 /// Var <= UB
4132 /// UB > Var
4133 /// UB >= Var
Kelvin Liefbe4af2018-11-21 19:10:48 +00004134 /// This will have no value when the condition is !=
4135 llvm::Optional<bool> TestIsLessOp;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004136 /// This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004137 bool TestIsStrictOp = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004138 /// This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004139 bool SubtractStep = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004140
4141public:
4142 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004143 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004144 /// Check init-expr for canonical loop form and save loop counter
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004145 /// variable - #Var and its initialization value - #LB.
Alexey Bataeve3727102018-04-18 15:57:46 +00004146 bool checkAndSetInit(Stmt *S, bool EmitDiags = true);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004147 /// Check test-expr for canonical form, save upper-bound (#UB), flags
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004148 /// for less/greater and for strict/non-strict comparison.
Alexey Bataeve3727102018-04-18 15:57:46 +00004149 bool checkAndSetCond(Expr *S);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004150 /// Check incr-expr for canonical loop form and return true if it
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004151 /// does not conform, otherwise save loop step (#Step).
Alexey Bataeve3727102018-04-18 15:57:46 +00004152 bool checkAndSetInc(Expr *S);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004153 /// Return the loop counter variable.
Alexey Bataeve3727102018-04-18 15:57:46 +00004154 ValueDecl *getLoopDecl() const { return LCDecl; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004155 /// Return the reference expression to loop counter variable.
Alexey Bataeve3727102018-04-18 15:57:46 +00004156 Expr *getLoopDeclRefExpr() const { return LCRef; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004157 /// Source range of the loop init.
Alexey Bataeve3727102018-04-18 15:57:46 +00004158 SourceRange getInitSrcRange() const { return InitSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004159 /// Source range of the loop condition.
Alexey Bataeve3727102018-04-18 15:57:46 +00004160 SourceRange getConditionSrcRange() const { return ConditionSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004161 /// Source range of the loop increment.
Alexey Bataeve3727102018-04-18 15:57:46 +00004162 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004163 /// True if the step should be subtracted.
Alexey Bataeve3727102018-04-18 15:57:46 +00004164 bool shouldSubtractStep() const { return SubtractStep; }
Alexey Bataev316ccf62019-01-29 18:51:58 +00004165 /// True, if the compare operator is strict (<, > or !=).
4166 bool isStrictTestOp() const { return TestIsStrictOp; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004167 /// Build the expression to calculate the number of iterations.
Alexey Bataeve3727102018-04-18 15:57:46 +00004168 Expr *buildNumIterations(
4169 Scope *S, const bool LimitedType,
4170 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004171 /// Build the precondition expression for the loops.
Alexey Bataeve3727102018-04-18 15:57:46 +00004172 Expr *
4173 buildPreCond(Scope *S, Expr *Cond,
4174 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004175 /// Build reference expression to the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004176 DeclRefExpr *
4177 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
4178 DSAStackTy &DSA) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004179 /// Build reference expression to the private counter be used for
Alexey Bataeva8899172015-08-06 12:30:57 +00004180 /// codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004181 Expr *buildPrivateCounterVar() const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004182 /// Build initialization of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004183 Expr *buildCounterInit() const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004184 /// Build step of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004185 Expr *buildCounterStep() const;
Alexey Bataevf138fda2018-08-13 19:04:24 +00004186 /// Build loop data with counter value for depend clauses in ordered
4187 /// directives.
4188 Expr *
4189 buildOrderedLoopData(Scope *S, Expr *Counter,
4190 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
4191 SourceLocation Loc, Expr *Inc = nullptr,
4192 OverloadedOperatorKind OOK = OO_Amp);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004193 /// Return true if any expression is dependent.
Alexey Bataeve3727102018-04-18 15:57:46 +00004194 bool dependent() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004195
4196private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004197 /// Check the right-hand side of an assignment in the increment
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004198 /// expression.
Alexey Bataeve3727102018-04-18 15:57:46 +00004199 bool checkAndSetIncRHS(Expr *RHS);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004200 /// Helper to set loop counter variable and its initializer.
Alexey Bataeve3727102018-04-18 15:57:46 +00004201 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004202 /// Helper to set upper bound.
Kelvin Liefbe4af2018-11-21 19:10:48 +00004203 bool setUB(Expr *NewUB, llvm::Optional<bool> LessOp, bool StrictOp,
4204 SourceRange SR, SourceLocation SL);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004205 /// Helper to set loop increment.
Alexey Bataeve3727102018-04-18 15:57:46 +00004206 bool setStep(Expr *NewStep, bool Subtract);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004207};
4208
Alexey Bataeve3727102018-04-18 15:57:46 +00004209bool OpenMPIterationSpaceChecker::dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004210 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004211 assert(!LB && !UB && !Step);
4212 return false;
4213 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004214 return LCDecl->getType()->isDependentType() ||
4215 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
4216 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004217}
4218
Alexey Bataeve3727102018-04-18 15:57:46 +00004219bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl,
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004220 Expr *NewLCRefExpr,
4221 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004222 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004223 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00004224 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004225 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004226 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004227 LCDecl = getCanonicalDecl(NewLCDecl);
4228 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004229 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
4230 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00004231 if ((Ctor->isCopyOrMoveConstructor() ||
4232 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4233 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004234 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004235 LB = NewLB;
4236 return false;
4237}
4238
Alexey Bataev316ccf62019-01-29 18:51:58 +00004239bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB,
4240 llvm::Optional<bool> LessOp,
Kelvin Liefbe4af2018-11-21 19:10:48 +00004241 bool StrictOp, SourceRange SR,
4242 SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004243 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004244 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
4245 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004246 if (!NewUB)
4247 return true;
4248 UB = NewUB;
Kelvin Liefbe4af2018-11-21 19:10:48 +00004249 if (LessOp)
4250 TestIsLessOp = LessOp;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004251 TestIsStrictOp = StrictOp;
4252 ConditionSrcRange = SR;
4253 ConditionLoc = SL;
4254 return false;
4255}
4256
Alexey Bataeve3727102018-04-18 15:57:46 +00004257bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004258 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004259 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004260 if (!NewStep)
4261 return true;
4262 if (!NewStep->isValueDependent()) {
4263 // Check that the step is integer expression.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004264 SourceLocation StepLoc = NewStep->getBeginLoc();
Alexey Bataev5372fb82017-08-31 23:06:52 +00004265 ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion(
4266 StepLoc, getExprAsWritten(NewStep));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004267 if (Val.isInvalid())
4268 return true;
4269 NewStep = Val.get();
4270
4271 // OpenMP [2.6, Canonical Loop Form, Restrictions]
4272 // If test-expr is of form var relational-op b and relational-op is < or
4273 // <= then incr-expr must cause var to increase on each iteration of the
4274 // loop. If test-expr is of form var relational-op b and relational-op is
4275 // > or >= then incr-expr must cause var to decrease on each iteration of
4276 // the loop.
4277 // If test-expr is of form b relational-op var and relational-op is < or
4278 // <= then incr-expr must cause var to decrease on each iteration of the
4279 // loop. If test-expr is of form b relational-op var and relational-op is
4280 // > or >= then incr-expr must cause var to increase on each iteration of
4281 // the loop.
4282 llvm::APSInt Result;
4283 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
4284 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
4285 bool IsConstNeg =
4286 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004287 bool IsConstPos =
4288 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004289 bool IsConstZero = IsConstant && !Result.getBoolValue();
Kelvin Liefbe4af2018-11-21 19:10:48 +00004290
4291 // != with increment is treated as <; != with decrement is treated as >
4292 if (!TestIsLessOp.hasValue())
4293 TestIsLessOp = IsConstPos || (IsUnsigned && !Subtract);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004294 if (UB && (IsConstZero ||
Gheorghe-Teodor Berceaa3afcf22019-01-09 20:38:35 +00004295 (TestIsLessOp.getValue() ?
Kelvin Liefbe4af2018-11-21 19:10:48 +00004296 (IsConstNeg || (IsUnsigned && Subtract)) :
4297 (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004298 SemaRef.Diag(NewStep->getExprLoc(),
4299 diag::err_omp_loop_incr_not_compatible)
Kelvin Liefbe4af2018-11-21 19:10:48 +00004300 << LCDecl << TestIsLessOp.getValue() << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004301 SemaRef.Diag(ConditionLoc,
4302 diag::note_omp_loop_cond_requres_compatible_incr)
Kelvin Liefbe4af2018-11-21 19:10:48 +00004303 << TestIsLessOp.getValue() << ConditionSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004304 return true;
4305 }
Kelvin Liefbe4af2018-11-21 19:10:48 +00004306 if (TestIsLessOp.getValue() == Subtract) {
David Majnemer9d168222016-08-05 17:44:54 +00004307 NewStep =
4308 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep)
4309 .get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004310 Subtract = !Subtract;
4311 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004312 }
4313
4314 Step = NewStep;
4315 SubtractStep = Subtract;
4316 return false;
4317}
4318
Alexey Bataeve3727102018-04-18 15:57:46 +00004319bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004320 // Check init-expr for canonical loop form and save loop counter
4321 // variable - #Var and its initialization value - #LB.
4322 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
4323 // var = lb
4324 // integer-type var = lb
4325 // random-access-iterator-type var = lb
4326 // pointer-type var = lb
4327 //
4328 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00004329 if (EmitDiags) {
4330 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
4331 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004332 return true;
4333 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004334 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4335 if (!ExprTemp->cleanupsHaveSideEffects())
4336 S = ExprTemp->getSubExpr();
4337
Alexander Musmana5f070a2014-10-01 06:03:56 +00004338 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004339 if (Expr *E = dyn_cast<Expr>(S))
4340 S = E->IgnoreParens();
David Majnemer9d168222016-08-05 17:44:54 +00004341 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004342 if (BO->getOpcode() == BO_Assign) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004343 Expr *LHS = BO->getLHS()->IgnoreParens();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004344 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
4345 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4346 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataeve3727102018-04-18 15:57:46 +00004347 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4348 return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004349 }
4350 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4351 if (ME->isArrow() &&
4352 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
Alexey Bataeve3727102018-04-18 15:57:46 +00004353 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004354 }
4355 }
David Majnemer9d168222016-08-05 17:44:54 +00004356 } else if (auto *DS = dyn_cast<DeclStmt>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004357 if (DS->isSingleDecl()) {
David Majnemer9d168222016-08-05 17:44:54 +00004358 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00004359 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004360 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00004361 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004362 SemaRef.Diag(S->getBeginLoc(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004363 diag::ext_omp_loop_not_canonical_init)
4364 << S->getSourceRange();
Alexey Bataevf138fda2018-08-13 19:04:24 +00004365 return setLCDeclAndLB(
4366 Var,
4367 buildDeclRefExpr(SemaRef, Var,
4368 Var->getType().getNonReferenceType(),
4369 DS->getBeginLoc()),
4370 Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004371 }
4372 }
4373 }
David Majnemer9d168222016-08-05 17:44:54 +00004374 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004375 if (CE->getOperator() == OO_Equal) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004376 Expr *LHS = CE->getArg(0);
David Majnemer9d168222016-08-05 17:44:54 +00004377 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004378 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4379 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataeve3727102018-04-18 15:57:46 +00004380 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4381 return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004382 }
4383 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4384 if (ME->isArrow() &&
4385 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
Alexey Bataeve3727102018-04-18 15:57:46 +00004386 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004387 }
4388 }
4389 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004390
Alexey Bataeve3727102018-04-18 15:57:46 +00004391 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004392 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00004393 if (EmitDiags) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004394 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init)
Alexey Bataev9c821032015-04-30 04:23:23 +00004395 << S->getSourceRange();
4396 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004397 return true;
4398}
4399
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004400/// Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004401/// variable (which may be the loop variable) if possible.
Alexey Bataeve3727102018-04-18 15:57:46 +00004402static const ValueDecl *getInitLCDecl(const Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004403 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00004404 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004405 E = getExprAsWritten(E);
Alexey Bataeve3727102018-04-18 15:57:46 +00004406 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004407 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00004408 if ((Ctor->isCopyOrMoveConstructor() ||
4409 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4410 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004411 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00004412 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
4413 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004414 return getCanonicalDecl(VD);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004415 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004416 if (const auto *ME = dyn_cast_or_null<MemberExpr>(E))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004417 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4418 return getCanonicalDecl(ME->getMemberDecl());
4419 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004420}
4421
Alexey Bataeve3727102018-04-18 15:57:46 +00004422bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004423 // Check test-expr for canonical form, save upper-bound UB, flags for
4424 // less/greater and for strict/non-strict comparison.
4425 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4426 // var relational-op b
4427 // b relational-op var
4428 //
4429 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004430 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004431 return true;
4432 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004433 S = getExprAsWritten(S);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004434 SourceLocation CondLoc = S->getBeginLoc();
David Majnemer9d168222016-08-05 17:44:54 +00004435 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004436 if (BO->isRelationalOp()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004437 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4438 return setUB(BO->getRHS(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004439 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
4440 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4441 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +00004442 if (getInitLCDecl(BO->getRHS()) == LCDecl)
4443 return setUB(BO->getLHS(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004444 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
4445 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4446 BO->getSourceRange(), BO->getOperatorLoc());
Kelvin Liefbe4af2018-11-21 19:10:48 +00004447 } else if (BO->getOpcode() == BO_NE)
4448 return setUB(getInitLCDecl(BO->getLHS()) == LCDecl ?
4449 BO->getRHS() : BO->getLHS(),
4450 /*LessOp=*/llvm::None,
4451 /*StrictOp=*/true,
4452 BO->getSourceRange(), BO->getOperatorLoc());
David Majnemer9d168222016-08-05 17:44:54 +00004453 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004454 if (CE->getNumArgs() == 2) {
4455 auto Op = CE->getOperator();
4456 switch (Op) {
4457 case OO_Greater:
4458 case OO_GreaterEqual:
4459 case OO_Less:
4460 case OO_LessEqual:
Alexey Bataeve3727102018-04-18 15:57:46 +00004461 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4462 return setUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004463 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4464 CE->getOperatorLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +00004465 if (getInitLCDecl(CE->getArg(1)) == LCDecl)
4466 return setUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004467 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4468 CE->getOperatorLoc());
4469 break;
Gheorghe-Teodor Berceaa3afcf22019-01-09 20:38:35 +00004470 case OO_ExclaimEqual:
Kelvin Liefbe4af2018-11-21 19:10:48 +00004471 return setUB(getInitLCDecl(CE->getArg(0)) == LCDecl ?
4472 CE->getArg(1) : CE->getArg(0),
4473 /*LessOp=*/llvm::None,
4474 /*StrictOp=*/true,
4475 CE->getSourceRange(),
4476 CE->getOperatorLoc());
4477 break;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004478 default:
4479 break;
4480 }
4481 }
4482 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004483 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004484 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004485 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004486 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004487 return true;
4488}
4489
Alexey Bataeve3727102018-04-18 15:57:46 +00004490bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004491 // RHS of canonical loop form increment can be:
4492 // var + incr
4493 // incr + var
4494 // var - incr
4495 //
4496 RHS = RHS->IgnoreParenImpCasts();
David Majnemer9d168222016-08-05 17:44:54 +00004497 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004498 if (BO->isAdditiveOp()) {
4499 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataeve3727102018-04-18 15:57:46 +00004500 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4501 return setStep(BO->getRHS(), !IsAdd);
4502 if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl)
4503 return setStep(BO->getLHS(), /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004504 }
David Majnemer9d168222016-08-05 17:44:54 +00004505 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004506 bool IsAdd = CE->getOperator() == OO_Plus;
4507 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004508 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4509 return setStep(CE->getArg(1), !IsAdd);
4510 if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl)
4511 return setStep(CE->getArg(0), /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004512 }
4513 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004514 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004515 return false;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004516 SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004517 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004518 return true;
4519}
4520
Alexey Bataeve3727102018-04-18 15:57:46 +00004521bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004522 // Check incr-expr for canonical loop form and return true if it
4523 // does not conform.
4524 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4525 // ++var
4526 // var++
4527 // --var
4528 // var--
4529 // var += incr
4530 // var -= incr
4531 // var = var + incr
4532 // var = incr + var
4533 // var = var - incr
4534 //
4535 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004536 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004537 return true;
4538 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004539 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4540 if (!ExprTemp->cleanupsHaveSideEffects())
4541 S = ExprTemp->getSubExpr();
4542
Alexander Musmana5f070a2014-10-01 06:03:56 +00004543 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004544 S = S->IgnoreParens();
David Majnemer9d168222016-08-05 17:44:54 +00004545 if (auto *UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004546 if (UO->isIncrementDecrementOp() &&
Alexey Bataeve3727102018-04-18 15:57:46 +00004547 getInitLCDecl(UO->getSubExpr()) == LCDecl)
4548 return setStep(SemaRef
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004549 .ActOnIntegerConstant(UO->getBeginLoc(),
David Majnemer9d168222016-08-05 17:44:54 +00004550 (UO->isDecrementOp() ? -1 : 1))
4551 .get(),
Alexey Bataeve3727102018-04-18 15:57:46 +00004552 /*Subtract=*/false);
David Majnemer9d168222016-08-05 17:44:54 +00004553 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004554 switch (BO->getOpcode()) {
4555 case BO_AddAssign:
4556 case BO_SubAssign:
Alexey Bataeve3727102018-04-18 15:57:46 +00004557 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4558 return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004559 break;
4560 case BO_Assign:
Alexey Bataeve3727102018-04-18 15:57:46 +00004561 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4562 return checkAndSetIncRHS(BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004563 break;
4564 default:
4565 break;
4566 }
David Majnemer9d168222016-08-05 17:44:54 +00004567 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004568 switch (CE->getOperator()) {
4569 case OO_PlusPlus:
4570 case OO_MinusMinus:
Alexey Bataeve3727102018-04-18 15:57:46 +00004571 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4572 return setStep(SemaRef
David Majnemer9d168222016-08-05 17:44:54 +00004573 .ActOnIntegerConstant(
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004574 CE->getBeginLoc(),
David Majnemer9d168222016-08-05 17:44:54 +00004575 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
4576 .get(),
Alexey Bataeve3727102018-04-18 15:57:46 +00004577 /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004578 break;
4579 case OO_PlusEqual:
4580 case OO_MinusEqual:
Alexey Bataeve3727102018-04-18 15:57:46 +00004581 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4582 return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004583 break;
4584 case OO_Equal:
Alexey Bataeve3727102018-04-18 15:57:46 +00004585 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4586 return checkAndSetIncRHS(CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004587 break;
4588 default:
4589 break;
4590 }
4591 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004592 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004593 return false;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004594 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004595 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004596 return true;
4597}
Alexander Musmana5f070a2014-10-01 06:03:56 +00004598
Alexey Bataev5a3af132016-03-29 08:58:54 +00004599static ExprResult
4600tryBuildCapture(Sema &SemaRef, Expr *Capture,
Alexey Bataeve3727102018-04-18 15:57:46 +00004601 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00004602 if (SemaRef.CurContext->isDependentContext())
4603 return ExprResult(Capture);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004604 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
4605 return SemaRef.PerformImplicitConversion(
4606 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
4607 /*AllowExplicit=*/true);
4608 auto I = Captures.find(Capture);
4609 if (I != Captures.end())
4610 return buildCapture(SemaRef, Capture, I->second);
4611 DeclRefExpr *Ref = nullptr;
4612 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
4613 Captures[Capture] = Ref;
4614 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004615}
4616
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004617/// Build the expression to calculate the number of iterations.
Alexey Bataeve3727102018-04-18 15:57:46 +00004618Expr *OpenMPIterationSpaceChecker::buildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004619 Scope *S, const bool LimitedType,
Alexey Bataeve3727102018-04-18 15:57:46 +00004620 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004621 ExprResult Diff;
Alexey Bataeve3727102018-04-18 15:57:46 +00004622 QualType VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004623 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004624 SemaRef.getLangOpts().CPlusPlus) {
4625 // Upper - Lower
Kelvin Liefbe4af2018-11-21 19:10:48 +00004626 Expr *UBExpr = TestIsLessOp.getValue() ? UB : LB;
4627 Expr *LBExpr = TestIsLessOp.getValue() ? LB : UB;
Alexey Bataev5a3af132016-03-29 08:58:54 +00004628 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
4629 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004630 if (!Upper || !Lower)
4631 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004632
4633 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4634
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004635 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004636 // BuildBinOp already emitted error, this one is to point user to upper
4637 // and lower bound, and to tell what is passed to 'operator-'.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004638 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004639 << Upper->getSourceRange() << Lower->getSourceRange();
4640 return nullptr;
4641 }
4642 }
4643
4644 if (!Diff.isUsable())
4645 return nullptr;
4646
4647 // Upper - Lower [- 1]
4648 if (TestIsStrictOp)
4649 Diff = SemaRef.BuildBinOp(
4650 S, DefaultLoc, BO_Sub, Diff.get(),
4651 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4652 if (!Diff.isUsable())
4653 return nullptr;
4654
4655 // Upper - Lower [- 1] + Step
Alexey Bataeve3727102018-04-18 15:57:46 +00004656 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004657 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004658 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004659 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004660 if (!Diff.isUsable())
4661 return nullptr;
4662
4663 // Parentheses (for dumping/debugging purposes only).
4664 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4665 if (!Diff.isUsable())
4666 return nullptr;
4667
4668 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004669 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004670 if (!Diff.isUsable())
4671 return nullptr;
4672
Alexander Musman174b3ca2014-10-06 11:16:29 +00004673 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004674 QualType Type = Diff.get()->getType();
Alexey Bataeve3727102018-04-18 15:57:46 +00004675 ASTContext &C = SemaRef.Context;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004676 bool UseVarType = VarType->hasIntegerRepresentation() &&
4677 C.getTypeSize(Type) > C.getTypeSize(VarType);
4678 if (!Type->isIntegerType() || UseVarType) {
4679 unsigned NewSize =
4680 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4681 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4682 : Type->hasSignedIntegerRepresentation();
4683 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00004684 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4685 Diff = SemaRef.PerformImplicitConversion(
4686 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4687 if (!Diff.isUsable())
4688 return nullptr;
4689 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004690 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004691 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00004692 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4693 if (NewSize != C.getTypeSize(Type)) {
4694 if (NewSize < C.getTypeSize(Type)) {
4695 assert(NewSize == 64 && "incorrect loop var size");
4696 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4697 << InitSrcRange << ConditionSrcRange;
4698 }
4699 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004700 NewSize, Type->hasSignedIntegerRepresentation() ||
4701 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00004702 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4703 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4704 Sema::AA_Converting, true);
4705 if (!Diff.isUsable())
4706 return nullptr;
4707 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004708 }
4709 }
4710
Alexander Musmana5f070a2014-10-01 06:03:56 +00004711 return Diff.get();
4712}
4713
Alexey Bataeve3727102018-04-18 15:57:46 +00004714Expr *OpenMPIterationSpaceChecker::buildPreCond(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004715 Scope *S, Expr *Cond,
Alexey Bataeve3727102018-04-18 15:57:46 +00004716 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004717 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4718 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4719 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004720
Alexey Bataeve3727102018-04-18 15:57:46 +00004721 ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures);
4722 ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004723 if (!NewLB.isUsable() || !NewUB.isUsable())
4724 return nullptr;
4725
Alexey Bataeve3727102018-04-18 15:57:46 +00004726 ExprResult CondExpr =
4727 SemaRef.BuildBinOp(S, DefaultLoc,
Gheorghe-Teodor Berceaa3afcf22019-01-09 20:38:35 +00004728 TestIsLessOp.getValue() ?
Kelvin Liefbe4af2018-11-21 19:10:48 +00004729 (TestIsStrictOp ? BO_LT : BO_LE) :
4730 (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataeve3727102018-04-18 15:57:46 +00004731 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004732 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004733 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4734 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00004735 CondExpr = SemaRef.PerformImplicitConversion(
4736 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4737 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004738 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00004739 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
Sergi Mateo Bellidof3e00fe2019-02-01 08:39:01 +00004740 // Otherwise use original loop condition and evaluate it in runtime.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004741 return CondExpr.isUsable() ? CondExpr.get() : Cond;
4742}
4743
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004744/// Build reference expression to the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004745DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar(
Alexey Bataevf138fda2018-08-13 19:04:24 +00004746 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
4747 DSAStackTy &DSA) const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004748 auto *VD = dyn_cast<VarDecl>(LCDecl);
4749 if (!VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004750 VD = SemaRef.isOpenMPCapturedDecl(LCDecl);
4751 DeclRefExpr *Ref = buildDeclRefExpr(
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004752 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
Alexey Bataeve3727102018-04-18 15:57:46 +00004753 const DSAStackTy::DSAVarData Data =
4754 DSA.getTopDSA(LCDecl, /*FromParent=*/false);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004755 // If the loop control decl is explicitly marked as private, do not mark it
4756 // as captured again.
4757 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4758 Captures.insert(std::make_pair(LCRef, Ref));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004759 return Ref;
4760 }
Alexey Bataev0d8fcdf2019-03-14 20:36:00 +00004761 return cast<DeclRefExpr>(LCRef);
Alexey Bataeva8899172015-08-06 12:30:57 +00004762}
4763
Alexey Bataeve3727102018-04-18 15:57:46 +00004764Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004765 if (LCDecl && !LCDecl->isInvalidDecl()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004766 QualType Type = LCDecl->getType().getNonReferenceType();
4767 VarDecl *PrivateVar = buildVarDecl(
Alexey Bataev63cc8e92018-03-20 14:45:59 +00004768 SemaRef, DefaultLoc, Type, LCDecl->getName(),
4769 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr,
4770 isa<VarDecl>(LCDecl)
4771 ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc)
4772 : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00004773 if (PrivateVar->isInvalidDecl())
4774 return nullptr;
4775 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4776 }
4777 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004778}
4779
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004780/// Build initialization of the counter to be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004781Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004782
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004783/// Build step of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004784Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004785
Alexey Bataevf138fda2018-08-13 19:04:24 +00004786Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData(
4787 Scope *S, Expr *Counter,
4788 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc,
4789 Expr *Inc, OverloadedOperatorKind OOK) {
4790 Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get();
4791 if (!Cnt)
4792 return nullptr;
4793 if (Inc) {
4794 assert((OOK == OO_Plus || OOK == OO_Minus) &&
4795 "Expected only + or - operations for depend clauses.");
4796 BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub;
4797 Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get();
4798 if (!Cnt)
4799 return nullptr;
4800 }
4801 ExprResult Diff;
4802 QualType VarType = LCDecl->getType().getNonReferenceType();
4803 if (VarType->isIntegerType() || VarType->isPointerType() ||
4804 SemaRef.getLangOpts().CPlusPlus) {
4805 // Upper - Lower
Alexey Bataev316ccf62019-01-29 18:51:58 +00004806 Expr *Upper = TestIsLessOp.getValue()
4807 ? Cnt
4808 : tryBuildCapture(SemaRef, UB, Captures).get();
4809 Expr *Lower = TestIsLessOp.getValue()
4810 ? tryBuildCapture(SemaRef, LB, Captures).get()
4811 : Cnt;
Alexey Bataevf138fda2018-08-13 19:04:24 +00004812 if (!Upper || !Lower)
4813 return nullptr;
4814
4815 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4816
4817 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
4818 // BuildBinOp already emitted error, this one is to point user to upper
4819 // and lower bound, and to tell what is passed to 'operator-'.
4820 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
4821 << Upper->getSourceRange() << Lower->getSourceRange();
4822 return nullptr;
4823 }
4824 }
4825
4826 if (!Diff.isUsable())
4827 return nullptr;
4828
4829 // Parentheses (for dumping/debugging purposes only).
4830 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4831 if (!Diff.isUsable())
4832 return nullptr;
4833
4834 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
4835 if (!NewStep.isUsable())
4836 return nullptr;
4837 // (Upper - Lower) / Step
4838 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
4839 if (!Diff.isUsable())
4840 return nullptr;
4841
4842 return Diff.get();
4843}
4844
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004845/// Iteration space of a single for loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004846struct LoopIterationSpace final {
Alexey Bataev316ccf62019-01-29 18:51:58 +00004847 /// True if the condition operator is the strict compare operator (<, > or
4848 /// !=).
4849 bool IsStrictCompare = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004850 /// Condition of the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004851 Expr *PreCond = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004852 /// This expression calculates the number of iterations in the loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004853 /// It is always possible to calculate it before starting the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004854 Expr *NumIterations = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004855 /// The loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004856 Expr *CounterVar = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004857 /// Private loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004858 Expr *PrivateCounterVar = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004859 /// This is initializer for the initial value of #CounterVar.
Alexey Bataev8b427062016-05-25 12:36:08 +00004860 Expr *CounterInit = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004861 /// This is step for the #CounterVar used to generate its update:
Alexander Musmana5f070a2014-10-01 06:03:56 +00004862 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
Alexey Bataev8b427062016-05-25 12:36:08 +00004863 Expr *CounterStep = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004864 /// Should step be subtracted?
Alexey Bataev8b427062016-05-25 12:36:08 +00004865 bool Subtract = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004866 /// Source range of the loop init.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004867 SourceRange InitSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004868 /// Source range of the loop condition.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004869 SourceRange CondSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004870 /// Source range of the loop increment.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004871 SourceRange IncSrcRange;
4872};
4873
Alexey Bataev23b69422014-06-18 07:08:49 +00004874} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004875
Alexey Bataev9c821032015-04-30 04:23:23 +00004876void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4877 assert(getLangOpts().OpenMP && "OpenMP is not active.");
4878 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004879 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4880 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00004881 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevce901812018-12-19 18:16:37 +00004882 DSAStack->loopStart();
Alexey Bataev9c821032015-04-30 04:23:23 +00004883 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataeve3727102018-04-18 15:57:46 +00004884 if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) {
4885 if (ValueDecl *D = ISC.getLoopDecl()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004886 auto *VD = dyn_cast<VarDecl>(D);
4887 if (!VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004888 if (VarDecl *Private = isOpenMPCapturedDecl(D)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004889 VD = Private;
Alexey Bataeve3727102018-04-18 15:57:46 +00004890 } else {
4891 DeclRefExpr *Ref = buildCapture(*this, D, ISC.getLoopDeclRefExpr(),
4892 /*WithInit=*/false);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004893 VD = cast<VarDecl>(Ref->getDecl());
4894 }
4895 }
4896 DSAStack->addLoopControlVariable(D, VD);
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00004897 const Decl *LD = DSAStack->getPossiblyLoopCunter();
4898 if (LD != D->getCanonicalDecl()) {
4899 DSAStack->resetPossibleLoopCounter();
4900 if (auto *Var = dyn_cast_or_null<VarDecl>(LD))
4901 MarkDeclarationsReferencedInExpr(
4902 buildDeclRefExpr(*this, const_cast<VarDecl *>(Var),
4903 Var->getType().getNonLValueExprType(Context),
4904 ForLoc, /*RefersToCapture=*/true));
4905 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004906 }
4907 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004908 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00004909 }
4910}
4911
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004912/// Called on a for stmt to check and extract its iteration space
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004913/// for further processing (such as collapsing).
Alexey Bataeve3727102018-04-18 15:57:46 +00004914static bool checkOpenMPIterationSpace(
Alexey Bataev4acb8592014-07-07 13:01:15 +00004915 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4916 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataevf138fda2018-08-13 19:04:24 +00004917 unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr,
4918 Expr *OrderedLoopCountExpr,
Alexey Bataeve3727102018-04-18 15:57:46 +00004919 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004920 LoopIterationSpace &ResultIterSpace,
Alexey Bataeve3727102018-04-18 15:57:46 +00004921 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004922 // OpenMP [2.6, Canonical Loop Form]
4923 // for (init-expr; test-expr; incr-expr) structured-block
David Majnemer9d168222016-08-05 17:44:54 +00004924 auto *For = dyn_cast_or_null<ForStmt>(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004925 if (!For) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004926 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004927 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
Alexey Bataevf138fda2018-08-13 19:04:24 +00004928 << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount
Alexey Bataev10e775f2015-07-30 11:36:16 +00004929 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
Alexey Bataevf138fda2018-08-13 19:04:24 +00004930 if (TotalNestedLoopCount > 1) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00004931 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4932 SemaRef.Diag(DSA.getConstructLoc(),
4933 diag::note_omp_collapse_ordered_expr)
4934 << 2 << CollapseLoopCountExpr->getSourceRange()
4935 << OrderedLoopCountExpr->getSourceRange();
4936 else if (CollapseLoopCountExpr)
4937 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4938 diag::note_omp_collapse_ordered_expr)
4939 << 0 << CollapseLoopCountExpr->getSourceRange();
4940 else
4941 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4942 diag::note_omp_collapse_ordered_expr)
4943 << 1 << OrderedLoopCountExpr->getSourceRange();
4944 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004945 return true;
4946 }
4947 assert(For->getBody());
4948
4949 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4950
4951 // Check init.
Alexey Bataeve3727102018-04-18 15:57:46 +00004952 Stmt *Init = For->getInit();
4953 if (ISC.checkAndSetInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004954 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004955
4956 bool HasErrors = false;
4957
4958 // Check loop variable's type.
Alexey Bataeve3727102018-04-18 15:57:46 +00004959 if (ValueDecl *LCDecl = ISC.getLoopDecl()) {
4960 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004961
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004962 // OpenMP [2.6, Canonical Loop Form]
4963 // Var is one of the following:
4964 // A variable of signed or unsigned integer type.
4965 // For C++, a variable of a random access iterator type.
4966 // For C, a variable of a pointer type.
Alexey Bataeve3727102018-04-18 15:57:46 +00004967 QualType VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004968 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4969 !VarType->isPointerType() &&
4970 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004971 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004972 << SemaRef.getLangOpts().CPlusPlus;
4973 HasErrors = true;
4974 }
4975
4976 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4977 // a Construct
4978 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4979 // parallel for construct is (are) private.
4980 // The loop iteration variable in the associated for-loop of a simd
4981 // construct with just one associated for-loop is linear with a
4982 // constant-linear-step that is the increment of the associated for-loop.
4983 // Exclude loop var from the list of variables with implicitly defined data
4984 // sharing attributes.
4985 VarsWithImplicitDSA.erase(LCDecl);
4986
4987 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4988 // in a Construct, C/C++].
4989 // The loop iteration variable in the associated for-loop of a simd
4990 // construct with just one associated for-loop may be listed in a linear
4991 // clause with a constant-linear-step that is the increment of the
4992 // associated for-loop.
4993 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4994 // parallel for construct may be listed in a private or lastprivate clause.
4995 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4996 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4997 // declared in the loop and it is predetermined as a private.
Alexey Bataeve3727102018-04-18 15:57:46 +00004998 OpenMPClauseKind PredeterminedCKind =
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004999 isOpenMPSimdDirective(DKind)
5000 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
5001 : OMPC_private;
5002 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
5003 DVar.CKind != PredeterminedCKind) ||
5004 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
5005 isOpenMPDistributeDirective(DKind)) &&
5006 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
5007 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
5008 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005009 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005010 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
5011 << getOpenMPClauseName(PredeterminedCKind);
5012 if (DVar.RefExpr == nullptr)
5013 DVar.CKind = PredeterminedCKind;
Alexey Bataeve3727102018-04-18 15:57:46 +00005014 reportOriginalDsa(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005015 HasErrors = true;
5016 } else if (LoopDeclRefExpr != nullptr) {
5017 // Make the loop iteration variable private (for worksharing constructs),
5018 // linear (for simd directives with the only one associated loop) or
5019 // lastprivate (for simd directives with several collapsed or ordered
5020 // loops).
5021 if (DVar.CKind == OMPC_unknown)
Alexey Bataevc2cdff62019-01-29 21:12:28 +00005022 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005023 }
5024
5025 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
5026
5027 // Check test-expr.
Alexey Bataeve3727102018-04-18 15:57:46 +00005028 HasErrors |= ISC.checkAndSetCond(For->getCond());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005029
5030 // Check incr-expr.
Alexey Bataeve3727102018-04-18 15:57:46 +00005031 HasErrors |= ISC.checkAndSetInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005032 }
5033
Alexey Bataeve3727102018-04-18 15:57:46 +00005034 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005035 return HasErrors;
5036
Alexander Musmana5f070a2014-10-01 06:03:56 +00005037 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00005038 ResultIterSpace.PreCond =
Alexey Bataeve3727102018-04-18 15:57:46 +00005039 ISC.buildPreCond(DSA.getCurScope(), For->getCond(), Captures);
5040 ResultIterSpace.NumIterations = ISC.buildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00005041 DSA.getCurScope(),
5042 (isOpenMPWorksharingDirective(DKind) ||
5043 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
5044 Captures);
Alexey Bataeve3727102018-04-18 15:57:46 +00005045 ResultIterSpace.CounterVar = ISC.buildCounterVar(Captures, DSA);
5046 ResultIterSpace.PrivateCounterVar = ISC.buildPrivateCounterVar();
5047 ResultIterSpace.CounterInit = ISC.buildCounterInit();
5048 ResultIterSpace.CounterStep = ISC.buildCounterStep();
5049 ResultIterSpace.InitSrcRange = ISC.getInitSrcRange();
5050 ResultIterSpace.CondSrcRange = ISC.getConditionSrcRange();
5051 ResultIterSpace.IncSrcRange = ISC.getIncrementSrcRange();
5052 ResultIterSpace.Subtract = ISC.shouldSubtractStep();
Alexey Bataev316ccf62019-01-29 18:51:58 +00005053 ResultIterSpace.IsStrictCompare = ISC.isStrictTestOp();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005054
Alexey Bataev62dbb972015-04-22 11:59:37 +00005055 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
5056 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00005057 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00005058 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00005059 ResultIterSpace.CounterInit == nullptr ||
5060 ResultIterSpace.CounterStep == nullptr);
Alexey Bataevf138fda2018-08-13 19:04:24 +00005061 if (!HasErrors && DSA.isOrderedRegion()) {
5062 if (DSA.getOrderedRegionParam().second->getNumForLoops()) {
5063 if (CurrentNestedLoopCount <
5064 DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) {
5065 DSA.getOrderedRegionParam().second->setLoopNumIterations(
5066 CurrentNestedLoopCount, ResultIterSpace.NumIterations);
5067 DSA.getOrderedRegionParam().second->setLoopCounter(
5068 CurrentNestedLoopCount, ResultIterSpace.CounterVar);
5069 }
5070 }
5071 for (auto &Pair : DSA.getDoacrossDependClauses()) {
5072 if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) {
5073 // Erroneous case - clause has some problems.
5074 continue;
5075 }
5076 if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink &&
5077 Pair.second.size() <= CurrentNestedLoopCount) {
5078 // Erroneous case - clause has some problems.
5079 Pair.first->setLoopData(CurrentNestedLoopCount, nullptr);
5080 continue;
5081 }
5082 Expr *CntValue;
5083 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
5084 CntValue = ISC.buildOrderedLoopData(
5085 DSA.getCurScope(), ResultIterSpace.CounterVar, Captures,
5086 Pair.first->getDependencyLoc());
5087 else
5088 CntValue = ISC.buildOrderedLoopData(
5089 DSA.getCurScope(), ResultIterSpace.CounterVar, Captures,
5090 Pair.first->getDependencyLoc(),
5091 Pair.second[CurrentNestedLoopCount].first,
5092 Pair.second[CurrentNestedLoopCount].second);
5093 Pair.first->setLoopData(CurrentNestedLoopCount, CntValue);
5094 }
5095 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005096
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005097 return HasErrors;
5098}
5099
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005100/// Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00005101static ExprResult
Alexey Bataeve3727102018-04-18 15:57:46 +00005102buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005103 ExprResult Start,
Alexey Bataeve3727102018-04-18 15:57:46 +00005104 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005105 // Build 'VarRef = Start.
Alexey Bataeve3727102018-04-18 15:57:46 +00005106 ExprResult NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00005107 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005108 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00005109 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00005110 VarRef.get()->getType())) {
5111 NewStart = SemaRef.PerformImplicitConversion(
5112 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
5113 /*AllowExplicit=*/true);
5114 if (!NewStart.isUsable())
5115 return ExprError();
5116 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005117
Alexey Bataeve3727102018-04-18 15:57:46 +00005118 ExprResult Init =
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005119 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
5120 return Init;
5121}
5122
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005123/// Build 'VarRef = Start + Iter * Step'.
Alexey Bataeve3727102018-04-18 15:57:46 +00005124static ExprResult buildCounterUpdate(
5125 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
5126 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract,
5127 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00005128 // Add parentheses (for debugging purposes only).
5129 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
5130 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
5131 !Step.isUsable())
5132 return ExprError();
5133
Alexey Bataev5a3af132016-03-29 08:58:54 +00005134 ExprResult NewStep = Step;
5135 if (Captures)
5136 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005137 if (NewStep.isInvalid())
5138 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005139 ExprResult Update =
5140 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005141 if (!Update.isUsable())
5142 return ExprError();
5143
Alexey Bataevc0214e02016-02-16 12:13:49 +00005144 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
5145 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00005146 ExprResult NewStart = Start;
5147 if (Captures)
5148 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005149 if (NewStart.isInvalid())
5150 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005151
Alexey Bataevc0214e02016-02-16 12:13:49 +00005152 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
5153 ExprResult SavedUpdate = Update;
5154 ExprResult UpdateVal;
5155 if (VarRef.get()->getType()->isOverloadableType() ||
5156 NewStart.get()->getType()->isOverloadableType() ||
5157 Update.get()->getType()->isOverloadableType()) {
5158 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
5159 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
5160 Update =
5161 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
5162 if (Update.isUsable()) {
5163 UpdateVal =
5164 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
5165 VarRef.get(), SavedUpdate.get());
5166 if (UpdateVal.isUsable()) {
5167 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
5168 UpdateVal.get());
5169 }
5170 }
5171 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
5172 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005173
Alexey Bataevc0214e02016-02-16 12:13:49 +00005174 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
5175 if (!Update.isUsable() || !UpdateVal.isUsable()) {
5176 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
5177 NewStart.get(), SavedUpdate.get());
5178 if (!Update.isUsable())
5179 return ExprError();
5180
Alexey Bataev11481f52016-02-17 10:29:05 +00005181 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
5182 VarRef.get()->getType())) {
5183 Update = SemaRef.PerformImplicitConversion(
5184 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
5185 if (!Update.isUsable())
5186 return ExprError();
5187 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00005188
5189 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
5190 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005191 return Update;
5192}
5193
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005194/// Convert integer expression \a E to make it have at least \a Bits
Alexander Musmana5f070a2014-10-01 06:03:56 +00005195/// bits.
Alexey Bataeve3727102018-04-18 15:57:46 +00005196static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00005197 if (E == nullptr)
5198 return ExprError();
Alexey Bataeve3727102018-04-18 15:57:46 +00005199 ASTContext &C = SemaRef.Context;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005200 QualType OldType = E->getType();
5201 unsigned HasBits = C.getTypeSize(OldType);
5202 if (HasBits >= Bits)
5203 return ExprResult(E);
5204 // OK to convert to signed, because new type has more bits than old.
5205 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
5206 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
5207 true);
5208}
5209
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005210/// Check if the given expression \a E is a constant integer that fits
Alexander Musmana5f070a2014-10-01 06:03:56 +00005211/// into \a Bits bits.
Alexey Bataeve3727102018-04-18 15:57:46 +00005212static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00005213 if (E == nullptr)
5214 return false;
5215 llvm::APSInt Result;
5216 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
5217 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
5218 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005219}
5220
Alexey Bataev5a3af132016-03-29 08:58:54 +00005221/// Build preinits statement for the given declarations.
5222static Stmt *buildPreInits(ASTContext &Context,
Alexey Bataevc5514062017-10-25 15:44:52 +00005223 MutableArrayRef<Decl *> PreInits) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005224 if (!PreInits.empty()) {
5225 return new (Context) DeclStmt(
5226 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
5227 SourceLocation(), SourceLocation());
5228 }
5229 return nullptr;
5230}
5231
5232/// Build preinits statement for the given declarations.
Alexey Bataevc5514062017-10-25 15:44:52 +00005233static Stmt *
5234buildPreInits(ASTContext &Context,
Alexey Bataeve3727102018-04-18 15:57:46 +00005235 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005236 if (!Captures.empty()) {
5237 SmallVector<Decl *, 16> PreInits;
Alexey Bataeve3727102018-04-18 15:57:46 +00005238 for (const auto &Pair : Captures)
Alexey Bataev5a3af132016-03-29 08:58:54 +00005239 PreInits.push_back(Pair.second->getDecl());
5240 return buildPreInits(Context, PreInits);
5241 }
5242 return nullptr;
5243}
5244
5245/// Build postupdate expression for the given list of postupdates expressions.
5246static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
5247 Expr *PostUpdate = nullptr;
5248 if (!PostUpdates.empty()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00005249 for (Expr *E : PostUpdates) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005250 Expr *ConvE = S.BuildCStyleCastExpr(
5251 E->getExprLoc(),
5252 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
5253 E->getExprLoc(), E)
5254 .get();
5255 PostUpdate = PostUpdate
5256 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
5257 PostUpdate, ConvE)
5258 .get()
5259 : ConvE;
5260 }
5261 }
5262 return PostUpdate;
5263}
5264
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005265/// Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00005266/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
5267/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005268static unsigned
Alexey Bataeve3727102018-04-18 15:57:46 +00005269checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
Alexey Bataev10e775f2015-07-30 11:36:16 +00005270 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
5271 DSAStackTy &DSA,
Alexey Bataeve3727102018-04-18 15:57:46 +00005272 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00005273 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005274 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005275 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005276 // Found 'collapse' clause - calculate collapse number.
Fangrui Song407659a2018-11-30 23:41:18 +00005277 Expr::EvalResult Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005278 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Fangrui Song407659a2018-11-30 23:41:18 +00005279 NestedLoopCount = Result.Val.getInt().getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005280 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00005281 unsigned OrderedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005282 if (OrderedLoopCountExpr) {
5283 // Found 'ordered' clause - calculate collapse number.
Fangrui Song407659a2018-11-30 23:41:18 +00005284 Expr::EvalResult EVResult;
5285 if (OrderedLoopCountExpr->EvaluateAsInt(EVResult, SemaRef.getASTContext())) {
5286 llvm::APSInt Result = EVResult.Val.getInt();
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005287 if (Result.getLimitedValue() < NestedLoopCount) {
5288 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
5289 diag::err_omp_wrong_ordered_loop_count)
5290 << OrderedLoopCountExpr->getSourceRange();
5291 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
5292 diag::note_collapse_loop_count)
5293 << CollapseLoopCountExpr->getSourceRange();
5294 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00005295 OrderedLoopCount = Result.getLimitedValue();
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005296 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005297 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005298 // This is helper routine for loop directives (e.g., 'for', 'simd',
5299 // 'for simd', etc.).
Alexey Bataeve3727102018-04-18 15:57:46 +00005300 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev316ccf62019-01-29 18:51:58 +00005301 SmallVector<LoopIterationSpace, 4> IterSpaces(
5302 std::max(OrderedLoopCount, NestedLoopCount));
Alexander Musmana5f070a2014-10-01 06:03:56 +00005303 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005304 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataevf138fda2018-08-13 19:04:24 +00005305 if (checkOpenMPIterationSpace(
5306 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
5307 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
5308 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt],
5309 Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00005310 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005311 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005312 // OpenMP [2.8.1, simd construct, Restrictions]
5313 // All loops associated with the construct must be perfectly nested; that
5314 // is, there must be no intervening code nor any OpenMP directive between
5315 // any two loops.
5316 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005317 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00005318 for (unsigned Cnt = NestedLoopCount; Cnt < OrderedLoopCount; ++Cnt) {
5319 if (checkOpenMPIterationSpace(
5320 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
5321 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
5322 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt],
5323 Captures))
5324 return 0;
5325 if (Cnt > 0 && IterSpaces[Cnt].CounterVar) {
5326 // Handle initialization of captured loop iterator variables.
5327 auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar);
5328 if (isa<OMPCapturedExprDecl>(DRE->getDecl())) {
5329 Captures[DRE] = DRE;
5330 }
5331 }
5332 // Move on to the next nested for loop, or to the loop body.
5333 // OpenMP [2.8.1, simd construct, Restrictions]
5334 // All loops associated with the construct must be perfectly nested; that
5335 // is, there must be no intervening code nor any OpenMP directive between
5336 // any two loops.
5337 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
5338 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005339
Alexander Musmana5f070a2014-10-01 06:03:56 +00005340 Built.clear(/* size */ NestedLoopCount);
5341
5342 if (SemaRef.CurContext->isDependentContext())
5343 return NestedLoopCount;
5344
5345 // An example of what is generated for the following code:
5346 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00005347 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00005348 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00005349 // for (k = 0; k < NK; ++k)
5350 // for (j = J0; j < NJ; j+=2) {
5351 // <loop body>
5352 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005353 //
5354 // We generate the code below.
5355 // Note: the loop body may be outlined in CodeGen.
5356 // Note: some counters may be C++ classes, operator- is used to find number of
5357 // iterations and operator+= to calculate counter value.
5358 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
5359 // or i64 is currently supported).
5360 //
5361 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
5362 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
5363 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
5364 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
5365 // // similar updates for vars in clauses (e.g. 'linear')
5366 // <loop body (using local i and j)>
5367 // }
5368 // i = NI; // assign final values of counters
5369 // j = NJ;
5370 //
5371
5372 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
5373 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00005374 // Precondition tests if there is at least one iteration (all conditions are
5375 // true).
5376 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexey Bataeve3727102018-04-18 15:57:46 +00005377 Expr *N0 = IterSpaces[0].NumIterations;
5378 ExprResult LastIteration32 =
5379 widenIterationCount(/*Bits=*/32,
5380 SemaRef
5381 .PerformImplicitConversion(
5382 N0->IgnoreImpCasts(), N0->getType(),
5383 Sema::AA_Converting, /*AllowExplicit=*/true)
5384 .get(),
5385 SemaRef);
5386 ExprResult LastIteration64 = widenIterationCount(
5387 /*Bits=*/64,
5388 SemaRef
5389 .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(),
5390 Sema::AA_Converting,
5391 /*AllowExplicit=*/true)
5392 .get(),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005393 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005394
5395 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
5396 return NestedLoopCount;
5397
Alexey Bataeve3727102018-04-18 15:57:46 +00005398 ASTContext &C = SemaRef.Context;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005399 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
5400
5401 Scope *CurScope = DSA.getCurScope();
5402 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00005403 if (PreCond.isUsable()) {
Alexey Bataeva7206b92016-12-20 16:51:02 +00005404 PreCond =
5405 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd,
5406 PreCond.get(), IterSpaces[Cnt].PreCond);
Alexey Bataev62dbb972015-04-22 11:59:37 +00005407 }
Alexey Bataeve3727102018-04-18 15:57:46 +00005408 Expr *N = IterSpaces[Cnt].NumIterations;
Alexey Bataeva7206b92016-12-20 16:51:02 +00005409 SourceLocation Loc = N->getExprLoc();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005410 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
5411 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005412 LastIteration32 = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005413 CurScope, Loc, BO_Mul, LastIteration32.get(),
David Majnemer9d168222016-08-05 17:44:54 +00005414 SemaRef
5415 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5416 Sema::AA_Converting,
5417 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005418 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005419 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005420 LastIteration64 = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005421 CurScope, Loc, BO_Mul, LastIteration64.get(),
David Majnemer9d168222016-08-05 17:44:54 +00005422 SemaRef
5423 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5424 Sema::AA_Converting,
5425 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005426 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005427 }
5428
5429 // Choose either the 32-bit or 64-bit version.
5430 ExprResult LastIteration = LastIteration64;
Gheorghe-Teodor Berceaa3afcf22019-01-09 20:38:35 +00005431 if (SemaRef.getLangOpts().OpenMPOptimisticCollapse ||
5432 (LastIteration32.isUsable() &&
5433 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
5434 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
5435 fitsInto(
5436 /*Bits=*/32,
5437 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
5438 LastIteration64.get(), SemaRef))))
Alexander Musmana5f070a2014-10-01 06:03:56 +00005439 LastIteration = LastIteration32;
Alexey Bataev7292c292016-04-25 12:22:29 +00005440 QualType VType = LastIteration.get()->getType();
5441 QualType RealVType = VType;
5442 QualType StrideVType = VType;
5443 if (isOpenMPTaskLoopDirective(DKind)) {
5444 VType =
5445 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
5446 StrideVType =
5447 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
5448 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005449
5450 if (!LastIteration.isUsable())
5451 return 0;
5452
5453 // Save the number of iterations.
5454 ExprResult NumIterations = LastIteration;
5455 {
5456 LastIteration = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005457 CurScope, LastIteration.get()->getExprLoc(), BO_Sub,
5458 LastIteration.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00005459 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5460 if (!LastIteration.isUsable())
5461 return 0;
5462 }
5463
5464 // Calculate the last iteration number beforehand instead of doing this on
5465 // each iteration. Do not do this if the number of iterations may be kfold-ed.
5466 llvm::APSInt Result;
5467 bool IsConstant =
5468 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
5469 ExprResult CalcLastIteration;
5470 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005471 ExprResult SaveRef =
5472 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005473 LastIteration = SaveRef;
5474
5475 // Prepare SaveRef + 1.
5476 NumIterations = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005477 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00005478 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5479 if (!NumIterations.isUsable())
5480 return 0;
5481 }
5482
5483 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
5484
David Majnemer9d168222016-08-05 17:44:54 +00005485 // Build variables passed into runtime, necessary for worksharing directives.
Carlo Bertolliffafe102017-04-20 00:39:39 +00005486 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005487 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5488 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005489 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005490 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
5491 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005492 SemaRef.AddInitializerToDecl(LBDecl,
5493 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5494 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005495
5496 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005497 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
5498 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005499 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
Richard Smith3beb7c62017-01-12 02:27:38 +00005500 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005501
5502 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
5503 // This will be used to implement clause 'lastprivate'.
5504 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005505 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
5506 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005507 SemaRef.AddInitializerToDecl(ILDecl,
5508 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5509 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005510
5511 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev7292c292016-04-25 12:22:29 +00005512 VarDecl *STDecl =
5513 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
5514 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005515 SemaRef.AddInitializerToDecl(STDecl,
5516 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
5517 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005518
5519 // Build expression: UB = min(UB, LastIteration)
David Majnemer9d168222016-08-05 17:44:54 +00005520 // It is necessary for CodeGen of directives with static scheduling.
Alexander Musmanc6388682014-12-15 07:07:06 +00005521 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
5522 UB.get(), LastIteration.get());
5523 ExprResult CondOp = SemaRef.ActOnConditionalOp(
Alexey Bataev86ec3fe2018-07-25 14:40:26 +00005524 LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(),
5525 LastIteration.get(), UB.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00005526 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
5527 CondOp.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005528 EUB = SemaRef.ActOnFinishFullExpr(EUB.get(), /*DiscardedValue*/ false);
Carlo Bertolli9925f152016-06-27 14:55:37 +00005529
5530 // If we have a combined directive that combines 'distribute', 'for' or
5531 // 'simd' we need to be able to access the bounds of the schedule of the
5532 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
5533 // by scheduling 'distribute' have to be passed to the schedule of 'for'.
5534 if (isOpenMPLoopBoundSharingDirective(DKind)) {
Carlo Bertolliffafe102017-04-20 00:39:39 +00005535 // Lower bound variable, initialized with zero.
5536 VarDecl *CombLBDecl =
5537 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb");
5538 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc);
5539 SemaRef.AddInitializerToDecl(
5540 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5541 /*DirectInit*/ false);
5542
5543 // Upper bound variable, initialized with last iteration number.
5544 VarDecl *CombUBDecl =
5545 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub");
5546 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc);
5547 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(),
5548 /*DirectInit*/ false);
5549
5550 ExprResult CombIsUBGreater = SemaRef.BuildBinOp(
5551 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get());
5552 ExprResult CombCondOp =
5553 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(),
5554 LastIteration.get(), CombUB.get());
5555 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(),
5556 CombCondOp.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005557 CombEUB =
5558 SemaRef.ActOnFinishFullExpr(CombEUB.get(), /*DiscardedValue*/ false);
Carlo Bertolliffafe102017-04-20 00:39:39 +00005559
Alexey Bataeve3727102018-04-18 15:57:46 +00005560 const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005561 // We expect to have at least 2 more parameters than the 'parallel'
5562 // directive does - the lower and upper bounds of the previous schedule.
5563 assert(CD->getNumParams() >= 4 &&
5564 "Unexpected number of parameters in loop combined directive");
5565
5566 // Set the proper type for the bounds given what we learned from the
5567 // enclosed loops.
Alexey Bataeve3727102018-04-18 15:57:46 +00005568 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
5569 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
Carlo Bertolli9925f152016-06-27 14:55:37 +00005570
5571 // Previous lower and upper bounds are obtained from the region
5572 // parameters.
5573 PrevLB =
5574 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
5575 PrevUB =
5576 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
5577 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005578 }
5579
5580 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005581 ExprResult IV;
Carlo Bertolliffafe102017-04-20 00:39:39 +00005582 ExprResult Init, CombInit;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005583 {
Alexey Bataev7292c292016-04-25 12:22:29 +00005584 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
5585 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
David Majnemer9d168222016-08-05 17:44:54 +00005586 Expr *RHS =
5587 (isOpenMPWorksharingDirective(DKind) ||
5588 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
5589 ? LB.get()
5590 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005591 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005592 Init = SemaRef.ActOnFinishFullExpr(Init.get(), /*DiscardedValue*/ false);
Carlo Bertolliffafe102017-04-20 00:39:39 +00005593
5594 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5595 Expr *CombRHS =
5596 (isOpenMPWorksharingDirective(DKind) ||
5597 isOpenMPTaskLoopDirective(DKind) ||
5598 isOpenMPDistributeDirective(DKind))
5599 ? CombLB.get()
5600 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
5601 CombInit =
5602 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS);
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005603 CombInit =
5604 SemaRef.ActOnFinishFullExpr(CombInit.get(), /*DiscardedValue*/ false);
Carlo Bertolliffafe102017-04-20 00:39:39 +00005605 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005606 }
5607
Alexey Bataev316ccf62019-01-29 18:51:58 +00005608 bool UseStrictCompare =
5609 RealVType->hasUnsignedIntegerRepresentation() &&
5610 llvm::all_of(IterSpaces, [](const LoopIterationSpace &LIS) {
5611 return LIS.IsStrictCompare;
5612 });
5613 // Loop condition (IV < NumIterations) or (IV <= UB or IV < UB + 1 (for
5614 // unsigned IV)) for worksharing loops.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005615 SourceLocation CondLoc = AStmt->getBeginLoc();
Alexey Bataev316ccf62019-01-29 18:51:58 +00005616 Expr *BoundUB = UB.get();
5617 if (UseStrictCompare) {
5618 BoundUB =
5619 SemaRef
5620 .BuildBinOp(CurScope, CondLoc, BO_Add, BoundUB,
5621 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get())
5622 .get();
5623 BoundUB =
5624 SemaRef.ActOnFinishFullExpr(BoundUB, /*DiscardedValue*/ false).get();
5625 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005626 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005627 (isOpenMPWorksharingDirective(DKind) ||
5628 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexey Bataev316ccf62019-01-29 18:51:58 +00005629 ? SemaRef.BuildBinOp(CurScope, CondLoc,
5630 UseStrictCompare ? BO_LT : BO_LE, IV.get(),
5631 BoundUB)
Alexander Musmanc6388682014-12-15 07:07:06 +00005632 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
5633 NumIterations.get());
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005634 ExprResult CombDistCond;
5635 if (isOpenMPLoopBoundSharingDirective(DKind)) {
Alexey Bataev316ccf62019-01-29 18:51:58 +00005636 CombDistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
5637 NumIterations.get());
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005638 }
5639
Carlo Bertolliffafe102017-04-20 00:39:39 +00005640 ExprResult CombCond;
5641 if (isOpenMPLoopBoundSharingDirective(DKind)) {
Alexey Bataev316ccf62019-01-29 18:51:58 +00005642 Expr *BoundCombUB = CombUB.get();
5643 if (UseStrictCompare) {
5644 BoundCombUB =
5645 SemaRef
5646 .BuildBinOp(
5647 CurScope, CondLoc, BO_Add, BoundCombUB,
5648 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get())
5649 .get();
5650 BoundCombUB =
5651 SemaRef.ActOnFinishFullExpr(BoundCombUB, /*DiscardedValue*/ false)
5652 .get();
5653 }
Carlo Bertolliffafe102017-04-20 00:39:39 +00005654 CombCond =
Alexey Bataev316ccf62019-01-29 18:51:58 +00005655 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE,
5656 IV.get(), BoundCombUB);
Carlo Bertolliffafe102017-04-20 00:39:39 +00005657 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005658 // Loop increment (IV = IV + 1)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005659 SourceLocation IncLoc = AStmt->getBeginLoc();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005660 ExprResult Inc =
5661 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
5662 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
5663 if (!Inc.isUsable())
5664 return 0;
5665 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005666 Inc = SemaRef.ActOnFinishFullExpr(Inc.get(), /*DiscardedValue*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005667 if (!Inc.isUsable())
5668 return 0;
5669
5670 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
5671 // Used for directives with static scheduling.
Carlo Bertolliffafe102017-04-20 00:39:39 +00005672 // In combined construct, add combined version that use CombLB and CombUB
5673 // base variables for the update
5674 ExprResult NextLB, NextUB, CombNextLB, CombNextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005675 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5676 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005677 // LB + ST
5678 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
5679 if (!NextLB.isUsable())
5680 return 0;
5681 // LB = LB + ST
5682 NextLB =
5683 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005684 NextLB =
5685 SemaRef.ActOnFinishFullExpr(NextLB.get(), /*DiscardedValue*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005686 if (!NextLB.isUsable())
5687 return 0;
5688 // UB + ST
5689 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
5690 if (!NextUB.isUsable())
5691 return 0;
5692 // UB = UB + ST
5693 NextUB =
5694 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005695 NextUB =
5696 SemaRef.ActOnFinishFullExpr(NextUB.get(), /*DiscardedValue*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005697 if (!NextUB.isUsable())
5698 return 0;
Carlo Bertolliffafe102017-04-20 00:39:39 +00005699 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5700 CombNextLB =
5701 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get());
5702 if (!NextLB.isUsable())
5703 return 0;
5704 // LB = LB + ST
5705 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(),
5706 CombNextLB.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005707 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get(),
5708 /*DiscardedValue*/ false);
Carlo Bertolliffafe102017-04-20 00:39:39 +00005709 if (!CombNextLB.isUsable())
5710 return 0;
5711 // UB + ST
5712 CombNextUB =
5713 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get());
5714 if (!CombNextUB.isUsable())
5715 return 0;
5716 // UB = UB + ST
5717 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(),
5718 CombNextUB.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005719 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get(),
5720 /*DiscardedValue*/ false);
Carlo Bertolliffafe102017-04-20 00:39:39 +00005721 if (!CombNextUB.isUsable())
5722 return 0;
5723 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005724 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005725
Carlo Bertolliffafe102017-04-20 00:39:39 +00005726 // Create increment expression for distribute loop when combined in a same
Carlo Bertolli8429d812017-02-17 21:29:13 +00005727 // directive with for as IV = IV + ST; ensure upper bound expression based
5728 // on PrevUB instead of NumIterations - used to implement 'for' when found
5729 // in combination with 'distribute', like in 'distribute parallel for'
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005730 SourceLocation DistIncLoc = AStmt->getBeginLoc();
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005731 ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond;
Carlo Bertolli8429d812017-02-17 21:29:13 +00005732 if (isOpenMPLoopBoundSharingDirective(DKind)) {
Alexey Bataev316ccf62019-01-29 18:51:58 +00005733 DistCond = SemaRef.BuildBinOp(
5734 CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, IV.get(), BoundUB);
Carlo Bertolli8429d812017-02-17 21:29:13 +00005735 assert(DistCond.isUsable() && "distribute cond expr was not built");
5736
5737 DistInc =
5738 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get());
5739 assert(DistInc.isUsable() && "distribute inc expr was not built");
5740 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(),
5741 DistInc.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005742 DistInc =
5743 SemaRef.ActOnFinishFullExpr(DistInc.get(), /*DiscardedValue*/ false);
Carlo Bertolli8429d812017-02-17 21:29:13 +00005744 assert(DistInc.isUsable() && "distribute inc expr was not built");
5745
5746 // Build expression: UB = min(UB, prevUB) for #for in composite or combined
5747 // construct
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005748 SourceLocation DistEUBLoc = AStmt->getBeginLoc();
Carlo Bertolli8429d812017-02-17 21:29:13 +00005749 ExprResult IsUBGreater =
5750 SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get());
5751 ExprResult CondOp = SemaRef.ActOnConditionalOp(
5752 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get());
5753 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(),
5754 CondOp.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005755 PrevEUB =
5756 SemaRef.ActOnFinishFullExpr(PrevEUB.get(), /*DiscardedValue*/ false);
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005757
Alexey Bataev316ccf62019-01-29 18:51:58 +00005758 // Build IV <= PrevUB or IV < PrevUB + 1 for unsigned IV to be used in
5759 // parallel for is in combination with a distribute directive with
5760 // schedule(static, 1)
5761 Expr *BoundPrevUB = PrevUB.get();
5762 if (UseStrictCompare) {
5763 BoundPrevUB =
5764 SemaRef
5765 .BuildBinOp(
5766 CurScope, CondLoc, BO_Add, BoundPrevUB,
5767 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get())
5768 .get();
5769 BoundPrevUB =
5770 SemaRef.ActOnFinishFullExpr(BoundPrevUB, /*DiscardedValue*/ false)
5771 .get();
5772 }
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005773 ParForInDistCond =
Alexey Bataev316ccf62019-01-29 18:51:58 +00005774 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE,
5775 IV.get(), BoundPrevUB);
Carlo Bertolli8429d812017-02-17 21:29:13 +00005776 }
5777
Alexander Musmana5f070a2014-10-01 06:03:56 +00005778 // Build updates and final values of the loop counters.
5779 bool HasErrors = false;
5780 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005781 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005782 Built.Updates.resize(NestedLoopCount);
5783 Built.Finals.resize(NestedLoopCount);
5784 {
Gheorghe-Teodor Bercea677960642019-01-09 20:45:26 +00005785 // We implement the following algorithm for obtaining the
5786 // original loop iteration variable values based on the
5787 // value of the collapsed loop iteration variable IV.
5788 //
5789 // Let n+1 be the number of collapsed loops in the nest.
5790 // Iteration variables (I0, I1, .... In)
5791 // Iteration counts (N0, N1, ... Nn)
5792 //
5793 // Acc = IV;
5794 //
5795 // To compute Ik for loop k, 0 <= k <= n, generate:
5796 // Prod = N(k+1) * N(k+2) * ... * Nn;
5797 // Ik = Acc / Prod;
5798 // Acc -= Ik * Prod;
5799 //
5800 ExprResult Acc = IV;
5801 for (unsigned int Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00005802 LoopIterationSpace &IS = IterSpaces[Cnt];
5803 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005804 ExprResult Iter;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005805
Gheorghe-Teodor Bercea677960642019-01-09 20:45:26 +00005806 // Compute prod
5807 ExprResult Prod =
5808 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
5809 for (unsigned int K = Cnt+1; K < NestedLoopCount; ++K)
5810 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Prod.get(),
5811 IterSpaces[K].NumIterations);
5812
5813 // Iter = Acc / Prod
5814 // If there is at least one more inner loop to avoid
5815 // multiplication by 1.
5816 if (Cnt + 1 < NestedLoopCount)
5817 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div,
5818 Acc.get(), Prod.get());
5819 else
5820 Iter = Acc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005821 if (!Iter.isUsable()) {
5822 HasErrors = true;
5823 break;
5824 }
5825
Gheorghe-Teodor Bercea677960642019-01-09 20:45:26 +00005826 // Update Acc:
5827 // Acc -= Iter * Prod
5828 // Check if there is at least one more inner loop to avoid
5829 // multiplication by 1.
5830 if (Cnt + 1 < NestedLoopCount)
5831 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul,
5832 Iter.get(), Prod.get());
5833 else
5834 Prod = Iter;
5835 Acc = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Sub,
5836 Acc.get(), Prod.get());
5837
Alexey Bataev39f915b82015-05-08 10:41:21 +00005838 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005839 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00005840 DeclRefExpr *CounterVar = buildDeclRefExpr(
5841 SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
5842 /*RefersToCapture=*/true);
5843 ExprResult Init = buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005844 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005845 if (!Init.isUsable()) {
5846 HasErrors = true;
5847 break;
5848 }
Alexey Bataeve3727102018-04-18 15:57:46 +00005849 ExprResult Update = buildCounterUpdate(
Alexey Bataev5a3af132016-03-29 08:58:54 +00005850 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
5851 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005852 if (!Update.isUsable()) {
5853 HasErrors = true;
5854 break;
5855 }
5856
5857 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
Alexey Bataeve3727102018-04-18 15:57:46 +00005858 ExprResult Final = buildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00005859 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005860 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005861 if (!Final.isUsable()) {
5862 HasErrors = true;
5863 break;
5864 }
5865
Alexander Musmana5f070a2014-10-01 06:03:56 +00005866 if (!Update.isUsable() || !Final.isUsable()) {
5867 HasErrors = true;
5868 break;
5869 }
5870 // Save results
5871 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00005872 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005873 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005874 Built.Updates[Cnt] = Update.get();
5875 Built.Finals[Cnt] = Final.get();
5876 }
5877 }
5878
5879 if (HasErrors)
5880 return 0;
5881
5882 // Save results
5883 Built.IterationVarRef = IV.get();
5884 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00005885 Built.NumIterations = NumIterations.get();
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005886 Built.CalcLastIteration = SemaRef
5887 .ActOnFinishFullExpr(CalcLastIteration.get(),
5888 /*DiscardedValue*/ false)
5889 .get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005890 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00005891 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005892 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005893 Built.Init = Init.get();
5894 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005895 Built.LB = LB.get();
5896 Built.UB = UB.get();
5897 Built.IL = IL.get();
5898 Built.ST = ST.get();
5899 Built.EUB = EUB.get();
5900 Built.NLB = NextLB.get();
5901 Built.NUB = NextUB.get();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005902 Built.PrevLB = PrevLB.get();
5903 Built.PrevUB = PrevUB.get();
Carlo Bertolli8429d812017-02-17 21:29:13 +00005904 Built.DistInc = DistInc.get();
5905 Built.PrevEUB = PrevEUB.get();
Carlo Bertolliffafe102017-04-20 00:39:39 +00005906 Built.DistCombinedFields.LB = CombLB.get();
5907 Built.DistCombinedFields.UB = CombUB.get();
5908 Built.DistCombinedFields.EUB = CombEUB.get();
5909 Built.DistCombinedFields.Init = CombInit.get();
5910 Built.DistCombinedFields.Cond = CombCond.get();
5911 Built.DistCombinedFields.NLB = CombNextLB.get();
5912 Built.DistCombinedFields.NUB = CombNextUB.get();
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005913 Built.DistCombinedFields.DistCond = CombDistCond.get();
5914 Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005915
Alexey Bataevabfc0692014-06-25 06:52:00 +00005916 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005917}
5918
Alexey Bataev10e775f2015-07-30 11:36:16 +00005919static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005920 auto CollapseClauses =
5921 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
5922 if (CollapseClauses.begin() != CollapseClauses.end())
5923 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005924 return nullptr;
5925}
5926
Alexey Bataev10e775f2015-07-30 11:36:16 +00005927static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005928 auto OrderedClauses =
5929 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
5930 if (OrderedClauses.begin() != OrderedClauses.end())
5931 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005932 return nullptr;
5933}
5934
Kelvin Lic5609492016-07-15 04:39:07 +00005935static bool checkSimdlenSafelenSpecified(Sema &S,
5936 const ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00005937 const OMPSafelenClause *Safelen = nullptr;
5938 const OMPSimdlenClause *Simdlen = nullptr;
Kelvin Lic5609492016-07-15 04:39:07 +00005939
Alexey Bataeve3727102018-04-18 15:57:46 +00005940 for (const OMPClause *Clause : Clauses) {
Kelvin Lic5609492016-07-15 04:39:07 +00005941 if (Clause->getClauseKind() == OMPC_safelen)
5942 Safelen = cast<OMPSafelenClause>(Clause);
5943 else if (Clause->getClauseKind() == OMPC_simdlen)
5944 Simdlen = cast<OMPSimdlenClause>(Clause);
5945 if (Safelen && Simdlen)
5946 break;
5947 }
5948
5949 if (Simdlen && Safelen) {
Alexey Bataeve3727102018-04-18 15:57:46 +00005950 const Expr *SimdlenLength = Simdlen->getSimdlen();
5951 const Expr *SafelenLength = Safelen->getSafelen();
Kelvin Lic5609492016-07-15 04:39:07 +00005952 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
5953 SimdlenLength->isInstantiationDependent() ||
5954 SimdlenLength->containsUnexpandedParameterPack())
5955 return false;
5956 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
5957 SafelenLength->isInstantiationDependent() ||
5958 SafelenLength->containsUnexpandedParameterPack())
5959 return false;
Fangrui Song407659a2018-11-30 23:41:18 +00005960 Expr::EvalResult SimdlenResult, SafelenResult;
5961 SimdlenLength->EvaluateAsInt(SimdlenResult, S.Context);
5962 SafelenLength->EvaluateAsInt(SafelenResult, S.Context);
5963 llvm::APSInt SimdlenRes = SimdlenResult.Val.getInt();
5964 llvm::APSInt SafelenRes = SafelenResult.Val.getInt();
Kelvin Lic5609492016-07-15 04:39:07 +00005965 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
5966 // If both simdlen and safelen clauses are specified, the value of the
5967 // simdlen parameter must be less than or equal to the value of the safelen
5968 // parameter.
5969 if (SimdlenRes > SafelenRes) {
5970 S.Diag(SimdlenLength->getExprLoc(),
5971 diag::err_omp_wrong_simdlen_safelen_values)
5972 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
5973 return true;
5974 }
Alexey Bataev66b15b52015-08-21 11:14:16 +00005975 }
5976 return false;
5977}
5978
Alexey Bataeve3727102018-04-18 15:57:46 +00005979StmtResult
5980Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
5981 SourceLocation StartLoc, SourceLocation EndLoc,
5982 VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005983 if (!AStmt)
5984 return StmtError();
5985
5986 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005987 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005988 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5989 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00005990 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataev10e775f2015-07-30 11:36:16 +00005991 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5992 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005993 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005994 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005995
Alexander Musmana5f070a2014-10-01 06:03:56 +00005996 assert((CurContext->isDependentContext() || B.builtAll()) &&
5997 "omp simd loop exprs were not built");
5998
Alexander Musman3276a272015-03-21 10:12:56 +00005999 if (!CurContext->isDependentContext()) {
6000 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00006001 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00006002 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexander Musman3276a272015-03-21 10:12:56 +00006003 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006004 B.NumIterations, *this, CurScope,
6005 DSAStack))
Alexander Musman3276a272015-03-21 10:12:56 +00006006 return StmtError();
6007 }
6008 }
6009
Kelvin Lic5609492016-07-15 04:39:07 +00006010 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00006011 return StmtError();
6012
Reid Kleckner87a31802018-03-12 21:43:02 +00006013 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00006014 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
6015 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00006016}
6017
Alexey Bataeve3727102018-04-18 15:57:46 +00006018StmtResult
6019Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
6020 SourceLocation StartLoc, SourceLocation EndLoc,
6021 VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006022 if (!AStmt)
6023 return StmtError();
6024
6025 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00006026 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006027 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6028 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00006029 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataev10e775f2015-07-30 11:36:16 +00006030 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
6031 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00006032 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00006033 return StmtError();
6034
Alexander Musmana5f070a2014-10-01 06:03:56 +00006035 assert((CurContext->isDependentContext() || B.builtAll()) &&
6036 "omp for loop exprs were not built");
6037
Alexey Bataev54acd402015-08-04 11:18:19 +00006038 if (!CurContext->isDependentContext()) {
6039 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00006040 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00006041 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00006042 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006043 B.NumIterations, *this, CurScope,
6044 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00006045 return StmtError();
6046 }
6047 }
6048
Reid Kleckner87a31802018-03-12 21:43:02 +00006049 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00006050 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00006051 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00006052}
6053
Alexander Musmanf82886e2014-09-18 05:12:34 +00006054StmtResult Sema::ActOnOpenMPForSimdDirective(
6055 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00006056 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006057 if (!AStmt)
6058 return StmtError();
6059
6060 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00006061 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006062 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6063 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00006064 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00006065 checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00006066 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6067 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00006068 if (NestedLoopCount == 0)
6069 return StmtError();
6070
Alexander Musmanc6388682014-12-15 07:07:06 +00006071 assert((CurContext->isDependentContext() || B.builtAll()) &&
6072 "omp for simd loop exprs were not built");
6073
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00006074 if (!CurContext->isDependentContext()) {
6075 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00006076 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00006077 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00006078 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006079 B.NumIterations, *this, CurScope,
6080 DSAStack))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00006081 return StmtError();
6082 }
6083 }
6084
Kelvin Lic5609492016-07-15 04:39:07 +00006085 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00006086 return StmtError();
6087
Reid Kleckner87a31802018-03-12 21:43:02 +00006088 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00006089 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
6090 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00006091}
6092
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00006093StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
6094 Stmt *AStmt,
6095 SourceLocation StartLoc,
6096 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006097 if (!AStmt)
6098 return StmtError();
6099
6100 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00006101 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00006102 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00006103 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00006104 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00006105 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00006106 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00006107 return StmtError();
6108 // All associated statements must be '#pragma omp section' except for
6109 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00006110 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00006111 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
6112 if (SectionStmt)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006113 Diag(SectionStmt->getBeginLoc(),
Alexey Bataev1e0498a2014-06-26 08:21:58 +00006114 diag::err_omp_sections_substmt_not_section);
6115 return StmtError();
6116 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006117 cast<OMPSectionDirective>(SectionStmt)
6118 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00006119 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00006120 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006121 Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00006122 return StmtError();
6123 }
6124
Reid Kleckner87a31802018-03-12 21:43:02 +00006125 setFunctionHasBranchProtectedScope();
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00006126
Alexey Bataev25e5b442015-09-15 12:52:43 +00006127 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
6128 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00006129}
6130
Alexey Bataev1e0498a2014-06-26 08:21:58 +00006131StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
6132 SourceLocation StartLoc,
6133 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006134 if (!AStmt)
6135 return StmtError();
6136
6137 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00006138
Reid Kleckner87a31802018-03-12 21:43:02 +00006139 setFunctionHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00006140 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00006141
Alexey Bataev25e5b442015-09-15 12:52:43 +00006142 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
6143 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00006144}
6145
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00006146StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
6147 Stmt *AStmt,
6148 SourceLocation StartLoc,
6149 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006150 if (!AStmt)
6151 return StmtError();
6152
6153 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00006154
Reid Kleckner87a31802018-03-12 21:43:02 +00006155 setFunctionHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00006156
Alexey Bataev3255bf32015-01-19 05:20:46 +00006157 // OpenMP [2.7.3, single Construct, Restrictions]
6158 // The copyprivate clause must not be used with the nowait clause.
Alexey Bataeve3727102018-04-18 15:57:46 +00006159 const OMPClause *Nowait = nullptr;
6160 const OMPClause *Copyprivate = nullptr;
6161 for (const OMPClause *Clause : Clauses) {
Alexey Bataev3255bf32015-01-19 05:20:46 +00006162 if (Clause->getClauseKind() == OMPC_nowait)
6163 Nowait = Clause;
6164 else if (Clause->getClauseKind() == OMPC_copyprivate)
6165 Copyprivate = Clause;
6166 if (Copyprivate && Nowait) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006167 Diag(Copyprivate->getBeginLoc(),
Alexey Bataev3255bf32015-01-19 05:20:46 +00006168 diag::err_omp_single_copyprivate_with_nowait);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006169 Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here);
Alexey Bataev3255bf32015-01-19 05:20:46 +00006170 return StmtError();
6171 }
6172 }
6173
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00006174 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6175}
6176
Alexander Musman80c22892014-07-17 08:54:58 +00006177StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
6178 SourceLocation StartLoc,
6179 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006180 if (!AStmt)
6181 return StmtError();
6182
6183 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00006184
Reid Kleckner87a31802018-03-12 21:43:02 +00006185 setFunctionHasBranchProtectedScope();
Alexander Musman80c22892014-07-17 08:54:58 +00006186
6187 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
6188}
6189
Alexey Bataev28c75412015-12-15 08:19:24 +00006190StmtResult Sema::ActOnOpenMPCriticalDirective(
6191 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
6192 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006193 if (!AStmt)
6194 return StmtError();
6195
6196 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00006197
Alexey Bataev28c75412015-12-15 08:19:24 +00006198 bool ErrorFound = false;
6199 llvm::APSInt Hint;
6200 SourceLocation HintLoc;
6201 bool DependentHint = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00006202 for (const OMPClause *C : Clauses) {
Alexey Bataev28c75412015-12-15 08:19:24 +00006203 if (C->getClauseKind() == OMPC_hint) {
6204 if (!DirName.getName()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006205 Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name);
Alexey Bataev28c75412015-12-15 08:19:24 +00006206 ErrorFound = true;
6207 }
6208 Expr *E = cast<OMPHintClause>(C)->getHint();
6209 if (E->isTypeDependent() || E->isValueDependent() ||
Alexey Bataeve3727102018-04-18 15:57:46 +00006210 E->isInstantiationDependent()) {
Alexey Bataev28c75412015-12-15 08:19:24 +00006211 DependentHint = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006212 } else {
Alexey Bataev28c75412015-12-15 08:19:24 +00006213 Hint = E->EvaluateKnownConstInt(Context);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006214 HintLoc = C->getBeginLoc();
Alexey Bataev28c75412015-12-15 08:19:24 +00006215 }
6216 }
6217 }
6218 if (ErrorFound)
6219 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00006220 const auto Pair = DSAStack->getCriticalWithHint(DirName);
Alexey Bataev28c75412015-12-15 08:19:24 +00006221 if (Pair.first && DirName.getName() && !DependentHint) {
6222 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
6223 Diag(StartLoc, diag::err_omp_critical_with_hint);
Alexey Bataeve3727102018-04-18 15:57:46 +00006224 if (HintLoc.isValid())
Alexey Bataev28c75412015-12-15 08:19:24 +00006225 Diag(HintLoc, diag::note_omp_critical_hint_here)
6226 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +00006227 else
Alexey Bataev28c75412015-12-15 08:19:24 +00006228 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
Alexey Bataeve3727102018-04-18 15:57:46 +00006229 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006230 Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here)
Alexey Bataev28c75412015-12-15 08:19:24 +00006231 << 1
6232 << C->getHint()->EvaluateKnownConstInt(Context).toString(
6233 /*Radix=*/10, /*Signed=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +00006234 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006235 Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1;
Alexey Bataeve3727102018-04-18 15:57:46 +00006236 }
Alexey Bataev28c75412015-12-15 08:19:24 +00006237 }
6238 }
6239
Reid Kleckner87a31802018-03-12 21:43:02 +00006240 setFunctionHasBranchProtectedScope();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00006241
Alexey Bataev28c75412015-12-15 08:19:24 +00006242 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
6243 Clauses, AStmt);
6244 if (!Pair.first && DirName.getName() && !DependentHint)
6245 DSAStack->addCriticalWithHint(Dir, Hint);
6246 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00006247}
6248
Alexey Bataev4acb8592014-07-07 13:01:15 +00006249StmtResult Sema::ActOnOpenMPParallelForDirective(
6250 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00006251 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006252 if (!AStmt)
6253 return StmtError();
6254
Alexey Bataeve3727102018-04-18 15:57:46 +00006255 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev4acb8592014-07-07 13:01:15 +00006256 // 1.2.2 OpenMP Language Terminology
6257 // Structured block - An executable statement with a single entry at the
6258 // top and a single exit at the bottom.
6259 // The point of exit cannot be a branch out of the structured block.
6260 // longjmp() and throw() must not violate the entry/exit criteria.
6261 CS->getCapturedDecl()->setNothrow();
6262
Alexander Musmanc6388682014-12-15 07:07:06 +00006263 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006264 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6265 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00006266 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00006267 checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00006268 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6269 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00006270 if (NestedLoopCount == 0)
6271 return StmtError();
6272
Alexander Musmana5f070a2014-10-01 06:03:56 +00006273 assert((CurContext->isDependentContext() || B.builtAll()) &&
6274 "omp parallel for loop exprs were not built");
6275
Alexey Bataev54acd402015-08-04 11:18:19 +00006276 if (!CurContext->isDependentContext()) {
6277 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00006278 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00006279 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00006280 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006281 B.NumIterations, *this, CurScope,
6282 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00006283 return StmtError();
6284 }
6285 }
6286
Reid Kleckner87a31802018-03-12 21:43:02 +00006287 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00006288 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00006289 NestedLoopCount, Clauses, AStmt, B,
6290 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00006291}
6292
Alexander Musmane4e893b2014-09-23 09:33:00 +00006293StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
6294 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00006295 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006296 if (!AStmt)
6297 return StmtError();
6298
Alexey Bataeve3727102018-04-18 15:57:46 +00006299 auto *CS = cast<CapturedStmt>(AStmt);
Alexander Musmane4e893b2014-09-23 09:33:00 +00006300 // 1.2.2 OpenMP Language Terminology
6301 // Structured block - An executable statement with a single entry at the
6302 // top and a single exit at the bottom.
6303 // The point of exit cannot be a branch out of the structured block.
6304 // longjmp() and throw() must not violate the entry/exit criteria.
6305 CS->getCapturedDecl()->setNothrow();
6306
Alexander Musmanc6388682014-12-15 07:07:06 +00006307 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006308 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6309 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00006310 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00006311 checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00006312 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6313 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00006314 if (NestedLoopCount == 0)
6315 return StmtError();
6316
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00006317 if (!CurContext->isDependentContext()) {
6318 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00006319 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00006320 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00006321 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006322 B.NumIterations, *this, CurScope,
6323 DSAStack))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00006324 return StmtError();
6325 }
6326 }
6327
Kelvin Lic5609492016-07-15 04:39:07 +00006328 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00006329 return StmtError();
6330
Reid Kleckner87a31802018-03-12 21:43:02 +00006331 setFunctionHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00006332 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00006333 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00006334}
6335
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006336StmtResult
6337Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
6338 Stmt *AStmt, SourceLocation StartLoc,
6339 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006340 if (!AStmt)
6341 return StmtError();
6342
6343 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006344 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00006345 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006346 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00006347 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006348 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00006349 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006350 return StmtError();
6351 // All associated statements must be '#pragma omp section' except for
6352 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00006353 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006354 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
6355 if (SectionStmt)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006356 Diag(SectionStmt->getBeginLoc(),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006357 diag::err_omp_parallel_sections_substmt_not_section);
6358 return StmtError();
6359 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006360 cast<OMPSectionDirective>(SectionStmt)
6361 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006362 }
6363 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006364 Diag(AStmt->getBeginLoc(),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006365 diag::err_omp_parallel_sections_not_compound_stmt);
6366 return StmtError();
6367 }
6368
Reid Kleckner87a31802018-03-12 21:43:02 +00006369 setFunctionHasBranchProtectedScope();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006370
Alexey Bataev25e5b442015-09-15 12:52:43 +00006371 return OMPParallelSectionsDirective::Create(
6372 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006373}
6374
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006375StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
6376 Stmt *AStmt, SourceLocation StartLoc,
6377 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006378 if (!AStmt)
6379 return StmtError();
6380
David Majnemer9d168222016-08-05 17:44:54 +00006381 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006382 // 1.2.2 OpenMP Language Terminology
6383 // Structured block - An executable statement with a single entry at the
6384 // top and a single exit at the bottom.
6385 // The point of exit cannot be a branch out of the structured block.
6386 // longjmp() and throw() must not violate the entry/exit criteria.
6387 CS->getCapturedDecl()->setNothrow();
6388
Reid Kleckner87a31802018-03-12 21:43:02 +00006389 setFunctionHasBranchProtectedScope();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006390
Alexey Bataev25e5b442015-09-15 12:52:43 +00006391 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
6392 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006393}
6394
Alexey Bataev68446b72014-07-18 07:47:19 +00006395StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
6396 SourceLocation EndLoc) {
6397 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
6398}
6399
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00006400StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
6401 SourceLocation EndLoc) {
6402 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
6403}
6404
Alexey Bataev2df347a2014-07-18 10:17:07 +00006405StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
6406 SourceLocation EndLoc) {
6407 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
6408}
6409
Alexey Bataev169d96a2017-07-18 20:17:46 +00006410StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses,
6411 Stmt *AStmt,
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006412 SourceLocation StartLoc,
6413 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006414 if (!AStmt)
6415 return StmtError();
6416
6417 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006418
Reid Kleckner87a31802018-03-12 21:43:02 +00006419 setFunctionHasBranchProtectedScope();
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006420
Alexey Bataev169d96a2017-07-18 20:17:46 +00006421 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses,
Alexey Bataev3b1b8952017-07-25 15:53:26 +00006422 AStmt,
6423 DSAStack->getTaskgroupReductionRef());
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006424}
6425
Alexey Bataev6125da92014-07-21 11:26:11 +00006426StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
6427 SourceLocation StartLoc,
6428 SourceLocation EndLoc) {
6429 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
6430 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
6431}
6432
Alexey Bataev346265e2015-09-25 10:37:12 +00006433StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
6434 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00006435 SourceLocation StartLoc,
6436 SourceLocation EndLoc) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006437 const OMPClause *DependFound = nullptr;
6438 const OMPClause *DependSourceClause = nullptr;
6439 const OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00006440 bool ErrorFound = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00006441 const OMPThreadsClause *TC = nullptr;
6442 const OMPSIMDClause *SC = nullptr;
6443 for (const OMPClause *C : Clauses) {
Alexey Bataeveb482352015-12-18 05:05:56 +00006444 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
6445 DependFound = C;
6446 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
6447 if (DependSourceClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006448 Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
Alexey Bataeveb482352015-12-18 05:05:56 +00006449 << getOpenMPDirectiveName(OMPD_ordered)
6450 << getOpenMPClauseName(OMPC_depend) << 2;
6451 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006452 } else {
Alexey Bataeveb482352015-12-18 05:05:56 +00006453 DependSourceClause = C;
Alexey Bataeve3727102018-04-18 15:57:46 +00006454 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006455 if (DependSinkClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006456 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006457 << 0;
6458 ErrorFound = true;
6459 }
6460 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
6461 if (DependSourceClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006462 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006463 << 1;
6464 ErrorFound = true;
6465 }
6466 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00006467 }
Alexey Bataeve3727102018-04-18 15:57:46 +00006468 } else if (C->getClauseKind() == OMPC_threads) {
Alexey Bataev346265e2015-09-25 10:37:12 +00006469 TC = cast<OMPThreadsClause>(C);
Alexey Bataeve3727102018-04-18 15:57:46 +00006470 } else if (C->getClauseKind() == OMPC_simd) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006471 SC = cast<OMPSIMDClause>(C);
Alexey Bataeve3727102018-04-18 15:57:46 +00006472 }
Alexey Bataev346265e2015-09-25 10:37:12 +00006473 }
Alexey Bataeveb482352015-12-18 05:05:56 +00006474 if (!ErrorFound && !SC &&
6475 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006476 // OpenMP [2.8.1,simd Construct, Restrictions]
6477 // An ordered construct with the simd clause is the only OpenMP construct
6478 // that can appear in the simd region.
6479 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00006480 ErrorFound = true;
6481 } else if (DependFound && (TC || SC)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006482 Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd)
Alexey Bataeveb482352015-12-18 05:05:56 +00006483 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
6484 ErrorFound = true;
Alexey Bataevf138fda2018-08-13 19:04:24 +00006485 } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006486 Diag(DependFound->getBeginLoc(),
Alexey Bataeveb482352015-12-18 05:05:56 +00006487 diag::err_omp_ordered_directive_without_param);
6488 ErrorFound = true;
6489 } else if (TC || Clauses.empty()) {
Alexey Bataevf138fda2018-08-13 19:04:24 +00006490 if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006491 SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc;
Alexey Bataeveb482352015-12-18 05:05:56 +00006492 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
6493 << (TC != nullptr);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006494 Diag(Param->getBeginLoc(), diag::note_omp_ordered_param);
Alexey Bataeveb482352015-12-18 05:05:56 +00006495 ErrorFound = true;
6496 }
6497 }
6498 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006499 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00006500
6501 if (AStmt) {
6502 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6503
Reid Kleckner87a31802018-03-12 21:43:02 +00006504 setFunctionHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006505 }
Alexey Bataev346265e2015-09-25 10:37:12 +00006506
6507 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00006508}
6509
Alexey Bataev1d160b12015-03-13 12:27:31 +00006510namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006511/// Helper class for checking expression in 'omp atomic [update]'
Alexey Bataev1d160b12015-03-13 12:27:31 +00006512/// construct.
6513class OpenMPAtomicUpdateChecker {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006514 /// Error results for atomic update expressions.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006515 enum ExprAnalysisErrorCode {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006516 /// A statement is not an expression statement.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006517 NotAnExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006518 /// Expression is not builtin binary or unary operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006519 NotABinaryOrUnaryExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006520 /// Unary operation is not post-/pre- increment/decrement operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006521 NotAnUnaryIncDecExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006522 /// An expression is not of scalar type.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006523 NotAScalarType,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006524 /// A binary operation is not an assignment operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006525 NotAnAssignmentOp,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006526 /// RHS part of the binary operation is not a binary expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006527 NotABinaryExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006528 /// RHS part is not additive/multiplicative/shift/biwise binary
Alexey Bataev1d160b12015-03-13 12:27:31 +00006529 /// expression.
6530 NotABinaryOperator,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006531 /// RHS binary operation does not have reference to the updated LHS
Alexey Bataev1d160b12015-03-13 12:27:31 +00006532 /// part.
6533 NotAnUpdateExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006534 /// No errors is found.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006535 NoError
6536 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006537 /// Reference to Sema.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006538 Sema &SemaRef;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006539 /// A location for note diagnostics (when error is found).
Alexey Bataev1d160b12015-03-13 12:27:31 +00006540 SourceLocation NoteLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006541 /// 'x' lvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006542 Expr *X;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006543 /// 'expr' rvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006544 Expr *E;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006545 /// Helper expression of the form
Alexey Bataevb4505a72015-03-30 05:20:59 +00006546 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6547 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6548 Expr *UpdateExpr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006549 /// Is 'x' a LHS in a RHS part of full update expression. It is
Alexey Bataevb4505a72015-03-30 05:20:59 +00006550 /// important for non-associative operations.
6551 bool IsXLHSInRHSPart;
6552 BinaryOperatorKind Op;
6553 SourceLocation OpLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006554 /// true if the source expression is a postfix unary operation, false
Alexey Bataevb78ca832015-04-01 03:33:17 +00006555 /// if it is a prefix unary operation.
6556 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006557
6558public:
6559 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00006560 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00006561 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006562 /// Check specified statement that it is suitable for 'atomic update'
Alexey Bataev1d160b12015-03-13 12:27:31 +00006563 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00006564 /// expression. If DiagId and NoteId == 0, then only check is performed
6565 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006566 /// \param DiagId Diagnostic which should be emitted if error is found.
6567 /// \param NoteId Diagnostic note for the main error message.
6568 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00006569 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006570 /// Return the 'x' lvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006571 Expr *getX() const { return X; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006572 /// Return the 'expr' rvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006573 Expr *getExpr() const { return E; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006574 /// Return the update expression used in calculation of the updated
Alexey Bataevb4505a72015-03-30 05:20:59 +00006575 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6576 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6577 Expr *getUpdateExpr() const { return UpdateExpr; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006578 /// Return true if 'x' is LHS in RHS part of full update expression,
Alexey Bataevb4505a72015-03-30 05:20:59 +00006579 /// false otherwise.
6580 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
6581
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006582 /// true if the source expression is a postfix unary operation, false
Alexey Bataevb78ca832015-04-01 03:33:17 +00006583 /// if it is a prefix unary operation.
6584 bool isPostfixUpdate() const { return IsPostfixUpdate; }
6585
Alexey Bataev1d160b12015-03-13 12:27:31 +00006586private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00006587 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
6588 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00006589};
6590} // namespace
6591
6592bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
6593 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
6594 ExprAnalysisErrorCode ErrorFound = NoError;
6595 SourceLocation ErrorLoc, NoteLoc;
6596 SourceRange ErrorRange, NoteRange;
6597 // Allowed constructs are:
6598 // x = x binop expr;
6599 // x = expr binop x;
6600 if (AtomicBinOp->getOpcode() == BO_Assign) {
6601 X = AtomicBinOp->getLHS();
Alexey Bataeve3727102018-04-18 15:57:46 +00006602 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006603 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
6604 if (AtomicInnerBinOp->isMultiplicativeOp() ||
6605 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
6606 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006607 Op = AtomicInnerBinOp->getOpcode();
6608 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataeve3727102018-04-18 15:57:46 +00006609 Expr *LHS = AtomicInnerBinOp->getLHS();
6610 Expr *RHS = AtomicInnerBinOp->getRHS();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006611 llvm::FoldingSetNodeID XId, LHSId, RHSId;
6612 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
6613 /*Canonical=*/true);
6614 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
6615 /*Canonical=*/true);
6616 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
6617 /*Canonical=*/true);
6618 if (XId == LHSId) {
6619 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006620 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006621 } else if (XId == RHSId) {
6622 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006623 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006624 } else {
6625 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6626 ErrorRange = AtomicInnerBinOp->getSourceRange();
6627 NoteLoc = X->getExprLoc();
6628 NoteRange = X->getSourceRange();
6629 ErrorFound = NotAnUpdateExpression;
6630 }
6631 } else {
6632 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6633 ErrorRange = AtomicInnerBinOp->getSourceRange();
6634 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
6635 NoteRange = SourceRange(NoteLoc, NoteLoc);
6636 ErrorFound = NotABinaryOperator;
6637 }
6638 } else {
6639 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
6640 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
6641 ErrorFound = NotABinaryExpression;
6642 }
6643 } else {
6644 ErrorLoc = AtomicBinOp->getExprLoc();
6645 ErrorRange = AtomicBinOp->getSourceRange();
6646 NoteLoc = AtomicBinOp->getOperatorLoc();
6647 NoteRange = SourceRange(NoteLoc, NoteLoc);
6648 ErrorFound = NotAnAssignmentOp;
6649 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006650 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006651 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6652 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6653 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006654 }
6655 if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006656 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006657 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006658}
6659
6660bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
6661 unsigned NoteId) {
6662 ExprAnalysisErrorCode ErrorFound = NoError;
6663 SourceLocation ErrorLoc, NoteLoc;
6664 SourceRange ErrorRange, NoteRange;
6665 // Allowed constructs are:
6666 // x++;
6667 // x--;
6668 // ++x;
6669 // --x;
6670 // x binop= expr;
6671 // x = x binop expr;
6672 // x = expr binop x;
6673 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
6674 AtomicBody = AtomicBody->IgnoreParenImpCasts();
6675 if (AtomicBody->getType()->isScalarType() ||
6676 AtomicBody->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006677 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006678 AtomicBody->IgnoreParenImpCasts())) {
6679 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00006680 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006681 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00006682 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006683 E = AtomicCompAssignOp->getRHS();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006684 X = AtomicCompAssignOp->getLHS()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006685 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006686 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
6687 AtomicBody->IgnoreParenImpCasts())) {
6688 // Check for Binary Operation
David Majnemer9d168222016-08-05 17:44:54 +00006689 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
Alexey Bataevb4505a72015-03-30 05:20:59 +00006690 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006691 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
David Majnemer9d168222016-08-05 17:44:54 +00006692 AtomicBody->IgnoreParenImpCasts())) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006693 // Check for Unary Operation
6694 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006695 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006696 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
6697 OpLoc = AtomicUnaryOp->getOperatorLoc();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006698 X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006699 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
6700 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006701 } else {
6702 ErrorFound = NotAnUnaryIncDecExpression;
6703 ErrorLoc = AtomicUnaryOp->getExprLoc();
6704 ErrorRange = AtomicUnaryOp->getSourceRange();
6705 NoteLoc = AtomicUnaryOp->getOperatorLoc();
6706 NoteRange = SourceRange(NoteLoc, NoteLoc);
6707 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006708 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006709 ErrorFound = NotABinaryOrUnaryExpression;
6710 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
6711 NoteRange = ErrorRange = AtomicBody->getSourceRange();
6712 }
6713 } else {
6714 ErrorFound = NotAScalarType;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006715 NoteLoc = ErrorLoc = AtomicBody->getBeginLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006716 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6717 }
6718 } else {
6719 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006720 NoteLoc = ErrorLoc = S->getBeginLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006721 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6722 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006723 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006724 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6725 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6726 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006727 }
6728 if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006729 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006730 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006731 // Build an update expression of form 'OpaqueValueExpr(x) binop
6732 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
6733 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
6734 auto *OVEX = new (SemaRef.getASTContext())
6735 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
6736 auto *OVEExpr = new (SemaRef.getASTContext())
6737 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
Alexey Bataeve3727102018-04-18 15:57:46 +00006738 ExprResult Update =
Alexey Bataevb4505a72015-03-30 05:20:59 +00006739 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
6740 IsXLHSInRHSPart ? OVEExpr : OVEX);
6741 if (Update.isInvalid())
6742 return true;
6743 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
6744 Sema::AA_Casting);
6745 if (Update.isInvalid())
6746 return true;
6747 UpdateExpr = Update.get();
6748 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00006749 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006750}
6751
Alexey Bataev0162e452014-07-22 10:10:35 +00006752StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
6753 Stmt *AStmt,
6754 SourceLocation StartLoc,
6755 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006756 if (!AStmt)
6757 return StmtError();
6758
David Majnemer9d168222016-08-05 17:44:54 +00006759 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00006760 // 1.2.2 OpenMP Language Terminology
6761 // Structured block - An executable statement with a single entry at the
6762 // top and a single exit at the bottom.
6763 // The point of exit cannot be a branch out of the structured block.
6764 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00006765 OpenMPClauseKind AtomicKind = OMPC_unknown;
6766 SourceLocation AtomicKindLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00006767 for (const OMPClause *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00006768 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00006769 C->getClauseKind() == OMPC_update ||
6770 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00006771 if (AtomicKind != OMPC_unknown) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006772 Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00006773 << SourceRange(C->getBeginLoc(), C->getEndLoc());
Alexey Bataevdea47612014-07-23 07:46:59 +00006774 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
6775 << getOpenMPClauseName(AtomicKind);
6776 } else {
6777 AtomicKind = C->getClauseKind();
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006778 AtomicKindLoc = C->getBeginLoc();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006779 }
6780 }
6781 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006782
Alexey Bataeve3727102018-04-18 15:57:46 +00006783 Stmt *Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00006784 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
6785 Body = EWC->getSubExpr();
6786
Alexey Bataev62cec442014-11-18 10:14:22 +00006787 Expr *X = nullptr;
6788 Expr *V = nullptr;
6789 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006790 Expr *UE = nullptr;
6791 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006792 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00006793 // OpenMP [2.12.6, atomic Construct]
6794 // In the next expressions:
6795 // * x and v (as applicable) are both l-value expressions with scalar type.
6796 // * During the execution of an atomic region, multiple syntactic
6797 // occurrences of x must designate the same storage location.
6798 // * Neither of v and expr (as applicable) may access the storage location
6799 // designated by x.
6800 // * Neither of x and expr (as applicable) may access the storage location
6801 // designated by v.
6802 // * expr is an expression with scalar type.
6803 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
6804 // * binop, binop=, ++, and -- are not overloaded operators.
6805 // * The expression x binop expr must be numerically equivalent to x binop
6806 // (expr). This requirement is satisfied if the operators in expr have
6807 // precedence greater than binop, or by using parentheses around expr or
6808 // subexpressions of expr.
6809 // * The expression expr binop x must be numerically equivalent to (expr)
6810 // binop x. This requirement is satisfied if the operators in expr have
6811 // precedence equal to or greater than binop, or by using parentheses around
6812 // expr or subexpressions of expr.
6813 // * For forms that allow multiple occurrences of x, the number of times
6814 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00006815 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006816 enum {
6817 NotAnExpression,
6818 NotAnAssignmentOp,
6819 NotAScalarType,
6820 NotAnLValue,
6821 NoError
6822 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00006823 SourceLocation ErrorLoc, NoteLoc;
6824 SourceRange ErrorRange, NoteRange;
6825 // If clause is read:
6826 // v = x;
Alexey Bataeve3727102018-04-18 15:57:46 +00006827 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
6828 const auto *AtomicBinOp =
Alexey Bataev62cec442014-11-18 10:14:22 +00006829 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6830 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6831 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6832 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
6833 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6834 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
6835 if (!X->isLValue() || !V->isLValue()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006836 const Expr *NotLValueExpr = X->isLValue() ? V : X;
Alexey Bataev62cec442014-11-18 10:14:22 +00006837 ErrorFound = NotAnLValue;
6838 ErrorLoc = AtomicBinOp->getExprLoc();
6839 ErrorRange = AtomicBinOp->getSourceRange();
6840 NoteLoc = NotLValueExpr->getExprLoc();
6841 NoteRange = NotLValueExpr->getSourceRange();
6842 }
6843 } else if (!X->isInstantiationDependent() ||
6844 !V->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006845 const Expr *NotScalarExpr =
Alexey Bataev62cec442014-11-18 10:14:22 +00006846 (X->isInstantiationDependent() || X->getType()->isScalarType())
6847 ? V
6848 : X;
6849 ErrorFound = NotAScalarType;
6850 ErrorLoc = AtomicBinOp->getExprLoc();
6851 ErrorRange = AtomicBinOp->getSourceRange();
6852 NoteLoc = NotScalarExpr->getExprLoc();
6853 NoteRange = NotScalarExpr->getSourceRange();
6854 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006855 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00006856 ErrorFound = NotAnAssignmentOp;
6857 ErrorLoc = AtomicBody->getExprLoc();
6858 ErrorRange = AtomicBody->getSourceRange();
6859 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6860 : AtomicBody->getExprLoc();
6861 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6862 : AtomicBody->getSourceRange();
6863 }
6864 } else {
6865 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006866 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataev62cec442014-11-18 10:14:22 +00006867 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006868 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006869 if (ErrorFound != NoError) {
6870 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
6871 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006872 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6873 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00006874 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00006875 }
6876 if (CurContext->isDependentContext())
Alexey Bataev62cec442014-11-18 10:14:22 +00006877 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00006878 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006879 enum {
6880 NotAnExpression,
6881 NotAnAssignmentOp,
6882 NotAScalarType,
6883 NotAnLValue,
6884 NoError
6885 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006886 SourceLocation ErrorLoc, NoteLoc;
6887 SourceRange ErrorRange, NoteRange;
6888 // If clause is write:
6889 // x = expr;
Alexey Bataeve3727102018-04-18 15:57:46 +00006890 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
6891 const auto *AtomicBinOp =
Alexey Bataevf33eba62014-11-28 07:21:40 +00006892 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6893 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00006894 X = AtomicBinOp->getLHS();
6895 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006896 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6897 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
6898 if (!X->isLValue()) {
6899 ErrorFound = NotAnLValue;
6900 ErrorLoc = AtomicBinOp->getExprLoc();
6901 ErrorRange = AtomicBinOp->getSourceRange();
6902 NoteLoc = X->getExprLoc();
6903 NoteRange = X->getSourceRange();
6904 }
6905 } else if (!X->isInstantiationDependent() ||
6906 !E->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006907 const Expr *NotScalarExpr =
Alexey Bataevf33eba62014-11-28 07:21:40 +00006908 (X->isInstantiationDependent() || X->getType()->isScalarType())
6909 ? E
6910 : X;
6911 ErrorFound = NotAScalarType;
6912 ErrorLoc = AtomicBinOp->getExprLoc();
6913 ErrorRange = AtomicBinOp->getSourceRange();
6914 NoteLoc = NotScalarExpr->getExprLoc();
6915 NoteRange = NotScalarExpr->getSourceRange();
6916 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006917 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00006918 ErrorFound = NotAnAssignmentOp;
6919 ErrorLoc = AtomicBody->getExprLoc();
6920 ErrorRange = AtomicBody->getSourceRange();
6921 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6922 : AtomicBody->getExprLoc();
6923 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6924 : AtomicBody->getSourceRange();
6925 }
6926 } else {
6927 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006928 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006929 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006930 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00006931 if (ErrorFound != NoError) {
6932 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
6933 << ErrorRange;
6934 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6935 << NoteRange;
6936 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00006937 }
6938 if (CurContext->isDependentContext())
Alexey Bataevf33eba62014-11-28 07:21:40 +00006939 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006940 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006941 // If clause is update:
6942 // x++;
6943 // x--;
6944 // ++x;
6945 // --x;
6946 // x binop= expr;
6947 // x = x binop expr;
6948 // x = expr binop x;
6949 OpenMPAtomicUpdateChecker Checker(*this);
6950 if (Checker.checkStatement(
6951 Body, (AtomicKind == OMPC_update)
6952 ? diag::err_omp_atomic_update_not_expression_statement
6953 : diag::err_omp_atomic_not_expression_statement,
6954 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00006955 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006956 if (!CurContext->isDependentContext()) {
6957 E = Checker.getExpr();
6958 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006959 UE = Checker.getUpdateExpr();
6960 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00006961 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006962 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006963 enum {
6964 NotAnAssignmentOp,
6965 NotACompoundStatement,
6966 NotTwoSubstatements,
6967 NotASpecificExpression,
6968 NoError
6969 } ErrorFound = NoError;
6970 SourceLocation ErrorLoc, NoteLoc;
6971 SourceRange ErrorRange, NoteRange;
Alexey Bataeve3727102018-04-18 15:57:46 +00006972 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006973 // If clause is a capture:
6974 // v = x++;
6975 // v = x--;
6976 // v = ++x;
6977 // v = --x;
6978 // v = x binop= expr;
6979 // v = x = x binop expr;
6980 // v = x = expr binop x;
Alexey Bataeve3727102018-04-18 15:57:46 +00006981 const auto *AtomicBinOp =
Alexey Bataevb78ca832015-04-01 03:33:17 +00006982 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6983 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6984 V = AtomicBinOp->getLHS();
6985 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6986 OpenMPAtomicUpdateChecker Checker(*this);
6987 if (Checker.checkStatement(
6988 Body, diag::err_omp_atomic_capture_not_expression_statement,
6989 diag::note_omp_atomic_update))
6990 return StmtError();
6991 E = Checker.getExpr();
6992 X = Checker.getX();
6993 UE = Checker.getUpdateExpr();
6994 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6995 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00006996 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006997 ErrorLoc = AtomicBody->getExprLoc();
6998 ErrorRange = AtomicBody->getSourceRange();
6999 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
7000 : AtomicBody->getExprLoc();
7001 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
7002 : AtomicBody->getSourceRange();
7003 ErrorFound = NotAnAssignmentOp;
7004 }
7005 if (ErrorFound != NoError) {
7006 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
7007 << ErrorRange;
7008 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
7009 return StmtError();
Alexey Bataevb78ca832015-04-01 03:33:17 +00007010 }
Alexey Bataeve3727102018-04-18 15:57:46 +00007011 if (CurContext->isDependentContext())
7012 UE = V = E = X = nullptr;
Alexey Bataevb78ca832015-04-01 03:33:17 +00007013 } else {
7014 // If clause is a capture:
7015 // { v = x; x = expr; }
7016 // { v = x; x++; }
7017 // { v = x; x--; }
7018 // { v = x; ++x; }
7019 // { v = x; --x; }
7020 // { v = x; x binop= expr; }
7021 // { v = x; x = x binop expr; }
7022 // { v = x; x = expr binop x; }
7023 // { x++; v = x; }
7024 // { x--; v = x; }
7025 // { ++x; v = x; }
7026 // { --x; v = x; }
7027 // { x binop= expr; v = x; }
7028 // { x = x binop expr; v = x; }
7029 // { x = expr binop x; v = x; }
7030 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
7031 // Check that this is { expr1; expr2; }
7032 if (CS->size() == 2) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007033 Stmt *First = CS->body_front();
7034 Stmt *Second = CS->body_back();
Alexey Bataevb78ca832015-04-01 03:33:17 +00007035 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
7036 First = EWC->getSubExpr()->IgnoreParenImpCasts();
7037 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
7038 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
7039 // Need to find what subexpression is 'v' and what is 'x'.
7040 OpenMPAtomicUpdateChecker Checker(*this);
7041 bool IsUpdateExprFound = !Checker.checkStatement(Second);
7042 BinaryOperator *BinOp = nullptr;
7043 if (IsUpdateExprFound) {
7044 BinOp = dyn_cast<BinaryOperator>(First);
7045 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
7046 }
7047 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
7048 // { v = x; x++; }
7049 // { v = x; x--; }
7050 // { v = x; ++x; }
7051 // { v = x; --x; }
7052 // { v = x; x binop= expr; }
7053 // { v = x; x = x binop expr; }
7054 // { v = x; x = expr binop x; }
7055 // Check that the first expression has form v = x.
Alexey Bataeve3727102018-04-18 15:57:46 +00007056 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataevb78ca832015-04-01 03:33:17 +00007057 llvm::FoldingSetNodeID XId, PossibleXId;
7058 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
7059 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
7060 IsUpdateExprFound = XId == PossibleXId;
7061 if (IsUpdateExprFound) {
7062 V = BinOp->getLHS();
7063 X = Checker.getX();
7064 E = Checker.getExpr();
7065 UE = Checker.getUpdateExpr();
7066 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00007067 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00007068 }
7069 }
7070 if (!IsUpdateExprFound) {
7071 IsUpdateExprFound = !Checker.checkStatement(First);
7072 BinOp = nullptr;
7073 if (IsUpdateExprFound) {
7074 BinOp = dyn_cast<BinaryOperator>(Second);
7075 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
7076 }
7077 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
7078 // { x++; v = x; }
7079 // { x--; v = x; }
7080 // { ++x; v = x; }
7081 // { --x; v = x; }
7082 // { x binop= expr; v = x; }
7083 // { x = x binop expr; v = x; }
7084 // { x = expr binop x; v = x; }
7085 // Check that the second expression has form v = x.
Alexey Bataeve3727102018-04-18 15:57:46 +00007086 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataevb78ca832015-04-01 03:33:17 +00007087 llvm::FoldingSetNodeID XId, PossibleXId;
7088 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
7089 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
7090 IsUpdateExprFound = XId == PossibleXId;
7091 if (IsUpdateExprFound) {
7092 V = BinOp->getLHS();
7093 X = Checker.getX();
7094 E = Checker.getExpr();
7095 UE = Checker.getUpdateExpr();
7096 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00007097 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00007098 }
7099 }
7100 }
7101 if (!IsUpdateExprFound) {
7102 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00007103 auto *FirstExpr = dyn_cast<Expr>(First);
7104 auto *SecondExpr = dyn_cast<Expr>(Second);
7105 if (!FirstExpr || !SecondExpr ||
7106 !(FirstExpr->isInstantiationDependent() ||
7107 SecondExpr->isInstantiationDependent())) {
7108 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
7109 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00007110 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00007111 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007112 : First->getBeginLoc();
Alexey Bataev5a195472015-09-04 12:55:50 +00007113 NoteRange = ErrorRange = FirstBinOp
7114 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00007115 : SourceRange(ErrorLoc, ErrorLoc);
7116 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00007117 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
7118 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
7119 ErrorFound = NotAnAssignmentOp;
7120 NoteLoc = ErrorLoc = SecondBinOp
7121 ? SecondBinOp->getOperatorLoc()
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007122 : Second->getBeginLoc();
Alexey Bataev5a195472015-09-04 12:55:50 +00007123 NoteRange = ErrorRange =
7124 SecondBinOp ? SecondBinOp->getSourceRange()
7125 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00007126 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +00007127 Expr *PossibleXRHSInFirst =
Alexey Bataev5a195472015-09-04 12:55:50 +00007128 FirstBinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00007129 Expr *PossibleXLHSInSecond =
Alexey Bataev5a195472015-09-04 12:55:50 +00007130 SecondBinOp->getLHS()->IgnoreParenImpCasts();
7131 llvm::FoldingSetNodeID X1Id, X2Id;
7132 PossibleXRHSInFirst->Profile(X1Id, Context,
7133 /*Canonical=*/true);
7134 PossibleXLHSInSecond->Profile(X2Id, Context,
7135 /*Canonical=*/true);
7136 IsUpdateExprFound = X1Id == X2Id;
7137 if (IsUpdateExprFound) {
7138 V = FirstBinOp->getLHS();
7139 X = SecondBinOp->getLHS();
7140 E = SecondBinOp->getRHS();
7141 UE = nullptr;
7142 IsXLHSInRHSPart = false;
7143 IsPostfixUpdate = true;
7144 } else {
7145 ErrorFound = NotASpecificExpression;
7146 ErrorLoc = FirstBinOp->getExprLoc();
7147 ErrorRange = FirstBinOp->getSourceRange();
7148 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
7149 NoteRange = SecondBinOp->getRHS()->getSourceRange();
7150 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00007151 }
7152 }
7153 }
7154 }
7155 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007156 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevb78ca832015-04-01 03:33:17 +00007157 NoteRange = ErrorRange =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007158 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
Alexey Bataevb78ca832015-04-01 03:33:17 +00007159 ErrorFound = NotTwoSubstatements;
7160 }
7161 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007162 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevb78ca832015-04-01 03:33:17 +00007163 NoteRange = ErrorRange =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007164 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
Alexey Bataevb78ca832015-04-01 03:33:17 +00007165 ErrorFound = NotACompoundStatement;
7166 }
7167 if (ErrorFound != NoError) {
7168 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
7169 << ErrorRange;
7170 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
7171 return StmtError();
Alexey Bataevb78ca832015-04-01 03:33:17 +00007172 }
Alexey Bataeve3727102018-04-18 15:57:46 +00007173 if (CurContext->isDependentContext())
7174 UE = V = E = X = nullptr;
Alexey Bataev459dec02014-07-24 06:46:57 +00007175 }
Alexey Bataevdea47612014-07-23 07:46:59 +00007176 }
Alexey Bataev0162e452014-07-22 10:10:35 +00007177
Reid Kleckner87a31802018-03-12 21:43:02 +00007178 setFunctionHasBranchProtectedScope();
Alexey Bataev0162e452014-07-22 10:10:35 +00007179
Alexey Bataev62cec442014-11-18 10:14:22 +00007180 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00007181 X, V, E, UE, IsXLHSInRHSPart,
7182 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00007183}
7184
Alexey Bataev0bd520b2014-09-19 08:19:49 +00007185StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
7186 Stmt *AStmt,
7187 SourceLocation StartLoc,
7188 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007189 if (!AStmt)
7190 return StmtError();
7191
Alexey Bataeve3727102018-04-18 15:57:46 +00007192 auto *CS = cast<CapturedStmt>(AStmt);
Samuel Antao4af1b7b2015-12-02 17:44:43 +00007193 // 1.2.2 OpenMP Language Terminology
7194 // Structured block - An executable statement with a single entry at the
7195 // top and a single exit at the bottom.
7196 // The point of exit cannot be a branch out of the structured block.
7197 // longjmp() and throw() must not violate the entry/exit criteria.
7198 CS->getCapturedDecl()->setNothrow();
Alexey Bataev8451efa2018-01-15 19:06:12 +00007199 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target);
7200 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7201 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7202 // 1.2.2 OpenMP Language Terminology
7203 // Structured block - An executable statement with a single entry at the
7204 // top and a single exit at the bottom.
7205 // The point of exit cannot be a branch out of the structured block.
7206 // longjmp() and throw() must not violate the entry/exit criteria.
7207 CS->getCapturedDecl()->setNothrow();
7208 }
Alexey Bataev0bd520b2014-09-19 08:19:49 +00007209
Alexey Bataev13314bf2014-10-09 04:18:56 +00007210 // OpenMP [2.16, Nesting of Regions]
7211 // If specified, a teams construct must be contained within a target
7212 // construct. That target construct must contain no statements or directives
7213 // outside of the teams construct.
7214 if (DSAStack->hasInnerTeamsRegion()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007215 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true);
Alexey Bataev13314bf2014-10-09 04:18:56 +00007216 bool OMPTeamsFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00007217 if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00007218 auto I = CS->body_begin();
7219 while (I != CS->body_end()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007220 const auto *OED = dyn_cast<OMPExecutableDirective>(*I);
Kelvin Li620ba602019-02-05 16:43:00 +00007221 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind()) ||
7222 OMPTeamsFound) {
7223
Alexey Bataev13314bf2014-10-09 04:18:56 +00007224 OMPTeamsFound = false;
7225 break;
7226 }
7227 ++I;
7228 }
7229 assert(I != CS->body_end() && "Not found statement");
7230 S = *I;
Kelvin Li3834dce2016-06-27 19:15:43 +00007231 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +00007232 const auto *OED = dyn_cast<OMPExecutableDirective>(S);
Kelvin Li3834dce2016-06-27 19:15:43 +00007233 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
Alexey Bataev13314bf2014-10-09 04:18:56 +00007234 }
7235 if (!OMPTeamsFound) {
7236 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
7237 Diag(DSAStack->getInnerTeamsRegionLoc(),
7238 diag::note_omp_nested_teams_construct_here);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007239 Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here)
Alexey Bataev13314bf2014-10-09 04:18:56 +00007240 << isa<OMPExecutableDirective>(S);
7241 return StmtError();
7242 }
7243 }
7244
Reid Kleckner87a31802018-03-12 21:43:02 +00007245 setFunctionHasBranchProtectedScope();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00007246
7247 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
7248}
7249
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00007250StmtResult
7251Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
7252 Stmt *AStmt, SourceLocation StartLoc,
7253 SourceLocation EndLoc) {
7254 if (!AStmt)
7255 return StmtError();
7256
Alexey Bataeve3727102018-04-18 15:57:46 +00007257 auto *CS = cast<CapturedStmt>(AStmt);
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00007258 // 1.2.2 OpenMP Language Terminology
7259 // Structured block - An executable statement with a single entry at the
7260 // top and a single exit at the bottom.
7261 // The point of exit cannot be a branch out of the structured block.
7262 // longjmp() and throw() must not violate the entry/exit criteria.
7263 CS->getCapturedDecl()->setNothrow();
Alexey Bataev8451efa2018-01-15 19:06:12 +00007264 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel);
7265 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7266 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7267 // 1.2.2 OpenMP Language Terminology
7268 // Structured block - An executable statement with a single entry at the
7269 // top and a single exit at the bottom.
7270 // The point of exit cannot be a branch out of the structured block.
7271 // longjmp() and throw() must not violate the entry/exit criteria.
7272 CS->getCapturedDecl()->setNothrow();
7273 }
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00007274
Reid Kleckner87a31802018-03-12 21:43:02 +00007275 setFunctionHasBranchProtectedScope();
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00007276
7277 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
7278 AStmt);
7279}
7280
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007281StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
7282 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007283 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007284 if (!AStmt)
7285 return StmtError();
7286
Alexey Bataeve3727102018-04-18 15:57:46 +00007287 auto *CS = cast<CapturedStmt>(AStmt);
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007288 // 1.2.2 OpenMP Language Terminology
7289 // Structured block - An executable statement with a single entry at the
7290 // top and a single exit at the bottom.
7291 // The point of exit cannot be a branch out of the structured block.
7292 // longjmp() and throw() must not violate the entry/exit criteria.
7293 CS->getCapturedDecl()->setNothrow();
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00007294 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
7295 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7296 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7297 // 1.2.2 OpenMP Language Terminology
7298 // Structured block - An executable statement with a single entry at the
7299 // top and a single exit at the bottom.
7300 // The point of exit cannot be a branch out of the structured block.
7301 // longjmp() and throw() must not violate the entry/exit criteria.
7302 CS->getCapturedDecl()->setNothrow();
7303 }
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007304
7305 OMPLoopDirective::HelperExprs B;
7306 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7307 // define the nested loops number.
7308 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007309 checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00007310 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007311 VarsWithImplicitDSA, B);
7312 if (NestedLoopCount == 0)
7313 return StmtError();
7314
7315 assert((CurContext->isDependentContext() || B.builtAll()) &&
7316 "omp target parallel for loop exprs were not built");
7317
7318 if (!CurContext->isDependentContext()) {
7319 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007320 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007321 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007322 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007323 B.NumIterations, *this, CurScope,
7324 DSAStack))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007325 return StmtError();
7326 }
7327 }
7328
Reid Kleckner87a31802018-03-12 21:43:02 +00007329 setFunctionHasBranchProtectedScope();
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007330 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
7331 NestedLoopCount, Clauses, AStmt,
7332 B, DSAStack->isCancelRegion());
7333}
7334
Alexey Bataev95b64a92017-05-30 16:00:04 +00007335/// Check for existence of a map clause in the list of clauses.
7336static bool hasClauses(ArrayRef<OMPClause *> Clauses,
7337 const OpenMPClauseKind K) {
7338 return llvm::any_of(
7339 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; });
7340}
Samuel Antaodf67fc42016-01-19 19:15:56 +00007341
Alexey Bataev95b64a92017-05-30 16:00:04 +00007342template <typename... Params>
7343static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K,
7344 const Params... ClauseTypes) {
7345 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...);
Samuel Antaodf67fc42016-01-19 19:15:56 +00007346}
7347
Michael Wong65f367f2015-07-21 13:44:28 +00007348StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
7349 Stmt *AStmt,
7350 SourceLocation StartLoc,
7351 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007352 if (!AStmt)
7353 return StmtError();
7354
7355 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7356
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00007357 // OpenMP [2.10.1, Restrictions, p. 97]
7358 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00007359 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr)) {
7360 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
7361 << "'map' or 'use_device_ptr'"
David Majnemer9d168222016-08-05 17:44:54 +00007362 << getOpenMPDirectiveName(OMPD_target_data);
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00007363 return StmtError();
7364 }
7365
Reid Kleckner87a31802018-03-12 21:43:02 +00007366 setFunctionHasBranchProtectedScope();
Michael Wong65f367f2015-07-21 13:44:28 +00007367
7368 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
7369 AStmt);
7370}
7371
Samuel Antaodf67fc42016-01-19 19:15:56 +00007372StmtResult
7373Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
7374 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00007375 SourceLocation EndLoc, Stmt *AStmt) {
7376 if (!AStmt)
7377 return StmtError();
7378
Alexey Bataeve3727102018-04-18 15:57:46 +00007379 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00007380 // 1.2.2 OpenMP Language Terminology
7381 // Structured block - An executable statement with a single entry at the
7382 // top and a single exit at the bottom.
7383 // The point of exit cannot be a branch out of the structured block.
7384 // longjmp() and throw() must not violate the entry/exit criteria.
7385 CS->getCapturedDecl()->setNothrow();
7386 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data);
7387 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7388 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7389 // 1.2.2 OpenMP Language Terminology
7390 // Structured block - An executable statement with a single entry at the
7391 // top and a single exit at the bottom.
7392 // The point of exit cannot be a branch out of the structured block.
7393 // longjmp() and throw() must not violate the entry/exit criteria.
7394 CS->getCapturedDecl()->setNothrow();
7395 }
7396
Samuel Antaodf67fc42016-01-19 19:15:56 +00007397 // OpenMP [2.10.2, Restrictions, p. 99]
7398 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00007399 if (!hasClauses(Clauses, OMPC_map)) {
7400 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
7401 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data);
Samuel Antaodf67fc42016-01-19 19:15:56 +00007402 return StmtError();
7403 }
7404
Alexey Bataev7828b252017-11-21 17:08:48 +00007405 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
7406 AStmt);
Samuel Antaodf67fc42016-01-19 19:15:56 +00007407}
7408
Samuel Antao72590762016-01-19 20:04:50 +00007409StmtResult
7410Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
7411 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00007412 SourceLocation EndLoc, Stmt *AStmt) {
7413 if (!AStmt)
7414 return StmtError();
7415
Alexey Bataeve3727102018-04-18 15:57:46 +00007416 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00007417 // 1.2.2 OpenMP Language Terminology
7418 // Structured block - An executable statement with a single entry at the
7419 // top and a single exit at the bottom.
7420 // The point of exit cannot be a branch out of the structured block.
7421 // longjmp() and throw() must not violate the entry/exit criteria.
7422 CS->getCapturedDecl()->setNothrow();
7423 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data);
7424 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7425 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7426 // 1.2.2 OpenMP Language Terminology
7427 // Structured block - An executable statement with a single entry at the
7428 // top and a single exit at the bottom.
7429 // The point of exit cannot be a branch out of the structured block.
7430 // longjmp() and throw() must not violate the entry/exit criteria.
7431 CS->getCapturedDecl()->setNothrow();
7432 }
7433
Samuel Antao72590762016-01-19 20:04:50 +00007434 // OpenMP [2.10.3, Restrictions, p. 102]
7435 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00007436 if (!hasClauses(Clauses, OMPC_map)) {
7437 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
7438 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data);
Samuel Antao72590762016-01-19 20:04:50 +00007439 return StmtError();
7440 }
7441
Alexey Bataev7828b252017-11-21 17:08:48 +00007442 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
7443 AStmt);
Samuel Antao72590762016-01-19 20:04:50 +00007444}
7445
Samuel Antao686c70c2016-05-26 17:30:50 +00007446StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
7447 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00007448 SourceLocation EndLoc,
7449 Stmt *AStmt) {
7450 if (!AStmt)
7451 return StmtError();
7452
Alexey Bataeve3727102018-04-18 15:57:46 +00007453 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00007454 // 1.2.2 OpenMP Language Terminology
7455 // Structured block - An executable statement with a single entry at the
7456 // top and a single exit at the bottom.
7457 // The point of exit cannot be a branch out of the structured block.
7458 // longjmp() and throw() must not violate the entry/exit criteria.
7459 CS->getCapturedDecl()->setNothrow();
7460 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update);
7461 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7462 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7463 // 1.2.2 OpenMP Language Terminology
7464 // Structured block - An executable statement with a single entry at the
7465 // top and a single exit at the bottom.
7466 // The point of exit cannot be a branch out of the structured block.
7467 // longjmp() and throw() must not violate the entry/exit criteria.
7468 CS->getCapturedDecl()->setNothrow();
7469 }
7470
Alexey Bataev95b64a92017-05-30 16:00:04 +00007471 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) {
Samuel Antao686c70c2016-05-26 17:30:50 +00007472 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
7473 return StmtError();
7474 }
Alexey Bataev7828b252017-11-21 17:08:48 +00007475 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses,
7476 AStmt);
Samuel Antao686c70c2016-05-26 17:30:50 +00007477}
7478
Alexey Bataev13314bf2014-10-09 04:18:56 +00007479StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
7480 Stmt *AStmt, SourceLocation StartLoc,
7481 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007482 if (!AStmt)
7483 return StmtError();
7484
Alexey Bataeve3727102018-04-18 15:57:46 +00007485 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev13314bf2014-10-09 04:18:56 +00007486 // 1.2.2 OpenMP Language Terminology
7487 // Structured block - An executable statement with a single entry at the
7488 // top and a single exit at the bottom.
7489 // The point of exit cannot be a branch out of the structured block.
7490 // longjmp() and throw() must not violate the entry/exit criteria.
7491 CS->getCapturedDecl()->setNothrow();
7492
Reid Kleckner87a31802018-03-12 21:43:02 +00007493 setFunctionHasBranchProtectedScope();
Alexey Bataev13314bf2014-10-09 04:18:56 +00007494
Alexey Bataevceabd412017-11-30 18:01:54 +00007495 DSAStack->setParentTeamsRegionLoc(StartLoc);
7496
Alexey Bataev13314bf2014-10-09 04:18:56 +00007497 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
7498}
7499
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007500StmtResult
7501Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
7502 SourceLocation EndLoc,
7503 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007504 if (DSAStack->isParentNowaitRegion()) {
7505 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
7506 return StmtError();
7507 }
7508 if (DSAStack->isParentOrderedRegion()) {
7509 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
7510 return StmtError();
7511 }
7512 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
7513 CancelRegion);
7514}
7515
Alexey Bataev87933c72015-09-18 08:07:34 +00007516StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
7517 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00007518 SourceLocation EndLoc,
7519 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev80909872015-07-02 11:25:17 +00007520 if (DSAStack->isParentNowaitRegion()) {
7521 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
7522 return StmtError();
7523 }
7524 if (DSAStack->isParentOrderedRegion()) {
7525 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
7526 return StmtError();
7527 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00007528 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00007529 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
7530 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00007531}
7532
Alexey Bataev382967a2015-12-08 12:06:20 +00007533static bool checkGrainsizeNumTasksClauses(Sema &S,
7534 ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007535 const OMPClause *PrevClause = nullptr;
Alexey Bataev382967a2015-12-08 12:06:20 +00007536 bool ErrorFound = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00007537 for (const OMPClause *C : Clauses) {
Alexey Bataev382967a2015-12-08 12:06:20 +00007538 if (C->getClauseKind() == OMPC_grainsize ||
7539 C->getClauseKind() == OMPC_num_tasks) {
7540 if (!PrevClause)
7541 PrevClause = C;
7542 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007543 S.Diag(C->getBeginLoc(),
Alexey Bataev382967a2015-12-08 12:06:20 +00007544 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
7545 << getOpenMPClauseName(C->getClauseKind())
7546 << getOpenMPClauseName(PrevClause->getClauseKind());
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007547 S.Diag(PrevClause->getBeginLoc(),
Alexey Bataev382967a2015-12-08 12:06:20 +00007548 diag::note_omp_previous_grainsize_num_tasks)
7549 << getOpenMPClauseName(PrevClause->getClauseKind());
7550 ErrorFound = true;
7551 }
7552 }
7553 }
7554 return ErrorFound;
7555}
7556
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007557static bool checkReductionClauseWithNogroup(Sema &S,
7558 ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007559 const OMPClause *ReductionClause = nullptr;
7560 const OMPClause *NogroupClause = nullptr;
7561 for (const OMPClause *C : Clauses) {
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007562 if (C->getClauseKind() == OMPC_reduction) {
7563 ReductionClause = C;
7564 if (NogroupClause)
7565 break;
7566 continue;
7567 }
7568 if (C->getClauseKind() == OMPC_nogroup) {
7569 NogroupClause = C;
7570 if (ReductionClause)
7571 break;
7572 continue;
7573 }
7574 }
7575 if (ReductionClause && NogroupClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007576 S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup)
7577 << SourceRange(NogroupClause->getBeginLoc(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007578 NogroupClause->getEndLoc());
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007579 return true;
7580 }
7581 return false;
7582}
7583
Alexey Bataev49f6e782015-12-01 04:18:41 +00007584StmtResult Sema::ActOnOpenMPTaskLoopDirective(
7585 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007586 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00007587 if (!AStmt)
7588 return StmtError();
7589
7590 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7591 OMPLoopDirective::HelperExprs B;
7592 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7593 // define the nested loops number.
7594 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007595 checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007596 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00007597 VarsWithImplicitDSA, B);
7598 if (NestedLoopCount == 0)
7599 return StmtError();
7600
7601 assert((CurContext->isDependentContext() || B.builtAll()) &&
7602 "omp for loop exprs were not built");
7603
Alexey Bataev382967a2015-12-08 12:06:20 +00007604 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7605 // The grainsize clause and num_tasks clause are mutually exclusive and may
7606 // not appear on the same taskloop directive.
7607 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7608 return StmtError();
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007609 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7610 // If a reduction clause is present on the taskloop directive, the nogroup
7611 // clause must not be specified.
7612 if (checkReductionClauseWithNogroup(*this, Clauses))
7613 return StmtError();
Alexey Bataev382967a2015-12-08 12:06:20 +00007614
Reid Kleckner87a31802018-03-12 21:43:02 +00007615 setFunctionHasBranchProtectedScope();
Alexey Bataev49f6e782015-12-01 04:18:41 +00007616 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
7617 NestedLoopCount, Clauses, AStmt, B);
7618}
7619
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007620StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
7621 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007622 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007623 if (!AStmt)
7624 return StmtError();
7625
7626 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7627 OMPLoopDirective::HelperExprs B;
7628 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7629 // define the nested loops number.
7630 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007631 checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007632 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
7633 VarsWithImplicitDSA, B);
7634 if (NestedLoopCount == 0)
7635 return StmtError();
7636
7637 assert((CurContext->isDependentContext() || B.builtAll()) &&
7638 "omp for loop exprs were not built");
7639
Alexey Bataev5a3af132016-03-29 08:58:54 +00007640 if (!CurContext->isDependentContext()) {
7641 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007642 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007643 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007644 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007645 B.NumIterations, *this, CurScope,
7646 DSAStack))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007647 return StmtError();
7648 }
7649 }
7650
Alexey Bataev382967a2015-12-08 12:06:20 +00007651 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7652 // The grainsize clause and num_tasks clause are mutually exclusive and may
7653 // not appear on the same taskloop directive.
7654 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7655 return StmtError();
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007656 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7657 // If a reduction clause is present on the taskloop directive, the nogroup
7658 // clause must not be specified.
7659 if (checkReductionClauseWithNogroup(*this, Clauses))
7660 return StmtError();
Alexey Bataev438388c2017-11-22 18:34:02 +00007661 if (checkSimdlenSafelenSpecified(*this, Clauses))
7662 return StmtError();
Alexey Bataev382967a2015-12-08 12:06:20 +00007663
Reid Kleckner87a31802018-03-12 21:43:02 +00007664 setFunctionHasBranchProtectedScope();
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007665 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
7666 NestedLoopCount, Clauses, AStmt, B);
7667}
7668
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007669StmtResult Sema::ActOnOpenMPDistributeDirective(
7670 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007671 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007672 if (!AStmt)
7673 return StmtError();
7674
7675 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7676 OMPLoopDirective::HelperExprs B;
7677 // In presence of clause 'collapse' with number of loops, it will
7678 // define the nested loops number.
7679 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007680 checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007681 nullptr /*ordered not a clause on distribute*/, AStmt,
7682 *this, *DSAStack, VarsWithImplicitDSA, B);
7683 if (NestedLoopCount == 0)
7684 return StmtError();
7685
7686 assert((CurContext->isDependentContext() || B.builtAll()) &&
7687 "omp for loop exprs were not built");
7688
Reid Kleckner87a31802018-03-12 21:43:02 +00007689 setFunctionHasBranchProtectedScope();
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007690 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
7691 NestedLoopCount, Clauses, AStmt, B);
7692}
7693
Carlo Bertolli9925f152016-06-27 14:55:37 +00007694StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
7695 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007696 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Carlo Bertolli9925f152016-06-27 14:55:37 +00007697 if (!AStmt)
7698 return StmtError();
7699
Alexey Bataeve3727102018-04-18 15:57:46 +00007700 auto *CS = cast<CapturedStmt>(AStmt);
Carlo Bertolli9925f152016-06-27 14:55:37 +00007701 // 1.2.2 OpenMP Language Terminology
7702 // Structured block - An executable statement with a single entry at the
7703 // top and a single exit at the bottom.
7704 // The point of exit cannot be a branch out of the structured block.
7705 // longjmp() and throw() must not violate the entry/exit criteria.
7706 CS->getCapturedDecl()->setNothrow();
Alexey Bataev7f96c372017-11-22 17:19:31 +00007707 for (int ThisCaptureLevel =
7708 getOpenMPCaptureLevels(OMPD_distribute_parallel_for);
7709 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7710 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7711 // 1.2.2 OpenMP Language Terminology
7712 // Structured block - An executable statement with a single entry at the
7713 // top and a single exit at the bottom.
7714 // The point of exit cannot be a branch out of the structured block.
7715 // longjmp() and throw() must not violate the entry/exit criteria.
7716 CS->getCapturedDecl()->setNothrow();
7717 }
Carlo Bertolli9925f152016-06-27 14:55:37 +00007718
7719 OMPLoopDirective::HelperExprs B;
7720 // In presence of clause 'collapse' with number of loops, it will
7721 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007722 unsigned NestedLoopCount = checkOpenMPLoop(
Carlo Bertolli9925f152016-06-27 14:55:37 +00007723 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataev7f96c372017-11-22 17:19:31 +00007724 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Carlo Bertolli9925f152016-06-27 14:55:37 +00007725 VarsWithImplicitDSA, B);
7726 if (NestedLoopCount == 0)
7727 return StmtError();
7728
7729 assert((CurContext->isDependentContext() || B.builtAll()) &&
7730 "omp for loop exprs were not built");
7731
Reid Kleckner87a31802018-03-12 21:43:02 +00007732 setFunctionHasBranchProtectedScope();
Carlo Bertolli9925f152016-06-27 14:55:37 +00007733 return OMPDistributeParallelForDirective::Create(
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00007734 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7735 DSAStack->isCancelRegion());
Carlo Bertolli9925f152016-06-27 14:55:37 +00007736}
7737
Kelvin Li4a39add2016-07-05 05:00:15 +00007738StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
7739 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007740 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li4a39add2016-07-05 05:00:15 +00007741 if (!AStmt)
7742 return StmtError();
7743
Alexey Bataeve3727102018-04-18 15:57:46 +00007744 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li4a39add2016-07-05 05:00:15 +00007745 // 1.2.2 OpenMP Language Terminology
7746 // Structured block - An executable statement with a single entry at the
7747 // top and a single exit at the bottom.
7748 // The point of exit cannot be a branch out of the structured block.
7749 // longjmp() and throw() must not violate the entry/exit criteria.
7750 CS->getCapturedDecl()->setNothrow();
Alexey Bataev974acd62017-11-27 19:38:52 +00007751 for (int ThisCaptureLevel =
7752 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd);
7753 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7754 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7755 // 1.2.2 OpenMP Language Terminology
7756 // Structured block - An executable statement with a single entry at the
7757 // top and a single exit at the bottom.
7758 // The point of exit cannot be a branch out of the structured block.
7759 // longjmp() and throw() must not violate the entry/exit criteria.
7760 CS->getCapturedDecl()->setNothrow();
7761 }
Kelvin Li4a39add2016-07-05 05:00:15 +00007762
7763 OMPLoopDirective::HelperExprs B;
7764 // In presence of clause 'collapse' with number of loops, it will
7765 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007766 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li4a39add2016-07-05 05:00:15 +00007767 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev974acd62017-11-27 19:38:52 +00007768 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li4a39add2016-07-05 05:00:15 +00007769 VarsWithImplicitDSA, B);
7770 if (NestedLoopCount == 0)
7771 return StmtError();
7772
7773 assert((CurContext->isDependentContext() || B.builtAll()) &&
7774 "omp for loop exprs were not built");
7775
Alexey Bataev438388c2017-11-22 18:34:02 +00007776 if (!CurContext->isDependentContext()) {
7777 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007778 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00007779 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7780 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7781 B.NumIterations, *this, CurScope,
7782 DSAStack))
7783 return StmtError();
7784 }
7785 }
7786
Kelvin Lic5609492016-07-15 04:39:07 +00007787 if (checkSimdlenSafelenSpecified(*this, Clauses))
7788 return StmtError();
7789
Reid Kleckner87a31802018-03-12 21:43:02 +00007790 setFunctionHasBranchProtectedScope();
Kelvin Li4a39add2016-07-05 05:00:15 +00007791 return OMPDistributeParallelForSimdDirective::Create(
7792 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7793}
7794
Kelvin Li787f3fc2016-07-06 04:45:38 +00007795StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
7796 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007797 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li787f3fc2016-07-06 04:45:38 +00007798 if (!AStmt)
7799 return StmtError();
7800
Alexey Bataeve3727102018-04-18 15:57:46 +00007801 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li787f3fc2016-07-06 04:45:38 +00007802 // 1.2.2 OpenMP Language Terminology
7803 // Structured block - An executable statement with a single entry at the
7804 // top and a single exit at the bottom.
7805 // The point of exit cannot be a branch out of the structured block.
7806 // longjmp() and throw() must not violate the entry/exit criteria.
7807 CS->getCapturedDecl()->setNothrow();
Alexey Bataev617db5f2017-12-04 15:38:33 +00007808 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd);
7809 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7810 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7811 // 1.2.2 OpenMP Language Terminology
7812 // Structured block - An executable statement with a single entry at the
7813 // top and a single exit at the bottom.
7814 // The point of exit cannot be a branch out of the structured block.
7815 // longjmp() and throw() must not violate the entry/exit criteria.
7816 CS->getCapturedDecl()->setNothrow();
7817 }
Kelvin Li787f3fc2016-07-06 04:45:38 +00007818
7819 OMPLoopDirective::HelperExprs B;
7820 // In presence of clause 'collapse' with number of loops, it will
7821 // define the nested loops number.
7822 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007823 checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev617db5f2017-12-04 15:38:33 +00007824 nullptr /*ordered not a clause on distribute*/, CS, *this,
7825 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li787f3fc2016-07-06 04:45:38 +00007826 if (NestedLoopCount == 0)
7827 return StmtError();
7828
7829 assert((CurContext->isDependentContext() || B.builtAll()) &&
7830 "omp for loop exprs were not built");
7831
Alexey Bataev438388c2017-11-22 18:34:02 +00007832 if (!CurContext->isDependentContext()) {
7833 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007834 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00007835 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7836 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7837 B.NumIterations, *this, CurScope,
7838 DSAStack))
7839 return StmtError();
7840 }
7841 }
7842
Kelvin Lic5609492016-07-15 04:39:07 +00007843 if (checkSimdlenSafelenSpecified(*this, Clauses))
7844 return StmtError();
7845
Reid Kleckner87a31802018-03-12 21:43:02 +00007846 setFunctionHasBranchProtectedScope();
Kelvin Li787f3fc2016-07-06 04:45:38 +00007847 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
7848 NestedLoopCount, Clauses, AStmt, B);
7849}
7850
Kelvin Lia579b912016-07-14 02:54:56 +00007851StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
7852 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007853 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Lia579b912016-07-14 02:54:56 +00007854 if (!AStmt)
7855 return StmtError();
7856
Alexey Bataeve3727102018-04-18 15:57:46 +00007857 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Lia579b912016-07-14 02:54:56 +00007858 // 1.2.2 OpenMP Language Terminology
7859 // Structured block - An executable statement with a single entry at the
7860 // top and a single exit at the bottom.
7861 // The point of exit cannot be a branch out of the structured block.
7862 // longjmp() and throw() must not violate the entry/exit criteria.
7863 CS->getCapturedDecl()->setNothrow();
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007864 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
7865 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7866 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7867 // 1.2.2 OpenMP Language Terminology
7868 // Structured block - An executable statement with a single entry at the
7869 // top and a single exit at the bottom.
7870 // The point of exit cannot be a branch out of the structured block.
7871 // longjmp() and throw() must not violate the entry/exit criteria.
7872 CS->getCapturedDecl()->setNothrow();
7873 }
Kelvin Lia579b912016-07-14 02:54:56 +00007874
7875 OMPLoopDirective::HelperExprs B;
7876 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7877 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007878 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Lia579b912016-07-14 02:54:56 +00007879 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007880 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Kelvin Lia579b912016-07-14 02:54:56 +00007881 VarsWithImplicitDSA, B);
7882 if (NestedLoopCount == 0)
7883 return StmtError();
7884
7885 assert((CurContext->isDependentContext() || B.builtAll()) &&
7886 "omp target parallel for simd loop exprs were not built");
7887
7888 if (!CurContext->isDependentContext()) {
7889 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007890 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007891 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Lia579b912016-07-14 02:54:56 +00007892 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7893 B.NumIterations, *this, CurScope,
7894 DSAStack))
7895 return StmtError();
7896 }
7897 }
Kelvin Lic5609492016-07-15 04:39:07 +00007898 if (checkSimdlenSafelenSpecified(*this, Clauses))
Kelvin Lia579b912016-07-14 02:54:56 +00007899 return StmtError();
7900
Reid Kleckner87a31802018-03-12 21:43:02 +00007901 setFunctionHasBranchProtectedScope();
Kelvin Lia579b912016-07-14 02:54:56 +00007902 return OMPTargetParallelForSimdDirective::Create(
7903 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7904}
7905
Kelvin Li986330c2016-07-20 22:57:10 +00007906StmtResult Sema::ActOnOpenMPTargetSimdDirective(
7907 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007908 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li986330c2016-07-20 22:57:10 +00007909 if (!AStmt)
7910 return StmtError();
7911
Alexey Bataeve3727102018-04-18 15:57:46 +00007912 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li986330c2016-07-20 22:57:10 +00007913 // 1.2.2 OpenMP Language Terminology
7914 // Structured block - An executable statement with a single entry at the
7915 // top and a single exit at the bottom.
7916 // The point of exit cannot be a branch out of the structured block.
7917 // longjmp() and throw() must not violate the entry/exit criteria.
7918 CS->getCapturedDecl()->setNothrow();
Alexey Bataevf8365372017-11-17 17:57:25 +00007919 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd);
7920 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7921 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7922 // 1.2.2 OpenMP Language Terminology
7923 // Structured block - An executable statement with a single entry at the
7924 // top and a single exit at the bottom.
7925 // The point of exit cannot be a branch out of the structured block.
7926 // longjmp() and throw() must not violate the entry/exit criteria.
7927 CS->getCapturedDecl()->setNothrow();
7928 }
7929
Kelvin Li986330c2016-07-20 22:57:10 +00007930 OMPLoopDirective::HelperExprs B;
7931 // In presence of clause 'collapse' with number of loops, it will define the
7932 // nested loops number.
David Majnemer9d168222016-08-05 17:44:54 +00007933 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007934 checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
Alexey Bataevf8365372017-11-17 17:57:25 +00007935 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Kelvin Li986330c2016-07-20 22:57:10 +00007936 VarsWithImplicitDSA, B);
7937 if (NestedLoopCount == 0)
7938 return StmtError();
7939
7940 assert((CurContext->isDependentContext() || B.builtAll()) &&
7941 "omp target simd loop exprs were not built");
7942
7943 if (!CurContext->isDependentContext()) {
7944 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007945 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007946 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Li986330c2016-07-20 22:57:10 +00007947 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7948 B.NumIterations, *this, CurScope,
7949 DSAStack))
7950 return StmtError();
7951 }
7952 }
7953
7954 if (checkSimdlenSafelenSpecified(*this, Clauses))
7955 return StmtError();
7956
Reid Kleckner87a31802018-03-12 21:43:02 +00007957 setFunctionHasBranchProtectedScope();
Kelvin Li986330c2016-07-20 22:57:10 +00007958 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
7959 NestedLoopCount, Clauses, AStmt, B);
7960}
7961
Kelvin Li02532872016-08-05 14:37:37 +00007962StmtResult Sema::ActOnOpenMPTeamsDistributeDirective(
7963 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007964 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li02532872016-08-05 14:37:37 +00007965 if (!AStmt)
7966 return StmtError();
7967
Alexey Bataeve3727102018-04-18 15:57:46 +00007968 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li02532872016-08-05 14:37:37 +00007969 // 1.2.2 OpenMP Language Terminology
7970 // Structured block - An executable statement with a single entry at the
7971 // top and a single exit at the bottom.
7972 // The point of exit cannot be a branch out of the structured block.
7973 // longjmp() and throw() must not violate the entry/exit criteria.
7974 CS->getCapturedDecl()->setNothrow();
Alexey Bataev95c6dd42017-11-29 15:14:16 +00007975 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute);
7976 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7977 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7978 // 1.2.2 OpenMP Language Terminology
7979 // Structured block - An executable statement with a single entry at the
7980 // top and a single exit at the bottom.
7981 // The point of exit cannot be a branch out of the structured block.
7982 // longjmp() and throw() must not violate the entry/exit criteria.
7983 CS->getCapturedDecl()->setNothrow();
7984 }
Kelvin Li02532872016-08-05 14:37:37 +00007985
7986 OMPLoopDirective::HelperExprs B;
7987 // In presence of clause 'collapse' with number of loops, it will
7988 // define the nested loops number.
7989 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007990 checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses),
Alexey Bataev95c6dd42017-11-29 15:14:16 +00007991 nullptr /*ordered not a clause on distribute*/, CS, *this,
7992 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li02532872016-08-05 14:37:37 +00007993 if (NestedLoopCount == 0)
7994 return StmtError();
7995
7996 assert((CurContext->isDependentContext() || B.builtAll()) &&
7997 "omp teams distribute loop exprs were not built");
7998
Reid Kleckner87a31802018-03-12 21:43:02 +00007999 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00008000
8001 DSAStack->setParentTeamsRegionLoc(StartLoc);
8002
David Majnemer9d168222016-08-05 17:44:54 +00008003 return OMPTeamsDistributeDirective::Create(
8004 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Kelvin Li02532872016-08-05 14:37:37 +00008005}
8006
Kelvin Li4e325f72016-10-25 12:50:55 +00008007StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective(
8008 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00008009 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li4e325f72016-10-25 12:50:55 +00008010 if (!AStmt)
8011 return StmtError();
8012
Alexey Bataeve3727102018-04-18 15:57:46 +00008013 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li4e325f72016-10-25 12:50:55 +00008014 // 1.2.2 OpenMP Language Terminology
8015 // Structured block - An executable statement with a single entry at the
8016 // top and a single exit at the bottom.
8017 // The point of exit cannot be a branch out of the structured block.
8018 // longjmp() and throw() must not violate the entry/exit criteria.
8019 CS->getCapturedDecl()->setNothrow();
Alexey Bataev999277a2017-12-06 14:31:09 +00008020 for (int ThisCaptureLevel =
8021 getOpenMPCaptureLevels(OMPD_teams_distribute_simd);
8022 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8023 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8024 // 1.2.2 OpenMP Language Terminology
8025 // Structured block - An executable statement with a single entry at the
8026 // top and a single exit at the bottom.
8027 // The point of exit cannot be a branch out of the structured block.
8028 // longjmp() and throw() must not violate the entry/exit criteria.
8029 CS->getCapturedDecl()->setNothrow();
8030 }
8031
Kelvin Li4e325f72016-10-25 12:50:55 +00008032
8033 OMPLoopDirective::HelperExprs B;
8034 // In presence of clause 'collapse' with number of loops, it will
8035 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00008036 unsigned NestedLoopCount = checkOpenMPLoop(
Samuel Antao4c8035b2016-12-12 18:00:20 +00008037 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev999277a2017-12-06 14:31:09 +00008038 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Samuel Antao4c8035b2016-12-12 18:00:20 +00008039 VarsWithImplicitDSA, B);
Kelvin Li4e325f72016-10-25 12:50:55 +00008040
8041 if (NestedLoopCount == 0)
8042 return StmtError();
8043
8044 assert((CurContext->isDependentContext() || B.builtAll()) &&
8045 "omp teams distribute simd loop exprs were not built");
8046
8047 if (!CurContext->isDependentContext()) {
8048 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00008049 for (OMPClause *C : Clauses) {
Kelvin Li4e325f72016-10-25 12:50:55 +00008050 if (auto *LC = dyn_cast<OMPLinearClause>(C))
8051 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8052 B.NumIterations, *this, CurScope,
8053 DSAStack))
8054 return StmtError();
8055 }
8056 }
8057
8058 if (checkSimdlenSafelenSpecified(*this, Clauses))
8059 return StmtError();
8060
Reid Kleckner87a31802018-03-12 21:43:02 +00008061 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00008062
8063 DSAStack->setParentTeamsRegionLoc(StartLoc);
8064
Kelvin Li4e325f72016-10-25 12:50:55 +00008065 return OMPTeamsDistributeSimdDirective::Create(
8066 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
8067}
8068
Kelvin Li579e41c2016-11-30 23:51:03 +00008069StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective(
8070 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00008071 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li579e41c2016-11-30 23:51:03 +00008072 if (!AStmt)
8073 return StmtError();
8074
Alexey Bataeve3727102018-04-18 15:57:46 +00008075 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li579e41c2016-11-30 23:51:03 +00008076 // 1.2.2 OpenMP Language Terminology
8077 // Structured block - An executable statement with a single entry at the
8078 // top and a single exit at the bottom.
8079 // The point of exit cannot be a branch out of the structured block.
8080 // longjmp() and throw() must not violate the entry/exit criteria.
8081 CS->getCapturedDecl()->setNothrow();
8082
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00008083 for (int ThisCaptureLevel =
8084 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd);
8085 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8086 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8087 // 1.2.2 OpenMP Language Terminology
8088 // Structured block - An executable statement with a single entry at the
8089 // top and a single exit at the bottom.
8090 // The point of exit cannot be a branch out of the structured block.
8091 // longjmp() and throw() must not violate the entry/exit criteria.
8092 CS->getCapturedDecl()->setNothrow();
8093 }
8094
Kelvin Li579e41c2016-11-30 23:51:03 +00008095 OMPLoopDirective::HelperExprs B;
8096 // In presence of clause 'collapse' with number of loops, it will
8097 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00008098 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li579e41c2016-11-30 23:51:03 +00008099 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00008100 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li579e41c2016-11-30 23:51:03 +00008101 VarsWithImplicitDSA, B);
8102
8103 if (NestedLoopCount == 0)
8104 return StmtError();
8105
8106 assert((CurContext->isDependentContext() || B.builtAll()) &&
8107 "omp for loop exprs were not built");
8108
8109 if (!CurContext->isDependentContext()) {
8110 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00008111 for (OMPClause *C : Clauses) {
Kelvin Li579e41c2016-11-30 23:51:03 +00008112 if (auto *LC = dyn_cast<OMPLinearClause>(C))
8113 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8114 B.NumIterations, *this, CurScope,
8115 DSAStack))
8116 return StmtError();
8117 }
8118 }
8119
8120 if (checkSimdlenSafelenSpecified(*this, Clauses))
8121 return StmtError();
8122
Reid Kleckner87a31802018-03-12 21:43:02 +00008123 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00008124
8125 DSAStack->setParentTeamsRegionLoc(StartLoc);
8126
Kelvin Li579e41c2016-11-30 23:51:03 +00008127 return OMPTeamsDistributeParallelForSimdDirective::Create(
8128 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
8129}
8130
Kelvin Li7ade93f2016-12-09 03:24:30 +00008131StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective(
8132 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00008133 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li7ade93f2016-12-09 03:24:30 +00008134 if (!AStmt)
8135 return StmtError();
8136
Alexey Bataeve3727102018-04-18 15:57:46 +00008137 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li7ade93f2016-12-09 03:24:30 +00008138 // 1.2.2 OpenMP Language Terminology
8139 // Structured block - An executable statement with a single entry at the
8140 // top and a single exit at the bottom.
8141 // The point of exit cannot be a branch out of the structured block.
8142 // longjmp() and throw() must not violate the entry/exit criteria.
8143 CS->getCapturedDecl()->setNothrow();
8144
Carlo Bertolli62fae152017-11-20 20:46:39 +00008145 for (int ThisCaptureLevel =
8146 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for);
8147 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8148 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8149 // 1.2.2 OpenMP Language Terminology
8150 // Structured block - An executable statement with a single entry at the
8151 // top and a single exit at the bottom.
8152 // The point of exit cannot be a branch out of the structured block.
8153 // longjmp() and throw() must not violate the entry/exit criteria.
8154 CS->getCapturedDecl()->setNothrow();
8155 }
8156
Kelvin Li7ade93f2016-12-09 03:24:30 +00008157 OMPLoopDirective::HelperExprs B;
8158 // In presence of clause 'collapse' with number of loops, it will
8159 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00008160 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li7ade93f2016-12-09 03:24:30 +00008161 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
Carlo Bertolli62fae152017-11-20 20:46:39 +00008162 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li7ade93f2016-12-09 03:24:30 +00008163 VarsWithImplicitDSA, B);
8164
8165 if (NestedLoopCount == 0)
8166 return StmtError();
8167
8168 assert((CurContext->isDependentContext() || B.builtAll()) &&
8169 "omp for loop exprs were not built");
8170
Reid Kleckner87a31802018-03-12 21:43:02 +00008171 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00008172
8173 DSAStack->setParentTeamsRegionLoc(StartLoc);
8174
Kelvin Li7ade93f2016-12-09 03:24:30 +00008175 return OMPTeamsDistributeParallelForDirective::Create(
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00008176 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
8177 DSAStack->isCancelRegion());
Kelvin Li7ade93f2016-12-09 03:24:30 +00008178}
8179
Kelvin Libf594a52016-12-17 05:48:59 +00008180StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses,
8181 Stmt *AStmt,
8182 SourceLocation StartLoc,
8183 SourceLocation EndLoc) {
8184 if (!AStmt)
8185 return StmtError();
8186
Alexey Bataeve3727102018-04-18 15:57:46 +00008187 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Libf594a52016-12-17 05:48:59 +00008188 // 1.2.2 OpenMP Language Terminology
8189 // Structured block - An executable statement with a single entry at the
8190 // top and a single exit at the bottom.
8191 // The point of exit cannot be a branch out of the structured block.
8192 // longjmp() and throw() must not violate the entry/exit criteria.
8193 CS->getCapturedDecl()->setNothrow();
8194
Alexey Bataevf9fc42e2017-11-22 14:25:55 +00008195 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams);
8196 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8197 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8198 // 1.2.2 OpenMP Language Terminology
8199 // Structured block - An executable statement with a single entry at the
8200 // top and a single exit at the bottom.
8201 // The point of exit cannot be a branch out of the structured block.
8202 // longjmp() and throw() must not violate the entry/exit criteria.
8203 CS->getCapturedDecl()->setNothrow();
8204 }
Reid Kleckner87a31802018-03-12 21:43:02 +00008205 setFunctionHasBranchProtectedScope();
Kelvin Libf594a52016-12-17 05:48:59 +00008206
8207 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses,
8208 AStmt);
8209}
8210
Kelvin Li83c451e2016-12-25 04:52:54 +00008211StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective(
8212 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00008213 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li83c451e2016-12-25 04:52:54 +00008214 if (!AStmt)
8215 return StmtError();
8216
Alexey Bataeve3727102018-04-18 15:57:46 +00008217 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li83c451e2016-12-25 04:52:54 +00008218 // 1.2.2 OpenMP Language Terminology
8219 // Structured block - An executable statement with a single entry at the
8220 // top and a single exit at the bottom.
8221 // The point of exit cannot be a branch out of the structured block.
8222 // longjmp() and throw() must not violate the entry/exit criteria.
8223 CS->getCapturedDecl()->setNothrow();
Alexey Bataevdfa430f2017-12-08 15:03:50 +00008224 for (int ThisCaptureLevel =
8225 getOpenMPCaptureLevels(OMPD_target_teams_distribute);
8226 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8227 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8228 // 1.2.2 OpenMP Language Terminology
8229 // Structured block - An executable statement with a single entry at the
8230 // top and a single exit at the bottom.
8231 // The point of exit cannot be a branch out of the structured block.
8232 // longjmp() and throw() must not violate the entry/exit criteria.
8233 CS->getCapturedDecl()->setNothrow();
8234 }
Kelvin Li83c451e2016-12-25 04:52:54 +00008235
8236 OMPLoopDirective::HelperExprs B;
8237 // In presence of clause 'collapse' with number of loops, it will
8238 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00008239 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataevdfa430f2017-12-08 15:03:50 +00008240 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses),
8241 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li83c451e2016-12-25 04:52:54 +00008242 VarsWithImplicitDSA, B);
8243 if (NestedLoopCount == 0)
8244 return StmtError();
8245
8246 assert((CurContext->isDependentContext() || B.builtAll()) &&
8247 "omp target teams distribute loop exprs were not built");
8248
Reid Kleckner87a31802018-03-12 21:43:02 +00008249 setFunctionHasBranchProtectedScope();
Kelvin Li83c451e2016-12-25 04:52:54 +00008250 return OMPTargetTeamsDistributeDirective::Create(
8251 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
8252}
8253
Kelvin Li80e8f562016-12-29 22:16:30 +00008254StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective(
8255 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00008256 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li80e8f562016-12-29 22:16:30 +00008257 if (!AStmt)
8258 return StmtError();
8259
Alexey Bataeve3727102018-04-18 15:57:46 +00008260 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li80e8f562016-12-29 22:16:30 +00008261 // 1.2.2 OpenMP Language Terminology
8262 // Structured block - An executable statement with a single entry at the
8263 // top and a single exit at the bottom.
8264 // The point of exit cannot be a branch out of the structured block.
8265 // longjmp() and throw() must not violate the entry/exit criteria.
8266 CS->getCapturedDecl()->setNothrow();
Carlo Bertolli52978c32018-01-03 21:12:44 +00008267 for (int ThisCaptureLevel =
8268 getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for);
8269 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8270 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8271 // 1.2.2 OpenMP Language Terminology
8272 // Structured block - An executable statement with a single entry at the
8273 // top and a single exit at the bottom.
8274 // The point of exit cannot be a branch out of the structured block.
8275 // longjmp() and throw() must not violate the entry/exit criteria.
8276 CS->getCapturedDecl()->setNothrow();
8277 }
8278
Kelvin Li80e8f562016-12-29 22:16:30 +00008279 OMPLoopDirective::HelperExprs B;
8280 // In presence of clause 'collapse' with number of loops, it will
8281 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00008282 unsigned NestedLoopCount = checkOpenMPLoop(
Carlo Bertolli52978c32018-01-03 21:12:44 +00008283 OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
8284 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li80e8f562016-12-29 22:16:30 +00008285 VarsWithImplicitDSA, B);
8286 if (NestedLoopCount == 0)
8287 return StmtError();
8288
8289 assert((CurContext->isDependentContext() || B.builtAll()) &&
8290 "omp target teams distribute parallel for loop exprs were not built");
8291
Alexey Bataev647dd842018-01-15 20:59:40 +00008292 if (!CurContext->isDependentContext()) {
8293 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00008294 for (OMPClause *C : Clauses) {
Alexey Bataev647dd842018-01-15 20:59:40 +00008295 if (auto *LC = dyn_cast<OMPLinearClause>(C))
8296 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8297 B.NumIterations, *this, CurScope,
8298 DSAStack))
8299 return StmtError();
8300 }
8301 }
8302
Reid Kleckner87a31802018-03-12 21:43:02 +00008303 setFunctionHasBranchProtectedScope();
Kelvin Li80e8f562016-12-29 22:16:30 +00008304 return OMPTargetTeamsDistributeParallelForDirective::Create(
Alexey Bataev16e79882017-11-22 21:12:03 +00008305 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
8306 DSAStack->isCancelRegion());
Kelvin Li80e8f562016-12-29 22:16:30 +00008307}
8308
Kelvin Li1851df52017-01-03 05:23:48 +00008309StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
8310 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00008311 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li1851df52017-01-03 05:23:48 +00008312 if (!AStmt)
8313 return StmtError();
8314
Alexey Bataeve3727102018-04-18 15:57:46 +00008315 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li1851df52017-01-03 05:23:48 +00008316 // 1.2.2 OpenMP Language Terminology
8317 // Structured block - An executable statement with a single entry at the
8318 // top and a single exit at the bottom.
8319 // The point of exit cannot be a branch out of the structured block.
8320 // longjmp() and throw() must not violate the entry/exit criteria.
8321 CS->getCapturedDecl()->setNothrow();
Alexey Bataev647dd842018-01-15 20:59:40 +00008322 for (int ThisCaptureLevel = getOpenMPCaptureLevels(
8323 OMPD_target_teams_distribute_parallel_for_simd);
8324 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8325 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8326 // 1.2.2 OpenMP Language Terminology
8327 // Structured block - An executable statement with a single entry at the
8328 // top and a single exit at the bottom.
8329 // The point of exit cannot be a branch out of the structured block.
8330 // longjmp() and throw() must not violate the entry/exit criteria.
8331 CS->getCapturedDecl()->setNothrow();
8332 }
Kelvin Li1851df52017-01-03 05:23:48 +00008333
8334 OMPLoopDirective::HelperExprs B;
8335 // In presence of clause 'collapse' with number of loops, it will
8336 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00008337 unsigned NestedLoopCount =
8338 checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd,
Alexey Bataev647dd842018-01-15 20:59:40 +00008339 getCollapseNumberExpr(Clauses),
8340 nullptr /*ordered not a clause on distribute*/, CS, *this,
8341 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li1851df52017-01-03 05:23:48 +00008342 if (NestedLoopCount == 0)
8343 return StmtError();
8344
8345 assert((CurContext->isDependentContext() || B.builtAll()) &&
8346 "omp target teams distribute parallel for simd loop exprs were not "
8347 "built");
8348
8349 if (!CurContext->isDependentContext()) {
8350 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00008351 for (OMPClause *C : Clauses) {
Kelvin Li1851df52017-01-03 05:23:48 +00008352 if (auto *LC = dyn_cast<OMPLinearClause>(C))
8353 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8354 B.NumIterations, *this, CurScope,
8355 DSAStack))
8356 return StmtError();
8357 }
8358 }
8359
Alexey Bataev438388c2017-11-22 18:34:02 +00008360 if (checkSimdlenSafelenSpecified(*this, Clauses))
8361 return StmtError();
8362
Reid Kleckner87a31802018-03-12 21:43:02 +00008363 setFunctionHasBranchProtectedScope();
Kelvin Li1851df52017-01-03 05:23:48 +00008364 return OMPTargetTeamsDistributeParallelForSimdDirective::Create(
8365 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
8366}
8367
Kelvin Lida681182017-01-10 18:08:18 +00008368StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective(
8369 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00008370 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Lida681182017-01-10 18:08:18 +00008371 if (!AStmt)
8372 return StmtError();
8373
8374 auto *CS = cast<CapturedStmt>(AStmt);
8375 // 1.2.2 OpenMP Language Terminology
8376 // Structured block - An executable statement with a single entry at the
8377 // top and a single exit at the bottom.
8378 // The point of exit cannot be a branch out of the structured block.
8379 // longjmp() and throw() must not violate the entry/exit criteria.
8380 CS->getCapturedDecl()->setNothrow();
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00008381 for (int ThisCaptureLevel =
8382 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd);
8383 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8384 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8385 // 1.2.2 OpenMP Language Terminology
8386 // Structured block - An executable statement with a single entry at the
8387 // top and a single exit at the bottom.
8388 // The point of exit cannot be a branch out of the structured block.
8389 // longjmp() and throw() must not violate the entry/exit criteria.
8390 CS->getCapturedDecl()->setNothrow();
8391 }
Kelvin Lida681182017-01-10 18:08:18 +00008392
8393 OMPLoopDirective::HelperExprs B;
8394 // In presence of clause 'collapse' with number of loops, it will
8395 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00008396 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Lida681182017-01-10 18:08:18 +00008397 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00008398 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Lida681182017-01-10 18:08:18 +00008399 VarsWithImplicitDSA, B);
8400 if (NestedLoopCount == 0)
8401 return StmtError();
8402
8403 assert((CurContext->isDependentContext() || B.builtAll()) &&
8404 "omp target teams distribute simd loop exprs were not built");
8405
Alexey Bataev438388c2017-11-22 18:34:02 +00008406 if (!CurContext->isDependentContext()) {
8407 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00008408 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00008409 if (auto *LC = dyn_cast<OMPLinearClause>(C))
8410 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8411 B.NumIterations, *this, CurScope,
8412 DSAStack))
8413 return StmtError();
8414 }
8415 }
8416
8417 if (checkSimdlenSafelenSpecified(*this, Clauses))
8418 return StmtError();
8419
Reid Kleckner87a31802018-03-12 21:43:02 +00008420 setFunctionHasBranchProtectedScope();
Kelvin Lida681182017-01-10 18:08:18 +00008421 return OMPTargetTeamsDistributeSimdDirective::Create(
8422 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
8423}
8424
Alexey Bataeved09d242014-05-28 05:53:51 +00008425OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008426 SourceLocation StartLoc,
8427 SourceLocation LParenLoc,
8428 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008429 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008430 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00008431 case OMPC_final:
8432 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
8433 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00008434 case OMPC_num_threads:
8435 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
8436 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00008437 case OMPC_safelen:
8438 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
8439 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00008440 case OMPC_simdlen:
8441 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
8442 break;
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00008443 case OMPC_allocator:
8444 Res = ActOnOpenMPAllocatorClause(Expr, StartLoc, LParenLoc, EndLoc);
8445 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00008446 case OMPC_collapse:
8447 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
8448 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00008449 case OMPC_ordered:
8450 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
8451 break;
Michael Wonge710d542015-08-07 16:16:36 +00008452 case OMPC_device:
8453 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
8454 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00008455 case OMPC_num_teams:
8456 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
8457 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008458 case OMPC_thread_limit:
8459 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
8460 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00008461 case OMPC_priority:
8462 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
8463 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008464 case OMPC_grainsize:
8465 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
8466 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00008467 case OMPC_num_tasks:
8468 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
8469 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00008470 case OMPC_hint:
8471 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
8472 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008473 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008474 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008475 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008476 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008477 case OMPC_private:
8478 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00008479 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008480 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00008481 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008482 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008483 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00008484 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008485 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008486 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008487 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00008488 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008489 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008490 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008491 case OMPC_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +00008492 case OMPC_allocate:
Alexey Bataev6125da92014-07-21 11:26:11 +00008493 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008494 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008495 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008496 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008497 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008498 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008499 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00008500 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008501 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008502 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00008503 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008504 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008505 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008506 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008507 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00008508 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00008509 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00008510 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00008511 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00008512 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00008513 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00008514 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00008515 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00008516 case OMPC_atomic_default_mem_order:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008517 llvm_unreachable("Clause is not allowed.");
8518 }
8519 return Res;
8520}
8521
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008522// An OpenMP directive such as 'target parallel' has two captured regions:
8523// for the 'target' and 'parallel' respectively. This function returns
8524// the region in which to capture expressions associated with a clause.
8525// A return value of OMPD_unknown signifies that the expression should not
8526// be captured.
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008527static OpenMPDirectiveKind getOpenMPCaptureRegionForClause(
8528 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
8529 OpenMPDirectiveKind NameModifier = OMPD_unknown) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008530 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008531 switch (CKind) {
8532 case OMPC_if:
8533 switch (DKind) {
8534 case OMPD_target_parallel:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008535 case OMPD_target_parallel_for:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00008536 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008537 // If this clause applies to the nested 'parallel' region, capture within
8538 // the 'target' region, otherwise do not capture.
8539 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
8540 CaptureRegion = OMPD_target;
8541 break;
Carlo Bertolli52978c32018-01-03 21:12:44 +00008542 case OMPD_target_teams_distribute_parallel_for:
8543 case OMPD_target_teams_distribute_parallel_for_simd:
8544 // If this clause applies to the nested 'parallel' region, capture within
8545 // the 'teams' region, otherwise do not capture.
8546 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
8547 CaptureRegion = OMPD_teams;
8548 break;
Carlo Bertolli62fae152017-11-20 20:46:39 +00008549 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008550 case OMPD_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008551 CaptureRegion = OMPD_teams;
8552 break;
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008553 case OMPD_target_update:
Alexey Bataevfab20e42017-12-27 18:49:38 +00008554 case OMPD_target_enter_data:
8555 case OMPD_target_exit_data:
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008556 CaptureRegion = OMPD_task;
8557 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008558 case OMPD_cancel:
8559 case OMPD_parallel:
8560 case OMPD_parallel_sections:
8561 case OMPD_parallel_for:
8562 case OMPD_parallel_for_simd:
8563 case OMPD_target:
8564 case OMPD_target_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008565 case OMPD_target_teams:
8566 case OMPD_target_teams_distribute:
8567 case OMPD_target_teams_distribute_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008568 case OMPD_distribute_parallel_for:
8569 case OMPD_distribute_parallel_for_simd:
8570 case OMPD_task:
8571 case OMPD_taskloop:
8572 case OMPD_taskloop_simd:
8573 case OMPD_target_data:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008574 // Do not capture if-clause expressions.
8575 break;
8576 case OMPD_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +00008577 case OMPD_allocate:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008578 case OMPD_taskyield:
8579 case OMPD_barrier:
8580 case OMPD_taskwait:
8581 case OMPD_cancellation_point:
8582 case OMPD_flush:
8583 case OMPD_declare_reduction:
Michael Kruse251e1482019-02-01 20:25:04 +00008584 case OMPD_declare_mapper:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008585 case OMPD_declare_simd:
8586 case OMPD_declare_target:
8587 case OMPD_end_declare_target:
8588 case OMPD_teams:
8589 case OMPD_simd:
8590 case OMPD_for:
8591 case OMPD_for_simd:
8592 case OMPD_sections:
8593 case OMPD_section:
8594 case OMPD_single:
8595 case OMPD_master:
8596 case OMPD_critical:
8597 case OMPD_taskgroup:
8598 case OMPD_distribute:
8599 case OMPD_ordered:
8600 case OMPD_atomic:
8601 case OMPD_distribute_simd:
8602 case OMPD_teams_distribute:
8603 case OMPD_teams_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008604 case OMPD_requires:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008605 llvm_unreachable("Unexpected OpenMP directive with if-clause");
8606 case OMPD_unknown:
8607 llvm_unreachable("Unknown OpenMP directive");
8608 }
8609 break;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008610 case OMPC_num_threads:
8611 switch (DKind) {
8612 case OMPD_target_parallel:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008613 case OMPD_target_parallel_for:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00008614 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008615 CaptureRegion = OMPD_target;
8616 break;
Carlo Bertolli62fae152017-11-20 20:46:39 +00008617 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008618 case OMPD_teams_distribute_parallel_for_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008619 case OMPD_target_teams_distribute_parallel_for:
8620 case OMPD_target_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008621 CaptureRegion = OMPD_teams;
8622 break;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008623 case OMPD_parallel:
8624 case OMPD_parallel_sections:
8625 case OMPD_parallel_for:
8626 case OMPD_parallel_for_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008627 case OMPD_distribute_parallel_for:
8628 case OMPD_distribute_parallel_for_simd:
8629 // Do not capture num_threads-clause expressions.
8630 break;
8631 case OMPD_target_data:
8632 case OMPD_target_enter_data:
8633 case OMPD_target_exit_data:
8634 case OMPD_target_update:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008635 case OMPD_target:
8636 case OMPD_target_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008637 case OMPD_target_teams:
8638 case OMPD_target_teams_distribute:
8639 case OMPD_target_teams_distribute_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008640 case OMPD_cancel:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008641 case OMPD_task:
8642 case OMPD_taskloop:
8643 case OMPD_taskloop_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008644 case OMPD_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +00008645 case OMPD_allocate:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008646 case OMPD_taskyield:
8647 case OMPD_barrier:
8648 case OMPD_taskwait:
8649 case OMPD_cancellation_point:
8650 case OMPD_flush:
8651 case OMPD_declare_reduction:
Michael Kruse251e1482019-02-01 20:25:04 +00008652 case OMPD_declare_mapper:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008653 case OMPD_declare_simd:
8654 case OMPD_declare_target:
8655 case OMPD_end_declare_target:
8656 case OMPD_teams:
8657 case OMPD_simd:
8658 case OMPD_for:
8659 case OMPD_for_simd:
8660 case OMPD_sections:
8661 case OMPD_section:
8662 case OMPD_single:
8663 case OMPD_master:
8664 case OMPD_critical:
8665 case OMPD_taskgroup:
8666 case OMPD_distribute:
8667 case OMPD_ordered:
8668 case OMPD_atomic:
8669 case OMPD_distribute_simd:
8670 case OMPD_teams_distribute:
8671 case OMPD_teams_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008672 case OMPD_requires:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008673 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause");
8674 case OMPD_unknown:
8675 llvm_unreachable("Unknown OpenMP directive");
8676 }
8677 break;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008678 case OMPC_num_teams:
8679 switch (DKind) {
8680 case OMPD_target_teams:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008681 case OMPD_target_teams_distribute:
8682 case OMPD_target_teams_distribute_simd:
8683 case OMPD_target_teams_distribute_parallel_for:
8684 case OMPD_target_teams_distribute_parallel_for_simd:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008685 CaptureRegion = OMPD_target;
8686 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008687 case OMPD_teams_distribute_parallel_for:
8688 case OMPD_teams_distribute_parallel_for_simd:
8689 case OMPD_teams:
8690 case OMPD_teams_distribute:
8691 case OMPD_teams_distribute_simd:
8692 // Do not capture num_teams-clause expressions.
8693 break;
8694 case OMPD_distribute_parallel_for:
8695 case OMPD_distribute_parallel_for_simd:
8696 case OMPD_task:
8697 case OMPD_taskloop:
8698 case OMPD_taskloop_simd:
8699 case OMPD_target_data:
8700 case OMPD_target_enter_data:
8701 case OMPD_target_exit_data:
8702 case OMPD_target_update:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008703 case OMPD_cancel:
8704 case OMPD_parallel:
8705 case OMPD_parallel_sections:
8706 case OMPD_parallel_for:
8707 case OMPD_parallel_for_simd:
8708 case OMPD_target:
8709 case OMPD_target_simd:
8710 case OMPD_target_parallel:
8711 case OMPD_target_parallel_for:
8712 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008713 case OMPD_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +00008714 case OMPD_allocate:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008715 case OMPD_taskyield:
8716 case OMPD_barrier:
8717 case OMPD_taskwait:
8718 case OMPD_cancellation_point:
8719 case OMPD_flush:
8720 case OMPD_declare_reduction:
Michael Kruse251e1482019-02-01 20:25:04 +00008721 case OMPD_declare_mapper:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008722 case OMPD_declare_simd:
8723 case OMPD_declare_target:
8724 case OMPD_end_declare_target:
8725 case OMPD_simd:
8726 case OMPD_for:
8727 case OMPD_for_simd:
8728 case OMPD_sections:
8729 case OMPD_section:
8730 case OMPD_single:
8731 case OMPD_master:
8732 case OMPD_critical:
8733 case OMPD_taskgroup:
8734 case OMPD_distribute:
8735 case OMPD_ordered:
8736 case OMPD_atomic:
8737 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008738 case OMPD_requires:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008739 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
8740 case OMPD_unknown:
8741 llvm_unreachable("Unknown OpenMP directive");
8742 }
8743 break;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008744 case OMPC_thread_limit:
8745 switch (DKind) {
8746 case OMPD_target_teams:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008747 case OMPD_target_teams_distribute:
8748 case OMPD_target_teams_distribute_simd:
8749 case OMPD_target_teams_distribute_parallel_for:
8750 case OMPD_target_teams_distribute_parallel_for_simd:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008751 CaptureRegion = OMPD_target;
8752 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008753 case OMPD_teams_distribute_parallel_for:
8754 case OMPD_teams_distribute_parallel_for_simd:
8755 case OMPD_teams:
8756 case OMPD_teams_distribute:
8757 case OMPD_teams_distribute_simd:
8758 // Do not capture thread_limit-clause expressions.
8759 break;
8760 case OMPD_distribute_parallel_for:
8761 case OMPD_distribute_parallel_for_simd:
8762 case OMPD_task:
8763 case OMPD_taskloop:
8764 case OMPD_taskloop_simd:
8765 case OMPD_target_data:
8766 case OMPD_target_enter_data:
8767 case OMPD_target_exit_data:
8768 case OMPD_target_update:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008769 case OMPD_cancel:
8770 case OMPD_parallel:
8771 case OMPD_parallel_sections:
8772 case OMPD_parallel_for:
8773 case OMPD_parallel_for_simd:
8774 case OMPD_target:
8775 case OMPD_target_simd:
8776 case OMPD_target_parallel:
8777 case OMPD_target_parallel_for:
8778 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008779 case OMPD_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +00008780 case OMPD_allocate:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008781 case OMPD_taskyield:
8782 case OMPD_barrier:
8783 case OMPD_taskwait:
8784 case OMPD_cancellation_point:
8785 case OMPD_flush:
8786 case OMPD_declare_reduction:
Michael Kruse251e1482019-02-01 20:25:04 +00008787 case OMPD_declare_mapper:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008788 case OMPD_declare_simd:
8789 case OMPD_declare_target:
8790 case OMPD_end_declare_target:
8791 case OMPD_simd:
8792 case OMPD_for:
8793 case OMPD_for_simd:
8794 case OMPD_sections:
8795 case OMPD_section:
8796 case OMPD_single:
8797 case OMPD_master:
8798 case OMPD_critical:
8799 case OMPD_taskgroup:
8800 case OMPD_distribute:
8801 case OMPD_ordered:
8802 case OMPD_atomic:
8803 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008804 case OMPD_requires:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008805 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause");
8806 case OMPD_unknown:
8807 llvm_unreachable("Unknown OpenMP directive");
8808 }
8809 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008810 case OMPC_schedule:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008811 switch (DKind) {
Alexey Bataev2ba67042017-11-28 21:11:44 +00008812 case OMPD_parallel_for:
8813 case OMPD_parallel_for_simd:
Alexey Bataev7f96c372017-11-22 17:19:31 +00008814 case OMPD_distribute_parallel_for:
Alexey Bataev974acd62017-11-27 19:38:52 +00008815 case OMPD_distribute_parallel_for_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008816 case OMPD_teams_distribute_parallel_for:
8817 case OMPD_teams_distribute_parallel_for_simd:
8818 case OMPD_target_parallel_for:
8819 case OMPD_target_parallel_for_simd:
8820 case OMPD_target_teams_distribute_parallel_for:
8821 case OMPD_target_teams_distribute_parallel_for_simd:
Alexey Bataev7f96c372017-11-22 17:19:31 +00008822 CaptureRegion = OMPD_parallel;
8823 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008824 case OMPD_for:
8825 case OMPD_for_simd:
8826 // Do not capture schedule-clause expressions.
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008827 break;
8828 case OMPD_task:
8829 case OMPD_taskloop:
8830 case OMPD_taskloop_simd:
8831 case OMPD_target_data:
8832 case OMPD_target_enter_data:
8833 case OMPD_target_exit_data:
8834 case OMPD_target_update:
8835 case OMPD_teams:
8836 case OMPD_teams_distribute:
8837 case OMPD_teams_distribute_simd:
8838 case OMPD_target_teams_distribute:
8839 case OMPD_target_teams_distribute_simd:
8840 case OMPD_target:
8841 case OMPD_target_simd:
8842 case OMPD_target_parallel:
8843 case OMPD_cancel:
8844 case OMPD_parallel:
8845 case OMPD_parallel_sections:
8846 case OMPD_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +00008847 case OMPD_allocate:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008848 case OMPD_taskyield:
8849 case OMPD_barrier:
8850 case OMPD_taskwait:
8851 case OMPD_cancellation_point:
8852 case OMPD_flush:
8853 case OMPD_declare_reduction:
Michael Kruse251e1482019-02-01 20:25:04 +00008854 case OMPD_declare_mapper:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008855 case OMPD_declare_simd:
8856 case OMPD_declare_target:
8857 case OMPD_end_declare_target:
8858 case OMPD_simd:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008859 case OMPD_sections:
8860 case OMPD_section:
8861 case OMPD_single:
8862 case OMPD_master:
8863 case OMPD_critical:
8864 case OMPD_taskgroup:
8865 case OMPD_distribute:
8866 case OMPD_ordered:
8867 case OMPD_atomic:
8868 case OMPD_distribute_simd:
8869 case OMPD_target_teams:
Kelvin Li1408f912018-09-26 04:28:39 +00008870 case OMPD_requires:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008871 llvm_unreachable("Unexpected OpenMP directive with schedule clause");
8872 case OMPD_unknown:
8873 llvm_unreachable("Unknown OpenMP directive");
8874 }
8875 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008876 case OMPC_dist_schedule:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008877 switch (DKind) {
8878 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008879 case OMPD_teams_distribute_parallel_for_simd:
8880 case OMPD_teams_distribute:
8881 case OMPD_teams_distribute_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008882 case OMPD_target_teams_distribute_parallel_for:
8883 case OMPD_target_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008884 case OMPD_target_teams_distribute:
8885 case OMPD_target_teams_distribute_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008886 CaptureRegion = OMPD_teams;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008887 break;
8888 case OMPD_distribute_parallel_for:
8889 case OMPD_distribute_parallel_for_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008890 case OMPD_distribute:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008891 case OMPD_distribute_simd:
8892 // Do not capture thread_limit-clause expressions.
8893 break;
8894 case OMPD_parallel_for:
8895 case OMPD_parallel_for_simd:
8896 case OMPD_target_parallel_for_simd:
8897 case OMPD_target_parallel_for:
8898 case OMPD_task:
8899 case OMPD_taskloop:
8900 case OMPD_taskloop_simd:
8901 case OMPD_target_data:
8902 case OMPD_target_enter_data:
8903 case OMPD_target_exit_data:
8904 case OMPD_target_update:
8905 case OMPD_teams:
8906 case OMPD_target:
8907 case OMPD_target_simd:
8908 case OMPD_target_parallel:
8909 case OMPD_cancel:
8910 case OMPD_parallel:
8911 case OMPD_parallel_sections:
8912 case OMPD_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +00008913 case OMPD_allocate:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008914 case OMPD_taskyield:
8915 case OMPD_barrier:
8916 case OMPD_taskwait:
8917 case OMPD_cancellation_point:
8918 case OMPD_flush:
8919 case OMPD_declare_reduction:
Michael Kruse251e1482019-02-01 20:25:04 +00008920 case OMPD_declare_mapper:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008921 case OMPD_declare_simd:
8922 case OMPD_declare_target:
8923 case OMPD_end_declare_target:
8924 case OMPD_simd:
8925 case OMPD_for:
8926 case OMPD_for_simd:
8927 case OMPD_sections:
8928 case OMPD_section:
8929 case OMPD_single:
8930 case OMPD_master:
8931 case OMPD_critical:
8932 case OMPD_taskgroup:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008933 case OMPD_ordered:
8934 case OMPD_atomic:
8935 case OMPD_target_teams:
Kelvin Li1408f912018-09-26 04:28:39 +00008936 case OMPD_requires:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008937 llvm_unreachable("Unexpected OpenMP directive with schedule clause");
8938 case OMPD_unknown:
8939 llvm_unreachable("Unknown OpenMP directive");
8940 }
8941 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008942 case OMPC_device:
8943 switch (DKind) {
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008944 case OMPD_target_update:
Alexey Bataevfab20e42017-12-27 18:49:38 +00008945 case OMPD_target_enter_data:
8946 case OMPD_target_exit_data:
Alexey Bataev8451efa2018-01-15 19:06:12 +00008947 case OMPD_target:
Alexey Bataevf41c88f2018-01-16 15:05:16 +00008948 case OMPD_target_simd:
Alexey Bataev0c869ef2018-01-16 15:57:07 +00008949 case OMPD_target_teams:
Alexey Bataev54d5c7d2018-01-16 16:27:49 +00008950 case OMPD_target_parallel:
Alexey Bataev79df7562018-01-16 16:46:46 +00008951 case OMPD_target_teams_distribute:
Alexey Bataev8d16a432018-01-16 17:22:50 +00008952 case OMPD_target_teams_distribute_simd:
Alexey Bataev8ed895512018-01-16 17:41:04 +00008953 case OMPD_target_parallel_for:
Alexey Bataevd60d1ba2018-01-16 17:55:15 +00008954 case OMPD_target_parallel_for_simd:
Alexey Bataev9f9fb0b2018-01-16 19:02:33 +00008955 case OMPD_target_teams_distribute_parallel_for:
Alexey Bataev9350fc32018-01-16 19:18:24 +00008956 case OMPD_target_teams_distribute_parallel_for_simd:
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008957 CaptureRegion = OMPD_task;
8958 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008959 case OMPD_target_data:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008960 // Do not capture device-clause expressions.
8961 break;
8962 case OMPD_teams_distribute_parallel_for:
8963 case OMPD_teams_distribute_parallel_for_simd:
8964 case OMPD_teams:
8965 case OMPD_teams_distribute:
8966 case OMPD_teams_distribute_simd:
8967 case OMPD_distribute_parallel_for:
8968 case OMPD_distribute_parallel_for_simd:
8969 case OMPD_task:
8970 case OMPD_taskloop:
8971 case OMPD_taskloop_simd:
8972 case OMPD_cancel:
8973 case OMPD_parallel:
8974 case OMPD_parallel_sections:
8975 case OMPD_parallel_for:
8976 case OMPD_parallel_for_simd:
8977 case OMPD_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +00008978 case OMPD_allocate:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008979 case OMPD_taskyield:
8980 case OMPD_barrier:
8981 case OMPD_taskwait:
8982 case OMPD_cancellation_point:
8983 case OMPD_flush:
8984 case OMPD_declare_reduction:
Michael Kruse251e1482019-02-01 20:25:04 +00008985 case OMPD_declare_mapper:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008986 case OMPD_declare_simd:
8987 case OMPD_declare_target:
8988 case OMPD_end_declare_target:
8989 case OMPD_simd:
8990 case OMPD_for:
8991 case OMPD_for_simd:
8992 case OMPD_sections:
8993 case OMPD_section:
8994 case OMPD_single:
8995 case OMPD_master:
8996 case OMPD_critical:
8997 case OMPD_taskgroup:
8998 case OMPD_distribute:
8999 case OMPD_ordered:
9000 case OMPD_atomic:
9001 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00009002 case OMPD_requires:
Alexey Bataev2ba67042017-11-28 21:11:44 +00009003 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
9004 case OMPD_unknown:
9005 llvm_unreachable("Unknown OpenMP directive");
9006 }
9007 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00009008 case OMPC_firstprivate:
9009 case OMPC_lastprivate:
9010 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00009011 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00009012 case OMPC_in_reduction:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00009013 case OMPC_linear:
9014 case OMPC_default:
9015 case OMPC_proc_bind:
9016 case OMPC_final:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00009017 case OMPC_safelen:
9018 case OMPC_simdlen:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00009019 case OMPC_allocator:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00009020 case OMPC_collapse:
9021 case OMPC_private:
9022 case OMPC_shared:
9023 case OMPC_aligned:
9024 case OMPC_copyin:
9025 case OMPC_copyprivate:
9026 case OMPC_ordered:
9027 case OMPC_nowait:
9028 case OMPC_untied:
9029 case OMPC_mergeable:
9030 case OMPC_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +00009031 case OMPC_allocate:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00009032 case OMPC_flush:
9033 case OMPC_read:
9034 case OMPC_write:
9035 case OMPC_update:
9036 case OMPC_capture:
9037 case OMPC_seq_cst:
9038 case OMPC_depend:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00009039 case OMPC_threads:
9040 case OMPC_simd:
9041 case OMPC_map:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00009042 case OMPC_priority:
9043 case OMPC_grainsize:
9044 case OMPC_nogroup:
9045 case OMPC_num_tasks:
9046 case OMPC_hint:
9047 case OMPC_defaultmap:
9048 case OMPC_unknown:
9049 case OMPC_uniform:
9050 case OMPC_to:
9051 case OMPC_from:
9052 case OMPC_use_device_ptr:
9053 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00009054 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00009055 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009056 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009057 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009058 case OMPC_atomic_default_mem_order:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00009059 llvm_unreachable("Unexpected OpenMP clause.");
9060 }
9061 return CaptureRegion;
9062}
9063
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009064OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
9065 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009066 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009067 SourceLocation NameModifierLoc,
9068 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009069 SourceLocation EndLoc) {
9070 Expr *ValExpr = Condition;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00009071 Stmt *HelperValStmt = nullptr;
9072 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009073 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
9074 !Condition->isInstantiationDependent() &&
9075 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00009076 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009077 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009078 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009079
Alexey Bataev8e769ee2017-12-22 21:01:52 +00009080 ValExpr = Val.get();
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00009081
9082 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
9083 CaptureRegion =
9084 getOpenMPCaptureRegionForClause(DKind, OMPC_if, NameModifier);
Alexey Bataev2ba67042017-11-28 21:11:44 +00009085 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00009086 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00009087 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00009088 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
9089 HelperValStmt = buildPreInits(Context, Captures);
9090 }
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009091 }
9092
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00009093 return new (Context)
9094 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
9095 LParenLoc, NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009096}
9097
Alexey Bataev3778b602014-07-17 07:32:53 +00009098OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
9099 SourceLocation StartLoc,
9100 SourceLocation LParenLoc,
9101 SourceLocation EndLoc) {
9102 Expr *ValExpr = Condition;
9103 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
9104 !Condition->isInstantiationDependent() &&
9105 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00009106 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataev3778b602014-07-17 07:32:53 +00009107 if (Val.isInvalid())
9108 return nullptr;
9109
Richard Smith03a4aa32016-06-23 19:02:52 +00009110 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataev3778b602014-07-17 07:32:53 +00009111 }
9112
9113 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9114}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00009115ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
9116 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00009117 if (!Op)
9118 return ExprError();
9119
9120 class IntConvertDiagnoser : public ICEConvertDiagnoser {
9121 public:
9122 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00009123 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00009124 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
9125 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00009126 return S.Diag(Loc, diag::err_omp_not_integral) << T;
9127 }
Alexey Bataeved09d242014-05-28 05:53:51 +00009128 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
9129 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00009130 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
9131 }
Alexey Bataeved09d242014-05-28 05:53:51 +00009132 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
9133 QualType T,
9134 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00009135 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
9136 }
Alexey Bataeved09d242014-05-28 05:53:51 +00009137 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
9138 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00009139 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00009140 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00009141 }
Alexey Bataeved09d242014-05-28 05:53:51 +00009142 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
9143 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00009144 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
9145 }
Alexey Bataeved09d242014-05-28 05:53:51 +00009146 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
9147 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00009148 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00009149 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00009150 }
Alexey Bataeved09d242014-05-28 05:53:51 +00009151 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
9152 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00009153 llvm_unreachable("conversion functions are permitted");
9154 }
9155 } ConvertDiagnoser;
9156 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
9157}
9158
Alexey Bataeve3727102018-04-18 15:57:46 +00009159static bool isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00009160 OpenMPClauseKind CKind,
9161 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009162 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
9163 !ValExpr->isInstantiationDependent()) {
9164 SourceLocation Loc = ValExpr->getExprLoc();
9165 ExprResult Value =
9166 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
9167 if (Value.isInvalid())
9168 return false;
9169
9170 ValExpr = Value.get();
9171 // The expression must evaluate to a non-negative integer value.
9172 llvm::APSInt Result;
9173 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00009174 Result.isSigned() &&
9175 !((!StrictlyPositive && Result.isNonNegative()) ||
9176 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009177 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00009178 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
9179 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009180 return false;
9181 }
9182 }
9183 return true;
9184}
9185
Alexey Bataev568a8332014-03-06 06:15:19 +00009186OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
9187 SourceLocation StartLoc,
9188 SourceLocation LParenLoc,
9189 SourceLocation EndLoc) {
9190 Expr *ValExpr = NumThreads;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00009191 Stmt *HelperValStmt = nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00009192
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009193 // OpenMP [2.5, Restrictions]
9194 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +00009195 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
Alexey Bataeva0569352015-12-01 10:17:31 +00009196 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009197 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00009198
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00009199 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +00009200 OpenMPDirectiveKind CaptureRegion =
9201 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads);
9202 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00009203 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00009204 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00009205 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
9206 HelperValStmt = buildPreInits(Context, Captures);
9207 }
9208
9209 return new (Context) OMPNumThreadsClause(
9210 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00009211}
9212
Alexey Bataev62c87d22014-03-21 04:51:18 +00009213ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009214 OpenMPClauseKind CKind,
9215 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00009216 if (!E)
9217 return ExprError();
9218 if (E->isValueDependent() || E->isTypeDependent() ||
9219 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00009220 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00009221 llvm::APSInt Result;
9222 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
9223 if (ICE.isInvalid())
9224 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009225 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
9226 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00009227 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009228 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
9229 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00009230 return ExprError();
9231 }
Alexander Musman09184fe2014-09-30 05:29:28 +00009232 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
9233 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
9234 << E->getSourceRange();
9235 return ExprError();
9236 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009237 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
9238 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00009239 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009240 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00009241 return ICE;
9242}
9243
9244OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
9245 SourceLocation LParenLoc,
9246 SourceLocation EndLoc) {
9247 // OpenMP [2.8.1, simd construct, Description]
9248 // The parameter of the safelen clause must be a constant
9249 // positive integer expression.
9250 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
9251 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009252 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00009253 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00009254 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00009255}
9256
Alexey Bataev66b15b52015-08-21 11:14:16 +00009257OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
9258 SourceLocation LParenLoc,
9259 SourceLocation EndLoc) {
9260 // OpenMP [2.8.1, simd construct, Description]
9261 // The parameter of the simdlen clause must be a constant
9262 // positive integer expression.
9263 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
9264 if (Simdlen.isInvalid())
9265 return nullptr;
9266 return new (Context)
9267 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
9268}
9269
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00009270/// Tries to find omp_allocator_handle_t type.
9271static bool FindOMPAllocatorHandleT(Sema &S, SourceLocation Loc,
9272 QualType &OMPAllocatorHandleT) {
9273 if (!OMPAllocatorHandleT.isNull())
9274 return true;
9275 DeclarationName OMPAllocatorHandleTName =
9276 &S.getASTContext().Idents.get("omp_allocator_handle_t");
9277 auto *TD = dyn_cast_or_null<TypeDecl>(S.LookupSingleName(
9278 S.TUScope, OMPAllocatorHandleTName, Loc, Sema::LookupAnyName));
9279 if (!TD) {
9280 S.Diag(Loc, diag::err_implied_omp_allocator_handle_t_not_found);
9281 return false;
9282 }
9283 OMPAllocatorHandleT = S.getASTContext().getTypeDeclType(TD);
9284 return true;
9285}
9286
9287OMPClause *Sema::ActOnOpenMPAllocatorClause(Expr *A, SourceLocation StartLoc,
9288 SourceLocation LParenLoc,
9289 SourceLocation EndLoc) {
9290 // OpenMP [2.11.3, allocate Directive, Description]
9291 // allocator is an expression of omp_allocator_handle_t type.
9292 if (!FindOMPAllocatorHandleT(*this, A->getExprLoc(), OMPAllocatorHandleT))
9293 return nullptr;
9294
9295 ExprResult Allocator = DefaultLvalueConversion(A);
9296 if (Allocator.isInvalid())
9297 return nullptr;
9298 Allocator = PerformImplicitConversion(Allocator.get(), OMPAllocatorHandleT,
9299 Sema::AA_Initializing,
9300 /*AllowExplicit=*/true);
9301 if (Allocator.isInvalid())
9302 return nullptr;
9303 return new (Context)
9304 OMPAllocatorClause(Allocator.get(), StartLoc, LParenLoc, EndLoc);
9305}
9306
Alexander Musman64d33f12014-06-04 07:53:32 +00009307OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
9308 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00009309 SourceLocation LParenLoc,
9310 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00009311 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00009312 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00009313 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00009314 // The parameter of the collapse clause must be a constant
9315 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00009316 ExprResult NumForLoopsResult =
9317 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
9318 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00009319 return nullptr;
9320 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00009321 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00009322}
9323
Alexey Bataev10e775f2015-07-30 11:36:16 +00009324OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
9325 SourceLocation EndLoc,
9326 SourceLocation LParenLoc,
9327 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00009328 // OpenMP [2.7.1, loop construct, Description]
9329 // OpenMP [2.8.1, simd construct, Description]
9330 // OpenMP [2.9.6, distribute construct, Description]
9331 // The parameter of the ordered clause must be a constant
9332 // positive integer expression if any.
9333 if (NumForLoops && LParenLoc.isValid()) {
9334 ExprResult NumForLoopsResult =
9335 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
9336 if (NumForLoopsResult.isInvalid())
9337 return nullptr;
9338 NumForLoops = NumForLoopsResult.get();
Alexey Bataeve3727102018-04-18 15:57:46 +00009339 } else {
Alexey Bataev346265e2015-09-25 10:37:12 +00009340 NumForLoops = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +00009341 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00009342 auto *Clause = OMPOrderedClause::Create(
9343 Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0,
9344 StartLoc, LParenLoc, EndLoc);
9345 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause);
9346 return Clause;
Alexey Bataev10e775f2015-07-30 11:36:16 +00009347}
9348
Alexey Bataeved09d242014-05-28 05:53:51 +00009349OMPClause *Sema::ActOnOpenMPSimpleClause(
9350 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
9351 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009352 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009353 switch (Kind) {
9354 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00009355 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00009356 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
9357 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009358 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009359 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00009360 Res = ActOnOpenMPProcBindClause(
9361 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
9362 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009363 break;
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009364 case OMPC_atomic_default_mem_order:
9365 Res = ActOnOpenMPAtomicDefaultMemOrderClause(
9366 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument),
9367 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
9368 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009369 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00009370 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00009371 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00009372 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009373 case OMPC_simdlen:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00009374 case OMPC_allocator:
Alexander Musman8bd31e62014-05-27 15:12:19 +00009375 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009376 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009377 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009378 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00009379 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00009380 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00009381 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00009382 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00009383 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00009384 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009385 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009386 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009387 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009388 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00009389 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009390 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009391 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009392 case OMPC_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +00009393 case OMPC_allocate:
Alexey Bataev6125da92014-07-21 11:26:11 +00009394 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009395 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00009396 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00009397 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00009398 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009399 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009400 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00009401 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00009402 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009403 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00009404 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009405 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009406 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009407 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009408 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00009409 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00009410 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009411 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00009412 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009413 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009414 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009415 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00009416 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00009417 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00009418 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00009419 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00009420 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00009421 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009422 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009423 case OMPC_dynamic_allocators:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009424 llvm_unreachable("Clause is not allowed.");
9425 }
9426 return Res;
9427}
9428
Alexey Bataev6402bca2015-12-28 07:25:51 +00009429static std::string
9430getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
9431 ArrayRef<unsigned> Exclude = llvm::None) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009432 SmallString<256> Buffer;
9433 llvm::raw_svector_ostream Out(Buffer);
Alexey Bataev6402bca2015-12-28 07:25:51 +00009434 unsigned Bound = Last >= 2 ? Last - 2 : 0;
9435 unsigned Skipped = Exclude.size();
9436 auto S = Exclude.begin(), E = Exclude.end();
Alexey Bataeve3727102018-04-18 15:57:46 +00009437 for (unsigned I = First; I < Last; ++I) {
9438 if (std::find(S, E, I) != E) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00009439 --Skipped;
9440 continue;
9441 }
Alexey Bataeve3727102018-04-18 15:57:46 +00009442 Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'";
9443 if (I == Bound - Skipped)
9444 Out << " or ";
9445 else if (I != Bound + 1 - Skipped)
9446 Out << ", ";
Alexey Bataev6402bca2015-12-28 07:25:51 +00009447 }
Alexey Bataeve3727102018-04-18 15:57:46 +00009448 return Out.str();
Alexey Bataev6402bca2015-12-28 07:25:51 +00009449}
9450
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009451OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
9452 SourceLocation KindKwLoc,
9453 SourceLocation StartLoc,
9454 SourceLocation LParenLoc,
9455 SourceLocation EndLoc) {
9456 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00009457 static_assert(OMPC_DEFAULT_unknown > 0,
9458 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009459 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009460 << getListOfPossibleValues(OMPC_default, /*First=*/0,
9461 /*Last=*/OMPC_DEFAULT_unknown)
9462 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009463 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009464 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00009465 switch (Kind) {
9466 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009467 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009468 break;
9469 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009470 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009471 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009472 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009473 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00009474 break;
9475 }
Alexey Bataeved09d242014-05-28 05:53:51 +00009476 return new (Context)
9477 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009478}
9479
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009480OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
9481 SourceLocation KindKwLoc,
9482 SourceLocation StartLoc,
9483 SourceLocation LParenLoc,
9484 SourceLocation EndLoc) {
9485 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009486 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009487 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
9488 /*Last=*/OMPC_PROC_BIND_unknown)
9489 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009490 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009491 }
Alexey Bataeved09d242014-05-28 05:53:51 +00009492 return new (Context)
9493 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009494}
9495
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009496OMPClause *Sema::ActOnOpenMPAtomicDefaultMemOrderClause(
9497 OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc,
9498 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
9499 if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) {
9500 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
9501 << getListOfPossibleValues(
9502 OMPC_atomic_default_mem_order, /*First=*/0,
9503 /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown)
9504 << getOpenMPClauseName(OMPC_atomic_default_mem_order);
9505 return nullptr;
9506 }
9507 return new (Context) OMPAtomicDefaultMemOrderClause(Kind, KindKwLoc, StartLoc,
9508 LParenLoc, EndLoc);
9509}
9510
Alexey Bataev56dafe82014-06-20 07:16:17 +00009511OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00009512 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00009513 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00009514 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00009515 SourceLocation EndLoc) {
9516 OMPClause *Res = nullptr;
9517 switch (Kind) {
9518 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00009519 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
9520 assert(Argument.size() == NumberOfElements &&
9521 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009522 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00009523 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
9524 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
9525 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
9526 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
9527 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009528 break;
9529 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00009530 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
9531 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
9532 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
9533 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009534 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00009535 case OMPC_dist_schedule:
9536 Res = ActOnOpenMPDistScheduleClause(
9537 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
9538 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
9539 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009540 case OMPC_defaultmap:
9541 enum { Modifier, DefaultmapKind };
9542 Res = ActOnOpenMPDefaultmapClause(
9543 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
9544 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
David Majnemer9d168222016-08-05 17:44:54 +00009545 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind],
9546 EndLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009547 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00009548 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009549 case OMPC_num_threads:
9550 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009551 case OMPC_simdlen:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00009552 case OMPC_allocator:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009553 case OMPC_collapse:
9554 case OMPC_default:
9555 case OMPC_proc_bind:
9556 case OMPC_private:
9557 case OMPC_firstprivate:
9558 case OMPC_lastprivate:
9559 case OMPC_shared:
9560 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00009561 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00009562 case OMPC_in_reduction:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009563 case OMPC_linear:
9564 case OMPC_aligned:
9565 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009566 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009567 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00009568 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009569 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009570 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009571 case OMPC_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +00009572 case OMPC_allocate:
Alexey Bataev6125da92014-07-21 11:26:11 +00009573 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009574 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00009575 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00009576 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00009577 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009578 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009579 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00009580 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00009581 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009582 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00009583 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009584 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009585 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009586 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009587 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00009588 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00009589 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009590 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009591 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009592 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00009593 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00009594 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00009595 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00009596 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00009597 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00009598 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009599 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009600 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009601 case OMPC_atomic_default_mem_order:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009602 llvm_unreachable("Clause is not allowed.");
9603 }
9604 return Res;
9605}
9606
Alexey Bataev6402bca2015-12-28 07:25:51 +00009607static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
9608 OpenMPScheduleClauseModifier M2,
9609 SourceLocation M1Loc, SourceLocation M2Loc) {
9610 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
9611 SmallVector<unsigned, 2> Excluded;
9612 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
9613 Excluded.push_back(M2);
9614 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
9615 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
9616 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
9617 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
9618 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
9619 << getListOfPossibleValues(OMPC_schedule,
9620 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
9621 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
9622 Excluded)
9623 << getOpenMPClauseName(OMPC_schedule);
9624 return true;
9625 }
9626 return false;
9627}
9628
Alexey Bataev56dafe82014-06-20 07:16:17 +00009629OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00009630 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00009631 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00009632 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
9633 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
9634 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
9635 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
9636 return nullptr;
9637 // OpenMP, 2.7.1, Loop Construct, Restrictions
9638 // Either the monotonic modifier or the nonmonotonic modifier can be specified
9639 // but not both.
9640 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
9641 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
9642 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
9643 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
9644 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
9645 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
9646 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
9647 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
9648 return nullptr;
9649 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00009650 if (Kind == OMPC_SCHEDULE_unknown) {
9651 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00009652 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
9653 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
9654 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
9655 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
9656 Exclude);
9657 } else {
9658 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
9659 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009660 }
9661 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
9662 << Values << getOpenMPClauseName(OMPC_schedule);
9663 return nullptr;
9664 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00009665 // OpenMP, 2.7.1, Loop Construct, Restrictions
9666 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
9667 // schedule(guided).
9668 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
9669 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
9670 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
9671 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
9672 diag::err_omp_schedule_nonmonotonic_static);
9673 return nullptr;
9674 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00009675 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00009676 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00009677 if (ChunkSize) {
9678 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
9679 !ChunkSize->isInstantiationDependent() &&
9680 !ChunkSize->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00009681 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
Alexey Bataev56dafe82014-06-20 07:16:17 +00009682 ExprResult Val =
9683 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
9684 if (Val.isInvalid())
9685 return nullptr;
9686
9687 ValExpr = Val.get();
9688
9689 // OpenMP [2.7.1, Restrictions]
9690 // chunk_size must be a loop invariant integer expression with a positive
9691 // value.
9692 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00009693 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
9694 if (Result.isSigned() && !Result.isStrictlyPositive()) {
9695 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00009696 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00009697 return nullptr;
9698 }
Alexey Bataev2ba67042017-11-28 21:11:44 +00009699 } else if (getOpenMPCaptureRegionForClause(
9700 DSAStack->getCurrentDirective(), OMPC_schedule) !=
9701 OMPD_unknown &&
Alexey Bataevb46cdea2016-06-15 11:20:48 +00009702 !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00009703 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00009704 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev5a3af132016-03-29 08:58:54 +00009705 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
9706 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009707 }
9708 }
9709 }
9710
Alexey Bataev6402bca2015-12-28 07:25:51 +00009711 return new (Context)
9712 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00009713 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009714}
9715
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009716OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
9717 SourceLocation StartLoc,
9718 SourceLocation EndLoc) {
9719 OMPClause *Res = nullptr;
9720 switch (Kind) {
9721 case OMPC_ordered:
9722 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
9723 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00009724 case OMPC_nowait:
9725 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
9726 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009727 case OMPC_untied:
9728 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
9729 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009730 case OMPC_mergeable:
9731 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
9732 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009733 case OMPC_read:
9734 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
9735 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00009736 case OMPC_write:
9737 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
9738 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00009739 case OMPC_update:
9740 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
9741 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00009742 case OMPC_capture:
9743 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
9744 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009745 case OMPC_seq_cst:
9746 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
9747 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00009748 case OMPC_threads:
9749 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
9750 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009751 case OMPC_simd:
9752 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
9753 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00009754 case OMPC_nogroup:
9755 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
9756 break;
Kelvin Li1408f912018-09-26 04:28:39 +00009757 case OMPC_unified_address:
9758 Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc);
9759 break;
Patrick Lyster4a370b92018-10-01 13:47:43 +00009760 case OMPC_unified_shared_memory:
9761 Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
9762 break;
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009763 case OMPC_reverse_offload:
9764 Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc);
9765 break;
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009766 case OMPC_dynamic_allocators:
9767 Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc);
9768 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009769 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00009770 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009771 case OMPC_num_threads:
9772 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009773 case OMPC_simdlen:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00009774 case OMPC_allocator:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009775 case OMPC_collapse:
9776 case OMPC_schedule:
9777 case OMPC_private:
9778 case OMPC_firstprivate:
9779 case OMPC_lastprivate:
9780 case OMPC_shared:
9781 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00009782 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00009783 case OMPC_in_reduction:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009784 case OMPC_linear:
9785 case OMPC_aligned:
9786 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009787 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009788 case OMPC_default:
9789 case OMPC_proc_bind:
9790 case OMPC_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +00009791 case OMPC_allocate:
Alexey Bataev6125da92014-07-21 11:26:11 +00009792 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009793 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00009794 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00009795 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009796 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009797 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009798 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009799 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00009800 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009801 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00009802 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009803 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009804 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009805 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00009806 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00009807 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00009808 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00009809 case OMPC_is_device_ptr:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009810 case OMPC_atomic_default_mem_order:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009811 llvm_unreachable("Clause is not allowed.");
9812 }
9813 return Res;
9814}
9815
Alexey Bataev236070f2014-06-20 11:19:47 +00009816OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
9817 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00009818 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00009819 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
9820}
9821
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009822OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
9823 SourceLocation EndLoc) {
9824 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
9825}
9826
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009827OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
9828 SourceLocation EndLoc) {
9829 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
9830}
9831
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009832OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
9833 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009834 return new (Context) OMPReadClause(StartLoc, EndLoc);
9835}
9836
Alexey Bataevdea47612014-07-23 07:46:59 +00009837OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
9838 SourceLocation EndLoc) {
9839 return new (Context) OMPWriteClause(StartLoc, EndLoc);
9840}
9841
Alexey Bataev67a4f222014-07-23 10:25:33 +00009842OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
9843 SourceLocation EndLoc) {
9844 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
9845}
9846
Alexey Bataev459dec02014-07-24 06:46:57 +00009847OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
9848 SourceLocation EndLoc) {
9849 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
9850}
9851
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009852OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
9853 SourceLocation EndLoc) {
9854 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
9855}
9856
Alexey Bataev346265e2015-09-25 10:37:12 +00009857OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
9858 SourceLocation EndLoc) {
9859 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
9860}
9861
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009862OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
9863 SourceLocation EndLoc) {
9864 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
9865}
9866
Alexey Bataevb825de12015-12-07 10:51:44 +00009867OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
9868 SourceLocation EndLoc) {
9869 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
9870}
9871
Kelvin Li1408f912018-09-26 04:28:39 +00009872OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc,
9873 SourceLocation EndLoc) {
9874 return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc);
9875}
9876
Patrick Lyster4a370b92018-10-01 13:47:43 +00009877OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc,
9878 SourceLocation EndLoc) {
9879 return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
9880}
9881
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009882OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc,
9883 SourceLocation EndLoc) {
9884 return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc);
9885}
9886
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009887OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc,
9888 SourceLocation EndLoc) {
9889 return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc);
9890}
9891
Alexey Bataevc5e02582014-06-16 07:08:35 +00009892OMPClause *Sema::ActOnOpenMPVarListClause(
9893 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
Michael Kruse4304e9d2019-02-19 16:38:20 +00009894 const OMPVarListLocTy &Locs, SourceLocation ColonLoc,
9895 CXXScopeSpec &ReductionOrMapperIdScopeSpec,
9896 DeclarationNameInfo &ReductionOrMapperId, OpenMPDependClauseKind DepKind,
Kelvin Lief579432018-12-18 22:18:41 +00009897 OpenMPLinearClauseKind LinKind,
9898 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers,
Michael Kruse4304e9d2019-02-19 16:38:20 +00009899 ArrayRef<SourceLocation> MapTypeModifiersLoc, OpenMPMapClauseKind MapType,
9900 bool IsMapTypeImplicit, SourceLocation DepLinMapLoc) {
9901 SourceLocation StartLoc = Locs.StartLoc;
9902 SourceLocation LParenLoc = Locs.LParenLoc;
9903 SourceLocation EndLoc = Locs.EndLoc;
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009904 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009905 switch (Kind) {
9906 case OMPC_private:
9907 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9908 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009909 case OMPC_firstprivate:
9910 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9911 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00009912 case OMPC_lastprivate:
9913 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9914 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00009915 case OMPC_shared:
9916 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
9917 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009918 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00009919 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
Michael Kruse4304e9d2019-02-19 16:38:20 +00009920 EndLoc, ReductionOrMapperIdScopeSpec,
9921 ReductionOrMapperId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00009922 break;
Alexey Bataev169d96a2017-07-18 20:17:46 +00009923 case OMPC_task_reduction:
9924 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
Michael Kruse4304e9d2019-02-19 16:38:20 +00009925 EndLoc, ReductionOrMapperIdScopeSpec,
9926 ReductionOrMapperId);
Alexey Bataev169d96a2017-07-18 20:17:46 +00009927 break;
Alexey Bataevfa312f32017-07-21 18:48:21 +00009928 case OMPC_in_reduction:
Michael Kruse4304e9d2019-02-19 16:38:20 +00009929 Res = ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9930 EndLoc, ReductionOrMapperIdScopeSpec,
9931 ReductionOrMapperId);
Alexey Bataevfa312f32017-07-21 18:48:21 +00009932 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00009933 case OMPC_linear:
9934 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00009935 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00009936 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009937 case OMPC_aligned:
9938 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
9939 ColonLoc, EndLoc);
9940 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009941 case OMPC_copyin:
9942 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
9943 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009944 case OMPC_copyprivate:
9945 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9946 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00009947 case OMPC_flush:
9948 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
9949 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009950 case OMPC_depend:
David Majnemer9d168222016-08-05 17:44:54 +00009951 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
Kelvin Li0bff7af2015-11-23 05:32:03 +00009952 StartLoc, LParenLoc, EndLoc);
9953 break;
9954 case OMPC_map:
Michael Kruse4304e9d2019-02-19 16:38:20 +00009955 Res = ActOnOpenMPMapClause(MapTypeModifiers, MapTypeModifiersLoc,
9956 ReductionOrMapperIdScopeSpec,
9957 ReductionOrMapperId, MapType, IsMapTypeImplicit,
9958 DepLinMapLoc, ColonLoc, VarList, Locs);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009959 break;
Samuel Antao661c0902016-05-26 17:39:58 +00009960 case OMPC_to:
Michael Kruse01f670d2019-02-22 22:29:42 +00009961 Res = ActOnOpenMPToClause(VarList, ReductionOrMapperIdScopeSpec,
9962 ReductionOrMapperId, Locs);
Samuel Antao661c0902016-05-26 17:39:58 +00009963 break;
Samuel Antaoec172c62016-05-26 17:49:04 +00009964 case OMPC_from:
Michael Kruse0336c752019-02-25 20:34:15 +00009965 Res = ActOnOpenMPFromClause(VarList, ReductionOrMapperIdScopeSpec,
9966 ReductionOrMapperId, Locs);
Samuel Antaoec172c62016-05-26 17:49:04 +00009967 break;
Carlo Bertolli2404b172016-07-13 15:37:16 +00009968 case OMPC_use_device_ptr:
Michael Kruse4304e9d2019-02-19 16:38:20 +00009969 Res = ActOnOpenMPUseDevicePtrClause(VarList, Locs);
Carlo Bertolli2404b172016-07-13 15:37:16 +00009970 break;
Carlo Bertolli70594e92016-07-13 17:16:49 +00009971 case OMPC_is_device_ptr:
Michael Kruse4304e9d2019-02-19 16:38:20 +00009972 Res = ActOnOpenMPIsDevicePtrClause(VarList, Locs);
Carlo Bertolli70594e92016-07-13 17:16:49 +00009973 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009974 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00009975 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00009976 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00009977 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009978 case OMPC_simdlen:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00009979 case OMPC_allocator:
Alexander Musman8bd31e62014-05-27 15:12:19 +00009980 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009981 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009982 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009983 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009984 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00009985 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009986 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009987 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009988 case OMPC_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +00009989 case OMPC_allocate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009990 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00009991 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00009992 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00009993 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009994 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00009995 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00009996 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009997 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009998 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009999 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +000010000 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000010001 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +000010002 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +000010003 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +000010004 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +000010005 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000010006 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000010007 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +000010008 case OMPC_uniform:
Kelvin Li1408f912018-09-26 04:28:39 +000010009 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +000010010 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +000010011 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +000010012 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +000010013 case OMPC_atomic_default_mem_order:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000010014 llvm_unreachable("Clause is not allowed.");
10015 }
10016 return Res;
10017}
10018
Alexey Bataev90c228f2016-02-08 09:29:13 +000010019ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +000010020 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +000010021 ExprResult Res = BuildDeclRefExpr(
10022 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
10023 if (!Res.isUsable())
10024 return ExprError();
10025 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
10026 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
10027 if (!Res.isUsable())
10028 return ExprError();
10029 }
10030 if (VK != VK_LValue && Res.get()->isGLValue()) {
10031 Res = DefaultLvalueConversion(Res.get());
10032 if (!Res.isUsable())
10033 return ExprError();
10034 }
10035 return Res;
10036}
10037
Alexey Bataev60da77e2016-02-29 05:54:20 +000010038static std::pair<ValueDecl *, bool>
10039getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
10040 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +000010041 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
10042 RefExpr->containsUnexpandedParameterPack())
10043 return std::make_pair(nullptr, true);
10044
Alexey Bataevd985eda2016-02-10 11:29:16 +000010045 // OpenMP [3.1, C/C++]
10046 // A list item is a variable name.
10047 // OpenMP [2.9.3.3, Restrictions, p.1]
10048 // A variable that is part of another variable (as an array or
10049 // structure element) cannot appear in a private clause.
10050 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +000010051 enum {
10052 NoArrayExpr = -1,
10053 ArraySubscript = 0,
10054 OMPArraySection = 1
10055 } IsArrayExpr = NoArrayExpr;
10056 if (AllowArraySection) {
10057 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010058 Expr *Base = ASE->getBase()->IgnoreParenImpCasts();
Alexey Bataev60da77e2016-02-29 05:54:20 +000010059 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
10060 Base = TempASE->getBase()->IgnoreParenImpCasts();
10061 RefExpr = Base;
10062 IsArrayExpr = ArraySubscript;
10063 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010064 Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
Alexey Bataev60da77e2016-02-29 05:54:20 +000010065 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
10066 Base = TempOASE->getBase()->IgnoreParenImpCasts();
10067 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
10068 Base = TempASE->getBase()->IgnoreParenImpCasts();
10069 RefExpr = Base;
10070 IsArrayExpr = OMPArraySection;
10071 }
10072 }
10073 ELoc = RefExpr->getExprLoc();
10074 ERange = RefExpr->getSourceRange();
10075 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +000010076 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
10077 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
10078 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
10079 (S.getCurrentThisType().isNull() || !ME ||
10080 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
10081 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010082 if (IsArrayExpr != NoArrayExpr) {
Alexey Bataev60da77e2016-02-29 05:54:20 +000010083 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
10084 << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +000010085 } else {
Alexey Bataev60da77e2016-02-29 05:54:20 +000010086 S.Diag(ELoc,
10087 AllowArraySection
10088 ? diag::err_omp_expected_var_name_member_expr_or_array_item
10089 : diag::err_omp_expected_var_name_member_expr)
10090 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
10091 }
Alexey Bataevd985eda2016-02-10 11:29:16 +000010092 return std::make_pair(nullptr, false);
10093 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +000010094 return std::make_pair(
10095 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false);
Alexey Bataevd985eda2016-02-10 11:29:16 +000010096}
10097
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000010098OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
10099 SourceLocation StartLoc,
10100 SourceLocation LParenLoc,
10101 SourceLocation EndLoc) {
10102 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +000010103 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeve3727102018-04-18 15:57:46 +000010104 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000010105 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +000010106 SourceLocation ELoc;
10107 SourceRange ERange;
10108 Expr *SimpleRefExpr = RefExpr;
10109 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +000010110 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000010111 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000010112 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +000010113 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000010114 }
Alexey Bataevd985eda2016-02-10 11:29:16 +000010115 ValueDecl *D = Res.first;
10116 if (!D)
10117 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000010118
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000010119 QualType Type = D->getType();
10120 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000010121
10122 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
10123 // A variable that appears in a private clause must not have an incomplete
10124 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000010125 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000010126 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010127 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000010128
Joel E. Dennye6234d1422019-01-04 22:11:31 +000010129 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
10130 // A variable that is privatized must not have a const-qualified type
10131 // unless it is of class type with a mutable member. This restriction does
10132 // not apply to the firstprivate clause.
10133 //
10134 // OpenMP 3.1 [2.9.3.3, private clause, Restrictions]
10135 // A variable that appears in a private clause must not have a
10136 // const-qualified type unless it is of class type with a mutable member.
Joel E. Dennyd2649292019-01-04 22:11:56 +000010137 if (rejectConstNotMutableType(*this, D, Type, OMPC_private, ELoc))
Joel E. Dennye6234d1422019-01-04 22:11:31 +000010138 continue;
10139
Alexey Bataev758e55e2013-09-06 18:03:48 +000010140 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
10141 // in a Construct]
10142 // Variables with the predetermined data-sharing attributes may not be
10143 // listed in data-sharing attributes clauses, except for the cases
10144 // listed below. For these exceptions only, listing a predetermined
10145 // variable in a data-sharing attribute clause is allowed and overrides
10146 // the variable's predetermined data-sharing attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +000010147 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010148 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +000010149 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
10150 << getOpenMPClauseName(OMPC_private);
Alexey Bataeve3727102018-04-18 15:57:46 +000010151 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010152 continue;
10153 }
10154
Alexey Bataeve3727102018-04-18 15:57:46 +000010155 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010156 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000010157 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Kelvin Libf594a52016-12-17 05:48:59 +000010158 isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010159 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
10160 << getOpenMPClauseName(OMPC_private) << Type
Kelvin Libf594a52016-12-17 05:48:59 +000010161 << getOpenMPDirectiveName(CurrDir);
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010162 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000010163 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010164 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000010165 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010166 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000010167 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010168 continue;
10169 }
10170
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010171 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
10172 // A list item cannot appear in both a map clause and a data-sharing
10173 // attribute clause on the same construct
Alexey Bataeve3727102018-04-18 15:57:46 +000010174 if (isOpenMPTargetExecutionDirective(CurrDir)) {
Samuel Antao6890b092016-07-28 14:25:09 +000010175 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +000010176 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +000010177 VD, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +000010178 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
10179 OpenMPClauseKind WhereFoundClauseKind) -> bool {
10180 ConflictKind = WhereFoundClauseKind;
10181 return true;
10182 })) {
10183 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010184 << getOpenMPClauseName(OMPC_private)
Samuel Antao6890b092016-07-28 14:25:09 +000010185 << getOpenMPClauseName(ConflictKind)
Kelvin Libf594a52016-12-17 05:48:59 +000010186 << getOpenMPDirectiveName(CurrDir);
Alexey Bataeve3727102018-04-18 15:57:46 +000010187 reportOriginalDsa(*this, DSAStack, D, DVar);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010188 continue;
10189 }
10190 }
10191
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010192 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
10193 // A variable of class type (or array thereof) that appears in a private
10194 // clause requires an accessible, unambiguous default constructor for the
10195 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +000010196 // Generate helper private variable and initialize it with the default
10197 // value. The address of the original variable is replaced by the address of
10198 // the new private variable in CodeGen. This new variable is not added to
10199 // IdResolver, so the code in the OpenMP region uses original variable for
10200 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010201 Type = Type.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010202 VarDecl *VDPrivate =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000010203 buildVarDecl(*this, ELoc, Type, D->getName(),
10204 D->hasAttrs() ? &D->getAttrs() : nullptr,
10205 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Richard Smith3beb7c62017-01-12 02:27:38 +000010206 ActOnUninitializedDecl(VDPrivate);
Alexey Bataev03b340a2014-10-21 03:16:40 +000010207 if (VDPrivate->isInvalidDecl())
10208 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +000010209 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000010210 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +000010211
Alexey Bataev90c228f2016-02-08 09:29:13 +000010212 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010213 if (!VD && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +000010214 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +000010215 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010216 Vars.push_back((VD || CurContext->isDependentContext())
10217 ? RefExpr->IgnoreParens()
10218 : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +000010219 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000010220 }
10221
Alexey Bataeved09d242014-05-28 05:53:51 +000010222 if (Vars.empty())
10223 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000010224
Alexey Bataev03b340a2014-10-21 03:16:40 +000010225 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
10226 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000010227}
10228
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010229namespace {
10230class DiagsUninitializedSeveretyRAII {
10231private:
10232 DiagnosticsEngine &Diags;
10233 SourceLocation SavedLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +000010234 bool IsIgnored = false;
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010235
10236public:
10237 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
10238 bool IsIgnored)
10239 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
10240 if (!IsIgnored) {
10241 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
10242 /*Map*/ diag::Severity::Ignored, Loc);
10243 }
10244 }
10245 ~DiagsUninitializedSeveretyRAII() {
10246 if (!IsIgnored)
10247 Diags.popMappings(SavedLoc);
10248 }
10249};
Alexander Kornienkoab9db512015-06-22 23:07:51 +000010250}
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010251
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010252OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
10253 SourceLocation StartLoc,
10254 SourceLocation LParenLoc,
10255 SourceLocation EndLoc) {
10256 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010257 SmallVector<Expr *, 8> PrivateCopies;
10258 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +000010259 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010260 bool IsImplicitClause =
10261 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
Alexey Bataeve3727102018-04-18 15:57:46 +000010262 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010263
Alexey Bataeve3727102018-04-18 15:57:46 +000010264 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000010265 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +000010266 SourceLocation ELoc;
10267 SourceRange ERange;
10268 Expr *SimpleRefExpr = RefExpr;
10269 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +000010270 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010271 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000010272 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010273 PrivateCopies.push_back(nullptr);
10274 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010275 }
Alexey Bataevd985eda2016-02-10 11:29:16 +000010276 ValueDecl *D = Res.first;
10277 if (!D)
10278 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010279
Alexey Bataev60da77e2016-02-29 05:54:20 +000010280 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +000010281 QualType Type = D->getType();
10282 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010283
10284 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
10285 // A variable that appears in a private clause must not have an incomplete
10286 // type or a reference type.
10287 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +000010288 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010289 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010290 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010291
10292 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
10293 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +000010294 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010295 // class type.
Alexey Bataeve3727102018-04-18 15:57:46 +000010296 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010297
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010298 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +000010299 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010300 if (!IsImplicitClause) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010301 DSAStackTy::DSAVarData DVar =
10302 DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +000010303 TopDVar = DVar;
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010304 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010305 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010306 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
10307 // A list item that specifies a given variable may not appear in more
10308 // than one clause on the same directive, except that a variable may be
10309 // specified in both firstprivate and lastprivate clauses.
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010310 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
10311 // A list item may appear in a firstprivate or lastprivate clause but not
10312 // both.
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010313 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevb358f992017-12-01 17:40:15 +000010314 (isOpenMPDistributeDirective(CurrDir) ||
10315 DVar.CKind != OMPC_lastprivate) &&
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010316 DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010317 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +000010318 << getOpenMPClauseName(DVar.CKind)
10319 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000010320 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010321 continue;
10322 }
10323
10324 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
10325 // in a Construct]
10326 // Variables with the predetermined data-sharing attributes may not be
10327 // listed in data-sharing attributes clauses, except for the cases
10328 // listed below. For these exceptions only, listing a predetermined
10329 // variable in a data-sharing attribute clause is allowed and overrides
10330 // the variable's predetermined data-sharing attributes.
10331 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
10332 // in a Construct, C/C++, p.2]
10333 // Variables with const-qualified type having no mutable member may be
10334 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +000010335 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010336 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
10337 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +000010338 << getOpenMPClauseName(DVar.CKind)
10339 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000010340 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010341 continue;
10342 }
10343
10344 // OpenMP [2.9.3.4, Restrictions, p.2]
10345 // A list item that is private within a parallel region must not appear
10346 // in a firstprivate clause on a worksharing construct if any of the
10347 // worksharing regions arising from the worksharing construct ever bind
10348 // to any of the parallel regions arising from the parallel construct.
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010349 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
10350 // A list item that is private within a teams region must not appear in a
10351 // firstprivate clause on a distribute construct if any of the distribute
10352 // regions arising from the distribute construct ever bind to any of the
10353 // teams regions arising from the teams construct.
10354 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
10355 // A list item that appears in a reduction clause of a teams construct
10356 // must not appear in a firstprivate clause on a distribute construct if
10357 // any of the distribute regions arising from the distribute construct
10358 // ever bind to any of the teams regions arising from the teams construct.
10359 if ((isOpenMPWorksharingDirective(CurrDir) ||
10360 isOpenMPDistributeDirective(CurrDir)) &&
Kelvin Li579e41c2016-11-30 23:51:03 +000010361 !isOpenMPParallelDirective(CurrDir) &&
10362 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +000010363 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010364 if (DVar.CKind != OMPC_shared &&
10365 (isOpenMPParallelDirective(DVar.DKind) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010366 isOpenMPTeamsDirective(DVar.DKind) ||
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010367 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +000010368 Diag(ELoc, diag::err_omp_required_access)
10369 << getOpenMPClauseName(OMPC_firstprivate)
10370 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +000010371 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +000010372 continue;
10373 }
10374 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010375 // OpenMP [2.9.3.4, Restrictions, p.3]
10376 // A list item that appears in a reduction clause of a parallel construct
10377 // must not appear in a firstprivate clause on a worksharing or task
10378 // construct if any of the worksharing or task regions arising from the
10379 // worksharing or task construct ever bind to any of the parallel regions
10380 // arising from the parallel construct.
10381 // OpenMP [2.9.3.4, Restrictions, p.4]
10382 // A list item that appears in a reduction clause in worksharing
10383 // construct must not appear in a firstprivate clause in a task construct
10384 // encountered during execution of any of the worksharing regions arising
10385 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +000010386 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010387 DVar = DSAStack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +000010388 D, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
10389 [](OpenMPDirectiveKind K) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010390 return isOpenMPParallelDirective(K) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010391 isOpenMPWorksharingDirective(K) ||
10392 isOpenMPTeamsDirective(K);
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010393 },
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010394 /*FromParent=*/true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010395 if (DVar.CKind == OMPC_reduction &&
10396 (isOpenMPParallelDirective(DVar.DKind) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010397 isOpenMPWorksharingDirective(DVar.DKind) ||
10398 isOpenMPTeamsDirective(DVar.DKind))) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010399 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
10400 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataeve3727102018-04-18 15:57:46 +000010401 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010402 continue;
10403 }
10404 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +000010405
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010406 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
10407 // A list item cannot appear in both a map clause and a data-sharing
10408 // attribute clause on the same construct
Alexey Bataevb358f992017-12-01 17:40:15 +000010409 if (isOpenMPTargetExecutionDirective(CurrDir)) {
Samuel Antao6890b092016-07-28 14:25:09 +000010410 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +000010411 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +000010412 VD, /*CurrentRegionOnly=*/true,
Alexey Bataeve3727102018-04-18 15:57:46 +000010413 [&ConflictKind](
10414 OMPClauseMappableExprCommon::MappableExprComponentListRef,
10415 OpenMPClauseKind WhereFoundClauseKind) {
Samuel Antao6890b092016-07-28 14:25:09 +000010416 ConflictKind = WhereFoundClauseKind;
10417 return true;
10418 })) {
10419 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010420 << getOpenMPClauseName(OMPC_firstprivate)
Samuel Antao6890b092016-07-28 14:25:09 +000010421 << getOpenMPClauseName(ConflictKind)
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010422 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000010423 reportOriginalDsa(*this, DSAStack, D, DVar);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010424 continue;
10425 }
10426 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010427 }
10428
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010429 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000010430 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +000010431 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010432 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
10433 << getOpenMPClauseName(OMPC_firstprivate) << Type
10434 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
10435 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +000010436 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010437 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +000010438 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010439 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +000010440 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010441 continue;
10442 }
10443
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010444 Type = Type.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010445 VarDecl *VDPrivate =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000010446 buildVarDecl(*this, ELoc, Type, D->getName(),
10447 D->hasAttrs() ? &D->getAttrs() : nullptr,
10448 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010449 // Generate helper private variable and initialize it with the value of the
10450 // original variable. The address of the original variable is replaced by
10451 // the address of the new private variable in the CodeGen. This new variable
10452 // is not added to IdResolver, so the code in the OpenMP region uses
10453 // original variable for proper diagnostics and variable capturing.
10454 Expr *VDInitRefExpr = nullptr;
10455 // For arrays generate initializer for single element and replace it by the
10456 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010457 if (Type->isArrayType()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010458 VarDecl *VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +000010459 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010460 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataeve3727102018-04-18 15:57:46 +000010461 Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010462 ElemType = ElemType.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010463 VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
10464 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +000010465 InitializedEntity Entity =
10466 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010467 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
10468
10469 InitializationSequence InitSeq(*this, Entity, Kind, Init);
10470 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
10471 if (Result.isInvalid())
10472 VDPrivate->setInvalidDecl();
10473 else
10474 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010475 // Remove temp variable declaration.
10476 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010477 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +000010478 VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
10479 ".firstprivate.temp");
Alexey Bataevd985eda2016-02-10 11:29:16 +000010480 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
10481 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +000010482 AddInitializerToDecl(VDPrivate,
10483 DefaultLvalueConversion(VDInitRefExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000010484 /*DirectInit=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010485 }
10486 if (VDPrivate->isInvalidDecl()) {
10487 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +000010488 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010489 diag::note_omp_task_predetermined_firstprivate_here);
10490 }
10491 continue;
10492 }
10493 CurContext->addDecl(VDPrivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000010494 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +000010495 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
10496 RefExpr->getExprLoc());
10497 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010498 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010499 if (TopDVar.CKind == OMPC_lastprivate) {
Alexey Bataev005248a2016-02-25 05:25:57 +000010500 Ref = TopDVar.PrivateCopy;
Alexey Bataeve3727102018-04-18 15:57:46 +000010501 } else {
Alexey Bataev61205072016-03-02 04:57:40 +000010502 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataeve3727102018-04-18 15:57:46 +000010503 if (!isOpenMPCapturedDecl(D))
Alexey Bataev005248a2016-02-25 05:25:57 +000010504 ExprCaptures.push_back(Ref->getDecl());
10505 }
Alexey Bataev417089f2016-02-17 13:19:37 +000010506 }
Alexey Bataevd985eda2016-02-10 11:29:16 +000010507 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010508 Vars.push_back((VD || CurContext->isDependentContext())
10509 ? RefExpr->IgnoreParens()
10510 : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010511 PrivateCopies.push_back(VDPrivateRefExpr);
10512 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010513 }
10514
Alexey Bataeved09d242014-05-28 05:53:51 +000010515 if (Vars.empty())
10516 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010517
10518 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +000010519 Vars, PrivateCopies, Inits,
10520 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010521}
10522
Alexander Musman1bb328c2014-06-04 13:06:39 +000010523OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
10524 SourceLocation StartLoc,
10525 SourceLocation LParenLoc,
10526 SourceLocation EndLoc) {
10527 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +000010528 SmallVector<Expr *, 8> SrcExprs;
10529 SmallVector<Expr *, 8> DstExprs;
10530 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +000010531 SmallVector<Decl *, 4> ExprCaptures;
10532 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeve3727102018-04-18 15:57:46 +000010533 for (Expr *RefExpr : VarList) {
Alexander Musman1bb328c2014-06-04 13:06:39 +000010534 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +000010535 SourceLocation ELoc;
10536 SourceRange ERange;
10537 Expr *SimpleRefExpr = RefExpr;
10538 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +000010539 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +000010540 // It will be analyzed later.
10541 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +000010542 SrcExprs.push_back(nullptr);
10543 DstExprs.push_back(nullptr);
10544 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010545 }
Alexey Bataev74caaf22016-02-20 04:09:36 +000010546 ValueDecl *D = Res.first;
10547 if (!D)
10548 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +000010549
Alexey Bataev74caaf22016-02-20 04:09:36 +000010550 QualType Type = D->getType();
10551 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010552
10553 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
10554 // A variable that appears in a lastprivate clause must not have an
10555 // incomplete type or a reference type.
10556 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +000010557 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +000010558 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010559 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +000010560
Joel E. Dennye6234d1422019-01-04 22:11:31 +000010561 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
10562 // A variable that is privatized must not have a const-qualified type
10563 // unless it is of class type with a mutable member. This restriction does
10564 // not apply to the firstprivate clause.
10565 //
10566 // OpenMP 3.1 [2.9.3.5, lastprivate clause, Restrictions]
10567 // A variable that appears in a lastprivate clause must not have a
10568 // const-qualified type unless it is of class type with a mutable member.
Joel E. Dennyd2649292019-01-04 22:11:56 +000010569 if (rejectConstNotMutableType(*this, D, Type, OMPC_lastprivate, ELoc))
Joel E. Dennye6234d1422019-01-04 22:11:31 +000010570 continue;
10571
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010572 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexander Musman1bb328c2014-06-04 13:06:39 +000010573 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
10574 // in a Construct]
10575 // Variables with the predetermined data-sharing attributes may not be
10576 // listed in data-sharing attributes clauses, except for the cases
10577 // listed below.
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010578 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
10579 // A list item may appear in a firstprivate or lastprivate clause but not
10580 // both.
Alexey Bataeve3727102018-04-18 15:57:46 +000010581 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010582 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
Alexey Bataevb358f992017-12-01 17:40:15 +000010583 (isOpenMPDistributeDirective(CurrDir) ||
10584 DVar.CKind != OMPC_firstprivate) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +000010585 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
10586 Diag(ELoc, diag::err_omp_wrong_dsa)
10587 << getOpenMPClauseName(DVar.CKind)
10588 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000010589 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010590 continue;
10591 }
10592
Alexey Bataevf29276e2014-06-18 04:14:57 +000010593 // OpenMP [2.14.3.5, Restrictions, p.2]
10594 // A list item that is private within a parallel region, or that appears in
10595 // the reduction clause of a parallel construct, must not appear in a
10596 // lastprivate clause on a worksharing construct if any of the corresponding
10597 // worksharing regions ever binds to any of the corresponding parallel
10598 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +000010599 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +000010600 if (isOpenMPWorksharingDirective(CurrDir) &&
Kelvin Li579e41c2016-11-30 23:51:03 +000010601 !isOpenMPParallelDirective(CurrDir) &&
10602 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +000010603 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +000010604 if (DVar.CKind != OMPC_shared) {
10605 Diag(ELoc, diag::err_omp_required_access)
10606 << getOpenMPClauseName(OMPC_lastprivate)
10607 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +000010608 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +000010609 continue;
10610 }
10611 }
Alexey Bataev74caaf22016-02-20 04:09:36 +000010612
Alexander Musman1bb328c2014-06-04 13:06:39 +000010613 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +000010614 // A variable of class type (or array thereof) that appears in a
10615 // lastprivate clause requires an accessible, unambiguous default
10616 // constructor for the class type, unless the list item is also specified
10617 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +000010618 // A variable of class type (or array thereof) that appears in a
10619 // lastprivate clause requires an accessible, unambiguous copy assignment
10620 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +000010621 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010622 VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(),
10623 Type.getUnqualifiedType(), ".lastprivate.src",
10624 D->hasAttrs() ? &D->getAttrs() : nullptr);
10625 DeclRefExpr *PseudoSrcExpr =
Alexey Bataev74caaf22016-02-20 04:09:36 +000010626 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataeve3727102018-04-18 15:57:46 +000010627 VarDecl *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +000010628 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +000010629 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000010630 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +000010631 // For arrays generate assignment operation for single element and replace
10632 // it by the original array element in CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +000010633 ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
10634 PseudoDstExpr, PseudoSrcExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +000010635 if (AssignmentOp.isInvalid())
10636 continue;
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000010637 AssignmentOp =
10638 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataev38e89532015-04-16 04:54:05 +000010639 if (AssignmentOp.isInvalid())
10640 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +000010641
Alexey Bataev74caaf22016-02-20 04:09:36 +000010642 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010643 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010644 if (TopDVar.CKind == OMPC_firstprivate) {
Alexey Bataev005248a2016-02-25 05:25:57 +000010645 Ref = TopDVar.PrivateCopy;
Alexey Bataeve3727102018-04-18 15:57:46 +000010646 } else {
Alexey Bataev61205072016-03-02 04:57:40 +000010647 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000010648 if (!isOpenMPCapturedDecl(D))
Alexey Bataev005248a2016-02-25 05:25:57 +000010649 ExprCaptures.push_back(Ref->getDecl());
10650 }
10651 if (TopDVar.CKind == OMPC_firstprivate ||
Alexey Bataeve3727102018-04-18 15:57:46 +000010652 (!isOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010653 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +000010654 ExprResult RefRes = DefaultLvalueConversion(Ref);
10655 if (!RefRes.isUsable())
10656 continue;
10657 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +000010658 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
10659 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +000010660 if (!PostUpdateRes.isUsable())
10661 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +000010662 ExprPostUpdates.push_back(
10663 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +000010664 }
10665 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010666 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010667 Vars.push_back((VD || CurContext->isDependentContext())
10668 ? RefExpr->IgnoreParens()
10669 : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +000010670 SrcExprs.push_back(PseudoSrcExpr);
10671 DstExprs.push_back(PseudoDstExpr);
10672 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +000010673 }
10674
10675 if (Vars.empty())
10676 return nullptr;
10677
10678 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +000010679 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +000010680 buildPreInits(Context, ExprCaptures),
10681 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +000010682}
10683
Alexey Bataev758e55e2013-09-06 18:03:48 +000010684OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
10685 SourceLocation StartLoc,
10686 SourceLocation LParenLoc,
10687 SourceLocation EndLoc) {
10688 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +000010689 for (Expr *RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010690 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +000010691 SourceLocation ELoc;
10692 SourceRange ERange;
10693 Expr *SimpleRefExpr = RefExpr;
10694 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010695 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +000010696 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000010697 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010698 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010699 ValueDecl *D = Res.first;
10700 if (!D)
10701 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +000010702
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010703 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010704 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
10705 // in a Construct]
10706 // Variables with the predetermined data-sharing attributes may not be
10707 // listed in data-sharing attributes clauses, except for the cases
10708 // listed below. For these exceptions only, listing a predetermined
10709 // variable in a data-sharing attribute clause is allowed and overrides
10710 // the variable's predetermined data-sharing attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +000010711 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataeved09d242014-05-28 05:53:51 +000010712 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
10713 DVar.RefExpr) {
10714 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
10715 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +000010716 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010717 continue;
10718 }
10719
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010720 DeclRefExpr *Ref = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000010721 if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +000010722 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010723 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010724 Vars.push_back((VD || !Ref || CurContext->isDependentContext())
10725 ? RefExpr->IgnoreParens()
10726 : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010727 }
10728
Alexey Bataeved09d242014-05-28 05:53:51 +000010729 if (Vars.empty())
10730 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +000010731
10732 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
10733}
10734
Alexey Bataevc5e02582014-06-16 07:08:35 +000010735namespace {
10736class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
10737 DSAStackTy *Stack;
10738
10739public:
10740 bool VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010741 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
10742 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataevc5e02582014-06-16 07:08:35 +000010743 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
10744 return false;
10745 if (DVar.CKind != OMPC_unknown)
10746 return true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010747 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +000010748 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) { return true; },
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010749 /*FromParent=*/true);
Alexey Bataeve3727102018-04-18 15:57:46 +000010750 return DVarPrivate.CKind != OMPC_unknown;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010751 }
10752 return false;
10753 }
10754 bool VisitStmt(Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010755 for (Stmt *Child : S->children()) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010756 if (Child && Visit(Child))
10757 return true;
10758 }
10759 return false;
10760 }
Alexey Bataev23b69422014-06-18 07:08:49 +000010761 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +000010762};
Alexey Bataev23b69422014-06-18 07:08:49 +000010763} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +000010764
Alexey Bataev60da77e2016-02-29 05:54:20 +000010765namespace {
10766// Transform MemberExpression for specified FieldDecl of current class to
10767// DeclRefExpr to specified OMPCapturedExprDecl.
10768class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
10769 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
Alexey Bataeve3727102018-04-18 15:57:46 +000010770 ValueDecl *Field = nullptr;
10771 DeclRefExpr *CapturedExpr = nullptr;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010772
10773public:
10774 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
10775 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
10776
10777 ExprResult TransformMemberExpr(MemberExpr *E) {
10778 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
10779 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +000010780 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +000010781 return CapturedExpr;
10782 }
10783 return BaseTransform::TransformMemberExpr(E);
10784 }
10785 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
10786};
10787} // namespace
10788
Alexey Bataev97d18bf2018-04-11 19:21:00 +000010789template <typename T, typename U>
Michael Kruse4304e9d2019-02-19 16:38:20 +000010790static T filterLookupForUDReductionAndMapper(
10791 SmallVectorImpl<U> &Lookups, const llvm::function_ref<T(ValueDecl *)> Gen) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010792 for (U &Set : Lookups) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010793 for (auto *D : Set) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010794 if (T Res = Gen(cast<ValueDecl>(D)))
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010795 return Res;
10796 }
10797 }
10798 return T();
10799}
10800
Alexey Bataev43b90b72018-09-12 16:31:59 +000010801static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) {
10802 assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case");
10803
10804 for (auto RD : D->redecls()) {
10805 // Don't bother with extra checks if we already know this one isn't visible.
10806 if (RD == D)
10807 continue;
10808
10809 auto ND = cast<NamedDecl>(RD);
10810 if (LookupResult::isVisible(SemaRef, ND))
10811 return ND;
10812 }
10813
10814 return nullptr;
10815}
10816
10817static void
Michael Kruse4304e9d2019-02-19 16:38:20 +000010818argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &Id,
Alexey Bataev43b90b72018-09-12 16:31:59 +000010819 SourceLocation Loc, QualType Ty,
10820 SmallVectorImpl<UnresolvedSet<8>> &Lookups) {
10821 // Find all of the associated namespaces and classes based on the
10822 // arguments we have.
10823 Sema::AssociatedNamespaceSet AssociatedNamespaces;
10824 Sema::AssociatedClassSet AssociatedClasses;
10825 OpaqueValueExpr OVE(Loc, Ty, VK_LValue);
10826 SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces,
10827 AssociatedClasses);
10828
10829 // C++ [basic.lookup.argdep]p3:
10830 // Let X be the lookup set produced by unqualified lookup (3.4.1)
10831 // and let Y be the lookup set produced by argument dependent
10832 // lookup (defined as follows). If X contains [...] then Y is
10833 // empty. Otherwise Y is the set of declarations found in the
10834 // namespaces associated with the argument types as described
10835 // below. The set of declarations found by the lookup of the name
10836 // is the union of X and Y.
10837 //
10838 // Here, we compute Y and add its members to the overloaded
10839 // candidate set.
10840 for (auto *NS : AssociatedNamespaces) {
10841 // When considering an associated namespace, the lookup is the
10842 // same as the lookup performed when the associated namespace is
10843 // used as a qualifier (3.4.3.2) except that:
10844 //
10845 // -- Any using-directives in the associated namespace are
10846 // ignored.
10847 //
10848 // -- Any namespace-scope friend functions declared in
10849 // associated classes are visible within their respective
10850 // namespaces even if they are not visible during an ordinary
10851 // lookup (11.4).
Michael Kruse4304e9d2019-02-19 16:38:20 +000010852 DeclContext::lookup_result R = NS->lookup(Id.getName());
Alexey Bataev43b90b72018-09-12 16:31:59 +000010853 for (auto *D : R) {
10854 auto *Underlying = D;
10855 if (auto *USD = dyn_cast<UsingShadowDecl>(D))
10856 Underlying = USD->getTargetDecl();
10857
Michael Kruse4304e9d2019-02-19 16:38:20 +000010858 if (!isa<OMPDeclareReductionDecl>(Underlying) &&
10859 !isa<OMPDeclareMapperDecl>(Underlying))
Alexey Bataev43b90b72018-09-12 16:31:59 +000010860 continue;
10861
10862 if (!SemaRef.isVisible(D)) {
10863 D = findAcceptableDecl(SemaRef, D);
10864 if (!D)
10865 continue;
10866 if (auto *USD = dyn_cast<UsingShadowDecl>(D))
10867 Underlying = USD->getTargetDecl();
10868 }
10869 Lookups.emplace_back();
10870 Lookups.back().addDecl(Underlying);
10871 }
10872 }
10873}
10874
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010875static ExprResult
10876buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
10877 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
10878 const DeclarationNameInfo &ReductionId, QualType Ty,
10879 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
10880 if (ReductionIdScopeSpec.isInvalid())
10881 return ExprError();
10882 SmallVector<UnresolvedSet<8>, 4> Lookups;
10883 if (S) {
10884 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
10885 Lookup.suppressDiagnostics();
10886 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010887 NamedDecl *D = Lookup.getRepresentativeDecl();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010888 do {
10889 S = S->getParent();
10890 } while (S && !S->isDeclScope(D));
10891 if (S)
10892 S = S->getParent();
Alexey Bataev43b90b72018-09-12 16:31:59 +000010893 Lookups.emplace_back();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010894 Lookups.back().append(Lookup.begin(), Lookup.end());
10895 Lookup.clear();
10896 }
10897 } else if (auto *ULE =
10898 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
10899 Lookups.push_back(UnresolvedSet<8>());
10900 Decl *PrevD = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000010901 for (NamedDecl *D : ULE->decls()) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010902 if (D == PrevD)
10903 Lookups.push_back(UnresolvedSet<8>());
Don Hintonf170dff2019-03-19 06:14:14 +000010904 else if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(D))
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010905 Lookups.back().addDecl(DRD);
10906 PrevD = D;
10907 }
10908 }
Alexey Bataevfdc20352017-08-25 15:43:55 +000010909 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() ||
10910 Ty->isInstantiationDependentType() ||
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010911 Ty->containsUnexpandedParameterPack() ||
Michael Kruse4304e9d2019-02-19 16:38:20 +000010912 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010913 return !D->isInvalidDecl() &&
10914 (D->getType()->isDependentType() ||
10915 D->getType()->isInstantiationDependentType() ||
10916 D->getType()->containsUnexpandedParameterPack());
10917 })) {
10918 UnresolvedSet<8> ResSet;
Alexey Bataeve3727102018-04-18 15:57:46 +000010919 for (const UnresolvedSet<8> &Set : Lookups) {
Alexey Bataev43b90b72018-09-12 16:31:59 +000010920 if (Set.empty())
10921 continue;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010922 ResSet.append(Set.begin(), Set.end());
10923 // The last item marks the end of all declarations at the specified scope.
10924 ResSet.addDecl(Set[Set.size() - 1]);
10925 }
10926 return UnresolvedLookupExpr::Create(
10927 SemaRef.Context, /*NamingClass=*/nullptr,
10928 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
10929 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
10930 }
Alexey Bataev43b90b72018-09-12 16:31:59 +000010931 // Lookup inside the classes.
10932 // C++ [over.match.oper]p3:
10933 // For a unary operator @ with an operand of a type whose
10934 // cv-unqualified version is T1, and for a binary operator @ with
10935 // a left operand of a type whose cv-unqualified version is T1 and
10936 // a right operand of a type whose cv-unqualified version is T2,
10937 // three sets of candidate functions, designated member
10938 // candidates, non-member candidates and built-in candidates, are
10939 // constructed as follows:
10940 // -- If T1 is a complete class type or a class currently being
10941 // defined, the set of member candidates is the result of the
10942 // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise,
10943 // the set of member candidates is empty.
10944 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
10945 Lookup.suppressDiagnostics();
10946 if (const auto *TyRec = Ty->getAs<RecordType>()) {
10947 // Complete the type if it can be completed.
10948 // If the type is neither complete nor being defined, bail out now.
10949 if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() ||
10950 TyRec->getDecl()->getDefinition()) {
10951 Lookup.clear();
10952 SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl());
10953 if (Lookup.empty()) {
10954 Lookups.emplace_back();
10955 Lookups.back().append(Lookup.begin(), Lookup.end());
10956 }
10957 }
10958 }
10959 // Perform ADL.
Alexey Bataev74a04e82019-03-13 19:31:34 +000010960 if (SemaRef.getLangOpts().CPlusPlus) {
10961 argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups);
10962 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
10963 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
10964 if (!D->isInvalidDecl() &&
10965 SemaRef.Context.hasSameType(D->getType(), Ty))
10966 return D;
10967 return nullptr;
10968 }))
10969 return SemaRef.BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(),
10970 VK_LValue, Loc);
10971 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
10972 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
10973 if (!D->isInvalidDecl() &&
10974 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
10975 !Ty.isMoreQualifiedThan(D->getType()))
10976 return D;
10977 return nullptr;
10978 })) {
10979 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
10980 /*DetectVirtual=*/false);
10981 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
10982 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
10983 VD->getType().getUnqualifiedType()))) {
10984 if (SemaRef.CheckBaseClassAccess(
10985 Loc, VD->getType(), Ty, Paths.front(),
10986 /*DiagID=*/0) != Sema::AR_inaccessible) {
10987 SemaRef.BuildBasePathArray(Paths, BasePath);
10988 return SemaRef.BuildDeclRefExpr(
10989 VD, VD->getType().getNonReferenceType(), VK_LValue, Loc);
10990 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010991 }
10992 }
10993 }
10994 }
10995 if (ReductionIdScopeSpec.isSet()) {
10996 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
10997 return ExprError();
10998 }
10999 return ExprEmpty();
11000}
11001
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011002namespace {
11003/// Data for the reduction-based clauses.
11004struct ReductionData {
11005 /// List of original reduction items.
11006 SmallVector<Expr *, 8> Vars;
11007 /// List of private copies of the reduction items.
11008 SmallVector<Expr *, 8> Privates;
11009 /// LHS expressions for the reduction_op expressions.
11010 SmallVector<Expr *, 8> LHSs;
11011 /// RHS expressions for the reduction_op expressions.
11012 SmallVector<Expr *, 8> RHSs;
11013 /// Reduction operation expression.
11014 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev88202be2017-07-27 13:20:36 +000011015 /// Taskgroup descriptors for the corresponding reduction items in
11016 /// in_reduction clauses.
11017 SmallVector<Expr *, 8> TaskgroupDescriptors;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011018 /// List of captures for clause.
11019 SmallVector<Decl *, 4> ExprCaptures;
11020 /// List of postupdate expressions.
11021 SmallVector<Expr *, 4> ExprPostUpdates;
11022 ReductionData() = delete;
11023 /// Reserves required memory for the reduction data.
11024 ReductionData(unsigned Size) {
11025 Vars.reserve(Size);
11026 Privates.reserve(Size);
11027 LHSs.reserve(Size);
11028 RHSs.reserve(Size);
11029 ReductionOps.reserve(Size);
Alexey Bataev88202be2017-07-27 13:20:36 +000011030 TaskgroupDescriptors.reserve(Size);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011031 ExprCaptures.reserve(Size);
11032 ExprPostUpdates.reserve(Size);
11033 }
11034 /// Stores reduction item and reduction operation only (required for dependent
11035 /// reduction item).
11036 void push(Expr *Item, Expr *ReductionOp) {
11037 Vars.emplace_back(Item);
11038 Privates.emplace_back(nullptr);
11039 LHSs.emplace_back(nullptr);
11040 RHSs.emplace_back(nullptr);
11041 ReductionOps.emplace_back(ReductionOp);
Alexey Bataev88202be2017-07-27 13:20:36 +000011042 TaskgroupDescriptors.emplace_back(nullptr);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011043 }
11044 /// Stores reduction data.
Alexey Bataev88202be2017-07-27 13:20:36 +000011045 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp,
11046 Expr *TaskgroupDescriptor) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011047 Vars.emplace_back(Item);
11048 Privates.emplace_back(Private);
11049 LHSs.emplace_back(LHS);
11050 RHSs.emplace_back(RHS);
11051 ReductionOps.emplace_back(ReductionOp);
Alexey Bataev88202be2017-07-27 13:20:36 +000011052 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011053 }
11054};
11055} // namespace
11056
Alexey Bataeve3727102018-04-18 15:57:46 +000011057static bool checkOMPArraySectionConstantForReduction(
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000011058 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement,
11059 SmallVectorImpl<llvm::APSInt> &ArraySizes) {
11060 const Expr *Length = OASE->getLength();
11061 if (Length == nullptr) {
11062 // For array sections of the form [1:] or [:], we would need to analyze
11063 // the lower bound...
11064 if (OASE->getColonLoc().isValid())
11065 return false;
11066
11067 // This is an array subscript which has implicit length 1!
11068 SingleElement = true;
11069 ArraySizes.push_back(llvm::APSInt::get(1));
11070 } else {
Fangrui Song407659a2018-11-30 23:41:18 +000011071 Expr::EvalResult Result;
11072 if (!Length->EvaluateAsInt(Result, Context))
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000011073 return false;
11074
Fangrui Song407659a2018-11-30 23:41:18 +000011075 llvm::APSInt ConstantLengthValue = Result.Val.getInt();
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000011076 SingleElement = (ConstantLengthValue.getSExtValue() == 1);
11077 ArraySizes.push_back(ConstantLengthValue);
11078 }
11079
11080 // Get the base of this array section and walk up from there.
11081 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
11082
11083 // We require length = 1 for all array sections except the right-most to
11084 // guarantee that the memory region is contiguous and has no holes in it.
11085 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) {
11086 Length = TempOASE->getLength();
11087 if (Length == nullptr) {
11088 // For array sections of the form [1:] or [:], we would need to analyze
11089 // the lower bound...
11090 if (OASE->getColonLoc().isValid())
11091 return false;
11092
11093 // This is an array subscript which has implicit length 1!
11094 ArraySizes.push_back(llvm::APSInt::get(1));
11095 } else {
Fangrui Song407659a2018-11-30 23:41:18 +000011096 Expr::EvalResult Result;
11097 if (!Length->EvaluateAsInt(Result, Context))
11098 return false;
11099
11100 llvm::APSInt ConstantLengthValue = Result.Val.getInt();
11101 if (ConstantLengthValue.getSExtValue() != 1)
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000011102 return false;
11103
11104 ArraySizes.push_back(ConstantLengthValue);
11105 }
11106 Base = TempOASE->getBase()->IgnoreParenImpCasts();
11107 }
11108
11109 // If we have a single element, we don't need to add the implicit lengths.
11110 if (!SingleElement) {
11111 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) {
11112 // Has implicit length 1!
11113 ArraySizes.push_back(llvm::APSInt::get(1));
11114 Base = TempASE->getBase()->IgnoreParenImpCasts();
11115 }
11116 }
11117
11118 // This array section can be privatized as a single value or as a constant
11119 // sized array.
11120 return true;
11121}
11122
Alexey Bataeve3727102018-04-18 15:57:46 +000011123static bool actOnOMPReductionKindClause(
Alexey Bataev169d96a2017-07-18 20:17:46 +000011124 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind,
11125 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11126 SourceLocation ColonLoc, SourceLocation EndLoc,
11127 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011128 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011129 DeclarationName DN = ReductionId.getName();
11130 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator();
Alexey Bataevc5e02582014-06-16 07:08:35 +000011131 BinaryOperatorKind BOK = BO_Comma;
11132
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011133 ASTContext &Context = S.Context;
Alexey Bataevc5e02582014-06-16 07:08:35 +000011134 // OpenMP [2.14.3.6, reduction clause]
11135 // C
11136 // reduction-identifier is either an identifier or one of the following
11137 // operators: +, -, *, &, |, ^, && and ||
11138 // C++
11139 // reduction-identifier is either an id-expression or one of the following
11140 // operators: +, -, *, &, |, ^, && and ||
Alexey Bataevc5e02582014-06-16 07:08:35 +000011141 switch (OOK) {
11142 case OO_Plus:
11143 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011144 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +000011145 break;
11146 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011147 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +000011148 break;
11149 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011150 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +000011151 break;
11152 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011153 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +000011154 break;
11155 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011156 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +000011157 break;
11158 case OO_AmpAmp:
11159 BOK = BO_LAnd;
11160 break;
11161 case OO_PipePipe:
11162 BOK = BO_LOr;
11163 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011164 case OO_New:
11165 case OO_Delete:
11166 case OO_Array_New:
11167 case OO_Array_Delete:
11168 case OO_Slash:
11169 case OO_Percent:
11170 case OO_Tilde:
11171 case OO_Exclaim:
11172 case OO_Equal:
11173 case OO_Less:
11174 case OO_Greater:
11175 case OO_LessEqual:
11176 case OO_GreaterEqual:
11177 case OO_PlusEqual:
11178 case OO_MinusEqual:
11179 case OO_StarEqual:
11180 case OO_SlashEqual:
11181 case OO_PercentEqual:
11182 case OO_CaretEqual:
11183 case OO_AmpEqual:
11184 case OO_PipeEqual:
11185 case OO_LessLess:
11186 case OO_GreaterGreater:
11187 case OO_LessLessEqual:
11188 case OO_GreaterGreaterEqual:
11189 case OO_EqualEqual:
11190 case OO_ExclaimEqual:
Richard Smithd30b23d2017-12-01 02:13:10 +000011191 case OO_Spaceship:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011192 case OO_PlusPlus:
11193 case OO_MinusMinus:
11194 case OO_Comma:
11195 case OO_ArrowStar:
11196 case OO_Arrow:
11197 case OO_Call:
11198 case OO_Subscript:
11199 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +000011200 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011201 case NUM_OVERLOADED_OPERATORS:
11202 llvm_unreachable("Unexpected reduction identifier");
11203 case OO_None:
Alexey Bataeve3727102018-04-18 15:57:46 +000011204 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000011205 if (II->isStr("max"))
11206 BOK = BO_GT;
11207 else if (II->isStr("min"))
11208 BOK = BO_LT;
11209 }
11210 break;
11211 }
11212 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011213 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +000011214 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataev4d4624c2017-07-20 16:47:47 +000011215 else
11216 ReductionIdRange.setBegin(ReductionId.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +000011217 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +000011218
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011219 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
11220 bool FirstIter = true;
Alexey Bataeve3727102018-04-18 15:57:46 +000011221 for (Expr *RefExpr : VarList) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000011222 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +000011223 // OpenMP [2.1, C/C++]
11224 // A list item is a variable or array section, subject to the restrictions
11225 // specified in Section 2.4 on page 42 and in each of the sections
11226 // describing clauses and directives for which a list appears.
11227 // OpenMP [2.14.3.3, Restrictions, p.1]
11228 // A variable that is part of another variable (as an array or
11229 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011230 if (!FirstIter && IR != ER)
11231 ++IR;
11232 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +000011233 SourceLocation ELoc;
11234 SourceRange ERange;
11235 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011236 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
Alexey Bataev60da77e2016-02-29 05:54:20 +000011237 /*AllowArraySection=*/true);
11238 if (Res.second) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011239 // Try to find 'declare reduction' corresponding construct before using
11240 // builtin/overloaded operators.
11241 QualType Type = Context.DependentTy;
11242 CXXCastPath BasePath;
11243 ExprResult DeclareReductionRef = buildDeclareReductionRef(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011244 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011245 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011246 Expr *ReductionOp = nullptr;
11247 if (S.CurContext->isDependentContext() &&
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011248 (DeclareReductionRef.isUnset() ||
11249 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011250 ReductionOp = DeclareReductionRef.get();
11251 // It will be analyzed later.
11252 RD.push(RefExpr, ReductionOp);
Alexey Bataevc5e02582014-06-16 07:08:35 +000011253 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000011254 ValueDecl *D = Res.first;
11255 if (!D)
11256 continue;
11257
Alexey Bataev88202be2017-07-27 13:20:36 +000011258 Expr *TaskgroupDescriptor = nullptr;
Alexey Bataeva1764212015-09-30 09:22:36 +000011259 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +000011260 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
11261 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
Alexey Bataeve3727102018-04-18 15:57:46 +000011262 if (ASE) {
Alexey Bataev31300ed2016-02-04 11:27:03 +000011263 Type = ASE->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000011264 } else if (OASE) {
11265 QualType BaseType =
11266 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
11267 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
Alexey Bataeva1764212015-09-30 09:22:36 +000011268 Type = ATy->getElementType();
11269 else
11270 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +000011271 Type = Type.getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000011272 } else {
Alexey Bataev60da77e2016-02-29 05:54:20 +000011273 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
Alexey Bataeve3727102018-04-18 15:57:46 +000011274 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000011275 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +000011276
Alexey Bataevc5e02582014-06-16 07:08:35 +000011277 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
11278 // A variable that appears in a private clause must not have an incomplete
11279 // type or a reference type.
Joel E. Denny3cabf732018-06-28 19:54:49 +000011280 if (S.RequireCompleteType(ELoc, D->getType(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011281 diag::err_omp_reduction_incomplete_type))
Alexey Bataevc5e02582014-06-16 07:08:35 +000011282 continue;
11283 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +000011284 // A list item that appears in a reduction clause must not be
11285 // const-qualified.
Joel E. Dennyd2649292019-01-04 22:11:56 +000011286 if (rejectConstNotMutableType(S, D, Type, ClauseKind, ELoc,
11287 /*AcceptIfMutable*/ false, ASE || OASE))
Alexey Bataevc5e02582014-06-16 07:08:35 +000011288 continue;
Alexey Bataevbc529672018-09-28 19:33:14 +000011289
11290 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective();
Alexey Bataevc5e02582014-06-16 07:08:35 +000011291 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
11292 // If a list-item is a reference type then it must bind to the same object
11293 // for all threads of the team.
Alexey Bataevbc529672018-09-28 19:33:14 +000011294 if (!ASE && !OASE) {
11295 if (VD) {
11296 VarDecl *VDDef = VD->getDefinition();
11297 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) {
11298 DSARefChecker Check(Stack);
11299 if (Check.Visit(VDDef->getInit())) {
11300 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg)
11301 << getOpenMPClauseName(ClauseKind) << ERange;
11302 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
11303 continue;
11304 }
Alexey Bataeva1764212015-09-30 09:22:36 +000011305 }
Alexey Bataevc5e02582014-06-16 07:08:35 +000011306 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011307
Alexey Bataevbc529672018-09-28 19:33:14 +000011308 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
11309 // in a Construct]
11310 // Variables with the predetermined data-sharing attributes may not be
11311 // listed in data-sharing attributes clauses, except for the cases
11312 // listed below. For these exceptions only, listing a predetermined
11313 // variable in a data-sharing attribute clause is allowed and overrides
11314 // the variable's predetermined data-sharing attributes.
11315 // OpenMP [2.14.3.6, Restrictions, p.3]
11316 // Any number of reduction clauses can be specified on the directive,
11317 // but a list item can appear only once in the reduction clauses for that
11318 // directive.
11319 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false);
11320 if (DVar.CKind == OMPC_reduction) {
11321 S.Diag(ELoc, diag::err_omp_once_referenced)
11322 << getOpenMPClauseName(ClauseKind);
11323 if (DVar.RefExpr)
11324 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
11325 continue;
11326 }
11327 if (DVar.CKind != OMPC_unknown) {
11328 S.Diag(ELoc, diag::err_omp_wrong_dsa)
11329 << getOpenMPClauseName(DVar.CKind)
11330 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataeve3727102018-04-18 15:57:46 +000011331 reportOriginalDsa(S, Stack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011332 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +000011333 }
Alexey Bataevbc529672018-09-28 19:33:14 +000011334
11335 // OpenMP [2.14.3.6, Restrictions, p.1]
11336 // A list item that appears in a reduction clause of a worksharing
11337 // construct must be shared in the parallel regions to which any of the
11338 // worksharing regions arising from the worksharing construct bind.
11339 if (isOpenMPWorksharingDirective(CurrDir) &&
11340 !isOpenMPParallelDirective(CurrDir) &&
11341 !isOpenMPTeamsDirective(CurrDir)) {
11342 DVar = Stack->getImplicitDSA(D, true);
11343 if (DVar.CKind != OMPC_shared) {
11344 S.Diag(ELoc, diag::err_omp_required_access)
11345 << getOpenMPClauseName(OMPC_reduction)
11346 << getOpenMPClauseName(OMPC_shared);
11347 reportOriginalDsa(S, Stack, D, DVar);
11348 continue;
11349 }
11350 }
Alexey Bataevf29276e2014-06-18 04:14:57 +000011351 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011352
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011353 // Try to find 'declare reduction' corresponding construct before using
11354 // builtin/overloaded operators.
11355 CXXCastPath BasePath;
11356 ExprResult DeclareReductionRef = buildDeclareReductionRef(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011357 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011358 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
11359 if (DeclareReductionRef.isInvalid())
11360 continue;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011361 if (S.CurContext->isDependentContext() &&
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011362 (DeclareReductionRef.isUnset() ||
11363 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011364 RD.push(RefExpr, DeclareReductionRef.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011365 continue;
11366 }
11367 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
11368 // Not allowed reduction identifier is found.
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011369 S.Diag(ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011370 diag::err_omp_unknown_reduction_identifier)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011371 << Type << ReductionIdRange;
11372 continue;
11373 }
11374
11375 // OpenMP [2.14.3.6, reduction clause, Restrictions]
11376 // The type of a list item that appears in a reduction clause must be valid
11377 // for the reduction-identifier. For a max or min reduction in C, the type
11378 // of the list item must be an allowed arithmetic data type: char, int,
11379 // float, double, or _Bool, possibly modified with long, short, signed, or
11380 // unsigned. For a max or min reduction in C++, the type of the list item
11381 // must be an allowed arithmetic data type: char, wchar_t, int, float,
11382 // double, or bool, possibly modified with long, short, signed, or unsigned.
11383 if (DeclareReductionRef.isUnset()) {
11384 if ((BOK == BO_GT || BOK == BO_LT) &&
11385 !(Type->isScalarType() ||
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011386 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
11387 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
Alexey Bataev169d96a2017-07-18 20:17:46 +000011388 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011389 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011390 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
11391 VarDecl::DeclarationOnly;
11392 S.Diag(D->getLocation(),
11393 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011394 << D;
11395 }
11396 continue;
11397 }
11398 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011399 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) {
Alexey Bataev169d96a2017-07-18 20:17:46 +000011400 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg)
11401 << getOpenMPClauseName(ClauseKind);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011402 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011403 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
11404 VarDecl::DeclarationOnly;
11405 S.Diag(D->getLocation(),
11406 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011407 << D;
11408 }
11409 continue;
11410 }
11411 }
11412
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011413 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000011414 VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs",
11415 D->hasAttrs() ? &D->getAttrs() : nullptr);
11416 VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(),
11417 D->hasAttrs() ? &D->getAttrs() : nullptr);
11418 QualType PrivateTy = Type;
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000011419
11420 // Try if we can determine constant lengths for all array sections and avoid
11421 // the VLA.
11422 bool ConstantLengthOASE = false;
11423 if (OASE) {
11424 bool SingleElement;
11425 llvm::SmallVector<llvm::APSInt, 4> ArraySizes;
Alexey Bataeve3727102018-04-18 15:57:46 +000011426 ConstantLengthOASE = checkOMPArraySectionConstantForReduction(
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000011427 Context, OASE, SingleElement, ArraySizes);
11428
11429 // If we don't have a single element, we must emit a constant array type.
11430 if (ConstantLengthOASE && !SingleElement) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011431 for (llvm::APSInt &Size : ArraySizes)
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000011432 PrivateTy = Context.getConstantArrayType(
11433 PrivateTy, Size, ArrayType::Normal, /*IndexTypeQuals=*/0);
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000011434 }
11435 }
11436
11437 if ((OASE && !ConstantLengthOASE) ||
Jonas Hahnfeld96087f32017-11-02 13:30:42 +000011438 (!OASE && !ASE &&
Alexey Bataev60da77e2016-02-29 05:54:20 +000011439 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Jonas Hahnfeld87d44262017-11-18 21:00:46 +000011440 if (!Context.getTargetInfo().isVLASupported() &&
11441 S.shouldDiagnoseTargetSupportFromOpenMP()) {
11442 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE;
11443 S.Diag(ELoc, diag::note_vla_unsupported);
11444 continue;
11445 }
David Majnemer9d168222016-08-05 17:44:54 +000011446 // For arrays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011447 // Create pseudo array type for private copy. The size for this array will
11448 // be generated during codegen.
11449 // For array subscripts or single variables Private Ty is the same as Type
11450 // (type of the variable or single array element).
11451 PrivateTy = Context.getVariableArrayType(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011452 Type,
Alexey Bataevd070a582017-10-25 15:54:04 +000011453 new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue),
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011454 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +000011455 } else if (!ASE && !OASE &&
Alexey Bataeve3727102018-04-18 15:57:46 +000011456 Context.getAsArrayType(D->getType().getNonReferenceType())) {
Alexey Bataev60da77e2016-02-29 05:54:20 +000011457 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000011458 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011459 // Private copy.
Alexey Bataeve3727102018-04-18 15:57:46 +000011460 VarDecl *PrivateVD =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000011461 buildVarDecl(S, ELoc, PrivateTy, D->getName(),
11462 D->hasAttrs() ? &D->getAttrs() : nullptr,
11463 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011464 // Add initializer for private variable.
11465 Expr *Init = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000011466 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc);
11467 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011468 if (DeclareReductionRef.isUsable()) {
11469 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
11470 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
11471 if (DRD->getInitializer()) {
11472 Init = DRDRef;
11473 RHSVD->setInit(DRDRef);
11474 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011475 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011476 } else {
11477 switch (BOK) {
11478 case BO_Add:
11479 case BO_Xor:
11480 case BO_Or:
11481 case BO_LOr:
11482 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
11483 if (Type->isScalarType() || Type->isAnyComplexType())
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011484 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011485 break;
11486 case BO_Mul:
11487 case BO_LAnd:
11488 if (Type->isScalarType() || Type->isAnyComplexType()) {
11489 // '*' and '&&' reduction ops - initializer is '1'.
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011490 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +000011491 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011492 break;
11493 case BO_And: {
11494 // '&' reduction op - initializer is '~0'.
11495 QualType OrigType = Type;
11496 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
11497 Type = ComplexTy->getElementType();
11498 if (Type->isRealFloatingType()) {
11499 llvm::APFloat InitValue =
11500 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
11501 /*isIEEE=*/true);
11502 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
11503 Type, ELoc);
11504 } else if (Type->isScalarType()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011505 uint64_t Size = Context.getTypeSize(Type);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011506 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
11507 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
11508 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
11509 }
11510 if (Init && OrigType->isAnyComplexType()) {
11511 // Init = 0xFFFF + 0xFFFFi;
11512 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011513 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011514 }
11515 Type = OrigType;
11516 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011517 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011518 case BO_LT:
11519 case BO_GT: {
11520 // 'min' reduction op - initializer is 'Largest representable number in
11521 // the reduction list item type'.
11522 // 'max' reduction op - initializer is 'Least representable number in
11523 // the reduction list item type'.
11524 if (Type->isIntegerType() || Type->isPointerType()) {
11525 bool IsSigned = Type->hasSignedIntegerRepresentation();
Alexey Bataeve3727102018-04-18 15:57:46 +000011526 uint64_t Size = Context.getTypeSize(Type);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011527 QualType IntTy =
11528 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
11529 llvm::APInt InitValue =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011530 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
11531 : llvm::APInt::getMinValue(Size)
11532 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
11533 : llvm::APInt::getMaxValue(Size);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011534 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
11535 if (Type->isPointerType()) {
11536 // Cast to pointer type.
Alexey Bataeve3727102018-04-18 15:57:46 +000011537 ExprResult CastExpr = S.BuildCStyleCastExpr(
Alexey Bataevd070a582017-10-25 15:54:04 +000011538 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011539 if (CastExpr.isInvalid())
11540 continue;
11541 Init = CastExpr.get();
11542 }
11543 } else if (Type->isRealFloatingType()) {
11544 llvm::APFloat InitValue = llvm::APFloat::getLargest(
11545 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
11546 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
11547 Type, ELoc);
11548 }
11549 break;
11550 }
11551 case BO_PtrMemD:
11552 case BO_PtrMemI:
11553 case BO_MulAssign:
11554 case BO_Div:
11555 case BO_Rem:
11556 case BO_Sub:
11557 case BO_Shl:
11558 case BO_Shr:
11559 case BO_LE:
11560 case BO_GE:
11561 case BO_EQ:
11562 case BO_NE:
Richard Smithc70f1d62017-12-14 15:16:18 +000011563 case BO_Cmp:
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011564 case BO_AndAssign:
11565 case BO_XorAssign:
11566 case BO_OrAssign:
11567 case BO_Assign:
11568 case BO_AddAssign:
11569 case BO_SubAssign:
11570 case BO_DivAssign:
11571 case BO_RemAssign:
11572 case BO_ShlAssign:
11573 case BO_ShrAssign:
11574 case BO_Comma:
11575 llvm_unreachable("Unexpected reduction operation");
11576 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011577 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011578 if (Init && DeclareReductionRef.isUnset())
11579 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false);
11580 else if (!Init)
11581 S.ActOnUninitializedDecl(RHSVD);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011582 if (RHSVD->isInvalidDecl())
11583 continue;
11584 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011585 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible)
11586 << Type << ReductionIdRange;
11587 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
11588 VarDecl::DeclarationOnly;
11589 S.Diag(D->getLocation(),
11590 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +000011591 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011592 continue;
11593 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011594 // Store initializer for single element in private copy. Will be used during
11595 // codegen.
11596 PrivateVD->setInit(RHSVD->getInit());
11597 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataeve3727102018-04-18 15:57:46 +000011598 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011599 ExprResult ReductionOp;
11600 if (DeclareReductionRef.isUsable()) {
11601 QualType RedTy = DeclareReductionRef.get()->getType();
11602 QualType PtrRedTy = Context.getPointerType(RedTy);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011603 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
11604 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011605 if (!BasePath.empty()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011606 LHS = S.DefaultLvalueConversion(LHS.get());
11607 RHS = S.DefaultLvalueConversion(RHS.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011608 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
11609 CK_UncheckedDerivedToBase, LHS.get(),
11610 &BasePath, LHS.get()->getValueKind());
11611 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
11612 CK_UncheckedDerivedToBase, RHS.get(),
11613 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011614 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011615 FunctionProtoType::ExtProtoInfo EPI;
11616 QualType Params[] = {PtrRedTy, PtrRedTy};
11617 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
11618 auto *OVE = new (Context) OpaqueValueExpr(
11619 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011620 S.DefaultLvalueConversion(DeclareReductionRef.get()).get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011621 Expr *Args[] = {LHS.get(), RHS.get()};
Bruno Riccic5885cf2018-12-21 15:20:32 +000011622 ReductionOp =
11623 CallExpr::Create(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011624 } else {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011625 ReductionOp = S.BuildBinOp(
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011626 Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, LHSDRE, RHSDRE);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011627 if (ReductionOp.isUsable()) {
11628 if (BOK != BO_LT && BOK != BO_GT) {
11629 ReductionOp =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011630 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011631 BO_Assign, LHSDRE, ReductionOp.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011632 } else {
Alexey Bataevd070a582017-10-25 15:54:04 +000011633 auto *ConditionalOp = new (Context)
11634 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE,
11635 Type, VK_LValue, OK_Ordinary);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011636 ReductionOp =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011637 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011638 BO_Assign, LHSDRE, ConditionalOp);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011639 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +000011640 if (ReductionOp.isUsable())
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000011641 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get(),
11642 /*DiscardedValue*/ false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011643 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +000011644 if (!ReductionOp.isUsable())
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011645 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +000011646 }
11647
Alexey Bataevfa312f32017-07-21 18:48:21 +000011648 // OpenMP [2.15.4.6, Restrictions, p.2]
11649 // A list item that appears in an in_reduction clause of a task construct
11650 // must appear in a task_reduction clause of a construct associated with a
11651 // taskgroup region that includes the participating task in its taskgroup
11652 // set. The construct associated with the innermost region that meets this
11653 // condition must specify the same reduction-identifier as the in_reduction
11654 // clause.
11655 if (ClauseKind == OMPC_in_reduction) {
Alexey Bataevfa312f32017-07-21 18:48:21 +000011656 SourceRange ParentSR;
11657 BinaryOperatorKind ParentBOK;
11658 const Expr *ParentReductionOp;
Alexey Bataev88202be2017-07-27 13:20:36 +000011659 Expr *ParentBOKTD, *ParentReductionOpTD;
Alexey Bataevf189cb72017-07-24 14:52:13 +000011660 DSAStackTy::DSAVarData ParentBOKDSA =
Alexey Bataev88202be2017-07-27 13:20:36 +000011661 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK,
11662 ParentBOKTD);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011663 DSAStackTy::DSAVarData ParentReductionOpDSA =
Alexey Bataev88202be2017-07-27 13:20:36 +000011664 Stack->getTopMostTaskgroupReductionData(
11665 D, ParentSR, ParentReductionOp, ParentReductionOpTD);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011666 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown;
11667 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown;
11668 if (!IsParentBOK && !IsParentReductionOp) {
11669 S.Diag(ELoc, diag::err_omp_in_reduction_not_task_reduction);
11670 continue;
11671 }
Alexey Bataevfa312f32017-07-21 18:48:21 +000011672 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) ||
11673 (DeclareReductionRef.isUsable() && IsParentBOK) || BOK != ParentBOK ||
11674 IsParentReductionOp) {
11675 bool EmitError = true;
11676 if (IsParentReductionOp && DeclareReductionRef.isUsable()) {
11677 llvm::FoldingSetNodeID RedId, ParentRedId;
11678 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true);
11679 DeclareReductionRef.get()->Profile(RedId, Context,
11680 /*Canonical=*/true);
11681 EmitError = RedId != ParentRedId;
11682 }
11683 if (EmitError) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011684 S.Diag(ReductionId.getBeginLoc(),
Alexey Bataevfa312f32017-07-21 18:48:21 +000011685 diag::err_omp_reduction_identifier_mismatch)
11686 << ReductionIdRange << RefExpr->getSourceRange();
11687 S.Diag(ParentSR.getBegin(),
11688 diag::note_omp_previous_reduction_identifier)
Alexey Bataevf189cb72017-07-24 14:52:13 +000011689 << ParentSR
11690 << (IsParentBOK ? ParentBOKDSA.RefExpr
11691 : ParentReductionOpDSA.RefExpr)
11692 ->getSourceRange();
Alexey Bataevfa312f32017-07-21 18:48:21 +000011693 continue;
11694 }
11695 }
Alexey Bataev88202be2017-07-27 13:20:36 +000011696 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD;
11697 assert(TaskgroupDescriptor && "Taskgroup descriptor must be defined.");
Alexey Bataevfa312f32017-07-21 18:48:21 +000011698 }
11699
Alexey Bataev60da77e2016-02-29 05:54:20 +000011700 DeclRefExpr *Ref = nullptr;
11701 Expr *VarsExpr = RefExpr->IgnoreParens();
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011702 if (!VD && !S.CurContext->isDependentContext()) {
Alexey Bataev60da77e2016-02-29 05:54:20 +000011703 if (ASE || OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011704 TransformExprToCaptures RebuildToCapture(S, D);
Alexey Bataev60da77e2016-02-29 05:54:20 +000011705 VarsExpr =
11706 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
11707 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +000011708 } else {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011709 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +000011710 }
Alexey Bataeve3727102018-04-18 15:57:46 +000011711 if (!S.isOpenMPCapturedDecl(D)) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011712 RD.ExprCaptures.emplace_back(Ref->getDecl());
Alexey Bataev5a3af132016-03-29 08:58:54 +000011713 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011714 ExprResult RefRes = S.DefaultLvalueConversion(Ref);
Alexey Bataev5a3af132016-03-29 08:58:54 +000011715 if (!RefRes.isUsable())
11716 continue;
11717 ExprResult PostUpdateRes =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011718 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
11719 RefRes.get());
Alexey Bataev5a3af132016-03-29 08:58:54 +000011720 if (!PostUpdateRes.isUsable())
11721 continue;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011722 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
11723 Stack->getCurrentDirective() == OMPD_taskgroup) {
11724 S.Diag(RefExpr->getExprLoc(),
11725 diag::err_omp_reduction_non_addressable_expression)
Alexey Bataevbcd0ae02017-07-11 19:16:44 +000011726 << RefExpr->getSourceRange();
11727 continue;
11728 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011729 RD.ExprPostUpdates.emplace_back(
11730 S.IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +000011731 }
11732 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000011733 }
Alexey Bataev169d96a2017-07-18 20:17:46 +000011734 // All reduction items are still marked as reduction (to do not increase
11735 // code base size).
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011736 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011737 if (CurrDir == OMPD_taskgroup) {
11738 if (DeclareReductionRef.isUsable())
Alexey Bataev3b1b8952017-07-25 15:53:26 +000011739 Stack->addTaskgroupReductionData(D, ReductionIdRange,
11740 DeclareReductionRef.get());
Alexey Bataevf189cb72017-07-24 14:52:13 +000011741 else
Alexey Bataev3b1b8952017-07-25 15:53:26 +000011742 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011743 }
Alexey Bataev88202be2017-07-27 13:20:36 +000011744 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(),
11745 TaskgroupDescriptor);
Alexey Bataevc5e02582014-06-16 07:08:35 +000011746 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011747 return RD.Vars.empty();
11748}
Alexey Bataevc5e02582014-06-16 07:08:35 +000011749
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011750OMPClause *Sema::ActOnOpenMPReductionClause(
11751 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11752 SourceLocation ColonLoc, SourceLocation EndLoc,
11753 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11754 ArrayRef<Expr *> UnresolvedReductions) {
11755 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000011756 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList,
Alexey Bataev169d96a2017-07-18 20:17:46 +000011757 StartLoc, LParenLoc, ColonLoc, EndLoc,
11758 ReductionIdScopeSpec, ReductionId,
11759 UnresolvedReductions, RD))
Alexey Bataevc5e02582014-06-16 07:08:35 +000011760 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +000011761
Alexey Bataevc5e02582014-06-16 07:08:35 +000011762 return OMPReductionClause::Create(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011763 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11764 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
11765 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
11766 buildPreInits(Context, RD.ExprCaptures),
11767 buildPostUpdate(*this, RD.ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +000011768}
11769
Alexey Bataev169d96a2017-07-18 20:17:46 +000011770OMPClause *Sema::ActOnOpenMPTaskReductionClause(
11771 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11772 SourceLocation ColonLoc, SourceLocation EndLoc,
11773 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11774 ArrayRef<Expr *> UnresolvedReductions) {
11775 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000011776 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList,
11777 StartLoc, LParenLoc, ColonLoc, EndLoc,
11778 ReductionIdScopeSpec, ReductionId,
Alexey Bataev169d96a2017-07-18 20:17:46 +000011779 UnresolvedReductions, RD))
11780 return nullptr;
11781
11782 return OMPTaskReductionClause::Create(
11783 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11784 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
11785 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
11786 buildPreInits(Context, RD.ExprCaptures),
11787 buildPostUpdate(*this, RD.ExprPostUpdates));
11788}
11789
Alexey Bataevfa312f32017-07-21 18:48:21 +000011790OMPClause *Sema::ActOnOpenMPInReductionClause(
11791 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11792 SourceLocation ColonLoc, SourceLocation EndLoc,
11793 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11794 ArrayRef<Expr *> UnresolvedReductions) {
11795 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000011796 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList,
Alexey Bataevfa312f32017-07-21 18:48:21 +000011797 StartLoc, LParenLoc, ColonLoc, EndLoc,
11798 ReductionIdScopeSpec, ReductionId,
11799 UnresolvedReductions, RD))
11800 return nullptr;
11801
11802 return OMPInReductionClause::Create(
11803 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11804 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
Alexey Bataev88202be2017-07-27 13:20:36 +000011805 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors,
Alexey Bataevfa312f32017-07-21 18:48:21 +000011806 buildPreInits(Context, RD.ExprCaptures),
11807 buildPostUpdate(*this, RD.ExprPostUpdates));
11808}
11809
Alexey Bataevecba70f2016-04-12 11:02:11 +000011810bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
11811 SourceLocation LinLoc) {
11812 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
11813 LinKind == OMPC_LINEAR_unknown) {
11814 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
11815 return true;
11816 }
11817 return false;
11818}
11819
Alexey Bataeve3727102018-04-18 15:57:46 +000011820bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc,
Alexey Bataevecba70f2016-04-12 11:02:11 +000011821 OpenMPLinearClauseKind LinKind,
11822 QualType Type) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011823 const auto *VD = dyn_cast_or_null<VarDecl>(D);
Alexey Bataevecba70f2016-04-12 11:02:11 +000011824 // A variable must not have an incomplete type or a reference type.
11825 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
11826 return true;
11827 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
11828 !Type->isReferenceType()) {
11829 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
11830 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
11831 return true;
11832 }
11833 Type = Type.getNonReferenceType();
11834
Joel E. Dennybae586f2019-01-04 22:12:13 +000011835 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
11836 // A variable that is privatized must not have a const-qualified type
11837 // unless it is of class type with a mutable member. This restriction does
11838 // not apply to the firstprivate clause.
11839 if (rejectConstNotMutableType(*this, D, Type, OMPC_linear, ELoc))
Alexey Bataevecba70f2016-04-12 11:02:11 +000011840 return true;
Alexey Bataevecba70f2016-04-12 11:02:11 +000011841
11842 // A list item must be of integral or pointer type.
11843 Type = Type.getUnqualifiedType().getCanonicalType();
11844 const auto *Ty = Type.getTypePtrOrNull();
11845 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
11846 !Ty->isPointerType())) {
11847 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
11848 if (D) {
11849 bool IsDecl =
11850 !VD ||
11851 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
11852 Diag(D->getLocation(),
11853 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
11854 << D;
11855 }
11856 return true;
11857 }
11858 return false;
11859}
11860
Alexey Bataev182227b2015-08-20 10:54:39 +000011861OMPClause *Sema::ActOnOpenMPLinearClause(
11862 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
11863 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
11864 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +000011865 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011866 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +000011867 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +000011868 SmallVector<Decl *, 4> ExprCaptures;
11869 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +000011870 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +000011871 LinKind = OMPC_LINEAR_val;
Alexey Bataeve3727102018-04-18 15:57:46 +000011872 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000011873 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011874 SourceLocation ELoc;
11875 SourceRange ERange;
11876 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011877 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011878 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +000011879 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000011880 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011881 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +000011882 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +000011883 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011884 ValueDecl *D = Res.first;
11885 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +000011886 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +000011887
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011888 QualType Type = D->getType();
11889 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +000011890
11891 // OpenMP [2.14.3.7, linear clause]
11892 // A list-item cannot appear in more than one linear clause.
11893 // A list-item that appears in a linear clause cannot appear in any
11894 // other data-sharing attribute clause.
Alexey Bataeve3727102018-04-18 15:57:46 +000011895 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexander Musman8dba6642014-04-22 13:09:42 +000011896 if (DVar.RefExpr) {
11897 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
11898 << getOpenMPClauseName(OMPC_linear);
Alexey Bataeve3727102018-04-18 15:57:46 +000011899 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +000011900 continue;
11901 }
11902
Alexey Bataevecba70f2016-04-12 11:02:11 +000011903 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +000011904 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +000011905 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +000011906
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011907 // Build private copy of original var.
Alexey Bataeve3727102018-04-18 15:57:46 +000011908 VarDecl *Private =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000011909 buildVarDecl(*this, ELoc, Type, D->getName(),
11910 D->hasAttrs() ? &D->getAttrs() : nullptr,
11911 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011912 DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +000011913 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011914 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011915 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011916 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000011917 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev78849fb2016-03-09 09:49:00 +000011918 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011919 if (!isOpenMPCapturedDecl(D)) {
Alexey Bataev78849fb2016-03-09 09:49:00 +000011920 ExprCaptures.push_back(Ref->getDecl());
11921 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
11922 ExprResult RefRes = DefaultLvalueConversion(Ref);
11923 if (!RefRes.isUsable())
11924 continue;
11925 ExprResult PostUpdateRes =
11926 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
11927 SimpleRefExpr, RefRes.get());
11928 if (!PostUpdateRes.isUsable())
11929 continue;
11930 ExprPostUpdates.push_back(
11931 IgnoredValueConversions(PostUpdateRes.get()).get());
11932 }
11933 }
11934 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011935 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011936 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011937 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011938 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011939 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000011940 /*DirectInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011941 DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011942
11943 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000011944 Vars.push_back((VD || CurContext->isDependentContext())
11945 ? RefExpr->IgnoreParens()
11946 : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011947 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +000011948 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +000011949 }
11950
11951 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000011952 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +000011953
11954 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +000011955 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +000011956 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
11957 !Step->isInstantiationDependent() &&
11958 !Step->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011959 SourceLocation StepLoc = Step->getBeginLoc();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +000011960 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +000011961 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000011962 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +000011963 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +000011964
Alexander Musman3276a272015-03-21 10:12:56 +000011965 // Build var to save the step value.
11966 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +000011967 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +000011968 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +000011969 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +000011970 ExprResult CalcStep =
11971 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000011972 CalcStep = ActOnFinishFullExpr(CalcStep.get(), /*DiscardedValue*/ false);
Alexander Musman3276a272015-03-21 10:12:56 +000011973
Alexander Musman8dba6642014-04-22 13:09:42 +000011974 // Warn about zero linear step (it would be probably better specified as
11975 // making corresponding variables 'const').
11976 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +000011977 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
11978 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +000011979 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
11980 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +000011981 if (!IsConstant && CalcStep.isUsable()) {
11982 // Calculate the step beforehand instead of doing this on each iteration.
11983 // (This is not used if the number of iterations may be kfold-ed).
11984 CalcStepExpr = CalcStep.get();
11985 }
Alexander Musman8dba6642014-04-22 13:09:42 +000011986 }
11987
Alexey Bataev182227b2015-08-20 10:54:39 +000011988 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
11989 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +000011990 StepExpr, CalcStepExpr,
11991 buildPreInits(Context, ExprCaptures),
11992 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +000011993}
11994
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011995static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
11996 Expr *NumIterations, Sema &SemaRef,
11997 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +000011998 // Walk the vars and build update/final expressions for the CodeGen.
11999 SmallVector<Expr *, 8> Updates;
12000 SmallVector<Expr *, 8> Finals;
12001 Expr *Step = Clause.getStep();
12002 Expr *CalcStep = Clause.getCalcStep();
12003 // OpenMP [2.14.3.7, linear clause]
12004 // If linear-step is not specified it is assumed to be 1.
Alexey Bataeve3727102018-04-18 15:57:46 +000012005 if (!Step)
Alexander Musman3276a272015-03-21 10:12:56 +000012006 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000012007 else if (CalcStep)
Alexander Musman3276a272015-03-21 10:12:56 +000012008 Step = cast<BinaryOperator>(CalcStep)->getLHS();
12009 bool HasErrors = false;
12010 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +000012011 auto CurPrivate = Clause.privates().begin();
Alexey Bataeve3727102018-04-18 15:57:46 +000012012 OpenMPLinearClauseKind LinKind = Clause.getModifier();
12013 for (Expr *RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +000012014 SourceLocation ELoc;
12015 SourceRange ERange;
12016 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000012017 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange);
Alexey Bataev5dff95c2016-04-22 03:56:56 +000012018 ValueDecl *D = Res.first;
12019 if (Res.second || !D) {
12020 Updates.push_back(nullptr);
12021 Finals.push_back(nullptr);
12022 HasErrors = true;
12023 continue;
12024 }
Alexey Bataev5dff95c2016-04-22 03:56:56 +000012025 auto &&Info = Stack->isLoopControlVariable(D);
Alexey Bataev2b86f212017-11-29 21:31:48 +000012026 // OpenMP [2.15.11, distribute simd Construct]
12027 // A list item may not appear in a linear clause, unless it is the loop
12028 // iteration variable.
12029 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) &&
12030 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) {
12031 SemaRef.Diag(ELoc,
12032 diag::err_omp_linear_distribute_var_non_loop_iteration);
12033 Updates.push_back(nullptr);
12034 Finals.push_back(nullptr);
12035 HasErrors = true;
12036 continue;
12037 }
Alexander Musman3276a272015-03-21 10:12:56 +000012038 Expr *InitExpr = *CurInit;
12039
12040 // Build privatized reference to the current linear var.
David Majnemer9d168222016-08-05 17:44:54 +000012041 auto *DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000012042 Expr *CapturedRef;
12043 if (LinKind == OMPC_LINEAR_uval)
12044 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
12045 else
12046 CapturedRef =
12047 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
12048 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
12049 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +000012050
12051 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000012052 ExprResult Update;
Alexey Bataeve3727102018-04-18 15:57:46 +000012053 if (!Info.first)
Alexey Bataev5dff95c2016-04-22 03:56:56 +000012054 Update =
Alexey Bataeve3727102018-04-18 15:57:46 +000012055 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexey Bataev5dff95c2016-04-22 03:56:56 +000012056 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataeve3727102018-04-18 15:57:46 +000012057 else
Alexey Bataev5dff95c2016-04-22 03:56:56 +000012058 Update = *CurPrivate;
Stephen Kellyf2ceec42018-08-09 21:08:08 +000012059 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000012060 /*DiscardedValue*/ false);
Alexander Musman3276a272015-03-21 10:12:56 +000012061
12062 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000012063 ExprResult Final;
Alexey Bataeve3727102018-04-18 15:57:46 +000012064 if (!Info.first)
12065 Final =
12066 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
12067 InitExpr, NumIterations, Step, /*Subtract=*/false);
12068 else
Alexey Bataev5dff95c2016-04-22 03:56:56 +000012069 Final = *CurPrivate;
Stephen Kellyf2ceec42018-08-09 21:08:08 +000012070 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000012071 /*DiscardedValue*/ false);
Alexey Bataev5dff95c2016-04-22 03:56:56 +000012072
Alexander Musman3276a272015-03-21 10:12:56 +000012073 if (!Update.isUsable() || !Final.isUsable()) {
12074 Updates.push_back(nullptr);
12075 Finals.push_back(nullptr);
12076 HasErrors = true;
12077 } else {
12078 Updates.push_back(Update.get());
12079 Finals.push_back(Final.get());
12080 }
Richard Trieucc3949d2016-02-18 22:34:54 +000012081 ++CurInit;
12082 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +000012083 }
12084 Clause.setUpdates(Updates);
12085 Clause.setFinals(Finals);
12086 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +000012087}
12088
Alexander Musmanf0d76e72014-05-29 14:36:25 +000012089OMPClause *Sema::ActOnOpenMPAlignedClause(
12090 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
12091 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000012092 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +000012093 for (Expr *RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +000012094 assert(RefExpr && "NULL expr in OpenMP linear clause.");
12095 SourceLocation ELoc;
12096 SourceRange ERange;
12097 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000012098 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev1efd1662016-03-29 10:59:56 +000012099 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000012100 // It will be analyzed later.
12101 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000012102 }
Alexey Bataev1efd1662016-03-29 10:59:56 +000012103 ValueDecl *D = Res.first;
12104 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +000012105 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000012106
Alexey Bataev1efd1662016-03-29 10:59:56 +000012107 QualType QType = D->getType();
12108 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000012109
12110 // OpenMP [2.8.1, simd construct, Restrictions]
12111 // The type of list items appearing in the aligned clause must be
12112 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000012113 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +000012114 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +000012115 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000012116 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +000012117 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000012118 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +000012119 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +000012120 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +000012121 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +000012122 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +000012123 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000012124 continue;
12125 }
12126
12127 // OpenMP [2.8.1, simd construct, Restrictions]
12128 // A list-item cannot appear in more than one aligned clause.
Alexey Bataeve3727102018-04-18 15:57:46 +000012129 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +000012130 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000012131 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
12132 << getOpenMPClauseName(OMPC_aligned);
12133 continue;
12134 }
12135
Alexey Bataev1efd1662016-03-29 10:59:56 +000012136 DeclRefExpr *Ref = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000012137 if (!VD && isOpenMPCapturedDecl(D))
Alexey Bataev1efd1662016-03-29 10:59:56 +000012138 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
12139 Vars.push_back(DefaultFunctionArrayConversion(
12140 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
12141 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +000012142 }
12143
12144 // OpenMP [2.8.1, simd construct, Description]
12145 // The parameter of the aligned clause, alignment, must be a constant
12146 // positive integer expression.
12147 // If no optional parameter is specified, implementation-defined default
12148 // alignments for SIMD instructions on the target platforms are assumed.
12149 if (Alignment != nullptr) {
12150 ExprResult AlignResult =
12151 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
12152 if (AlignResult.isInvalid())
12153 return nullptr;
12154 Alignment = AlignResult.get();
12155 }
12156 if (Vars.empty())
12157 return nullptr;
12158
12159 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
12160 EndLoc, Vars, Alignment);
12161}
12162
Alexey Bataevd48bcd82014-03-31 03:36:38 +000012163OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
12164 SourceLocation StartLoc,
12165 SourceLocation LParenLoc,
12166 SourceLocation EndLoc) {
12167 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +000012168 SmallVector<Expr *, 8> SrcExprs;
12169 SmallVector<Expr *, 8> DstExprs;
12170 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeve3727102018-04-18 15:57:46 +000012171 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000012172 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
12173 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +000012174 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000012175 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000012176 SrcExprs.push_back(nullptr);
12177 DstExprs.push_back(nullptr);
12178 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000012179 continue;
12180 }
12181
Alexey Bataeved09d242014-05-28 05:53:51 +000012182 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000012183 // OpenMP [2.1, C/C++]
12184 // A list item is a variable name.
12185 // OpenMP [2.14.4.1, Restrictions, p.1]
12186 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeve3727102018-04-18 15:57:46 +000012187 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000012188 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000012189 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
12190 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000012191 continue;
12192 }
12193
12194 Decl *D = DE->getDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000012195 auto *VD = cast<VarDecl>(D);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000012196
12197 QualType Type = VD->getType();
12198 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
12199 // It will be analyzed later.
12200 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000012201 SrcExprs.push_back(nullptr);
12202 DstExprs.push_back(nullptr);
12203 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000012204 continue;
12205 }
12206
12207 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
12208 // A list item that appears in a copyin clause must be threadprivate.
12209 if (!DSAStack->isThreadPrivate(VD)) {
12210 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +000012211 << getOpenMPClauseName(OMPC_copyin)
12212 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000012213 continue;
12214 }
12215
12216 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
12217 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +000012218 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +000012219 // operator for the class type.
Alexey Bataeve3727102018-04-18 15:57:46 +000012220 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
12221 VarDecl *SrcVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000012222 buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000012223 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000012224 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +000012225 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +000012226 VarDecl *DstVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000012227 buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst",
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000012228 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000012229 DeclRefExpr *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +000012230 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +000012231 // For arrays generate assignment operation for single element and replace
12232 // it by the original array element in CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +000012233 ExprResult AssignmentOp =
12234 BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr,
12235 PseudoSrcExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000012236 if (AssignmentOp.isInvalid())
12237 continue;
12238 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000012239 /*DiscardedValue*/ false);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000012240 if (AssignmentOp.isInvalid())
12241 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000012242
12243 DSAStack->addDSA(VD, DE, OMPC_copyin);
12244 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000012245 SrcExprs.push_back(PseudoSrcExpr);
12246 DstExprs.push_back(PseudoDstExpr);
12247 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +000012248 }
12249
Alexey Bataeved09d242014-05-28 05:53:51 +000012250 if (Vars.empty())
12251 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000012252
Alexey Bataevf56f98c2015-04-16 05:39:01 +000012253 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
12254 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000012255}
12256
Alexey Bataevbae9a792014-06-27 10:37:06 +000012257OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
12258 SourceLocation StartLoc,
12259 SourceLocation LParenLoc,
12260 SourceLocation EndLoc) {
12261 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +000012262 SmallVector<Expr *, 8> SrcExprs;
12263 SmallVector<Expr *, 8> DstExprs;
12264 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeve3727102018-04-18 15:57:46 +000012265 for (Expr *RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +000012266 assert(RefExpr && "NULL expr in OpenMP linear clause.");
12267 SourceLocation ELoc;
12268 SourceRange ERange;
12269 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000012270 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataeve122da12016-03-17 10:50:17 +000012271 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000012272 // It will be analyzed later.
12273 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000012274 SrcExprs.push_back(nullptr);
12275 DstExprs.push_back(nullptr);
12276 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +000012277 }
Alexey Bataeve122da12016-03-17 10:50:17 +000012278 ValueDecl *D = Res.first;
12279 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +000012280 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000012281
Alexey Bataeve122da12016-03-17 10:50:17 +000012282 QualType Type = D->getType();
12283 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +000012284
12285 // OpenMP [2.14.4.2, Restrictions, p.2]
12286 // A list item that appears in a copyprivate clause may not appear in a
12287 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +000012288 if (!VD || !DSAStack->isThreadPrivate(VD)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012289 DSAStackTy::DSAVarData DVar =
12290 DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataeva63048e2015-03-23 06:18:07 +000012291 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
12292 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000012293 Diag(ELoc, diag::err_omp_wrong_dsa)
12294 << getOpenMPClauseName(DVar.CKind)
12295 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000012296 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000012297 continue;
12298 }
12299
12300 // OpenMP [2.11.4.2, Restrictions, p.1]
12301 // All list items that appear in a copyprivate clause must be either
12302 // threadprivate or private in the enclosing context.
12303 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +000012304 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +000012305 if (DVar.CKind == OMPC_shared) {
12306 Diag(ELoc, diag::err_omp_required_access)
12307 << getOpenMPClauseName(OMPC_copyprivate)
12308 << "threadprivate or private in the enclosing context";
Alexey Bataeve3727102018-04-18 15:57:46 +000012309 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000012310 continue;
12311 }
12312 }
12313 }
12314
Alexey Bataev7a3e5852015-05-19 08:19:24 +000012315 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000012316 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +000012317 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +000012318 << getOpenMPClauseName(OMPC_copyprivate) << Type
12319 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +000012320 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +000012321 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +000012322 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +000012323 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +000012324 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +000012325 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +000012326 continue;
12327 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +000012328
Alexey Bataevbae9a792014-06-27 10:37:06 +000012329 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
12330 // A variable of class type (or array thereof) that appears in a
12331 // copyin clause requires an accessible, unambiguous copy assignment
12332 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +000012333 Type = Context.getBaseElementType(Type.getNonReferenceType())
12334 .getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000012335 VarDecl *SrcVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000012336 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src",
Alexey Bataeve122da12016-03-17 10:50:17 +000012337 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000012338 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
12339 VarDecl *DstVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000012340 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst",
Alexey Bataeve122da12016-03-17 10:50:17 +000012341 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000012342 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
12343 ExprResult AssignmentOp = BuildBinOp(
12344 DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000012345 if (AssignmentOp.isInvalid())
12346 continue;
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000012347 AssignmentOp =
12348 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataeva63048e2015-03-23 06:18:07 +000012349 if (AssignmentOp.isInvalid())
12350 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000012351
12352 // No need to mark vars as copyprivate, they are already threadprivate or
12353 // implicitly private.
Alexey Bataeve3727102018-04-18 15:57:46 +000012354 assert(VD || isOpenMPCapturedDecl(D));
Alexey Bataeve122da12016-03-17 10:50:17 +000012355 Vars.push_back(
12356 VD ? RefExpr->IgnoreParens()
12357 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +000012358 SrcExprs.push_back(PseudoSrcExpr);
12359 DstExprs.push_back(PseudoDstExpr);
12360 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +000012361 }
12362
12363 if (Vars.empty())
12364 return nullptr;
12365
Alexey Bataeva63048e2015-03-23 06:18:07 +000012366 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
12367 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +000012368}
12369
Alexey Bataev6125da92014-07-21 11:26:11 +000012370OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
12371 SourceLocation StartLoc,
12372 SourceLocation LParenLoc,
12373 SourceLocation EndLoc) {
12374 if (VarList.empty())
12375 return nullptr;
12376
12377 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
12378}
Alexey Bataevdea47612014-07-23 07:46:59 +000012379
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012380OMPClause *
12381Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
12382 SourceLocation DepLoc, SourceLocation ColonLoc,
12383 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
12384 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +000012385 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012386 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +000012387 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000012388 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +000012389 return nullptr;
12390 }
12391 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012392 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
12393 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +000012394 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012395 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000012396 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
12397 /*Last=*/OMPC_DEPEND_unknown, Except)
12398 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012399 return nullptr;
12400 }
12401 SmallVector<Expr *, 8> Vars;
Alexey Bataev8b427062016-05-25 12:36:08 +000012402 DSAStackTy::OperatorOffsetTy OpsOffs;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012403 llvm::APSInt DepCounter(/*BitWidth=*/32);
12404 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
Alexey Bataevf138fda2018-08-13 19:04:24 +000012405 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) {
12406 if (const Expr *OrderedCountExpr =
12407 DSAStack->getParentOrderedRegionParam().first) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012408 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
12409 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012410 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012411 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012412 for (Expr *RefExpr : VarList) {
Alexey Bataev17daedf2018-02-15 22:42:57 +000012413 assert(RefExpr && "NULL expr in OpenMP shared clause.");
12414 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
12415 // It will be analyzed later.
12416 Vars.push_back(RefExpr);
12417 continue;
12418 }
12419
12420 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataeve3727102018-04-18 15:57:46 +000012421 Expr *SimpleExpr = RefExpr->IgnoreParenCasts();
Alexey Bataev17daedf2018-02-15 22:42:57 +000012422 if (DepKind == OMPC_DEPEND_sink) {
Alexey Bataevf138fda2018-08-13 19:04:24 +000012423 if (DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000012424 DepCounter >= TotalDepCount) {
12425 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
12426 continue;
12427 }
12428 ++DepCounter;
12429 // OpenMP [2.13.9, Summary]
12430 // depend(dependence-type : vec), where dependence-type is:
12431 // 'sink' and where vec is the iteration vector, which has the form:
12432 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
12433 // where n is the value specified by the ordered clause in the loop
12434 // directive, xi denotes the loop iteration variable of the i-th nested
12435 // loop associated with the loop directive, and di is a constant
12436 // non-negative integer.
12437 if (CurContext->isDependentContext()) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012438 // It will be analyzed later.
12439 Vars.push_back(RefExpr);
12440 continue;
12441 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000012442 SimpleExpr = SimpleExpr->IgnoreImplicit();
12443 OverloadedOperatorKind OOK = OO_None;
12444 SourceLocation OOLoc;
12445 Expr *LHS = SimpleExpr;
12446 Expr *RHS = nullptr;
12447 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
12448 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
12449 OOLoc = BO->getOperatorLoc();
12450 LHS = BO->getLHS()->IgnoreParenImpCasts();
12451 RHS = BO->getRHS()->IgnoreParenImpCasts();
12452 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
12453 OOK = OCE->getOperator();
12454 OOLoc = OCE->getOperatorLoc();
12455 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
12456 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
12457 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
12458 OOK = MCE->getMethodDecl()
12459 ->getNameInfo()
12460 .getName()
12461 .getCXXOverloadedOperator();
12462 OOLoc = MCE->getCallee()->getExprLoc();
12463 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
12464 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012465 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000012466 SourceLocation ELoc;
12467 SourceRange ERange;
Alexey Bataevbc529672018-09-28 19:33:14 +000012468 auto Res = getPrivateItem(*this, LHS, ELoc, ERange);
Alexey Bataev17daedf2018-02-15 22:42:57 +000012469 if (Res.second) {
12470 // It will be analyzed later.
12471 Vars.push_back(RefExpr);
12472 }
12473 ValueDecl *D = Res.first;
12474 if (!D)
12475 continue;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012476
Alexey Bataev17daedf2018-02-15 22:42:57 +000012477 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
12478 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
12479 continue;
12480 }
12481 if (RHS) {
12482 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
12483 RHS, OMPC_depend, /*StrictlyPositive=*/false);
12484 if (RHSRes.isInvalid())
12485 continue;
12486 }
12487 if (!CurContext->isDependentContext() &&
Alexey Bataevf138fda2018-08-13 19:04:24 +000012488 DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000012489 DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012490 const ValueDecl *VD =
Alexey Bataev17daedf2018-02-15 22:42:57 +000012491 DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue());
Alexey Bataeve3727102018-04-18 15:57:46 +000012492 if (VD)
Alexey Bataev17daedf2018-02-15 22:42:57 +000012493 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
12494 << 1 << VD;
Alexey Bataeve3727102018-04-18 15:57:46 +000012495 else
Alexey Bataev17daedf2018-02-15 22:42:57 +000012496 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0;
Alexey Bataev17daedf2018-02-15 22:42:57 +000012497 continue;
12498 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012499 OpsOffs.emplace_back(RHS, OOK);
Alexey Bataev17daedf2018-02-15 22:42:57 +000012500 } else {
12501 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
12502 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
12503 (ASE &&
12504 !ASE->getBase()->getType().getNonReferenceType()->isPointerType() &&
12505 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
12506 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
12507 << RefExpr->getSourceRange();
12508 continue;
12509 }
12510 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
12511 getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
12512 ExprResult Res =
12513 CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RefExpr->IgnoreParenImpCasts());
12514 getDiagnostics().setSuppressAllDiagnostics(Suppress);
12515 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr)) {
12516 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
12517 << RefExpr->getSourceRange();
12518 continue;
12519 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012520 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000012521 Vars.push_back(RefExpr->IgnoreParenImpCasts());
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012522 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000012523
12524 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
12525 TotalDepCount > VarList.size() &&
Alexey Bataevf138fda2018-08-13 19:04:24 +000012526 DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000012527 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) {
12528 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
12529 << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
12530 }
12531 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
12532 Vars.empty())
12533 return nullptr;
12534
Alexey Bataev8b427062016-05-25 12:36:08 +000012535 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataevf138fda2018-08-13 19:04:24 +000012536 DepKind, DepLoc, ColonLoc, Vars,
12537 TotalDepCount.getZExtValue());
Alexey Bataev17daedf2018-02-15 22:42:57 +000012538 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) &&
12539 DSAStack->isParentOrderedRegion())
Alexey Bataev8b427062016-05-25 12:36:08 +000012540 DSAStack->addDoacrossDependClause(C, OpsOffs);
12541 return C;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012542}
Michael Wonge710d542015-08-07 16:16:36 +000012543
12544OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
12545 SourceLocation LParenLoc,
12546 SourceLocation EndLoc) {
12547 Expr *ValExpr = Device;
Alexey Bataev931e19b2017-10-02 16:32:39 +000012548 Stmt *HelperValStmt = nullptr;
Michael Wonge710d542015-08-07 16:16:36 +000012549
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012550 // OpenMP [2.9.1, Restrictions]
12551 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000012552 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
Alexey Bataeva0569352015-12-01 10:17:31 +000012553 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012554 return nullptr;
12555
Alexey Bataev931e19b2017-10-02 16:32:39 +000012556 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000012557 OpenMPDirectiveKind CaptureRegion =
12558 getOpenMPCaptureRegionForClause(DKind, OMPC_device);
12559 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000012560 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000012561 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev931e19b2017-10-02 16:32:39 +000012562 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
12563 HelperValStmt = buildPreInits(Context, Captures);
12564 }
12565
Alexey Bataev8451efa2018-01-15 19:06:12 +000012566 return new (Context) OMPDeviceClause(ValExpr, HelperValStmt, CaptureRegion,
12567 StartLoc, LParenLoc, EndLoc);
Michael Wonge710d542015-08-07 16:16:36 +000012568}
Kelvin Li0bff7af2015-11-23 05:32:03 +000012569
Alexey Bataeve3727102018-04-18 15:57:46 +000012570static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
Alexey Bataev95c23e72018-02-27 21:31:11 +000012571 DSAStackTy *Stack, QualType QTy,
12572 bool FullCheck = true) {
Kelvin Li0bff7af2015-11-23 05:32:03 +000012573 NamedDecl *ND;
12574 if (QTy->isIncompleteType(&ND)) {
12575 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
12576 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +000012577 }
Alexey Bataev95c23e72018-02-27 21:31:11 +000012578 if (FullCheck && !SemaRef.CurContext->isDependentContext() &&
12579 !QTy.isTrivialType(SemaRef.Context))
12580 SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR;
Kelvin Li0bff7af2015-11-23 05:32:03 +000012581 return true;
12582}
12583
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000012584/// Return true if it can be proven that the provided array expression
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012585/// (array section or array subscript) does NOT specify the whole size of the
12586/// array whose base type is \a BaseQTy.
Alexey Bataeve3727102018-04-18 15:57:46 +000012587static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012588 const Expr *E,
12589 QualType BaseQTy) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012590 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012591
12592 // If this is an array subscript, it refers to the whole size if the size of
12593 // the dimension is constant and equals 1. Also, an array section assumes the
12594 // format of an array subscript if no colon is used.
12595 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012596 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012597 return ATy->getSize().getSExtValue() != 1;
12598 // Size can't be evaluated statically.
12599 return false;
12600 }
12601
12602 assert(OASE && "Expecting array section if not an array subscript.");
Alexey Bataeve3727102018-04-18 15:57:46 +000012603 const Expr *LowerBound = OASE->getLowerBound();
12604 const Expr *Length = OASE->getLength();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012605
12606 // If there is a lower bound that does not evaluates to zero, we are not
David Majnemer9d168222016-08-05 17:44:54 +000012607 // covering the whole dimension.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012608 if (LowerBound) {
Fangrui Song407659a2018-11-30 23:41:18 +000012609 Expr::EvalResult Result;
12610 if (!LowerBound->EvaluateAsInt(Result, SemaRef.getASTContext()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012611 return false; // Can't get the integer value as a constant.
Fangrui Song407659a2018-11-30 23:41:18 +000012612
12613 llvm::APSInt ConstLowerBound = Result.Val.getInt();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012614 if (ConstLowerBound.getSExtValue())
12615 return true;
12616 }
12617
12618 // If we don't have a length we covering the whole dimension.
12619 if (!Length)
12620 return false;
12621
12622 // If the base is a pointer, we don't have a way to get the size of the
12623 // pointee.
12624 if (BaseQTy->isPointerType())
12625 return false;
12626
12627 // We can only check if the length is the same as the size of the dimension
12628 // if we have a constant array.
Alexey Bataeve3727102018-04-18 15:57:46 +000012629 const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012630 if (!CATy)
12631 return false;
12632
Fangrui Song407659a2018-11-30 23:41:18 +000012633 Expr::EvalResult Result;
12634 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012635 return false; // Can't get the integer value as a constant.
12636
Fangrui Song407659a2018-11-30 23:41:18 +000012637 llvm::APSInt ConstLength = Result.Val.getInt();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012638 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
12639}
12640
12641// Return true if it can be proven that the provided array expression (array
12642// section or array subscript) does NOT specify a single element of the array
12643// whose base type is \a BaseQTy.
Alexey Bataeve3727102018-04-18 15:57:46 +000012644static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
David Majnemer9d168222016-08-05 17:44:54 +000012645 const Expr *E,
12646 QualType BaseQTy) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012647 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012648
12649 // An array subscript always refer to a single element. Also, an array section
12650 // assumes the format of an array subscript if no colon is used.
12651 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
12652 return false;
12653
12654 assert(OASE && "Expecting array section if not an array subscript.");
Alexey Bataeve3727102018-04-18 15:57:46 +000012655 const Expr *Length = OASE->getLength();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012656
12657 // If we don't have a length we have to check if the array has unitary size
12658 // for this dimension. Also, we should always expect a length if the base type
12659 // is pointer.
12660 if (!Length) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012661 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012662 return ATy->getSize().getSExtValue() != 1;
12663 // We cannot assume anything.
12664 return false;
12665 }
12666
12667 // Check if the length evaluates to 1.
Fangrui Song407659a2018-11-30 23:41:18 +000012668 Expr::EvalResult Result;
12669 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012670 return false; // Can't get the integer value as a constant.
12671
Fangrui Song407659a2018-11-30 23:41:18 +000012672 llvm::APSInt ConstLength = Result.Val.getInt();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012673 return ConstLength.getSExtValue() != 1;
12674}
12675
Samuel Antao661c0902016-05-26 17:39:58 +000012676// Return the expression of the base of the mappable expression or null if it
12677// cannot be determined and do all the necessary checks to see if the expression
12678// is valid as a standalone mappable expression. In the process, record all the
Samuel Antao90927002016-04-26 14:54:23 +000012679// components of the expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000012680static const Expr *checkMapClauseExpressionBase(
Samuel Antao90927002016-04-26 14:54:23 +000012681 Sema &SemaRef, Expr *E,
Samuel Antao661c0902016-05-26 17:39:58 +000012682 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012683 OpenMPClauseKind CKind, bool NoDiagnose) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012684 SourceLocation ELoc = E->getExprLoc();
12685 SourceRange ERange = E->getSourceRange();
12686
12687 // The base of elements of list in a map clause have to be either:
12688 // - a reference to variable or field.
12689 // - a member expression.
12690 // - an array expression.
12691 //
12692 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
12693 // reference to 'r'.
12694 //
12695 // If we have:
12696 //
12697 // struct SS {
12698 // Bla S;
12699 // foo() {
12700 // #pragma omp target map (S.Arr[:12]);
12701 // }
12702 // }
12703 //
12704 // We want to retrieve the member expression 'this->S';
12705
Alexey Bataeve3727102018-04-18 15:57:46 +000012706 const Expr *RelevantExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012707
Samuel Antao5de996e2016-01-22 20:21:36 +000012708 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
12709 // If a list item is an array section, it must specify contiguous storage.
12710 //
12711 // For this restriction it is sufficient that we make sure only references
12712 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012713 // exist except in the rightmost expression (unless they cover the whole
12714 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +000012715 //
12716 // r.ArrS[3:5].Arr[6:7]
12717 //
12718 // r.ArrS[3:5].x
12719 //
12720 // but these would be valid:
12721 // r.ArrS[3].Arr[6:7]
12722 //
12723 // r.ArrS[3].x
12724
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012725 bool AllowUnitySizeArraySection = true;
12726 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +000012727
Dmitry Polukhin644a9252016-03-11 07:58:34 +000012728 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012729 E = E->IgnoreParenImpCasts();
12730
12731 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
12732 if (!isa<VarDecl>(CurE->getDecl()))
Alexey Bataev27041fa2017-12-05 15:22:49 +000012733 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012734
12735 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012736
12737 // If we got a reference to a declaration, we should not expect any array
12738 // section before that.
12739 AllowUnitySizeArraySection = false;
12740 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000012741
12742 // Record the component.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012743 CurComponents.emplace_back(CurE, CurE->getDecl());
12744 } else if (auto *CurE = dyn_cast<MemberExpr>(E)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012745 Expr *BaseE = CurE->getBase()->IgnoreParenImpCasts();
Samuel Antao5de996e2016-01-22 20:21:36 +000012746
12747 if (isa<CXXThisExpr>(BaseE))
12748 // We found a base expression: this->Val.
12749 RelevantExpr = CurE;
12750 else
12751 E = BaseE;
12752
12753 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012754 if (!NoDiagnose) {
12755 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
12756 << CurE->getSourceRange();
12757 return nullptr;
12758 }
12759 if (RelevantExpr)
12760 return nullptr;
12761 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000012762 }
12763
12764 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
12765
12766 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
12767 // A bit-field cannot appear in a map clause.
12768 //
12769 if (FD->isBitField()) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012770 if (!NoDiagnose) {
12771 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
12772 << CurE->getSourceRange() << getOpenMPClauseName(CKind);
12773 return nullptr;
12774 }
12775 if (RelevantExpr)
12776 return nullptr;
12777 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000012778 }
12779
12780 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12781 // If the type of a list item is a reference to a type T then the type
12782 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012783 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000012784
12785 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
12786 // A list item cannot be a variable that is a member of a structure with
12787 // a union type.
12788 //
Alexey Bataeve3727102018-04-18 15:57:46 +000012789 if (CurType->isUnionType()) {
12790 if (!NoDiagnose) {
12791 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
12792 << CurE->getSourceRange();
12793 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012794 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012795 continue;
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012796 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012797
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012798 // If we got a member expression, we should not expect any array section
12799 // before that:
12800 //
12801 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
12802 // If a list item is an element of a structure, only the rightmost symbol
12803 // of the variable reference can be an array section.
12804 //
12805 AllowUnitySizeArraySection = false;
12806 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000012807
12808 // Record the component.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012809 CurComponents.emplace_back(CurE, FD);
12810 } else if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012811 E = CurE->getBase()->IgnoreParenImpCasts();
12812
12813 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012814 if (!NoDiagnose) {
12815 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
12816 << 0 << CurE->getSourceRange();
12817 return nullptr;
12818 }
12819 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000012820 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012821
12822 // If we got an array subscript that express the whole dimension we
12823 // can have any array expressions before. If it only expressing part of
12824 // the dimension, we can only have unitary-size array expressions.
Alexey Bataeve3727102018-04-18 15:57:46 +000012825 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012826 E->getType()))
12827 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000012828
Patrick Lystere13b1e32019-01-02 19:28:48 +000012829 if (const auto *TE = dyn_cast<CXXThisExpr>(E)) {
12830 Expr::EvalResult Result;
12831 if (CurE->getIdx()->EvaluateAsInt(Result, SemaRef.getASTContext())) {
12832 if (!Result.Val.getInt().isNullValue()) {
12833 SemaRef.Diag(CurE->getIdx()->getExprLoc(),
12834 diag::err_omp_invalid_map_this_expr);
12835 SemaRef.Diag(CurE->getIdx()->getExprLoc(),
12836 diag::note_omp_invalid_subscript_on_this_ptr_map);
12837 }
12838 }
12839 RelevantExpr = TE;
12840 }
12841
Samuel Antao90927002016-04-26 14:54:23 +000012842 // Record the component - we don't have any declaration associated.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012843 CurComponents.emplace_back(CurE, nullptr);
12844 } else if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012845 assert(!NoDiagnose && "Array sections cannot be implicitly mapped.");
Samuel Antao5de996e2016-01-22 20:21:36 +000012846 E = CurE->getBase()->IgnoreParenImpCasts();
12847
Alexey Bataev27041fa2017-12-05 15:22:49 +000012848 QualType CurType =
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012849 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
12850
Samuel Antao5de996e2016-01-22 20:21:36 +000012851 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12852 // If the type of a list item is a reference to a type T then the type
12853 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +000012854 if (CurType->isReferenceType())
12855 CurType = CurType->getPointeeType();
12856
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012857 bool IsPointer = CurType->isAnyPointerType();
12858
12859 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012860 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
12861 << 0 << CurE->getSourceRange();
Alexey Bataev27041fa2017-12-05 15:22:49 +000012862 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012863 }
12864
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012865 bool NotWhole =
Alexey Bataeve3727102018-04-18 15:57:46 +000012866 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012867 bool NotUnity =
Alexey Bataeve3727102018-04-18 15:57:46 +000012868 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012869
Samuel Antaodab51bb2016-07-18 23:22:11 +000012870 if (AllowWholeSizeArraySection) {
12871 // Any array section is currently allowed. Allowing a whole size array
12872 // section implies allowing a unity array section as well.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012873 //
12874 // If this array section refers to the whole dimension we can still
12875 // accept other array sections before this one, except if the base is a
12876 // pointer. Otherwise, only unitary sections are accepted.
12877 if (NotWhole || IsPointer)
12878 AllowWholeSizeArraySection = false;
Samuel Antaodab51bb2016-07-18 23:22:11 +000012879 } else if (AllowUnitySizeArraySection && NotUnity) {
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012880 // A unity or whole array section is not allowed and that is not
12881 // compatible with the properties of the current array section.
12882 SemaRef.Diag(
12883 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
12884 << CurE->getSourceRange();
Alexey Bataev27041fa2017-12-05 15:22:49 +000012885 return nullptr;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012886 }
Samuel Antao90927002016-04-26 14:54:23 +000012887
Patrick Lystere13b1e32019-01-02 19:28:48 +000012888 if (const auto *TE = dyn_cast<CXXThisExpr>(E)) {
12889 Expr::EvalResult ResultR;
12890 Expr::EvalResult ResultL;
12891 if (CurE->getLength()->EvaluateAsInt(ResultR,
12892 SemaRef.getASTContext())) {
12893 if (!ResultR.Val.getInt().isOneValue()) {
12894 SemaRef.Diag(CurE->getLength()->getExprLoc(),
12895 diag::err_omp_invalid_map_this_expr);
12896 SemaRef.Diag(CurE->getLength()->getExprLoc(),
12897 diag::note_omp_invalid_length_on_this_ptr_mapping);
12898 }
12899 }
12900 if (CurE->getLowerBound() && CurE->getLowerBound()->EvaluateAsInt(
12901 ResultL, SemaRef.getASTContext())) {
12902 if (!ResultL.Val.getInt().isNullValue()) {
12903 SemaRef.Diag(CurE->getLowerBound()->getExprLoc(),
12904 diag::err_omp_invalid_map_this_expr);
12905 SemaRef.Diag(CurE->getLowerBound()->getExprLoc(),
12906 diag::note_omp_invalid_lower_bound_on_this_ptr_mapping);
12907 }
12908 }
12909 RelevantExpr = TE;
12910 }
12911
Samuel Antao90927002016-04-26 14:54:23 +000012912 // Record the component - we don't have any declaration associated.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012913 CurComponents.emplace_back(CurE, nullptr);
12914 } else {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012915 if (!NoDiagnose) {
12916 // If nothing else worked, this is not a valid map clause expression.
12917 SemaRef.Diag(
12918 ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
12919 << ERange;
12920 }
Alexey Bataev27041fa2017-12-05 15:22:49 +000012921 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012922 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012923 }
12924
12925 return RelevantExpr;
12926}
12927
12928// Return true if expression E associated with value VD has conflicts with other
12929// map information.
Alexey Bataeve3727102018-04-18 15:57:46 +000012930static bool checkMapConflicts(
12931 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E,
Samuel Antao90927002016-04-26 14:54:23 +000012932 bool CurrentRegionOnly,
Samuel Antao661c0902016-05-26 17:39:58 +000012933 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
12934 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012935 assert(VD && E);
Samuel Antao5de996e2016-01-22 20:21:36 +000012936 SourceLocation ELoc = E->getExprLoc();
12937 SourceRange ERange = E->getSourceRange();
12938
12939 // In order to easily check the conflicts we need to match each component of
12940 // the expression under test with the components of the expressions that are
12941 // already in the stack.
12942
Samuel Antao5de996e2016-01-22 20:21:36 +000012943 assert(!CurComponents.empty() && "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000012944 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000012945 "Map clause expression with unexpected base!");
12946
12947 // Variables to help detecting enclosing problems in data environment nests.
12948 bool IsEnclosedByDataEnvironmentExpr = false;
Samuel Antao90927002016-04-26 14:54:23 +000012949 const Expr *EnclosingExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012950
Samuel Antao90927002016-04-26 14:54:23 +000012951 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
12952 VD, CurrentRegionOnly,
Alexey Bataeve3727102018-04-18 15:57:46 +000012953 [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc,
12954 ERange, CKind, &EnclosingExpr,
12955 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef
12956 StackComponents,
12957 OpenMPClauseKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012958 assert(!StackComponents.empty() &&
12959 "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000012960 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000012961 "Map clause expression with unexpected base!");
Fangrui Song16fe49a2018-04-18 19:32:01 +000012962 (void)VD;
Samuel Antao5de996e2016-01-22 20:21:36 +000012963
Samuel Antao90927002016-04-26 14:54:23 +000012964 // The whole expression in the stack.
Alexey Bataeve3727102018-04-18 15:57:46 +000012965 const Expr *RE = StackComponents.front().getAssociatedExpression();
Samuel Antao90927002016-04-26 14:54:23 +000012966
Samuel Antao5de996e2016-01-22 20:21:36 +000012967 // Expressions must start from the same base. Here we detect at which
12968 // point both expressions diverge from each other and see if we can
12969 // detect if the memory referred to both expressions is contiguous and
12970 // do not overlap.
12971 auto CI = CurComponents.rbegin();
12972 auto CE = CurComponents.rend();
12973 auto SI = StackComponents.rbegin();
12974 auto SE = StackComponents.rend();
12975 for (; CI != CE && SI != SE; ++CI, ++SI) {
12976
12977 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
12978 // At most one list item can be an array item derived from a given
12979 // variable in map clauses of the same construct.
Samuel Antao90927002016-04-26 14:54:23 +000012980 if (CurrentRegionOnly &&
12981 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
12982 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
12983 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
12984 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
12985 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
Samuel Antao5de996e2016-01-22 20:21:36 +000012986 diag::err_omp_multiple_array_items_in_map_clause)
Samuel Antao90927002016-04-26 14:54:23 +000012987 << CI->getAssociatedExpression()->getSourceRange();
12988 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
12989 diag::note_used_here)
12990 << SI->getAssociatedExpression()->getSourceRange();
Samuel Antao5de996e2016-01-22 20:21:36 +000012991 return true;
12992 }
12993
12994 // Do both expressions have the same kind?
Samuel Antao90927002016-04-26 14:54:23 +000012995 if (CI->getAssociatedExpression()->getStmtClass() !=
12996 SI->getAssociatedExpression()->getStmtClass())
Samuel Antao5de996e2016-01-22 20:21:36 +000012997 break;
12998
12999 // Are we dealing with different variables/fields?
Samuel Antao90927002016-04-26 14:54:23 +000013000 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
Samuel Antao5de996e2016-01-22 20:21:36 +000013001 break;
13002 }
Kelvin Li9f645ae2016-07-18 22:49:16 +000013003 // Check if the extra components of the expressions in the enclosing
13004 // data environment are redundant for the current base declaration.
13005 // If they are, the maps completely overlap, which is legal.
13006 for (; SI != SE; ++SI) {
13007 QualType Type;
Alexey Bataeve3727102018-04-18 15:57:46 +000013008 if (const auto *ASE =
David Majnemer9d168222016-08-05 17:44:54 +000013009 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
Kelvin Li9f645ae2016-07-18 22:49:16 +000013010 Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
Alexey Bataeve3727102018-04-18 15:57:46 +000013011 } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(
David Majnemer9d168222016-08-05 17:44:54 +000013012 SI->getAssociatedExpression())) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013013 const Expr *E = OASE->getBase()->IgnoreParenImpCasts();
Kelvin Li9f645ae2016-07-18 22:49:16 +000013014 Type =
13015 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
13016 }
13017 if (Type.isNull() || Type->isAnyPointerType() ||
Alexey Bataeve3727102018-04-18 15:57:46 +000013018 checkArrayExpressionDoesNotReferToWholeSize(
Kelvin Li9f645ae2016-07-18 22:49:16 +000013019 SemaRef, SI->getAssociatedExpression(), Type))
13020 break;
13021 }
Samuel Antao5de996e2016-01-22 20:21:36 +000013022
13023 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
13024 // List items of map clauses in the same construct must not share
13025 // original storage.
13026 //
13027 // If the expressions are exactly the same or one is a subset of the
13028 // other, it means they are sharing storage.
13029 if (CI == CE && SI == SE) {
13030 if (CurrentRegionOnly) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013031 if (CKind == OMPC_map) {
Samuel Antao661c0902016-05-26 17:39:58 +000013032 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +000013033 } else {
Samuel Antaoec172c62016-05-26 17:49:04 +000013034 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000013035 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
13036 << ERange;
13037 }
Samuel Antao5de996e2016-01-22 20:21:36 +000013038 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
13039 << RE->getSourceRange();
13040 return true;
Samuel Antao5de996e2016-01-22 20:21:36 +000013041 }
Alexey Bataeve3727102018-04-18 15:57:46 +000013042 // If we find the same expression in the enclosing data environment,
13043 // that is legal.
13044 IsEnclosedByDataEnvironmentExpr = true;
13045 return false;
Samuel Antao5de996e2016-01-22 20:21:36 +000013046 }
13047
Samuel Antao90927002016-04-26 14:54:23 +000013048 QualType DerivedType =
13049 std::prev(CI)->getAssociatedDeclaration()->getType();
13050 SourceLocation DerivedLoc =
13051 std::prev(CI)->getAssociatedExpression()->getExprLoc();
Samuel Antao5de996e2016-01-22 20:21:36 +000013052
13053 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
13054 // If the type of a list item is a reference to a type T then the type
13055 // will be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000013056 DerivedType = DerivedType.getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000013057
13058 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
13059 // A variable for which the type is pointer and an array section
13060 // derived from that variable must not appear as list items of map
13061 // clauses of the same construct.
13062 //
13063 // Also, cover one of the cases in:
13064 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
13065 // If any part of the original storage of a list item has corresponding
13066 // storage in the device data environment, all of the original storage
13067 // must have corresponding storage in the device data environment.
13068 //
13069 if (DerivedType->isAnyPointerType()) {
13070 if (CI == CE || SI == SE) {
13071 SemaRef.Diag(
13072 DerivedLoc,
13073 diag::err_omp_pointer_mapped_along_with_derived_section)
13074 << DerivedLoc;
Alexey Bataev2819260b2018-02-27 17:42:00 +000013075 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
13076 << RE->getSourceRange();
13077 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +000013078 }
13079 if (CI->getAssociatedExpression()->getStmtClass() !=
Alexey Bataev2819260b2018-02-27 17:42:00 +000013080 SI->getAssociatedExpression()->getStmtClass() ||
13081 CI->getAssociatedDeclaration()->getCanonicalDecl() ==
13082 SI->getAssociatedDeclaration()->getCanonicalDecl()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000013083 assert(CI != CE && SI != SE);
Alexey Bataev2819260b2018-02-27 17:42:00 +000013084 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced)
Samuel Antao5de996e2016-01-22 20:21:36 +000013085 << DerivedLoc;
Alexey Bataev2819260b2018-02-27 17:42:00 +000013086 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
13087 << RE->getSourceRange();
13088 return true;
Samuel Antao5de996e2016-01-22 20:21:36 +000013089 }
Samuel Antao5de996e2016-01-22 20:21:36 +000013090 }
13091
13092 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
13093 // List items of map clauses in the same construct must not share
13094 // original storage.
13095 //
13096 // An expression is a subset of the other.
13097 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013098 if (CKind == OMPC_map) {
Alexey Bataeve82445f2018-09-20 13:54:02 +000013099 if (CI != CE || SI != SE) {
13100 // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is
13101 // a pointer.
13102 auto Begin =
13103 CI != CE ? CurComponents.begin() : StackComponents.begin();
13104 auto End = CI != CE ? CurComponents.end() : StackComponents.end();
13105 auto It = Begin;
13106 while (It != End && !It->getAssociatedDeclaration())
13107 std::advance(It, 1);
13108 assert(It != End &&
13109 "Expected at least one component with the declaration.");
13110 if (It != Begin && It->getAssociatedDeclaration()
13111 ->getType()
13112 .getCanonicalType()
13113 ->isAnyPointerType()) {
13114 IsEnclosedByDataEnvironmentExpr = false;
13115 EnclosingExpr = nullptr;
13116 return false;
13117 }
13118 }
Samuel Antao661c0902016-05-26 17:39:58 +000013119 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +000013120 } else {
Samuel Antaoec172c62016-05-26 17:49:04 +000013121 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000013122 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
13123 << ERange;
13124 }
Samuel Antao5de996e2016-01-22 20:21:36 +000013125 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
13126 << RE->getSourceRange();
13127 return true;
13128 }
13129
13130 // The current expression uses the same base as other expression in the
Samuel Antao90927002016-04-26 14:54:23 +000013131 // data environment but does not contain it completely.
Samuel Antao5de996e2016-01-22 20:21:36 +000013132 if (!CurrentRegionOnly && SI != SE)
13133 EnclosingExpr = RE;
13134
13135 // The current expression is a subset of the expression in the data
13136 // environment.
13137 IsEnclosedByDataEnvironmentExpr |=
13138 (!CurrentRegionOnly && CI != CE && SI == SE);
13139
13140 return false;
13141 });
13142
13143 if (CurrentRegionOnly)
13144 return FoundError;
13145
13146 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
13147 // If any part of the original storage of a list item has corresponding
13148 // storage in the device data environment, all of the original storage must
13149 // have corresponding storage in the device data environment.
13150 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
13151 // If a list item is an element of a structure, and a different element of
13152 // the structure has a corresponding list item in the device data environment
13153 // prior to a task encountering the construct associated with the map clause,
Samuel Antao90927002016-04-26 14:54:23 +000013154 // then the list item must also have a corresponding list item in the device
Samuel Antao5de996e2016-01-22 20:21:36 +000013155 // data environment prior to the task encountering the construct.
13156 //
13157 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
13158 SemaRef.Diag(ELoc,
13159 diag::err_omp_original_storage_is_shared_and_does_not_contain)
13160 << ERange;
13161 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
13162 << EnclosingExpr->getSourceRange();
13163 return true;
13164 }
13165
13166 return FoundError;
13167}
13168
Michael Kruse4304e9d2019-02-19 16:38:20 +000013169// Look up the user-defined mapper given the mapper name and mapped type, and
13170// build a reference to it.
13171ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S,
13172 CXXScopeSpec &MapperIdScopeSpec,
13173 const DeclarationNameInfo &MapperId,
13174 QualType Type, Expr *UnresolvedMapper) {
13175 if (MapperIdScopeSpec.isInvalid())
13176 return ExprError();
13177 // Find all user-defined mappers with the given MapperId.
13178 SmallVector<UnresolvedSet<8>, 4> Lookups;
13179 LookupResult Lookup(SemaRef, MapperId, Sema::LookupOMPMapperName);
13180 Lookup.suppressDiagnostics();
13181 if (S) {
13182 while (S && SemaRef.LookupParsedName(Lookup, S, &MapperIdScopeSpec)) {
13183 NamedDecl *D = Lookup.getRepresentativeDecl();
13184 while (S && !S->isDeclScope(D))
13185 S = S->getParent();
13186 if (S)
13187 S = S->getParent();
13188 Lookups.emplace_back();
13189 Lookups.back().append(Lookup.begin(), Lookup.end());
13190 Lookup.clear();
13191 }
13192 } else if (auto *ULE = cast_or_null<UnresolvedLookupExpr>(UnresolvedMapper)) {
13193 // Extract the user-defined mappers with the given MapperId.
13194 Lookups.push_back(UnresolvedSet<8>());
13195 for (NamedDecl *D : ULE->decls()) {
13196 auto *DMD = cast<OMPDeclareMapperDecl>(D);
13197 assert(DMD && "Expect valid OMPDeclareMapperDecl during instantiation.");
13198 Lookups.back().addDecl(DMD);
13199 }
13200 }
13201 // Defer the lookup for dependent types. The results will be passed through
13202 // UnresolvedMapper on instantiation.
13203 if (SemaRef.CurContext->isDependentContext() || Type->isDependentType() ||
13204 Type->isInstantiationDependentType() ||
13205 Type->containsUnexpandedParameterPack() ||
13206 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) {
13207 return !D->isInvalidDecl() &&
13208 (D->getType()->isDependentType() ||
13209 D->getType()->isInstantiationDependentType() ||
13210 D->getType()->containsUnexpandedParameterPack());
13211 })) {
13212 UnresolvedSet<8> URS;
13213 for (const UnresolvedSet<8> &Set : Lookups) {
13214 if (Set.empty())
13215 continue;
13216 URS.append(Set.begin(), Set.end());
13217 }
13218 return UnresolvedLookupExpr::Create(
13219 SemaRef.Context, /*NamingClass=*/nullptr,
13220 MapperIdScopeSpec.getWithLocInContext(SemaRef.Context), MapperId,
13221 /*ADL=*/false, /*Overloaded=*/true, URS.begin(), URS.end());
13222 }
13223 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
13224 // The type must be of struct, union or class type in C and C++
13225 if (!Type->isStructureOrClassType() && !Type->isUnionType())
13226 return ExprEmpty();
13227 SourceLocation Loc = MapperId.getLoc();
13228 // Perform argument dependent lookup.
13229 if (SemaRef.getLangOpts().CPlusPlus && !MapperIdScopeSpec.isSet())
13230 argumentDependentLookup(SemaRef, MapperId, Loc, Type, Lookups);
13231 // Return the first user-defined mapper with the desired type.
13232 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
13233 Lookups, [&SemaRef, Type](ValueDecl *D) -> ValueDecl * {
13234 if (!D->isInvalidDecl() &&
13235 SemaRef.Context.hasSameType(D->getType(), Type))
13236 return D;
13237 return nullptr;
13238 }))
13239 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc);
13240 // Find the first user-defined mapper with a type derived from the desired
13241 // type.
13242 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
13243 Lookups, [&SemaRef, Type, Loc](ValueDecl *D) -> ValueDecl * {
13244 if (!D->isInvalidDecl() &&
13245 SemaRef.IsDerivedFrom(Loc, Type, D->getType()) &&
13246 !Type.isMoreQualifiedThan(D->getType()))
13247 return D;
13248 return nullptr;
13249 })) {
13250 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
13251 /*DetectVirtual=*/false);
13252 if (SemaRef.IsDerivedFrom(Loc, Type, VD->getType(), Paths)) {
13253 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
13254 VD->getType().getUnqualifiedType()))) {
13255 if (SemaRef.CheckBaseClassAccess(
13256 Loc, VD->getType(), Type, Paths.front(),
13257 /*DiagID=*/0) != Sema::AR_inaccessible) {
13258 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc);
13259 }
13260 }
13261 }
13262 }
13263 // Report error if a mapper is specified, but cannot be found.
13264 if (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default") {
13265 SemaRef.Diag(Loc, diag::err_omp_invalid_mapper)
13266 << Type << MapperId.getName();
13267 return ExprError();
13268 }
13269 return ExprEmpty();
13270}
13271
Samuel Antao661c0902016-05-26 17:39:58 +000013272namespace {
13273// Utility struct that gathers all the related lists associated with a mappable
13274// expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000013275struct MappableVarListInfo {
Samuel Antao661c0902016-05-26 17:39:58 +000013276 // The list of expressions.
13277 ArrayRef<Expr *> VarList;
13278 // The list of processed expressions.
13279 SmallVector<Expr *, 16> ProcessedVarList;
13280 // The mappble components for each expression.
13281 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
13282 // The base declaration of the variable.
13283 SmallVector<ValueDecl *, 16> VarBaseDeclarations;
Michael Kruse4304e9d2019-02-19 16:38:20 +000013284 // The reference to the user-defined mapper associated with every expression.
13285 SmallVector<Expr *, 16> UDMapperList;
Samuel Antao661c0902016-05-26 17:39:58 +000013286
13287 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
13288 // We have a list of components and base declarations for each entry in the
13289 // variable list.
13290 VarComponents.reserve(VarList.size());
13291 VarBaseDeclarations.reserve(VarList.size());
13292 }
13293};
13294}
13295
13296// Check the validity of the provided variable list for the provided clause kind
Michael Kruse4304e9d2019-02-19 16:38:20 +000013297// \a CKind. In the check process the valid expressions, mappable expression
13298// components, variables, and user-defined mappers are extracted and used to
13299// fill \a ProcessedVarList, \a VarComponents, \a VarBaseDeclarations, and \a
13300// UDMapperList in MVLI. \a MapType, \a IsMapTypeImplicit, \a MapperIdScopeSpec,
13301// and \a MapperId are expected to be valid if the clause kind is 'map'.
13302static void checkMappableExpressionList(
13303 Sema &SemaRef, DSAStackTy *DSAS, OpenMPClauseKind CKind,
13304 MappableVarListInfo &MVLI, SourceLocation StartLoc,
Michael Kruse01f670d2019-02-22 22:29:42 +000013305 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo MapperId,
13306 ArrayRef<Expr *> UnresolvedMappers,
Michael Kruse4304e9d2019-02-19 16:38:20 +000013307 OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
Michael Kruse01f670d2019-02-22 22:29:42 +000013308 bool IsMapTypeImplicit = false) {
Samuel Antaoec172c62016-05-26 17:49:04 +000013309 // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
13310 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
Samuel Antao661c0902016-05-26 17:39:58 +000013311 "Unexpected clause kind with mappable expressions!");
Michael Kruse01f670d2019-02-22 22:29:42 +000013312
13313 // If the identifier of user-defined mapper is not specified, it is "default".
13314 // We do not change the actual name in this clause to distinguish whether a
13315 // mapper is specified explicitly, i.e., it is not explicitly specified when
13316 // MapperId.getName() is empty.
13317 if (!MapperId.getName() || MapperId.getName().isEmpty()) {
13318 auto &DeclNames = SemaRef.getASTContext().DeclarationNames;
13319 MapperId.setName(DeclNames.getIdentifier(
13320 &SemaRef.getASTContext().Idents.get("default")));
13321 }
Michael Kruse4304e9d2019-02-19 16:38:20 +000013322
13323 // Iterators to find the current unresolved mapper expression.
13324 auto UMIt = UnresolvedMappers.begin(), UMEnd = UnresolvedMappers.end();
13325 bool UpdateUMIt = false;
13326 Expr *UnresolvedMapper = nullptr;
Kelvin Li0bff7af2015-11-23 05:32:03 +000013327
Samuel Antao90927002016-04-26 14:54:23 +000013328 // Keep track of the mappable components and base declarations in this clause.
13329 // Each entry in the list is going to have a list of components associated. We
13330 // record each set of the components so that we can build the clause later on.
13331 // In the end we should have the same amount of declarations and component
13332 // lists.
Samuel Antao90927002016-04-26 14:54:23 +000013333
Alexey Bataeve3727102018-04-18 15:57:46 +000013334 for (Expr *RE : MVLI.VarList) {
Samuel Antaoec172c62016-05-26 17:49:04 +000013335 assert(RE && "Null expr in omp to/from/map clause");
Kelvin Li0bff7af2015-11-23 05:32:03 +000013336 SourceLocation ELoc = RE->getExprLoc();
13337
Michael Kruse4304e9d2019-02-19 16:38:20 +000013338 // Find the current unresolved mapper expression.
13339 if (UpdateUMIt && UMIt != UMEnd) {
13340 UMIt++;
13341 assert(
13342 UMIt != UMEnd &&
13343 "Expect the size of UnresolvedMappers to match with that of VarList");
13344 }
13345 UpdateUMIt = true;
13346 if (UMIt != UMEnd)
13347 UnresolvedMapper = *UMIt;
13348
Alexey Bataeve3727102018-04-18 15:57:46 +000013349 const Expr *VE = RE->IgnoreParenLValueCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000013350
13351 if (VE->isValueDependent() || VE->isTypeDependent() ||
13352 VE->isInstantiationDependent() ||
13353 VE->containsUnexpandedParameterPack()) {
Michael Kruse0336c752019-02-25 20:34:15 +000013354 // Try to find the associated user-defined mapper.
13355 ExprResult ER = buildUserDefinedMapperRef(
13356 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
13357 VE->getType().getCanonicalType(), UnresolvedMapper);
13358 if (ER.isInvalid())
13359 continue;
13360 MVLI.UDMapperList.push_back(ER.get());
Samuel Antao5de996e2016-01-22 20:21:36 +000013361 // We can only analyze this information once the missing information is
13362 // resolved.
Samuel Antao661c0902016-05-26 17:39:58 +000013363 MVLI.ProcessedVarList.push_back(RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +000013364 continue;
13365 }
13366
Alexey Bataeve3727102018-04-18 15:57:46 +000013367 Expr *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000013368
Samuel Antao5de996e2016-01-22 20:21:36 +000013369 if (!RE->IgnoreParenImpCasts()->isLValue()) {
Samuel Antao661c0902016-05-26 17:39:58 +000013370 SemaRef.Diag(ELoc,
13371 diag::err_omp_expected_named_var_member_or_array_expression)
Samuel Antao5de996e2016-01-22 20:21:36 +000013372 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000013373 continue;
13374 }
13375
Samuel Antao90927002016-04-26 14:54:23 +000013376 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
13377 ValueDecl *CurDeclaration = nullptr;
13378
13379 // Obtain the array or member expression bases if required. Also, fill the
13380 // components array with all the components identified in the process.
Alexey Bataeve3727102018-04-18 15:57:46 +000013381 const Expr *BE = checkMapClauseExpressionBase(
13382 SemaRef, SimpleExpr, CurComponents, CKind, /*NoDiagnose=*/false);
Samuel Antao5de996e2016-01-22 20:21:36 +000013383 if (!BE)
13384 continue;
13385
Samuel Antao90927002016-04-26 14:54:23 +000013386 assert(!CurComponents.empty() &&
13387 "Invalid mappable expression information.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000013388
Patrick Lystere13b1e32019-01-02 19:28:48 +000013389 if (const auto *TE = dyn_cast<CXXThisExpr>(BE)) {
13390 // Add store "this" pointer to class in DSAStackTy for future checking
13391 DSAS->addMappedClassesQualTypes(TE->getType());
Michael Kruse0336c752019-02-25 20:34:15 +000013392 // Try to find the associated user-defined mapper.
13393 ExprResult ER = buildUserDefinedMapperRef(
13394 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
13395 VE->getType().getCanonicalType(), UnresolvedMapper);
13396 if (ER.isInvalid())
13397 continue;
13398 MVLI.UDMapperList.push_back(ER.get());
Patrick Lystere13b1e32019-01-02 19:28:48 +000013399 // Skip restriction checking for variable or field declarations
13400 MVLI.ProcessedVarList.push_back(RE);
13401 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13402 MVLI.VarComponents.back().append(CurComponents.begin(),
13403 CurComponents.end());
13404 MVLI.VarBaseDeclarations.push_back(nullptr);
13405 continue;
13406 }
13407
Samuel Antao90927002016-04-26 14:54:23 +000013408 // For the following checks, we rely on the base declaration which is
13409 // expected to be associated with the last component. The declaration is
13410 // expected to be a variable or a field (if 'this' is being mapped).
13411 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
13412 assert(CurDeclaration && "Null decl on map clause.");
13413 assert(
13414 CurDeclaration->isCanonicalDecl() &&
13415 "Expecting components to have associated only canonical declarations.");
13416
13417 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
Alexey Bataeve3727102018-04-18 15:57:46 +000013418 const auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
Samuel Antao5de996e2016-01-22 20:21:36 +000013419
13420 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000013421 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000013422
13423 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
Samuel Antao661c0902016-05-26 17:39:58 +000013424 // threadprivate variables cannot appear in a map clause.
13425 // OpenMP 4.5 [2.10.5, target update Construct]
13426 // threadprivate variables cannot appear in a from clause.
13427 if (VD && DSAS->isThreadPrivate(VD)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013428 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
Samuel Antao661c0902016-05-26 17:39:58 +000013429 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
13430 << getOpenMPClauseName(CKind);
Alexey Bataeve3727102018-04-18 15:57:46 +000013431 reportOriginalDsa(SemaRef, DSAS, VD, DVar);
Kelvin Li0bff7af2015-11-23 05:32:03 +000013432 continue;
13433 }
13434
Samuel Antao5de996e2016-01-22 20:21:36 +000013435 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
13436 // A list item cannot appear in both a map clause and a data-sharing
13437 // attribute clause on the same construct.
Kelvin Li0bff7af2015-11-23 05:32:03 +000013438
Samuel Antao5de996e2016-01-22 20:21:36 +000013439 // Check conflicts with other map clause expressions. We check the conflicts
13440 // with the current construct separately from the enclosing data
Samuel Antao661c0902016-05-26 17:39:58 +000013441 // environment, because the restrictions are different. We only have to
13442 // check conflicts across regions for the map clauses.
Alexey Bataeve3727102018-04-18 15:57:46 +000013443 if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
Samuel Antao661c0902016-05-26 17:39:58 +000013444 /*CurrentRegionOnly=*/true, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000013445 break;
Samuel Antao661c0902016-05-26 17:39:58 +000013446 if (CKind == OMPC_map &&
Alexey Bataeve3727102018-04-18 15:57:46 +000013447 checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
Samuel Antao661c0902016-05-26 17:39:58 +000013448 /*CurrentRegionOnly=*/false, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000013449 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000013450
Samuel Antao661c0902016-05-26 17:39:58 +000013451 // OpenMP 4.5 [2.10.5, target update Construct]
Samuel Antao5de996e2016-01-22 20:21:36 +000013452 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
13453 // If the type of a list item is a reference to a type T then the type will
13454 // be considered to be T for all purposes of this clause.
Alexey Bataev354df2e2018-05-02 18:44:10 +000013455 auto I = llvm::find_if(
13456 CurComponents,
13457 [](const OMPClauseMappableExprCommon::MappableComponent &MC) {
13458 return MC.getAssociatedDeclaration();
13459 });
13460 assert(I != CurComponents.end() && "Null decl on map clause.");
13461 QualType Type =
13462 I->getAssociatedDeclaration()->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000013463
Samuel Antao661c0902016-05-26 17:39:58 +000013464 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
13465 // A list item in a to or from clause must have a mappable type.
Samuel Antao5de996e2016-01-22 20:21:36 +000013466 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000013467 // A list item must have a mappable type.
Alexey Bataeve3727102018-04-18 15:57:46 +000013468 if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
Samuel Antao661c0902016-05-26 17:39:58 +000013469 DSAS, Type))
Kelvin Li0bff7af2015-11-23 05:32:03 +000013470 continue;
13471
Samuel Antao661c0902016-05-26 17:39:58 +000013472 if (CKind == OMPC_map) {
13473 // target enter data
13474 // OpenMP [2.10.2, Restrictions, p. 99]
13475 // A map-type must be specified in all map clauses and must be either
13476 // to or alloc.
13477 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
13478 if (DKind == OMPD_target_enter_data &&
13479 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
13480 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
13481 << (IsMapTypeImplicit ? 1 : 0)
13482 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
13483 << getOpenMPDirectiveName(DKind);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000013484 continue;
13485 }
Samuel Antao661c0902016-05-26 17:39:58 +000013486
13487 // target exit_data
13488 // OpenMP [2.10.3, Restrictions, p. 102]
13489 // A map-type must be specified in all map clauses and must be either
13490 // from, release, or delete.
13491 if (DKind == OMPD_target_exit_data &&
13492 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
13493 MapType == OMPC_MAP_delete)) {
13494 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
13495 << (IsMapTypeImplicit ? 1 : 0)
13496 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
13497 << getOpenMPDirectiveName(DKind);
13498 continue;
13499 }
13500
13501 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
13502 // A list item cannot appear in both a map clause and a data-sharing
13503 // attribute clause on the same construct
Alexey Bataeve3727102018-04-18 15:57:46 +000013504 if (VD && isOpenMPTargetExecutionDirective(DKind)) {
13505 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
Samuel Antao661c0902016-05-26 17:39:58 +000013506 if (isOpenMPPrivate(DVar.CKind)) {
Samuel Antao6890b092016-07-28 14:25:09 +000013507 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Samuel Antao661c0902016-05-26 17:39:58 +000013508 << getOpenMPClauseName(DVar.CKind)
Samuel Antao6890b092016-07-28 14:25:09 +000013509 << getOpenMPClauseName(OMPC_map)
Samuel Antao661c0902016-05-26 17:39:58 +000013510 << getOpenMPDirectiveName(DSAS->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000013511 reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar);
Samuel Antao661c0902016-05-26 17:39:58 +000013512 continue;
13513 }
13514 }
Michael Kruse01f670d2019-02-22 22:29:42 +000013515 }
Michael Kruse4304e9d2019-02-19 16:38:20 +000013516
Michael Kruse01f670d2019-02-22 22:29:42 +000013517 // Try to find the associated user-defined mapper.
Michael Kruse0336c752019-02-25 20:34:15 +000013518 ExprResult ER = buildUserDefinedMapperRef(
13519 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
13520 Type.getCanonicalType(), UnresolvedMapper);
13521 if (ER.isInvalid())
13522 continue;
13523 MVLI.UDMapperList.push_back(ER.get());
Carlo Bertollib74bfc82016-03-18 21:43:32 +000013524
Samuel Antao90927002016-04-26 14:54:23 +000013525 // Save the current expression.
Samuel Antao661c0902016-05-26 17:39:58 +000013526 MVLI.ProcessedVarList.push_back(RE);
Samuel Antao90927002016-04-26 14:54:23 +000013527
13528 // Store the components in the stack so that they can be used to check
13529 // against other clauses later on.
Samuel Antao6890b092016-07-28 14:25:09 +000013530 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents,
13531 /*WhereFoundClauseKind=*/OMPC_map);
Samuel Antao90927002016-04-26 14:54:23 +000013532
13533 // Save the components and declaration to create the clause. For purposes of
13534 // the clause creation, any component list that has has base 'this' uses
Samuel Antao686c70c2016-05-26 17:30:50 +000013535 // null as base declaration.
Samuel Antao661c0902016-05-26 17:39:58 +000013536 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13537 MVLI.VarComponents.back().append(CurComponents.begin(),
13538 CurComponents.end());
13539 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
13540 : CurDeclaration);
Kelvin Li0bff7af2015-11-23 05:32:03 +000013541 }
Samuel Antao661c0902016-05-26 17:39:58 +000013542}
13543
Michael Kruse4304e9d2019-02-19 16:38:20 +000013544OMPClause *Sema::ActOnOpenMPMapClause(
13545 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers,
13546 ArrayRef<SourceLocation> MapTypeModifiersLoc,
13547 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId,
13548 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, SourceLocation MapLoc,
13549 SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
13550 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) {
13551 OpenMPMapModifierKind Modifiers[] = {OMPC_MAP_MODIFIER_unknown,
13552 OMPC_MAP_MODIFIER_unknown,
13553 OMPC_MAP_MODIFIER_unknown};
Kelvin Lief579432018-12-18 22:18:41 +000013554 SourceLocation ModifiersLoc[OMPMapClause::NumberOfModifiers];
13555
13556 // Process map-type-modifiers, flag errors for duplicate modifiers.
13557 unsigned Count = 0;
13558 for (unsigned I = 0, E = MapTypeModifiers.size(); I < E; ++I) {
13559 if (MapTypeModifiers[I] != OMPC_MAP_MODIFIER_unknown &&
13560 llvm::find(Modifiers, MapTypeModifiers[I]) != std::end(Modifiers)) {
13561 Diag(MapTypeModifiersLoc[I], diag::err_omp_duplicate_map_type_modifier);
13562 continue;
13563 }
13564 assert(Count < OMPMapClause::NumberOfModifiers &&
Gheorghe-Teodor Berceaa3afcf22019-01-09 20:38:35 +000013565 "Modifiers exceed the allowed number of map type modifiers");
Kelvin Lief579432018-12-18 22:18:41 +000013566 Modifiers[Count] = MapTypeModifiers[I];
13567 ModifiersLoc[Count] = MapTypeModifiersLoc[I];
13568 ++Count;
13569 }
13570
Michael Kruse4304e9d2019-02-19 16:38:20 +000013571 MappableVarListInfo MVLI(VarList);
13572 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, Locs.StartLoc,
Michael Kruse01f670d2019-02-22 22:29:42 +000013573 MapperIdScopeSpec, MapperId, UnresolvedMappers,
13574 MapType, IsMapTypeImplicit);
Michael Kruse4304e9d2019-02-19 16:38:20 +000013575
Samuel Antao5de996e2016-01-22 20:21:36 +000013576 // We need to produce a map clause even if we don't have variables so that
13577 // other diagnostics related with non-existing map clauses are accurate.
Michael Kruse4304e9d2019-02-19 16:38:20 +000013578 return OMPMapClause::Create(Context, Locs, MVLI.ProcessedVarList,
13579 MVLI.VarBaseDeclarations, MVLI.VarComponents,
13580 MVLI.UDMapperList, Modifiers, ModifiersLoc,
13581 MapperIdScopeSpec.getWithLocInContext(Context),
13582 MapperId, MapType, IsMapTypeImplicit, MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000013583}
Kelvin Li099bb8c2015-11-24 20:50:12 +000013584
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013585QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
13586 TypeResult ParsedType) {
13587 assert(ParsedType.isUsable());
13588
13589 QualType ReductionType = GetTypeFromParser(ParsedType.get());
13590 if (ReductionType.isNull())
13591 return QualType();
13592
13593 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
13594 // A type name in a declare reduction directive cannot be a function type, an
13595 // array type, a reference type, or a type qualified with const, volatile or
13596 // restrict.
13597 if (ReductionType.hasQualifiers()) {
13598 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
13599 return QualType();
13600 }
13601
13602 if (ReductionType->isFunctionType()) {
13603 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
13604 return QualType();
13605 }
13606 if (ReductionType->isReferenceType()) {
13607 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
13608 return QualType();
13609 }
13610 if (ReductionType->isArrayType()) {
13611 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
13612 return QualType();
13613 }
13614 return ReductionType;
13615}
13616
13617Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
13618 Scope *S, DeclContext *DC, DeclarationName Name,
13619 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
13620 AccessSpecifier AS, Decl *PrevDeclInScope) {
13621 SmallVector<Decl *, 8> Decls;
13622 Decls.reserve(ReductionTypes.size());
13623
13624 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
Richard Smithbecb92d2017-10-10 22:33:17 +000013625 forRedeclarationInCurContext());
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013626 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
13627 // A reduction-identifier may not be re-declared in the current scope for the
13628 // same type or for a type that is compatible according to the base language
13629 // rules.
13630 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
13631 OMPDeclareReductionDecl *PrevDRD = nullptr;
13632 bool InCompoundScope = true;
13633 if (S != nullptr) {
13634 // Find previous declaration with the same name not referenced in other
13635 // declarations.
13636 FunctionScopeInfo *ParentFn = getEnclosingFunction();
13637 InCompoundScope =
13638 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
13639 LookupName(Lookup, S);
13640 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
13641 /*AllowInlineNamespace=*/false);
13642 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
Alexey Bataeve3727102018-04-18 15:57:46 +000013643 LookupResult::Filter Filter = Lookup.makeFilter();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013644 while (Filter.hasNext()) {
13645 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
13646 if (InCompoundScope) {
13647 auto I = UsedAsPrevious.find(PrevDecl);
13648 if (I == UsedAsPrevious.end())
13649 UsedAsPrevious[PrevDecl] = false;
Alexey Bataeve3727102018-04-18 15:57:46 +000013650 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope())
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013651 UsedAsPrevious[D] = true;
13652 }
13653 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
13654 PrevDecl->getLocation();
13655 }
13656 Filter.done();
13657 if (InCompoundScope) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013658 for (const auto &PrevData : UsedAsPrevious) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013659 if (!PrevData.second) {
13660 PrevDRD = PrevData.first;
13661 break;
13662 }
13663 }
13664 }
13665 } else if (PrevDeclInScope != nullptr) {
13666 auto *PrevDRDInScope = PrevDRD =
13667 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
13668 do {
13669 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
13670 PrevDRDInScope->getLocation();
13671 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
13672 } while (PrevDRDInScope != nullptr);
13673 }
Alexey Bataeve3727102018-04-18 15:57:46 +000013674 for (const auto &TyData : ReductionTypes) {
13675 const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013676 bool Invalid = false;
13677 if (I != PreviousRedeclTypes.end()) {
13678 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
13679 << TyData.first;
13680 Diag(I->second, diag::note_previous_definition);
13681 Invalid = true;
13682 }
13683 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
13684 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
13685 Name, TyData.first, PrevDRD);
13686 DC->addDecl(DRD);
13687 DRD->setAccess(AS);
13688 Decls.push_back(DRD);
13689 if (Invalid)
13690 DRD->setInvalidDecl();
13691 else
13692 PrevDRD = DRD;
13693 }
13694
13695 return DeclGroupPtrTy::make(
13696 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
13697}
13698
13699void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
13700 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13701
13702 // Enter new function scope.
13703 PushFunctionScope();
Reid Kleckner87a31802018-03-12 21:43:02 +000013704 setFunctionHasBranchProtectedScope();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013705 getCurFunction()->setHasOMPDeclareReductionCombiner();
13706
13707 if (S != nullptr)
13708 PushDeclContext(S, DRD);
13709 else
13710 CurContext = DRD;
13711
Faisal Valid143a0c2017-04-01 21:30:49 +000013712 PushExpressionEvaluationContext(
13713 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013714
13715 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013716 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
13717 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
13718 // uses semantics of argument handles by value, but it should be passed by
13719 // reference. C lang does not support references, so pass all parameters as
13720 // pointers.
13721 // Create 'T omp_in;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000013722 VarDecl *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013723 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013724 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
13725 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
13726 // uses semantics of argument handles by value, but it should be passed by
13727 // reference. C lang does not support references, so pass all parameters as
13728 // pointers.
13729 // Create 'T omp_out;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000013730 VarDecl *OmpOutParm =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013731 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
13732 if (S != nullptr) {
13733 PushOnScopeChains(OmpInParm, S);
13734 PushOnScopeChains(OmpOutParm, S);
13735 } else {
13736 DRD->addDecl(OmpInParm);
13737 DRD->addDecl(OmpOutParm);
13738 }
Alexey Bataeve6aa4692018-09-13 16:54:05 +000013739 Expr *InE =
13740 ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation());
13741 Expr *OutE =
13742 ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation());
13743 DRD->setCombinerData(InE, OutE);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013744}
13745
13746void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
13747 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13748 DiscardCleanupsInEvaluationContext();
13749 PopExpressionEvaluationContext();
13750
13751 PopDeclContext();
13752 PopFunctionScopeInfo();
13753
13754 if (Combiner != nullptr)
13755 DRD->setCombiner(Combiner);
13756 else
13757 DRD->setInvalidDecl();
13758}
13759
Alexey Bataev070f43a2017-09-06 14:49:58 +000013760VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013761 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13762
13763 // Enter new function scope.
13764 PushFunctionScope();
Reid Kleckner87a31802018-03-12 21:43:02 +000013765 setFunctionHasBranchProtectedScope();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013766
13767 if (S != nullptr)
13768 PushDeclContext(S, DRD);
13769 else
13770 CurContext = DRD;
13771
Faisal Valid143a0c2017-04-01 21:30:49 +000013772 PushExpressionEvaluationContext(
13773 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013774
13775 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013776 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
13777 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
13778 // uses semantics of argument handles by value, but it should be passed by
13779 // reference. C lang does not support references, so pass all parameters as
13780 // pointers.
13781 // Create 'T omp_priv;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000013782 VarDecl *OmpPrivParm =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013783 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013784 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
13785 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
13786 // uses semantics of argument handles by value, but it should be passed by
13787 // reference. C lang does not support references, so pass all parameters as
13788 // pointers.
13789 // Create 'T omp_orig;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000013790 VarDecl *OmpOrigParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013791 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013792 if (S != nullptr) {
13793 PushOnScopeChains(OmpPrivParm, S);
13794 PushOnScopeChains(OmpOrigParm, S);
13795 } else {
13796 DRD->addDecl(OmpPrivParm);
13797 DRD->addDecl(OmpOrigParm);
13798 }
Alexey Bataeve6aa4692018-09-13 16:54:05 +000013799 Expr *OrigE =
13800 ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation());
13801 Expr *PrivE =
13802 ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation());
13803 DRD->setInitializerData(OrigE, PrivE);
Alexey Bataev070f43a2017-09-06 14:49:58 +000013804 return OmpPrivParm;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013805}
13806
Alexey Bataev070f43a2017-09-06 14:49:58 +000013807void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer,
13808 VarDecl *OmpPrivParm) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013809 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13810 DiscardCleanupsInEvaluationContext();
13811 PopExpressionEvaluationContext();
13812
13813 PopDeclContext();
13814 PopFunctionScopeInfo();
13815
Alexey Bataev070f43a2017-09-06 14:49:58 +000013816 if (Initializer != nullptr) {
13817 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit);
13818 } else if (OmpPrivParm->hasInit()) {
13819 DRD->setInitializer(OmpPrivParm->getInit(),
13820 OmpPrivParm->isDirectInit()
13821 ? OMPDeclareReductionDecl::DirectInit
13822 : OMPDeclareReductionDecl::CopyInit);
13823 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013824 DRD->setInvalidDecl();
Alexey Bataev070f43a2017-09-06 14:49:58 +000013825 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013826}
13827
13828Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
13829 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013830 for (Decl *D : DeclReductions.get()) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013831 if (IsValid) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013832 if (S)
13833 PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S,
13834 /*AddToContext=*/false);
13835 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013836 D->setInvalidDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000013837 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013838 }
13839 return DeclReductions;
13840}
13841
Michael Kruse251e1482019-02-01 20:25:04 +000013842TypeResult Sema::ActOnOpenMPDeclareMapperVarDecl(Scope *S, Declarator &D) {
13843 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
13844 QualType T = TInfo->getType();
13845 if (D.isInvalidType())
13846 return true;
13847
13848 if (getLangOpts().CPlusPlus) {
13849 // Check that there are no default arguments (C++ only).
13850 CheckExtraCXXDefaultArguments(D);
13851 }
13852
13853 return CreateParsedType(T, TInfo);
13854}
13855
13856QualType Sema::ActOnOpenMPDeclareMapperType(SourceLocation TyLoc,
13857 TypeResult ParsedType) {
13858 assert(ParsedType.isUsable() && "Expect usable parsed mapper type");
13859
13860 QualType MapperType = GetTypeFromParser(ParsedType.get());
13861 assert(!MapperType.isNull() && "Expect valid mapper type");
13862
13863 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
13864 // The type must be of struct, union or class type in C and C++
13865 if (!MapperType->isStructureOrClassType() && !MapperType->isUnionType()) {
13866 Diag(TyLoc, diag::err_omp_mapper_wrong_type);
13867 return QualType();
13868 }
13869 return MapperType;
13870}
13871
13872OMPDeclareMapperDecl *Sema::ActOnOpenMPDeclareMapperDirectiveStart(
13873 Scope *S, DeclContext *DC, DeclarationName Name, QualType MapperType,
13874 SourceLocation StartLoc, DeclarationName VN, AccessSpecifier AS,
13875 Decl *PrevDeclInScope) {
13876 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPMapperName,
13877 forRedeclarationInCurContext());
13878 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
13879 // A mapper-identifier may not be redeclared in the current scope for the
13880 // same type or for a type that is compatible according to the base language
13881 // rules.
13882 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
13883 OMPDeclareMapperDecl *PrevDMD = nullptr;
13884 bool InCompoundScope = true;
13885 if (S != nullptr) {
13886 // Find previous declaration with the same name not referenced in other
13887 // declarations.
13888 FunctionScopeInfo *ParentFn = getEnclosingFunction();
13889 InCompoundScope =
13890 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
13891 LookupName(Lookup, S);
13892 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
13893 /*AllowInlineNamespace=*/false);
13894 llvm::DenseMap<OMPDeclareMapperDecl *, bool> UsedAsPrevious;
13895 LookupResult::Filter Filter = Lookup.makeFilter();
13896 while (Filter.hasNext()) {
13897 auto *PrevDecl = cast<OMPDeclareMapperDecl>(Filter.next());
13898 if (InCompoundScope) {
13899 auto I = UsedAsPrevious.find(PrevDecl);
13900 if (I == UsedAsPrevious.end())
13901 UsedAsPrevious[PrevDecl] = false;
13902 if (OMPDeclareMapperDecl *D = PrevDecl->getPrevDeclInScope())
13903 UsedAsPrevious[D] = true;
13904 }
13905 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
13906 PrevDecl->getLocation();
13907 }
13908 Filter.done();
13909 if (InCompoundScope) {
13910 for (const auto &PrevData : UsedAsPrevious) {
13911 if (!PrevData.second) {
13912 PrevDMD = PrevData.first;
13913 break;
13914 }
13915 }
13916 }
13917 } else if (PrevDeclInScope) {
13918 auto *PrevDMDInScope = PrevDMD =
13919 cast<OMPDeclareMapperDecl>(PrevDeclInScope);
13920 do {
13921 PreviousRedeclTypes[PrevDMDInScope->getType().getCanonicalType()] =
13922 PrevDMDInScope->getLocation();
13923 PrevDMDInScope = PrevDMDInScope->getPrevDeclInScope();
13924 } while (PrevDMDInScope != nullptr);
13925 }
13926 const auto I = PreviousRedeclTypes.find(MapperType.getCanonicalType());
13927 bool Invalid = false;
13928 if (I != PreviousRedeclTypes.end()) {
13929 Diag(StartLoc, diag::err_omp_declare_mapper_redefinition)
13930 << MapperType << Name;
13931 Diag(I->second, diag::note_previous_definition);
13932 Invalid = true;
13933 }
13934 auto *DMD = OMPDeclareMapperDecl::Create(Context, DC, StartLoc, Name,
13935 MapperType, VN, PrevDMD);
13936 DC->addDecl(DMD);
13937 DMD->setAccess(AS);
13938 if (Invalid)
13939 DMD->setInvalidDecl();
13940
13941 // Enter new function scope.
13942 PushFunctionScope();
13943 setFunctionHasBranchProtectedScope();
13944
13945 CurContext = DMD;
13946
13947 return DMD;
13948}
13949
13950void Sema::ActOnOpenMPDeclareMapperDirectiveVarDecl(OMPDeclareMapperDecl *DMD,
13951 Scope *S,
13952 QualType MapperType,
13953 SourceLocation StartLoc,
13954 DeclarationName VN) {
13955 VarDecl *VD = buildVarDecl(*this, StartLoc, MapperType, VN.getAsString());
13956 if (S)
13957 PushOnScopeChains(VD, S);
13958 else
13959 DMD->addDecl(VD);
13960 Expr *MapperVarRefExpr = buildDeclRefExpr(*this, VD, MapperType, StartLoc);
13961 DMD->setMapperVarRef(MapperVarRefExpr);
13962}
13963
13964Sema::DeclGroupPtrTy
13965Sema::ActOnOpenMPDeclareMapperDirectiveEnd(OMPDeclareMapperDecl *D, Scope *S,
13966 ArrayRef<OMPClause *> ClauseList) {
13967 PopDeclContext();
13968 PopFunctionScopeInfo();
13969
13970 if (D) {
13971 if (S)
13972 PushOnScopeChains(D, S, /*AddToContext=*/false);
13973 D->CreateClauses(Context, ClauseList);
13974 }
13975
13976 return DeclGroupPtrTy::make(DeclGroupRef(D));
13977}
13978
David Majnemer9d168222016-08-05 17:44:54 +000013979OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
Kelvin Li099bb8c2015-11-24 20:50:12 +000013980 SourceLocation StartLoc,
13981 SourceLocation LParenLoc,
13982 SourceLocation EndLoc) {
13983 Expr *ValExpr = NumTeams;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000013984 Stmt *HelperValStmt = nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000013985
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013986 // OpenMP [teams Constrcut, Restrictions]
13987 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000013988 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
Alexey Bataeva0569352015-12-01 10:17:31 +000013989 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013990 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000013991
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000013992 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000013993 OpenMPDirectiveKind CaptureRegion =
13994 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams);
13995 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000013996 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000013997 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000013998 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
13999 HelperValStmt = buildPreInits(Context, Captures);
14000 }
14001
14002 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion,
14003 StartLoc, LParenLoc, EndLoc);
Kelvin Li099bb8c2015-11-24 20:50:12 +000014004}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000014005
14006OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
14007 SourceLocation StartLoc,
14008 SourceLocation LParenLoc,
14009 SourceLocation EndLoc) {
14010 Expr *ValExpr = ThreadLimit;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000014011 Stmt *HelperValStmt = nullptr;
Kelvin Lia15fb1a2015-11-27 18:47:36 +000014012
14013 // OpenMP [teams Constrcut, Restrictions]
14014 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000014015 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
Alexey Bataeva0569352015-12-01 10:17:31 +000014016 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000014017 return nullptr;
14018
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000014019 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000014020 OpenMPDirectiveKind CaptureRegion =
14021 getOpenMPCaptureRegionForClause(DKind, OMPC_thread_limit);
14022 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000014023 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000014024 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000014025 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
14026 HelperValStmt = buildPreInits(Context, Captures);
14027 }
14028
14029 return new (Context) OMPThreadLimitClause(
14030 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
Kelvin Lia15fb1a2015-11-27 18:47:36 +000014031}
Alexey Bataeva0569352015-12-01 10:17:31 +000014032
14033OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
14034 SourceLocation StartLoc,
14035 SourceLocation LParenLoc,
14036 SourceLocation EndLoc) {
14037 Expr *ValExpr = Priority;
14038
14039 // OpenMP [2.9.1, task Constrcut]
14040 // The priority-value is a non-negative numerical scalar expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000014041 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
Alexey Bataeva0569352015-12-01 10:17:31 +000014042 /*StrictlyPositive=*/false))
14043 return nullptr;
14044
14045 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
14046}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000014047
14048OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
14049 SourceLocation StartLoc,
14050 SourceLocation LParenLoc,
14051 SourceLocation EndLoc) {
14052 Expr *ValExpr = Grainsize;
14053
14054 // OpenMP [2.9.2, taskloop Constrcut]
14055 // The parameter of the grainsize clause must be a positive integer
14056 // expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000014057 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000014058 /*StrictlyPositive=*/true))
14059 return nullptr;
14060
14061 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
14062}
Alexey Bataev382967a2015-12-08 12:06:20 +000014063
14064OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
14065 SourceLocation StartLoc,
14066 SourceLocation LParenLoc,
14067 SourceLocation EndLoc) {
14068 Expr *ValExpr = NumTasks;
14069
14070 // OpenMP [2.9.2, taskloop Constrcut]
14071 // The parameter of the num_tasks clause must be a positive integer
14072 // expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000014073 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
Alexey Bataev382967a2015-12-08 12:06:20 +000014074 /*StrictlyPositive=*/true))
14075 return nullptr;
14076
14077 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
14078}
14079
Alexey Bataev28c75412015-12-15 08:19:24 +000014080OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
14081 SourceLocation LParenLoc,
14082 SourceLocation EndLoc) {
14083 // OpenMP [2.13.2, critical construct, Description]
14084 // ... where hint-expression is an integer constant expression that evaluates
14085 // to a valid lock hint.
14086 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
14087 if (HintExpr.isInvalid())
14088 return nullptr;
14089 return new (Context)
14090 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
14091}
14092
Carlo Bertollib4adf552016-01-15 18:50:31 +000014093OMPClause *Sema::ActOnOpenMPDistScheduleClause(
14094 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
14095 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
14096 SourceLocation EndLoc) {
14097 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
14098 std::string Values;
14099 Values += "'";
14100 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
14101 Values += "'";
14102 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
14103 << Values << getOpenMPClauseName(OMPC_dist_schedule);
14104 return nullptr;
14105 }
14106 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000014107 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000014108 if (ChunkSize) {
14109 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
14110 !ChunkSize->isInstantiationDependent() &&
14111 !ChunkSize->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000014112 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
Carlo Bertollib4adf552016-01-15 18:50:31 +000014113 ExprResult Val =
14114 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
14115 if (Val.isInvalid())
14116 return nullptr;
14117
14118 ValExpr = Val.get();
14119
14120 // OpenMP [2.7.1, Restrictions]
14121 // chunk_size must be a loop invariant integer expression with a positive
14122 // value.
14123 llvm::APSInt Result;
14124 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
14125 if (Result.isSigned() && !Result.isStrictlyPositive()) {
14126 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
14127 << "dist_schedule" << ChunkSize->getSourceRange();
14128 return nullptr;
14129 }
Alexey Bataev2ba67042017-11-28 21:11:44 +000014130 } else if (getOpenMPCaptureRegionForClause(
14131 DSAStack->getCurrentDirective(), OMPC_dist_schedule) !=
14132 OMPD_unknown &&
Alexey Bataevb46cdea2016-06-15 11:20:48 +000014133 !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000014134 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000014135 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev5a3af132016-03-29 08:58:54 +000014136 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
14137 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000014138 }
14139 }
14140 }
14141
14142 return new (Context)
14143 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000014144 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000014145}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000014146
14147OMPClause *Sema::ActOnOpenMPDefaultmapClause(
14148 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
14149 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
14150 SourceLocation KindLoc, SourceLocation EndLoc) {
14151 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
David Majnemer9d168222016-08-05 17:44:54 +000014152 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) {
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000014153 std::string Value;
14154 SourceLocation Loc;
14155 Value += "'";
14156 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
14157 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000014158 OMPC_DEFAULTMAP_MODIFIER_tofrom);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000014159 Loc = MLoc;
14160 } else {
14161 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000014162 OMPC_DEFAULTMAP_scalar);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000014163 Loc = KindLoc;
14164 }
14165 Value += "'";
14166 Diag(Loc, diag::err_omp_unexpected_clause_value)
14167 << Value << getOpenMPClauseName(OMPC_defaultmap);
14168 return nullptr;
14169 }
Alexey Bataev2fd0cb22017-10-05 17:51:39 +000014170 DSAStack->setDefaultDMAToFromScalar(StartLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000014171
14172 return new (Context)
14173 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
14174}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000014175
14176bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
14177 DeclContext *CurLexicalContext = getCurLexicalContext();
14178 if (!CurLexicalContext->isFileContext() &&
14179 !CurLexicalContext->isExternCContext() &&
Alexey Bataev502ec492017-10-03 20:00:00 +000014180 !CurLexicalContext->isExternCXXContext() &&
14181 !isa<CXXRecordDecl>(CurLexicalContext) &&
14182 !isa<ClassTemplateDecl>(CurLexicalContext) &&
14183 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) &&
14184 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000014185 Diag(Loc, diag::err_omp_region_not_file_context);
14186 return false;
14187 }
Kelvin Libc38e632018-09-10 02:07:09 +000014188 ++DeclareTargetNestingLevel;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000014189 return true;
14190}
14191
14192void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
Kelvin Libc38e632018-09-10 02:07:09 +000014193 assert(DeclareTargetNestingLevel > 0 &&
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000014194 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
Kelvin Libc38e632018-09-10 02:07:09 +000014195 --DeclareTargetNestingLevel;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000014196}
14197
David Majnemer9d168222016-08-05 17:44:54 +000014198void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope,
14199 CXXScopeSpec &ScopeSpec,
14200 const DeclarationNameInfo &Id,
14201 OMPDeclareTargetDeclAttr::MapTypeTy MT,
14202 NamedDeclSetType &SameDirectiveDecls) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000014203 LookupResult Lookup(*this, Id, LookupOrdinaryName);
14204 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
14205
14206 if (Lookup.isAmbiguous())
14207 return;
14208 Lookup.suppressDiagnostics();
14209
14210 if (!Lookup.isSingleResult()) {
14211 if (TypoCorrection Corrected =
14212 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
14213 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
14214 CTK_ErrorRecovery)) {
14215 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
14216 << Id.getName());
14217 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
14218 return;
14219 }
14220
14221 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
14222 return;
14223 }
14224
14225 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
Alexey Bataev30a78212018-09-11 13:59:10 +000014226 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND) ||
14227 isa<FunctionTemplateDecl>(ND)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000014228 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
14229 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
Alexey Bataev30a78212018-09-11 13:59:10 +000014230 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
14231 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
14232 cast<ValueDecl>(ND));
14233 if (!Res) {
Alexey Bataeve3727102018-04-18 15:57:46 +000014234 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
Dmitry Polukhind69b5052016-05-09 14:59:13 +000014235 ND->addAttr(A);
14236 if (ASTMutationListener *ML = Context.getASTMutationListener())
14237 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
Kelvin Li1ce87c72017-12-12 20:08:12 +000014238 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Id.getLoc());
Alexey Bataev30a78212018-09-11 13:59:10 +000014239 } else if (*Res != MT) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000014240 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
14241 << Id.getName();
14242 }
Alexey Bataeve3727102018-04-18 15:57:46 +000014243 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000014244 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
Alexey Bataeve3727102018-04-18 15:57:46 +000014245 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +000014246}
14247
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000014248static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
14249 Sema &SemaRef, Decl *D) {
Alexey Bataev30a78212018-09-11 13:59:10 +000014250 if (!D || !isa<VarDecl>(D))
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000014251 return;
Alexey Bataev30a78212018-09-11 13:59:10 +000014252 auto *VD = cast<VarDecl>(D);
14253 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
14254 return;
14255 SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context);
14256 SemaRef.Diag(SL, diag::note_used_here) << SR;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000014257}
14258
14259static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
14260 Sema &SemaRef, DSAStackTy *Stack,
14261 ValueDecl *VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +000014262 return VD->hasAttr<OMPDeclareTargetDeclAttr>() ||
14263 checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(),
14264 /*FullCheck=*/false);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000014265}
14266
Kelvin Li1ce87c72017-12-12 20:08:12 +000014267void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D,
14268 SourceLocation IdLoc) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000014269 if (!D || D->isInvalidDecl())
14270 return;
14271 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
Stephen Kellyf2ceec42018-08-09 21:08:08 +000014272 SourceLocation SL = E ? E->getBeginLoc() : D->getLocation();
Alexey Bataeve3727102018-04-18 15:57:46 +000014273 if (auto *VD = dyn_cast<VarDecl>(D)) {
Alexey Bataevc1943e72018-07-09 19:58:08 +000014274 // Only global variables can be marked as declare target.
Alexey Bataev30a78212018-09-11 13:59:10 +000014275 if (!VD->isFileVarDecl() && !VD->isStaticLocal() &&
14276 !VD->isStaticDataMember())
Alexey Bataevc1943e72018-07-09 19:58:08 +000014277 return;
14278 // 2.10.6: threadprivate variable cannot appear in a declare target
14279 // directive.
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000014280 if (DSAStack->isThreadPrivate(VD)) {
14281 Diag(SL, diag::err_omp_threadprivate_in_target);
Alexey Bataeve3727102018-04-18 15:57:46 +000014282 reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000014283 return;
14284 }
14285 }
Alexey Bataev97b72212018-08-14 18:31:20 +000014286 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
14287 D = FTD->getTemplatedDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000014288 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Alexey Bataev30a78212018-09-11 13:59:10 +000014289 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
14290 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD);
14291 if (Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) {
Kelvin Li1ce87c72017-12-12 20:08:12 +000014292 assert(IdLoc.isValid() && "Source location is expected");
14293 Diag(IdLoc, diag::err_omp_function_in_link_clause);
14294 Diag(FD->getLocation(), diag::note_defined_here) << FD;
14295 return;
14296 }
14297 }
Alexey Bataev30a78212018-09-11 13:59:10 +000014298 if (auto *VD = dyn_cast<ValueDecl>(D)) {
14299 // Problem if any with var declared with incomplete type will be reported
14300 // as normal, so no need to check it here.
14301 if ((E || !VD->getType()->isIncompleteType()) &&
14302 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD))
14303 return;
14304 if (!E && !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) {
14305 // Checking declaration inside declare target region.
14306 if (isa<VarDecl>(D) || isa<FunctionDecl>(D) ||
14307 isa<FunctionTemplateDecl>(D)) {
14308 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
14309 Context, OMPDeclareTargetDeclAttr::MT_To);
14310 D->addAttr(A);
14311 if (ASTMutationListener *ML = Context.getASTMutationListener())
14312 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
14313 }
14314 return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000014315 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000014316 }
Alexey Bataev30a78212018-09-11 13:59:10 +000014317 if (!E)
14318 return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000014319 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
14320}
Samuel Antao661c0902016-05-26 17:39:58 +000014321
14322OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
Michael Kruse01f670d2019-02-22 22:29:42 +000014323 CXXScopeSpec &MapperIdScopeSpec,
14324 DeclarationNameInfo &MapperId,
14325 const OMPVarListLocTy &Locs,
14326 ArrayRef<Expr *> UnresolvedMappers) {
Samuel Antao661c0902016-05-26 17:39:58 +000014327 MappableVarListInfo MVLI(VarList);
Michael Kruse01f670d2019-02-22 22:29:42 +000014328 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, Locs.StartLoc,
14329 MapperIdScopeSpec, MapperId, UnresolvedMappers);
Samuel Antao661c0902016-05-26 17:39:58 +000014330 if (MVLI.ProcessedVarList.empty())
14331 return nullptr;
14332
Michael Kruse01f670d2019-02-22 22:29:42 +000014333 return OMPToClause::Create(
14334 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
14335 MVLI.VarComponents, MVLI.UDMapperList,
14336 MapperIdScopeSpec.getWithLocInContext(Context), MapperId);
Samuel Antao661c0902016-05-26 17:39:58 +000014337}
Samuel Antaoec172c62016-05-26 17:49:04 +000014338
14339OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
Michael Kruse0336c752019-02-25 20:34:15 +000014340 CXXScopeSpec &MapperIdScopeSpec,
14341 DeclarationNameInfo &MapperId,
14342 const OMPVarListLocTy &Locs,
14343 ArrayRef<Expr *> UnresolvedMappers) {
Samuel Antaoec172c62016-05-26 17:49:04 +000014344 MappableVarListInfo MVLI(VarList);
Michael Kruse01f670d2019-02-22 22:29:42 +000014345 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, Locs.StartLoc,
14346 MapperIdScopeSpec, MapperId, UnresolvedMappers);
Samuel Antaoec172c62016-05-26 17:49:04 +000014347 if (MVLI.ProcessedVarList.empty())
14348 return nullptr;
14349
Michael Kruse0336c752019-02-25 20:34:15 +000014350 return OMPFromClause::Create(
14351 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
14352 MVLI.VarComponents, MVLI.UDMapperList,
14353 MapperIdScopeSpec.getWithLocInContext(Context), MapperId);
Samuel Antaoec172c62016-05-26 17:49:04 +000014354}
Carlo Bertolli2404b172016-07-13 15:37:16 +000014355
14356OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
Michael Kruse4304e9d2019-02-19 16:38:20 +000014357 const OMPVarListLocTy &Locs) {
Samuel Antaocc10b852016-07-28 14:23:26 +000014358 MappableVarListInfo MVLI(VarList);
14359 SmallVector<Expr *, 8> PrivateCopies;
14360 SmallVector<Expr *, 8> Inits;
14361
Alexey Bataeve3727102018-04-18 15:57:46 +000014362 for (Expr *RefExpr : VarList) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000014363 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
14364 SourceLocation ELoc;
14365 SourceRange ERange;
14366 Expr *SimpleRefExpr = RefExpr;
14367 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
14368 if (Res.second) {
14369 // It will be analyzed later.
Samuel Antaocc10b852016-07-28 14:23:26 +000014370 MVLI.ProcessedVarList.push_back(RefExpr);
14371 PrivateCopies.push_back(nullptr);
14372 Inits.push_back(nullptr);
Carlo Bertolli2404b172016-07-13 15:37:16 +000014373 }
14374 ValueDecl *D = Res.first;
14375 if (!D)
14376 continue;
14377
14378 QualType Type = D->getType();
Samuel Antaocc10b852016-07-28 14:23:26 +000014379 Type = Type.getNonReferenceType().getUnqualifiedType();
14380
14381 auto *VD = dyn_cast<VarDecl>(D);
14382
14383 // Item should be a pointer or reference to pointer.
14384 if (!Type->isPointerType()) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000014385 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
14386 << 0 << RefExpr->getSourceRange();
14387 continue;
14388 }
Samuel Antaocc10b852016-07-28 14:23:26 +000014389
14390 // Build the private variable and the expression that refers to it.
Alexey Bataev63cc8e92018-03-20 14:45:59 +000014391 auto VDPrivate =
14392 buildVarDecl(*this, ELoc, Type, D->getName(),
14393 D->hasAttrs() ? &D->getAttrs() : nullptr,
14394 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Samuel Antaocc10b852016-07-28 14:23:26 +000014395 if (VDPrivate->isInvalidDecl())
14396 continue;
14397
14398 CurContext->addDecl(VDPrivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000014399 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Samuel Antaocc10b852016-07-28 14:23:26 +000014400 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
14401
14402 // Add temporary variable to initialize the private copy of the pointer.
Alexey Bataeve3727102018-04-18 15:57:46 +000014403 VarDecl *VDInit =
Samuel Antaocc10b852016-07-28 14:23:26 +000014404 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp");
Alexey Bataeve3727102018-04-18 15:57:46 +000014405 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr(
14406 *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc());
Samuel Antaocc10b852016-07-28 14:23:26 +000014407 AddInitializerToDecl(VDPrivate,
14408 DefaultLvalueConversion(VDInitRefExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000014409 /*DirectInit=*/false);
Samuel Antaocc10b852016-07-28 14:23:26 +000014410
14411 // If required, build a capture to implement the privatization initialized
14412 // with the current list item value.
14413 DeclRefExpr *Ref = nullptr;
14414 if (!VD)
14415 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
14416 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
14417 PrivateCopies.push_back(VDPrivateRefExpr);
14418 Inits.push_back(VDInitRefExpr);
14419
14420 // We need to add a data sharing attribute for this variable to make sure it
14421 // is correctly captured. A variable that shows up in a use_device_ptr has
14422 // similar properties of a first private variable.
14423 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
14424
14425 // Create a mappable component for the list item. List items in this clause
14426 // only need a component.
14427 MVLI.VarBaseDeclarations.push_back(D);
14428 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
14429 MVLI.VarComponents.back().push_back(
14430 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D));
Carlo Bertolli2404b172016-07-13 15:37:16 +000014431 }
14432
Samuel Antaocc10b852016-07-28 14:23:26 +000014433 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli2404b172016-07-13 15:37:16 +000014434 return nullptr;
14435
Samuel Antaocc10b852016-07-28 14:23:26 +000014436 return OMPUseDevicePtrClause::Create(
Michael Kruse4304e9d2019-02-19 16:38:20 +000014437 Context, Locs, MVLI.ProcessedVarList, PrivateCopies, Inits,
14438 MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli2404b172016-07-13 15:37:16 +000014439}
Carlo Bertolli70594e92016-07-13 17:16:49 +000014440
14441OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
Michael Kruse4304e9d2019-02-19 16:38:20 +000014442 const OMPVarListLocTy &Locs) {
Samuel Antao6890b092016-07-28 14:25:09 +000014443 MappableVarListInfo MVLI(VarList);
Alexey Bataeve3727102018-04-18 15:57:46 +000014444 for (Expr *RefExpr : VarList) {
Kelvin Li84376252016-12-14 15:39:58 +000014445 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause.");
Carlo Bertolli70594e92016-07-13 17:16:49 +000014446 SourceLocation ELoc;
14447 SourceRange ERange;
14448 Expr *SimpleRefExpr = RefExpr;
14449 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
14450 if (Res.second) {
14451 // It will be analyzed later.
Samuel Antao6890b092016-07-28 14:25:09 +000014452 MVLI.ProcessedVarList.push_back(RefExpr);
Carlo Bertolli70594e92016-07-13 17:16:49 +000014453 }
14454 ValueDecl *D = Res.first;
14455 if (!D)
14456 continue;
14457
14458 QualType Type = D->getType();
14459 // item should be a pointer or array or reference to pointer or array
14460 if (!Type.getNonReferenceType()->isPointerType() &&
14461 !Type.getNonReferenceType()->isArrayType()) {
14462 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
14463 << 0 << RefExpr->getSourceRange();
14464 continue;
14465 }
Samuel Antao6890b092016-07-28 14:25:09 +000014466
14467 // Check if the declaration in the clause does not show up in any data
14468 // sharing attribute.
Alexey Bataeve3727102018-04-18 15:57:46 +000014469 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Samuel Antao6890b092016-07-28 14:25:09 +000014470 if (isOpenMPPrivate(DVar.CKind)) {
14471 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
14472 << getOpenMPClauseName(DVar.CKind)
14473 << getOpenMPClauseName(OMPC_is_device_ptr)
14474 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000014475 reportOriginalDsa(*this, DSAStack, D, DVar);
Samuel Antao6890b092016-07-28 14:25:09 +000014476 continue;
14477 }
14478
Alexey Bataeve3727102018-04-18 15:57:46 +000014479 const Expr *ConflictExpr;
Samuel Antao6890b092016-07-28 14:25:09 +000014480 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +000014481 D, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +000014482 [&ConflictExpr](
14483 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
14484 OpenMPClauseKind) -> bool {
14485 ConflictExpr = R.front().getAssociatedExpression();
14486 return true;
14487 })) {
14488 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
14489 Diag(ConflictExpr->getExprLoc(), diag::note_used_here)
14490 << ConflictExpr->getSourceRange();
14491 continue;
14492 }
14493
14494 // Store the components in the stack so that they can be used to check
14495 // against other clauses later on.
14496 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D);
14497 DSAStack->addMappableExpressionComponents(
14498 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
14499
14500 // Record the expression we've just processed.
14501 MVLI.ProcessedVarList.push_back(SimpleRefExpr);
14502
14503 // Create a mappable component for the list item. List items in this clause
14504 // only need a component. We use a null declaration to signal fields in
14505 // 'this'.
14506 assert((isa<DeclRefExpr>(SimpleRefExpr) ||
14507 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
14508 "Unexpected device pointer expression!");
14509 MVLI.VarBaseDeclarations.push_back(
14510 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr);
14511 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
14512 MVLI.VarComponents.back().push_back(MC);
Carlo Bertolli70594e92016-07-13 17:16:49 +000014513 }
14514
Samuel Antao6890b092016-07-28 14:25:09 +000014515 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli70594e92016-07-13 17:16:49 +000014516 return nullptr;
14517
Michael Kruse4304e9d2019-02-19 16:38:20 +000014518 return OMPIsDevicePtrClause::Create(Context, Locs, MVLI.ProcessedVarList,
14519 MVLI.VarBaseDeclarations,
14520 MVLI.VarComponents);
Carlo Bertolli70594e92016-07-13 17:16:49 +000014521}