blob: 68ee887db3c7d5e3934feb5712dc8b8b62fc82fe [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 }
4761 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
Alexey Bataeva8899172015-08-06 12:30:57 +00004762 DefaultLoc);
4763}
4764
Alexey Bataeve3727102018-04-18 15:57:46 +00004765Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004766 if (LCDecl && !LCDecl->isInvalidDecl()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004767 QualType Type = LCDecl->getType().getNonReferenceType();
4768 VarDecl *PrivateVar = buildVarDecl(
Alexey Bataev63cc8e92018-03-20 14:45:59 +00004769 SemaRef, DefaultLoc, Type, LCDecl->getName(),
4770 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr,
4771 isa<VarDecl>(LCDecl)
4772 ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc)
4773 : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00004774 if (PrivateVar->isInvalidDecl())
4775 return nullptr;
4776 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4777 }
4778 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004779}
4780
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004781/// Build initialization of the counter to be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004782Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004783
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004784/// Build step of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004785Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004786
Alexey Bataevf138fda2018-08-13 19:04:24 +00004787Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData(
4788 Scope *S, Expr *Counter,
4789 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc,
4790 Expr *Inc, OverloadedOperatorKind OOK) {
4791 Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get();
4792 if (!Cnt)
4793 return nullptr;
4794 if (Inc) {
4795 assert((OOK == OO_Plus || OOK == OO_Minus) &&
4796 "Expected only + or - operations for depend clauses.");
4797 BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub;
4798 Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get();
4799 if (!Cnt)
4800 return nullptr;
4801 }
4802 ExprResult Diff;
4803 QualType VarType = LCDecl->getType().getNonReferenceType();
4804 if (VarType->isIntegerType() || VarType->isPointerType() ||
4805 SemaRef.getLangOpts().CPlusPlus) {
4806 // Upper - Lower
Alexey Bataev316ccf62019-01-29 18:51:58 +00004807 Expr *Upper = TestIsLessOp.getValue()
4808 ? Cnt
4809 : tryBuildCapture(SemaRef, UB, Captures).get();
4810 Expr *Lower = TestIsLessOp.getValue()
4811 ? tryBuildCapture(SemaRef, LB, Captures).get()
4812 : Cnt;
Alexey Bataevf138fda2018-08-13 19:04:24 +00004813 if (!Upper || !Lower)
4814 return nullptr;
4815
4816 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4817
4818 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
4819 // BuildBinOp already emitted error, this one is to point user to upper
4820 // and lower bound, and to tell what is passed to 'operator-'.
4821 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
4822 << Upper->getSourceRange() << Lower->getSourceRange();
4823 return nullptr;
4824 }
4825 }
4826
4827 if (!Diff.isUsable())
4828 return nullptr;
4829
4830 // Parentheses (for dumping/debugging purposes only).
4831 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4832 if (!Diff.isUsable())
4833 return nullptr;
4834
4835 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
4836 if (!NewStep.isUsable())
4837 return nullptr;
4838 // (Upper - Lower) / Step
4839 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
4840 if (!Diff.isUsable())
4841 return nullptr;
4842
4843 return Diff.get();
4844}
4845
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004846/// Iteration space of a single for loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004847struct LoopIterationSpace final {
Alexey Bataev316ccf62019-01-29 18:51:58 +00004848 /// True if the condition operator is the strict compare operator (<, > or
4849 /// !=).
4850 bool IsStrictCompare = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004851 /// Condition of the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004852 Expr *PreCond = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004853 /// This expression calculates the number of iterations in the loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004854 /// It is always possible to calculate it before starting the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004855 Expr *NumIterations = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004856 /// The loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004857 Expr *CounterVar = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004858 /// Private loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004859 Expr *PrivateCounterVar = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004860 /// This is initializer for the initial value of #CounterVar.
Alexey Bataev8b427062016-05-25 12:36:08 +00004861 Expr *CounterInit = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004862 /// This is step for the #CounterVar used to generate its update:
Alexander Musmana5f070a2014-10-01 06:03:56 +00004863 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
Alexey Bataev8b427062016-05-25 12:36:08 +00004864 Expr *CounterStep = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004865 /// Should step be subtracted?
Alexey Bataev8b427062016-05-25 12:36:08 +00004866 bool Subtract = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004867 /// Source range of the loop init.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004868 SourceRange InitSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004869 /// Source range of the loop condition.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004870 SourceRange CondSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004871 /// Source range of the loop increment.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004872 SourceRange IncSrcRange;
4873};
4874
Alexey Bataev23b69422014-06-18 07:08:49 +00004875} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004876
Alexey Bataev9c821032015-04-30 04:23:23 +00004877void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4878 assert(getLangOpts().OpenMP && "OpenMP is not active.");
4879 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004880 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4881 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00004882 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevce901812018-12-19 18:16:37 +00004883 DSAStack->loopStart();
Alexey Bataev9c821032015-04-30 04:23:23 +00004884 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataeve3727102018-04-18 15:57:46 +00004885 if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) {
4886 if (ValueDecl *D = ISC.getLoopDecl()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004887 auto *VD = dyn_cast<VarDecl>(D);
4888 if (!VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004889 if (VarDecl *Private = isOpenMPCapturedDecl(D)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004890 VD = Private;
Alexey Bataeve3727102018-04-18 15:57:46 +00004891 } else {
4892 DeclRefExpr *Ref = buildCapture(*this, D, ISC.getLoopDeclRefExpr(),
4893 /*WithInit=*/false);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004894 VD = cast<VarDecl>(Ref->getDecl());
4895 }
4896 }
4897 DSAStack->addLoopControlVariable(D, VD);
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00004898 const Decl *LD = DSAStack->getPossiblyLoopCunter();
4899 if (LD != D->getCanonicalDecl()) {
4900 DSAStack->resetPossibleLoopCounter();
4901 if (auto *Var = dyn_cast_or_null<VarDecl>(LD))
4902 MarkDeclarationsReferencedInExpr(
4903 buildDeclRefExpr(*this, const_cast<VarDecl *>(Var),
4904 Var->getType().getNonLValueExprType(Context),
4905 ForLoc, /*RefersToCapture=*/true));
4906 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004907 }
4908 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004909 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00004910 }
4911}
4912
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004913/// Called on a for stmt to check and extract its iteration space
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004914/// for further processing (such as collapsing).
Alexey Bataeve3727102018-04-18 15:57:46 +00004915static bool checkOpenMPIterationSpace(
Alexey Bataev4acb8592014-07-07 13:01:15 +00004916 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4917 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataevf138fda2018-08-13 19:04:24 +00004918 unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr,
4919 Expr *OrderedLoopCountExpr,
Alexey Bataeve3727102018-04-18 15:57:46 +00004920 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004921 LoopIterationSpace &ResultIterSpace,
Alexey Bataeve3727102018-04-18 15:57:46 +00004922 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004923 // OpenMP [2.6, Canonical Loop Form]
4924 // for (init-expr; test-expr; incr-expr) structured-block
David Majnemer9d168222016-08-05 17:44:54 +00004925 auto *For = dyn_cast_or_null<ForStmt>(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004926 if (!For) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004927 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004928 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
Alexey Bataevf138fda2018-08-13 19:04:24 +00004929 << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount
Alexey Bataev10e775f2015-07-30 11:36:16 +00004930 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
Alexey Bataevf138fda2018-08-13 19:04:24 +00004931 if (TotalNestedLoopCount > 1) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00004932 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4933 SemaRef.Diag(DSA.getConstructLoc(),
4934 diag::note_omp_collapse_ordered_expr)
4935 << 2 << CollapseLoopCountExpr->getSourceRange()
4936 << OrderedLoopCountExpr->getSourceRange();
4937 else if (CollapseLoopCountExpr)
4938 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4939 diag::note_omp_collapse_ordered_expr)
4940 << 0 << CollapseLoopCountExpr->getSourceRange();
4941 else
4942 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4943 diag::note_omp_collapse_ordered_expr)
4944 << 1 << OrderedLoopCountExpr->getSourceRange();
4945 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004946 return true;
4947 }
4948 assert(For->getBody());
4949
4950 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4951
4952 // Check init.
Alexey Bataeve3727102018-04-18 15:57:46 +00004953 Stmt *Init = For->getInit();
4954 if (ISC.checkAndSetInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004955 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004956
4957 bool HasErrors = false;
4958
4959 // Check loop variable's type.
Alexey Bataeve3727102018-04-18 15:57:46 +00004960 if (ValueDecl *LCDecl = ISC.getLoopDecl()) {
4961 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004962
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004963 // OpenMP [2.6, Canonical Loop Form]
4964 // Var is one of the following:
4965 // A variable of signed or unsigned integer type.
4966 // For C++, a variable of a random access iterator type.
4967 // For C, a variable of a pointer type.
Alexey Bataeve3727102018-04-18 15:57:46 +00004968 QualType VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004969 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4970 !VarType->isPointerType() &&
4971 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004972 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004973 << SemaRef.getLangOpts().CPlusPlus;
4974 HasErrors = true;
4975 }
4976
4977 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4978 // a Construct
4979 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4980 // parallel for construct is (are) private.
4981 // The loop iteration variable in the associated for-loop of a simd
4982 // construct with just one associated for-loop is linear with a
4983 // constant-linear-step that is the increment of the associated for-loop.
4984 // Exclude loop var from the list of variables with implicitly defined data
4985 // sharing attributes.
4986 VarsWithImplicitDSA.erase(LCDecl);
4987
4988 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4989 // in a Construct, C/C++].
4990 // The loop iteration variable in the associated for-loop of a simd
4991 // construct with just one associated for-loop may be listed in a linear
4992 // clause with a constant-linear-step that is the increment of the
4993 // associated for-loop.
4994 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4995 // parallel for construct may be listed in a private or lastprivate clause.
4996 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4997 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4998 // declared in the loop and it is predetermined as a private.
Alexey Bataeve3727102018-04-18 15:57:46 +00004999 OpenMPClauseKind PredeterminedCKind =
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005000 isOpenMPSimdDirective(DKind)
5001 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
5002 : OMPC_private;
5003 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
5004 DVar.CKind != PredeterminedCKind) ||
5005 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
5006 isOpenMPDistributeDirective(DKind)) &&
5007 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
5008 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
5009 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005010 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005011 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
5012 << getOpenMPClauseName(PredeterminedCKind);
5013 if (DVar.RefExpr == nullptr)
5014 DVar.CKind = PredeterminedCKind;
Alexey Bataeve3727102018-04-18 15:57:46 +00005015 reportOriginalDsa(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005016 HasErrors = true;
5017 } else if (LoopDeclRefExpr != nullptr) {
5018 // Make the loop iteration variable private (for worksharing constructs),
5019 // linear (for simd directives with the only one associated loop) or
5020 // lastprivate (for simd directives with several collapsed or ordered
5021 // loops).
5022 if (DVar.CKind == OMPC_unknown)
Alexey Bataevc2cdff62019-01-29 21:12:28 +00005023 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005024 }
5025
5026 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
5027
5028 // Check test-expr.
Alexey Bataeve3727102018-04-18 15:57:46 +00005029 HasErrors |= ISC.checkAndSetCond(For->getCond());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00005030
5031 // Check incr-expr.
Alexey Bataeve3727102018-04-18 15:57:46 +00005032 HasErrors |= ISC.checkAndSetInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005033 }
5034
Alexey Bataeve3727102018-04-18 15:57:46 +00005035 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005036 return HasErrors;
5037
Alexander Musmana5f070a2014-10-01 06:03:56 +00005038 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00005039 ResultIterSpace.PreCond =
Alexey Bataeve3727102018-04-18 15:57:46 +00005040 ISC.buildPreCond(DSA.getCurScope(), For->getCond(), Captures);
5041 ResultIterSpace.NumIterations = ISC.buildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00005042 DSA.getCurScope(),
5043 (isOpenMPWorksharingDirective(DKind) ||
5044 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
5045 Captures);
Alexey Bataeve3727102018-04-18 15:57:46 +00005046 ResultIterSpace.CounterVar = ISC.buildCounterVar(Captures, DSA);
5047 ResultIterSpace.PrivateCounterVar = ISC.buildPrivateCounterVar();
5048 ResultIterSpace.CounterInit = ISC.buildCounterInit();
5049 ResultIterSpace.CounterStep = ISC.buildCounterStep();
5050 ResultIterSpace.InitSrcRange = ISC.getInitSrcRange();
5051 ResultIterSpace.CondSrcRange = ISC.getConditionSrcRange();
5052 ResultIterSpace.IncSrcRange = ISC.getIncrementSrcRange();
5053 ResultIterSpace.Subtract = ISC.shouldSubtractStep();
Alexey Bataev316ccf62019-01-29 18:51:58 +00005054 ResultIterSpace.IsStrictCompare = ISC.isStrictTestOp();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005055
Alexey Bataev62dbb972015-04-22 11:59:37 +00005056 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
5057 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00005058 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00005059 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00005060 ResultIterSpace.CounterInit == nullptr ||
5061 ResultIterSpace.CounterStep == nullptr);
Alexey Bataevf138fda2018-08-13 19:04:24 +00005062 if (!HasErrors && DSA.isOrderedRegion()) {
5063 if (DSA.getOrderedRegionParam().second->getNumForLoops()) {
5064 if (CurrentNestedLoopCount <
5065 DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) {
5066 DSA.getOrderedRegionParam().second->setLoopNumIterations(
5067 CurrentNestedLoopCount, ResultIterSpace.NumIterations);
5068 DSA.getOrderedRegionParam().second->setLoopCounter(
5069 CurrentNestedLoopCount, ResultIterSpace.CounterVar);
5070 }
5071 }
5072 for (auto &Pair : DSA.getDoacrossDependClauses()) {
5073 if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) {
5074 // Erroneous case - clause has some problems.
5075 continue;
5076 }
5077 if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink &&
5078 Pair.second.size() <= CurrentNestedLoopCount) {
5079 // Erroneous case - clause has some problems.
5080 Pair.first->setLoopData(CurrentNestedLoopCount, nullptr);
5081 continue;
5082 }
5083 Expr *CntValue;
5084 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
5085 CntValue = ISC.buildOrderedLoopData(
5086 DSA.getCurScope(), ResultIterSpace.CounterVar, Captures,
5087 Pair.first->getDependencyLoc());
5088 else
5089 CntValue = ISC.buildOrderedLoopData(
5090 DSA.getCurScope(), ResultIterSpace.CounterVar, Captures,
5091 Pair.first->getDependencyLoc(),
5092 Pair.second[CurrentNestedLoopCount].first,
5093 Pair.second[CurrentNestedLoopCount].second);
5094 Pair.first->setLoopData(CurrentNestedLoopCount, CntValue);
5095 }
5096 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005097
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005098 return HasErrors;
5099}
5100
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005101/// Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00005102static ExprResult
Alexey Bataeve3727102018-04-18 15:57:46 +00005103buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005104 ExprResult Start,
Alexey Bataeve3727102018-04-18 15:57:46 +00005105 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005106 // Build 'VarRef = Start.
Alexey Bataeve3727102018-04-18 15:57:46 +00005107 ExprResult NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00005108 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005109 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00005110 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00005111 VarRef.get()->getType())) {
5112 NewStart = SemaRef.PerformImplicitConversion(
5113 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
5114 /*AllowExplicit=*/true);
5115 if (!NewStart.isUsable())
5116 return ExprError();
5117 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005118
Alexey Bataeve3727102018-04-18 15:57:46 +00005119 ExprResult Init =
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005120 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
5121 return Init;
5122}
5123
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005124/// Build 'VarRef = Start + Iter * Step'.
Alexey Bataeve3727102018-04-18 15:57:46 +00005125static ExprResult buildCounterUpdate(
5126 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
5127 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract,
5128 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00005129 // Add parentheses (for debugging purposes only).
5130 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
5131 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
5132 !Step.isUsable())
5133 return ExprError();
5134
Alexey Bataev5a3af132016-03-29 08:58:54 +00005135 ExprResult NewStep = Step;
5136 if (Captures)
5137 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005138 if (NewStep.isInvalid())
5139 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005140 ExprResult Update =
5141 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005142 if (!Update.isUsable())
5143 return ExprError();
5144
Alexey Bataevc0214e02016-02-16 12:13:49 +00005145 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
5146 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00005147 ExprResult NewStart = Start;
5148 if (Captures)
5149 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005150 if (NewStart.isInvalid())
5151 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005152
Alexey Bataevc0214e02016-02-16 12:13:49 +00005153 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
5154 ExprResult SavedUpdate = Update;
5155 ExprResult UpdateVal;
5156 if (VarRef.get()->getType()->isOverloadableType() ||
5157 NewStart.get()->getType()->isOverloadableType() ||
5158 Update.get()->getType()->isOverloadableType()) {
5159 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
5160 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
5161 Update =
5162 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
5163 if (Update.isUsable()) {
5164 UpdateVal =
5165 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
5166 VarRef.get(), SavedUpdate.get());
5167 if (UpdateVal.isUsable()) {
5168 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
5169 UpdateVal.get());
5170 }
5171 }
5172 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
5173 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005174
Alexey Bataevc0214e02016-02-16 12:13:49 +00005175 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
5176 if (!Update.isUsable() || !UpdateVal.isUsable()) {
5177 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
5178 NewStart.get(), SavedUpdate.get());
5179 if (!Update.isUsable())
5180 return ExprError();
5181
Alexey Bataev11481f52016-02-17 10:29:05 +00005182 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
5183 VarRef.get()->getType())) {
5184 Update = SemaRef.PerformImplicitConversion(
5185 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
5186 if (!Update.isUsable())
5187 return ExprError();
5188 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00005189
5190 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
5191 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005192 return Update;
5193}
5194
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005195/// Convert integer expression \a E to make it have at least \a Bits
Alexander Musmana5f070a2014-10-01 06:03:56 +00005196/// bits.
Alexey Bataeve3727102018-04-18 15:57:46 +00005197static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00005198 if (E == nullptr)
5199 return ExprError();
Alexey Bataeve3727102018-04-18 15:57:46 +00005200 ASTContext &C = SemaRef.Context;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005201 QualType OldType = E->getType();
5202 unsigned HasBits = C.getTypeSize(OldType);
5203 if (HasBits >= Bits)
5204 return ExprResult(E);
5205 // OK to convert to signed, because new type has more bits than old.
5206 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
5207 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
5208 true);
5209}
5210
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005211/// Check if the given expression \a E is a constant integer that fits
Alexander Musmana5f070a2014-10-01 06:03:56 +00005212/// into \a Bits bits.
Alexey Bataeve3727102018-04-18 15:57:46 +00005213static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00005214 if (E == nullptr)
5215 return false;
5216 llvm::APSInt Result;
5217 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
5218 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
5219 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005220}
5221
Alexey Bataev5a3af132016-03-29 08:58:54 +00005222/// Build preinits statement for the given declarations.
5223static Stmt *buildPreInits(ASTContext &Context,
Alexey Bataevc5514062017-10-25 15:44:52 +00005224 MutableArrayRef<Decl *> PreInits) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005225 if (!PreInits.empty()) {
5226 return new (Context) DeclStmt(
5227 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
5228 SourceLocation(), SourceLocation());
5229 }
5230 return nullptr;
5231}
5232
5233/// Build preinits statement for the given declarations.
Alexey Bataevc5514062017-10-25 15:44:52 +00005234static Stmt *
5235buildPreInits(ASTContext &Context,
Alexey Bataeve3727102018-04-18 15:57:46 +00005236 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005237 if (!Captures.empty()) {
5238 SmallVector<Decl *, 16> PreInits;
Alexey Bataeve3727102018-04-18 15:57:46 +00005239 for (const auto &Pair : Captures)
Alexey Bataev5a3af132016-03-29 08:58:54 +00005240 PreInits.push_back(Pair.second->getDecl());
5241 return buildPreInits(Context, PreInits);
5242 }
5243 return nullptr;
5244}
5245
5246/// Build postupdate expression for the given list of postupdates expressions.
5247static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
5248 Expr *PostUpdate = nullptr;
5249 if (!PostUpdates.empty()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00005250 for (Expr *E : PostUpdates) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005251 Expr *ConvE = S.BuildCStyleCastExpr(
5252 E->getExprLoc(),
5253 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
5254 E->getExprLoc(), E)
5255 .get();
5256 PostUpdate = PostUpdate
5257 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
5258 PostUpdate, ConvE)
5259 .get()
5260 : ConvE;
5261 }
5262 }
5263 return PostUpdate;
5264}
5265
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005266/// Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00005267/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
5268/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005269static unsigned
Alexey Bataeve3727102018-04-18 15:57:46 +00005270checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
Alexey Bataev10e775f2015-07-30 11:36:16 +00005271 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
5272 DSAStackTy &DSA,
Alexey Bataeve3727102018-04-18 15:57:46 +00005273 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00005274 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005275 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005276 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005277 // Found 'collapse' clause - calculate collapse number.
Fangrui Song407659a2018-11-30 23:41:18 +00005278 Expr::EvalResult Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005279 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Fangrui Song407659a2018-11-30 23:41:18 +00005280 NestedLoopCount = Result.Val.getInt().getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005281 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00005282 unsigned OrderedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005283 if (OrderedLoopCountExpr) {
5284 // Found 'ordered' clause - calculate collapse number.
Fangrui Song407659a2018-11-30 23:41:18 +00005285 Expr::EvalResult EVResult;
5286 if (OrderedLoopCountExpr->EvaluateAsInt(EVResult, SemaRef.getASTContext())) {
5287 llvm::APSInt Result = EVResult.Val.getInt();
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005288 if (Result.getLimitedValue() < NestedLoopCount) {
5289 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
5290 diag::err_omp_wrong_ordered_loop_count)
5291 << OrderedLoopCountExpr->getSourceRange();
5292 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
5293 diag::note_collapse_loop_count)
5294 << CollapseLoopCountExpr->getSourceRange();
5295 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00005296 OrderedLoopCount = Result.getLimitedValue();
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005297 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005298 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005299 // This is helper routine for loop directives (e.g., 'for', 'simd',
5300 // 'for simd', etc.).
Alexey Bataeve3727102018-04-18 15:57:46 +00005301 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev316ccf62019-01-29 18:51:58 +00005302 SmallVector<LoopIterationSpace, 4> IterSpaces(
5303 std::max(OrderedLoopCount, NestedLoopCount));
Alexander Musmana5f070a2014-10-01 06:03:56 +00005304 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005305 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataevf138fda2018-08-13 19:04:24 +00005306 if (checkOpenMPIterationSpace(
5307 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
5308 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
5309 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt],
5310 Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00005311 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005312 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005313 // OpenMP [2.8.1, simd construct, Restrictions]
5314 // All loops associated with the construct must be perfectly nested; that
5315 // is, there must be no intervening code nor any OpenMP directive between
5316 // any two loops.
5317 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005318 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00005319 for (unsigned Cnt = NestedLoopCount; Cnt < OrderedLoopCount; ++Cnt) {
5320 if (checkOpenMPIterationSpace(
5321 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
5322 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
5323 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt],
5324 Captures))
5325 return 0;
5326 if (Cnt > 0 && IterSpaces[Cnt].CounterVar) {
5327 // Handle initialization of captured loop iterator variables.
5328 auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar);
5329 if (isa<OMPCapturedExprDecl>(DRE->getDecl())) {
5330 Captures[DRE] = DRE;
5331 }
5332 }
5333 // Move on to the next nested for loop, or to the loop body.
5334 // OpenMP [2.8.1, simd construct, Restrictions]
5335 // All loops associated with the construct must be perfectly nested; that
5336 // is, there must be no intervening code nor any OpenMP directive between
5337 // any two loops.
5338 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
5339 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005340
Alexander Musmana5f070a2014-10-01 06:03:56 +00005341 Built.clear(/* size */ NestedLoopCount);
5342
5343 if (SemaRef.CurContext->isDependentContext())
5344 return NestedLoopCount;
5345
5346 // An example of what is generated for the following code:
5347 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00005348 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00005349 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00005350 // for (k = 0; k < NK; ++k)
5351 // for (j = J0; j < NJ; j+=2) {
5352 // <loop body>
5353 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005354 //
5355 // We generate the code below.
5356 // Note: the loop body may be outlined in CodeGen.
5357 // Note: some counters may be C++ classes, operator- is used to find number of
5358 // iterations and operator+= to calculate counter value.
5359 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
5360 // or i64 is currently supported).
5361 //
5362 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
5363 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
5364 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
5365 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
5366 // // similar updates for vars in clauses (e.g. 'linear')
5367 // <loop body (using local i and j)>
5368 // }
5369 // i = NI; // assign final values of counters
5370 // j = NJ;
5371 //
5372
5373 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
5374 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00005375 // Precondition tests if there is at least one iteration (all conditions are
5376 // true).
5377 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexey Bataeve3727102018-04-18 15:57:46 +00005378 Expr *N0 = IterSpaces[0].NumIterations;
5379 ExprResult LastIteration32 =
5380 widenIterationCount(/*Bits=*/32,
5381 SemaRef
5382 .PerformImplicitConversion(
5383 N0->IgnoreImpCasts(), N0->getType(),
5384 Sema::AA_Converting, /*AllowExplicit=*/true)
5385 .get(),
5386 SemaRef);
5387 ExprResult LastIteration64 = widenIterationCount(
5388 /*Bits=*/64,
5389 SemaRef
5390 .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(),
5391 Sema::AA_Converting,
5392 /*AllowExplicit=*/true)
5393 .get(),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005394 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005395
5396 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
5397 return NestedLoopCount;
5398
Alexey Bataeve3727102018-04-18 15:57:46 +00005399 ASTContext &C = SemaRef.Context;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005400 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
5401
5402 Scope *CurScope = DSA.getCurScope();
5403 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00005404 if (PreCond.isUsable()) {
Alexey Bataeva7206b92016-12-20 16:51:02 +00005405 PreCond =
5406 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd,
5407 PreCond.get(), IterSpaces[Cnt].PreCond);
Alexey Bataev62dbb972015-04-22 11:59:37 +00005408 }
Alexey Bataeve3727102018-04-18 15:57:46 +00005409 Expr *N = IterSpaces[Cnt].NumIterations;
Alexey Bataeva7206b92016-12-20 16:51:02 +00005410 SourceLocation Loc = N->getExprLoc();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005411 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
5412 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005413 LastIteration32 = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005414 CurScope, Loc, BO_Mul, LastIteration32.get(),
David Majnemer9d168222016-08-05 17:44:54 +00005415 SemaRef
5416 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5417 Sema::AA_Converting,
5418 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005419 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005420 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005421 LastIteration64 = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005422 CurScope, Loc, BO_Mul, LastIteration64.get(),
David Majnemer9d168222016-08-05 17:44:54 +00005423 SemaRef
5424 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5425 Sema::AA_Converting,
5426 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005427 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005428 }
5429
5430 // Choose either the 32-bit or 64-bit version.
5431 ExprResult LastIteration = LastIteration64;
Gheorghe-Teodor Berceaa3afcf22019-01-09 20:38:35 +00005432 if (SemaRef.getLangOpts().OpenMPOptimisticCollapse ||
5433 (LastIteration32.isUsable() &&
5434 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
5435 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
5436 fitsInto(
5437 /*Bits=*/32,
5438 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
5439 LastIteration64.get(), SemaRef))))
Alexander Musmana5f070a2014-10-01 06:03:56 +00005440 LastIteration = LastIteration32;
Alexey Bataev7292c292016-04-25 12:22:29 +00005441 QualType VType = LastIteration.get()->getType();
5442 QualType RealVType = VType;
5443 QualType StrideVType = VType;
5444 if (isOpenMPTaskLoopDirective(DKind)) {
5445 VType =
5446 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
5447 StrideVType =
5448 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
5449 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005450
5451 if (!LastIteration.isUsable())
5452 return 0;
5453
5454 // Save the number of iterations.
5455 ExprResult NumIterations = LastIteration;
5456 {
5457 LastIteration = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005458 CurScope, LastIteration.get()->getExprLoc(), BO_Sub,
5459 LastIteration.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00005460 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5461 if (!LastIteration.isUsable())
5462 return 0;
5463 }
5464
5465 // Calculate the last iteration number beforehand instead of doing this on
5466 // each iteration. Do not do this if the number of iterations may be kfold-ed.
5467 llvm::APSInt Result;
5468 bool IsConstant =
5469 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
5470 ExprResult CalcLastIteration;
5471 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005472 ExprResult SaveRef =
5473 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005474 LastIteration = SaveRef;
5475
5476 // Prepare SaveRef + 1.
5477 NumIterations = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005478 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00005479 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5480 if (!NumIterations.isUsable())
5481 return 0;
5482 }
5483
5484 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
5485
David Majnemer9d168222016-08-05 17:44:54 +00005486 // Build variables passed into runtime, necessary for worksharing directives.
Carlo Bertolliffafe102017-04-20 00:39:39 +00005487 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005488 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5489 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005490 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005491 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
5492 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005493 SemaRef.AddInitializerToDecl(LBDecl,
5494 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5495 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005496
5497 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005498 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
5499 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005500 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
Richard Smith3beb7c62017-01-12 02:27:38 +00005501 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005502
5503 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
5504 // This will be used to implement clause 'lastprivate'.
5505 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005506 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
5507 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005508 SemaRef.AddInitializerToDecl(ILDecl,
5509 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5510 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005511
5512 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev7292c292016-04-25 12:22:29 +00005513 VarDecl *STDecl =
5514 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
5515 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005516 SemaRef.AddInitializerToDecl(STDecl,
5517 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
5518 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005519
5520 // Build expression: UB = min(UB, LastIteration)
David Majnemer9d168222016-08-05 17:44:54 +00005521 // It is necessary for CodeGen of directives with static scheduling.
Alexander Musmanc6388682014-12-15 07:07:06 +00005522 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
5523 UB.get(), LastIteration.get());
5524 ExprResult CondOp = SemaRef.ActOnConditionalOp(
Alexey Bataev86ec3fe2018-07-25 14:40:26 +00005525 LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(),
5526 LastIteration.get(), UB.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00005527 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
5528 CondOp.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005529 EUB = SemaRef.ActOnFinishFullExpr(EUB.get(), /*DiscardedValue*/ false);
Carlo Bertolli9925f152016-06-27 14:55:37 +00005530
5531 // If we have a combined directive that combines 'distribute', 'for' or
5532 // 'simd' we need to be able to access the bounds of the schedule of the
5533 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
5534 // by scheduling 'distribute' have to be passed to the schedule of 'for'.
5535 if (isOpenMPLoopBoundSharingDirective(DKind)) {
Carlo Bertolliffafe102017-04-20 00:39:39 +00005536 // Lower bound variable, initialized with zero.
5537 VarDecl *CombLBDecl =
5538 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb");
5539 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc);
5540 SemaRef.AddInitializerToDecl(
5541 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5542 /*DirectInit*/ false);
5543
5544 // Upper bound variable, initialized with last iteration number.
5545 VarDecl *CombUBDecl =
5546 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub");
5547 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc);
5548 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(),
5549 /*DirectInit*/ false);
5550
5551 ExprResult CombIsUBGreater = SemaRef.BuildBinOp(
5552 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get());
5553 ExprResult CombCondOp =
5554 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(),
5555 LastIteration.get(), CombUB.get());
5556 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(),
5557 CombCondOp.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005558 CombEUB =
5559 SemaRef.ActOnFinishFullExpr(CombEUB.get(), /*DiscardedValue*/ false);
Carlo Bertolliffafe102017-04-20 00:39:39 +00005560
Alexey Bataeve3727102018-04-18 15:57:46 +00005561 const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005562 // We expect to have at least 2 more parameters than the 'parallel'
5563 // directive does - the lower and upper bounds of the previous schedule.
5564 assert(CD->getNumParams() >= 4 &&
5565 "Unexpected number of parameters in loop combined directive");
5566
5567 // Set the proper type for the bounds given what we learned from the
5568 // enclosed loops.
Alexey Bataeve3727102018-04-18 15:57:46 +00005569 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
5570 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
Carlo Bertolli9925f152016-06-27 14:55:37 +00005571
5572 // Previous lower and upper bounds are obtained from the region
5573 // parameters.
5574 PrevLB =
5575 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
5576 PrevUB =
5577 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
5578 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005579 }
5580
5581 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005582 ExprResult IV;
Carlo Bertolliffafe102017-04-20 00:39:39 +00005583 ExprResult Init, CombInit;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005584 {
Alexey Bataev7292c292016-04-25 12:22:29 +00005585 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
5586 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
David Majnemer9d168222016-08-05 17:44:54 +00005587 Expr *RHS =
5588 (isOpenMPWorksharingDirective(DKind) ||
5589 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
5590 ? LB.get()
5591 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005592 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005593 Init = SemaRef.ActOnFinishFullExpr(Init.get(), /*DiscardedValue*/ false);
Carlo Bertolliffafe102017-04-20 00:39:39 +00005594
5595 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5596 Expr *CombRHS =
5597 (isOpenMPWorksharingDirective(DKind) ||
5598 isOpenMPTaskLoopDirective(DKind) ||
5599 isOpenMPDistributeDirective(DKind))
5600 ? CombLB.get()
5601 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
5602 CombInit =
5603 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS);
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005604 CombInit =
5605 SemaRef.ActOnFinishFullExpr(CombInit.get(), /*DiscardedValue*/ false);
Carlo Bertolliffafe102017-04-20 00:39:39 +00005606 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005607 }
5608
Alexey Bataev316ccf62019-01-29 18:51:58 +00005609 bool UseStrictCompare =
5610 RealVType->hasUnsignedIntegerRepresentation() &&
5611 llvm::all_of(IterSpaces, [](const LoopIterationSpace &LIS) {
5612 return LIS.IsStrictCompare;
5613 });
5614 // Loop condition (IV < NumIterations) or (IV <= UB or IV < UB + 1 (for
5615 // unsigned IV)) for worksharing loops.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005616 SourceLocation CondLoc = AStmt->getBeginLoc();
Alexey Bataev316ccf62019-01-29 18:51:58 +00005617 Expr *BoundUB = UB.get();
5618 if (UseStrictCompare) {
5619 BoundUB =
5620 SemaRef
5621 .BuildBinOp(CurScope, CondLoc, BO_Add, BoundUB,
5622 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get())
5623 .get();
5624 BoundUB =
5625 SemaRef.ActOnFinishFullExpr(BoundUB, /*DiscardedValue*/ false).get();
5626 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005627 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005628 (isOpenMPWorksharingDirective(DKind) ||
5629 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexey Bataev316ccf62019-01-29 18:51:58 +00005630 ? SemaRef.BuildBinOp(CurScope, CondLoc,
5631 UseStrictCompare ? BO_LT : BO_LE, IV.get(),
5632 BoundUB)
Alexander Musmanc6388682014-12-15 07:07:06 +00005633 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
5634 NumIterations.get());
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005635 ExprResult CombDistCond;
5636 if (isOpenMPLoopBoundSharingDirective(DKind)) {
Alexey Bataev316ccf62019-01-29 18:51:58 +00005637 CombDistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
5638 NumIterations.get());
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005639 }
5640
Carlo Bertolliffafe102017-04-20 00:39:39 +00005641 ExprResult CombCond;
5642 if (isOpenMPLoopBoundSharingDirective(DKind)) {
Alexey Bataev316ccf62019-01-29 18:51:58 +00005643 Expr *BoundCombUB = CombUB.get();
5644 if (UseStrictCompare) {
5645 BoundCombUB =
5646 SemaRef
5647 .BuildBinOp(
5648 CurScope, CondLoc, BO_Add, BoundCombUB,
5649 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get())
5650 .get();
5651 BoundCombUB =
5652 SemaRef.ActOnFinishFullExpr(BoundCombUB, /*DiscardedValue*/ false)
5653 .get();
5654 }
Carlo Bertolliffafe102017-04-20 00:39:39 +00005655 CombCond =
Alexey Bataev316ccf62019-01-29 18:51:58 +00005656 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE,
5657 IV.get(), BoundCombUB);
Carlo Bertolliffafe102017-04-20 00:39:39 +00005658 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005659 // Loop increment (IV = IV + 1)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005660 SourceLocation IncLoc = AStmt->getBeginLoc();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005661 ExprResult Inc =
5662 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
5663 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
5664 if (!Inc.isUsable())
5665 return 0;
5666 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005667 Inc = SemaRef.ActOnFinishFullExpr(Inc.get(), /*DiscardedValue*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005668 if (!Inc.isUsable())
5669 return 0;
5670
5671 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
5672 // Used for directives with static scheduling.
Carlo Bertolliffafe102017-04-20 00:39:39 +00005673 // In combined construct, add combined version that use CombLB and CombUB
5674 // base variables for the update
5675 ExprResult NextLB, NextUB, CombNextLB, CombNextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005676 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5677 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005678 // LB + ST
5679 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
5680 if (!NextLB.isUsable())
5681 return 0;
5682 // LB = LB + ST
5683 NextLB =
5684 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005685 NextLB =
5686 SemaRef.ActOnFinishFullExpr(NextLB.get(), /*DiscardedValue*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005687 if (!NextLB.isUsable())
5688 return 0;
5689 // UB + ST
5690 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
5691 if (!NextUB.isUsable())
5692 return 0;
5693 // UB = UB + ST
5694 NextUB =
5695 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005696 NextUB =
5697 SemaRef.ActOnFinishFullExpr(NextUB.get(), /*DiscardedValue*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005698 if (!NextUB.isUsable())
5699 return 0;
Carlo Bertolliffafe102017-04-20 00:39:39 +00005700 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5701 CombNextLB =
5702 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get());
5703 if (!NextLB.isUsable())
5704 return 0;
5705 // LB = LB + ST
5706 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(),
5707 CombNextLB.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005708 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get(),
5709 /*DiscardedValue*/ false);
Carlo Bertolliffafe102017-04-20 00:39:39 +00005710 if (!CombNextLB.isUsable())
5711 return 0;
5712 // UB + ST
5713 CombNextUB =
5714 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get());
5715 if (!CombNextUB.isUsable())
5716 return 0;
5717 // UB = UB + ST
5718 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(),
5719 CombNextUB.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005720 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get(),
5721 /*DiscardedValue*/ false);
Carlo Bertolliffafe102017-04-20 00:39:39 +00005722 if (!CombNextUB.isUsable())
5723 return 0;
5724 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005725 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005726
Carlo Bertolliffafe102017-04-20 00:39:39 +00005727 // Create increment expression for distribute loop when combined in a same
Carlo Bertolli8429d812017-02-17 21:29:13 +00005728 // directive with for as IV = IV + ST; ensure upper bound expression based
5729 // on PrevUB instead of NumIterations - used to implement 'for' when found
5730 // in combination with 'distribute', like in 'distribute parallel for'
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005731 SourceLocation DistIncLoc = AStmt->getBeginLoc();
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005732 ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond;
Carlo Bertolli8429d812017-02-17 21:29:13 +00005733 if (isOpenMPLoopBoundSharingDirective(DKind)) {
Alexey Bataev316ccf62019-01-29 18:51:58 +00005734 DistCond = SemaRef.BuildBinOp(
5735 CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, IV.get(), BoundUB);
Carlo Bertolli8429d812017-02-17 21:29:13 +00005736 assert(DistCond.isUsable() && "distribute cond expr was not built");
5737
5738 DistInc =
5739 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get());
5740 assert(DistInc.isUsable() && "distribute inc expr was not built");
5741 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(),
5742 DistInc.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005743 DistInc =
5744 SemaRef.ActOnFinishFullExpr(DistInc.get(), /*DiscardedValue*/ false);
Carlo Bertolli8429d812017-02-17 21:29:13 +00005745 assert(DistInc.isUsable() && "distribute inc expr was not built");
5746
5747 // Build expression: UB = min(UB, prevUB) for #for in composite or combined
5748 // construct
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005749 SourceLocation DistEUBLoc = AStmt->getBeginLoc();
Carlo Bertolli8429d812017-02-17 21:29:13 +00005750 ExprResult IsUBGreater =
5751 SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get());
5752 ExprResult CondOp = SemaRef.ActOnConditionalOp(
5753 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get());
5754 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(),
5755 CondOp.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005756 PrevEUB =
5757 SemaRef.ActOnFinishFullExpr(PrevEUB.get(), /*DiscardedValue*/ false);
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005758
Alexey Bataev316ccf62019-01-29 18:51:58 +00005759 // Build IV <= PrevUB or IV < PrevUB + 1 for unsigned IV to be used in
5760 // parallel for is in combination with a distribute directive with
5761 // schedule(static, 1)
5762 Expr *BoundPrevUB = PrevUB.get();
5763 if (UseStrictCompare) {
5764 BoundPrevUB =
5765 SemaRef
5766 .BuildBinOp(
5767 CurScope, CondLoc, BO_Add, BoundPrevUB,
5768 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get())
5769 .get();
5770 BoundPrevUB =
5771 SemaRef.ActOnFinishFullExpr(BoundPrevUB, /*DiscardedValue*/ false)
5772 .get();
5773 }
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005774 ParForInDistCond =
Alexey Bataev316ccf62019-01-29 18:51:58 +00005775 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE,
5776 IV.get(), BoundPrevUB);
Carlo Bertolli8429d812017-02-17 21:29:13 +00005777 }
5778
Alexander Musmana5f070a2014-10-01 06:03:56 +00005779 // Build updates and final values of the loop counters.
5780 bool HasErrors = false;
5781 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005782 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005783 Built.Updates.resize(NestedLoopCount);
5784 Built.Finals.resize(NestedLoopCount);
5785 {
Gheorghe-Teodor Bercea677960642019-01-09 20:45:26 +00005786 // We implement the following algorithm for obtaining the
5787 // original loop iteration variable values based on the
5788 // value of the collapsed loop iteration variable IV.
5789 //
5790 // Let n+1 be the number of collapsed loops in the nest.
5791 // Iteration variables (I0, I1, .... In)
5792 // Iteration counts (N0, N1, ... Nn)
5793 //
5794 // Acc = IV;
5795 //
5796 // To compute Ik for loop k, 0 <= k <= n, generate:
5797 // Prod = N(k+1) * N(k+2) * ... * Nn;
5798 // Ik = Acc / Prod;
5799 // Acc -= Ik * Prod;
5800 //
5801 ExprResult Acc = IV;
5802 for (unsigned int Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00005803 LoopIterationSpace &IS = IterSpaces[Cnt];
5804 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005805 ExprResult Iter;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005806
Gheorghe-Teodor Bercea677960642019-01-09 20:45:26 +00005807 // Compute prod
5808 ExprResult Prod =
5809 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
5810 for (unsigned int K = Cnt+1; K < NestedLoopCount; ++K)
5811 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Prod.get(),
5812 IterSpaces[K].NumIterations);
5813
5814 // Iter = Acc / Prod
5815 // If there is at least one more inner loop to avoid
5816 // multiplication by 1.
5817 if (Cnt + 1 < NestedLoopCount)
5818 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div,
5819 Acc.get(), Prod.get());
5820 else
5821 Iter = Acc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005822 if (!Iter.isUsable()) {
5823 HasErrors = true;
5824 break;
5825 }
5826
Gheorghe-Teodor Bercea677960642019-01-09 20:45:26 +00005827 // Update Acc:
5828 // Acc -= Iter * Prod
5829 // Check if there is at least one more inner loop to avoid
5830 // multiplication by 1.
5831 if (Cnt + 1 < NestedLoopCount)
5832 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul,
5833 Iter.get(), Prod.get());
5834 else
5835 Prod = Iter;
5836 Acc = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Sub,
5837 Acc.get(), Prod.get());
5838
Alexey Bataev39f915b82015-05-08 10:41:21 +00005839 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005840 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00005841 DeclRefExpr *CounterVar = buildDeclRefExpr(
5842 SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
5843 /*RefersToCapture=*/true);
5844 ExprResult Init = buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005845 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005846 if (!Init.isUsable()) {
5847 HasErrors = true;
5848 break;
5849 }
Alexey Bataeve3727102018-04-18 15:57:46 +00005850 ExprResult Update = buildCounterUpdate(
Alexey Bataev5a3af132016-03-29 08:58:54 +00005851 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
5852 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005853 if (!Update.isUsable()) {
5854 HasErrors = true;
5855 break;
5856 }
5857
5858 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
Alexey Bataeve3727102018-04-18 15:57:46 +00005859 ExprResult Final = buildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00005860 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005861 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005862 if (!Final.isUsable()) {
5863 HasErrors = true;
5864 break;
5865 }
5866
Alexander Musmana5f070a2014-10-01 06:03:56 +00005867 if (!Update.isUsable() || !Final.isUsable()) {
5868 HasErrors = true;
5869 break;
5870 }
5871 // Save results
5872 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00005873 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005874 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005875 Built.Updates[Cnt] = Update.get();
5876 Built.Finals[Cnt] = Final.get();
5877 }
5878 }
5879
5880 if (HasErrors)
5881 return 0;
5882
5883 // Save results
5884 Built.IterationVarRef = IV.get();
5885 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00005886 Built.NumIterations = NumIterations.get();
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005887 Built.CalcLastIteration = SemaRef
5888 .ActOnFinishFullExpr(CalcLastIteration.get(),
5889 /*DiscardedValue*/ false)
5890 .get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005891 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00005892 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005893 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005894 Built.Init = Init.get();
5895 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005896 Built.LB = LB.get();
5897 Built.UB = UB.get();
5898 Built.IL = IL.get();
5899 Built.ST = ST.get();
5900 Built.EUB = EUB.get();
5901 Built.NLB = NextLB.get();
5902 Built.NUB = NextUB.get();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005903 Built.PrevLB = PrevLB.get();
5904 Built.PrevUB = PrevUB.get();
Carlo Bertolli8429d812017-02-17 21:29:13 +00005905 Built.DistInc = DistInc.get();
5906 Built.PrevEUB = PrevEUB.get();
Carlo Bertolliffafe102017-04-20 00:39:39 +00005907 Built.DistCombinedFields.LB = CombLB.get();
5908 Built.DistCombinedFields.UB = CombUB.get();
5909 Built.DistCombinedFields.EUB = CombEUB.get();
5910 Built.DistCombinedFields.Init = CombInit.get();
5911 Built.DistCombinedFields.Cond = CombCond.get();
5912 Built.DistCombinedFields.NLB = CombNextLB.get();
5913 Built.DistCombinedFields.NUB = CombNextUB.get();
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005914 Built.DistCombinedFields.DistCond = CombDistCond.get();
5915 Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005916
Alexey Bataevabfc0692014-06-25 06:52:00 +00005917 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005918}
5919
Alexey Bataev10e775f2015-07-30 11:36:16 +00005920static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005921 auto CollapseClauses =
5922 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
5923 if (CollapseClauses.begin() != CollapseClauses.end())
5924 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005925 return nullptr;
5926}
5927
Alexey Bataev10e775f2015-07-30 11:36:16 +00005928static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005929 auto OrderedClauses =
5930 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
5931 if (OrderedClauses.begin() != OrderedClauses.end())
5932 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005933 return nullptr;
5934}
5935
Kelvin Lic5609492016-07-15 04:39:07 +00005936static bool checkSimdlenSafelenSpecified(Sema &S,
5937 const ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00005938 const OMPSafelenClause *Safelen = nullptr;
5939 const OMPSimdlenClause *Simdlen = nullptr;
Kelvin Lic5609492016-07-15 04:39:07 +00005940
Alexey Bataeve3727102018-04-18 15:57:46 +00005941 for (const OMPClause *Clause : Clauses) {
Kelvin Lic5609492016-07-15 04:39:07 +00005942 if (Clause->getClauseKind() == OMPC_safelen)
5943 Safelen = cast<OMPSafelenClause>(Clause);
5944 else if (Clause->getClauseKind() == OMPC_simdlen)
5945 Simdlen = cast<OMPSimdlenClause>(Clause);
5946 if (Safelen && Simdlen)
5947 break;
5948 }
5949
5950 if (Simdlen && Safelen) {
Alexey Bataeve3727102018-04-18 15:57:46 +00005951 const Expr *SimdlenLength = Simdlen->getSimdlen();
5952 const Expr *SafelenLength = Safelen->getSafelen();
Kelvin Lic5609492016-07-15 04:39:07 +00005953 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
5954 SimdlenLength->isInstantiationDependent() ||
5955 SimdlenLength->containsUnexpandedParameterPack())
5956 return false;
5957 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
5958 SafelenLength->isInstantiationDependent() ||
5959 SafelenLength->containsUnexpandedParameterPack())
5960 return false;
Fangrui Song407659a2018-11-30 23:41:18 +00005961 Expr::EvalResult SimdlenResult, SafelenResult;
5962 SimdlenLength->EvaluateAsInt(SimdlenResult, S.Context);
5963 SafelenLength->EvaluateAsInt(SafelenResult, S.Context);
5964 llvm::APSInt SimdlenRes = SimdlenResult.Val.getInt();
5965 llvm::APSInt SafelenRes = SafelenResult.Val.getInt();
Kelvin Lic5609492016-07-15 04:39:07 +00005966 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
5967 // If both simdlen and safelen clauses are specified, the value of the
5968 // simdlen parameter must be less than or equal to the value of the safelen
5969 // parameter.
5970 if (SimdlenRes > SafelenRes) {
5971 S.Diag(SimdlenLength->getExprLoc(),
5972 diag::err_omp_wrong_simdlen_safelen_values)
5973 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
5974 return true;
5975 }
Alexey Bataev66b15b52015-08-21 11:14:16 +00005976 }
5977 return false;
5978}
5979
Alexey Bataeve3727102018-04-18 15:57:46 +00005980StmtResult
5981Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
5982 SourceLocation StartLoc, SourceLocation EndLoc,
5983 VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005984 if (!AStmt)
5985 return StmtError();
5986
5987 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005988 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005989 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5990 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00005991 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataev10e775f2015-07-30 11:36:16 +00005992 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5993 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005994 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005995 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005996
Alexander Musmana5f070a2014-10-01 06:03:56 +00005997 assert((CurContext->isDependentContext() || B.builtAll()) &&
5998 "omp simd loop exprs were not built");
5999
Alexander Musman3276a272015-03-21 10:12:56 +00006000 if (!CurContext->isDependentContext()) {
6001 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00006002 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00006003 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexander Musman3276a272015-03-21 10:12:56 +00006004 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006005 B.NumIterations, *this, CurScope,
6006 DSAStack))
Alexander Musman3276a272015-03-21 10:12:56 +00006007 return StmtError();
6008 }
6009 }
6010
Kelvin Lic5609492016-07-15 04:39:07 +00006011 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00006012 return StmtError();
6013
Reid Kleckner87a31802018-03-12 21:43:02 +00006014 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00006015 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
6016 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00006017}
6018
Alexey Bataeve3727102018-04-18 15:57:46 +00006019StmtResult
6020Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
6021 SourceLocation StartLoc, SourceLocation EndLoc,
6022 VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006023 if (!AStmt)
6024 return StmtError();
6025
6026 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00006027 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006028 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6029 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00006030 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataev10e775f2015-07-30 11:36:16 +00006031 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
6032 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00006033 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00006034 return StmtError();
6035
Alexander Musmana5f070a2014-10-01 06:03:56 +00006036 assert((CurContext->isDependentContext() || B.builtAll()) &&
6037 "omp for loop exprs were not built");
6038
Alexey Bataev54acd402015-08-04 11:18:19 +00006039 if (!CurContext->isDependentContext()) {
6040 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00006041 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00006042 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00006043 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006044 B.NumIterations, *this, CurScope,
6045 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00006046 return StmtError();
6047 }
6048 }
6049
Reid Kleckner87a31802018-03-12 21:43:02 +00006050 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00006051 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00006052 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00006053}
6054
Alexander Musmanf82886e2014-09-18 05:12:34 +00006055StmtResult Sema::ActOnOpenMPForSimdDirective(
6056 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00006057 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006058 if (!AStmt)
6059 return StmtError();
6060
6061 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00006062 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006063 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6064 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00006065 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00006066 checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00006067 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6068 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00006069 if (NestedLoopCount == 0)
6070 return StmtError();
6071
Alexander Musmanc6388682014-12-15 07:07:06 +00006072 assert((CurContext->isDependentContext() || B.builtAll()) &&
6073 "omp for simd loop exprs were not built");
6074
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00006075 if (!CurContext->isDependentContext()) {
6076 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00006077 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00006078 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00006079 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006080 B.NumIterations, *this, CurScope,
6081 DSAStack))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00006082 return StmtError();
6083 }
6084 }
6085
Kelvin Lic5609492016-07-15 04:39:07 +00006086 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00006087 return StmtError();
6088
Reid Kleckner87a31802018-03-12 21:43:02 +00006089 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00006090 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
6091 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00006092}
6093
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00006094StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
6095 Stmt *AStmt,
6096 SourceLocation StartLoc,
6097 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006098 if (!AStmt)
6099 return StmtError();
6100
6101 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00006102 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00006103 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00006104 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00006105 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00006106 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00006107 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00006108 return StmtError();
6109 // All associated statements must be '#pragma omp section' except for
6110 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00006111 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00006112 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
6113 if (SectionStmt)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006114 Diag(SectionStmt->getBeginLoc(),
Alexey Bataev1e0498a2014-06-26 08:21:58 +00006115 diag::err_omp_sections_substmt_not_section);
6116 return StmtError();
6117 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006118 cast<OMPSectionDirective>(SectionStmt)
6119 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00006120 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00006121 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006122 Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00006123 return StmtError();
6124 }
6125
Reid Kleckner87a31802018-03-12 21:43:02 +00006126 setFunctionHasBranchProtectedScope();
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00006127
Alexey Bataev25e5b442015-09-15 12:52:43 +00006128 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
6129 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00006130}
6131
Alexey Bataev1e0498a2014-06-26 08:21:58 +00006132StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
6133 SourceLocation StartLoc,
6134 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006135 if (!AStmt)
6136 return StmtError();
6137
6138 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00006139
Reid Kleckner87a31802018-03-12 21:43:02 +00006140 setFunctionHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00006141 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00006142
Alexey Bataev25e5b442015-09-15 12:52:43 +00006143 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
6144 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00006145}
6146
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00006147StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
6148 Stmt *AStmt,
6149 SourceLocation StartLoc,
6150 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006151 if (!AStmt)
6152 return StmtError();
6153
6154 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00006155
Reid Kleckner87a31802018-03-12 21:43:02 +00006156 setFunctionHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00006157
Alexey Bataev3255bf32015-01-19 05:20:46 +00006158 // OpenMP [2.7.3, single Construct, Restrictions]
6159 // The copyprivate clause must not be used with the nowait clause.
Alexey Bataeve3727102018-04-18 15:57:46 +00006160 const OMPClause *Nowait = nullptr;
6161 const OMPClause *Copyprivate = nullptr;
6162 for (const OMPClause *Clause : Clauses) {
Alexey Bataev3255bf32015-01-19 05:20:46 +00006163 if (Clause->getClauseKind() == OMPC_nowait)
6164 Nowait = Clause;
6165 else if (Clause->getClauseKind() == OMPC_copyprivate)
6166 Copyprivate = Clause;
6167 if (Copyprivate && Nowait) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006168 Diag(Copyprivate->getBeginLoc(),
Alexey Bataev3255bf32015-01-19 05:20:46 +00006169 diag::err_omp_single_copyprivate_with_nowait);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006170 Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here);
Alexey Bataev3255bf32015-01-19 05:20:46 +00006171 return StmtError();
6172 }
6173 }
6174
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00006175 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6176}
6177
Alexander Musman80c22892014-07-17 08:54:58 +00006178StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
6179 SourceLocation StartLoc,
6180 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006181 if (!AStmt)
6182 return StmtError();
6183
6184 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00006185
Reid Kleckner87a31802018-03-12 21:43:02 +00006186 setFunctionHasBranchProtectedScope();
Alexander Musman80c22892014-07-17 08:54:58 +00006187
6188 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
6189}
6190
Alexey Bataev28c75412015-12-15 08:19:24 +00006191StmtResult Sema::ActOnOpenMPCriticalDirective(
6192 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
6193 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006194 if (!AStmt)
6195 return StmtError();
6196
6197 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00006198
Alexey Bataev28c75412015-12-15 08:19:24 +00006199 bool ErrorFound = false;
6200 llvm::APSInt Hint;
6201 SourceLocation HintLoc;
6202 bool DependentHint = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00006203 for (const OMPClause *C : Clauses) {
Alexey Bataev28c75412015-12-15 08:19:24 +00006204 if (C->getClauseKind() == OMPC_hint) {
6205 if (!DirName.getName()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006206 Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name);
Alexey Bataev28c75412015-12-15 08:19:24 +00006207 ErrorFound = true;
6208 }
6209 Expr *E = cast<OMPHintClause>(C)->getHint();
6210 if (E->isTypeDependent() || E->isValueDependent() ||
Alexey Bataeve3727102018-04-18 15:57:46 +00006211 E->isInstantiationDependent()) {
Alexey Bataev28c75412015-12-15 08:19:24 +00006212 DependentHint = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006213 } else {
Alexey Bataev28c75412015-12-15 08:19:24 +00006214 Hint = E->EvaluateKnownConstInt(Context);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006215 HintLoc = C->getBeginLoc();
Alexey Bataev28c75412015-12-15 08:19:24 +00006216 }
6217 }
6218 }
6219 if (ErrorFound)
6220 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00006221 const auto Pair = DSAStack->getCriticalWithHint(DirName);
Alexey Bataev28c75412015-12-15 08:19:24 +00006222 if (Pair.first && DirName.getName() && !DependentHint) {
6223 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
6224 Diag(StartLoc, diag::err_omp_critical_with_hint);
Alexey Bataeve3727102018-04-18 15:57:46 +00006225 if (HintLoc.isValid())
Alexey Bataev28c75412015-12-15 08:19:24 +00006226 Diag(HintLoc, diag::note_omp_critical_hint_here)
6227 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +00006228 else
Alexey Bataev28c75412015-12-15 08:19:24 +00006229 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
Alexey Bataeve3727102018-04-18 15:57:46 +00006230 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006231 Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here)
Alexey Bataev28c75412015-12-15 08:19:24 +00006232 << 1
6233 << C->getHint()->EvaluateKnownConstInt(Context).toString(
6234 /*Radix=*/10, /*Signed=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +00006235 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006236 Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1;
Alexey Bataeve3727102018-04-18 15:57:46 +00006237 }
Alexey Bataev28c75412015-12-15 08:19:24 +00006238 }
6239 }
6240
Reid Kleckner87a31802018-03-12 21:43:02 +00006241 setFunctionHasBranchProtectedScope();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00006242
Alexey Bataev28c75412015-12-15 08:19:24 +00006243 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
6244 Clauses, AStmt);
6245 if (!Pair.first && DirName.getName() && !DependentHint)
6246 DSAStack->addCriticalWithHint(Dir, Hint);
6247 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00006248}
6249
Alexey Bataev4acb8592014-07-07 13:01:15 +00006250StmtResult Sema::ActOnOpenMPParallelForDirective(
6251 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00006252 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006253 if (!AStmt)
6254 return StmtError();
6255
Alexey Bataeve3727102018-04-18 15:57:46 +00006256 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev4acb8592014-07-07 13:01:15 +00006257 // 1.2.2 OpenMP Language Terminology
6258 // Structured block - An executable statement with a single entry at the
6259 // top and a single exit at the bottom.
6260 // The point of exit cannot be a branch out of the structured block.
6261 // longjmp() and throw() must not violate the entry/exit criteria.
6262 CS->getCapturedDecl()->setNothrow();
6263
Alexander Musmanc6388682014-12-15 07:07:06 +00006264 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006265 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6266 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00006267 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00006268 checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00006269 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6270 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00006271 if (NestedLoopCount == 0)
6272 return StmtError();
6273
Alexander Musmana5f070a2014-10-01 06:03:56 +00006274 assert((CurContext->isDependentContext() || B.builtAll()) &&
6275 "omp parallel for loop exprs were not built");
6276
Alexey Bataev54acd402015-08-04 11:18:19 +00006277 if (!CurContext->isDependentContext()) {
6278 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00006279 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00006280 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00006281 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006282 B.NumIterations, *this, CurScope,
6283 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00006284 return StmtError();
6285 }
6286 }
6287
Reid Kleckner87a31802018-03-12 21:43:02 +00006288 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00006289 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00006290 NestedLoopCount, Clauses, AStmt, B,
6291 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00006292}
6293
Alexander Musmane4e893b2014-09-23 09:33:00 +00006294StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
6295 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00006296 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006297 if (!AStmt)
6298 return StmtError();
6299
Alexey Bataeve3727102018-04-18 15:57:46 +00006300 auto *CS = cast<CapturedStmt>(AStmt);
Alexander Musmane4e893b2014-09-23 09:33:00 +00006301 // 1.2.2 OpenMP Language Terminology
6302 // Structured block - An executable statement with a single entry at the
6303 // top and a single exit at the bottom.
6304 // The point of exit cannot be a branch out of the structured block.
6305 // longjmp() and throw() must not violate the entry/exit criteria.
6306 CS->getCapturedDecl()->setNothrow();
6307
Alexander Musmanc6388682014-12-15 07:07:06 +00006308 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006309 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6310 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00006311 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00006312 checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00006313 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6314 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00006315 if (NestedLoopCount == 0)
6316 return StmtError();
6317
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00006318 if (!CurContext->isDependentContext()) {
6319 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00006320 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00006321 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00006322 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006323 B.NumIterations, *this, CurScope,
6324 DSAStack))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00006325 return StmtError();
6326 }
6327 }
6328
Kelvin Lic5609492016-07-15 04:39:07 +00006329 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00006330 return StmtError();
6331
Reid Kleckner87a31802018-03-12 21:43:02 +00006332 setFunctionHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00006333 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00006334 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00006335}
6336
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006337StmtResult
6338Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
6339 Stmt *AStmt, SourceLocation StartLoc,
6340 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006341 if (!AStmt)
6342 return StmtError();
6343
6344 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006345 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00006346 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006347 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00006348 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006349 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00006350 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006351 return StmtError();
6352 // All associated statements must be '#pragma omp section' except for
6353 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00006354 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006355 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
6356 if (SectionStmt)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006357 Diag(SectionStmt->getBeginLoc(),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006358 diag::err_omp_parallel_sections_substmt_not_section);
6359 return StmtError();
6360 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006361 cast<OMPSectionDirective>(SectionStmt)
6362 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006363 }
6364 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006365 Diag(AStmt->getBeginLoc(),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006366 diag::err_omp_parallel_sections_not_compound_stmt);
6367 return StmtError();
6368 }
6369
Reid Kleckner87a31802018-03-12 21:43:02 +00006370 setFunctionHasBranchProtectedScope();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006371
Alexey Bataev25e5b442015-09-15 12:52:43 +00006372 return OMPParallelSectionsDirective::Create(
6373 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006374}
6375
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006376StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
6377 Stmt *AStmt, SourceLocation StartLoc,
6378 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006379 if (!AStmt)
6380 return StmtError();
6381
David Majnemer9d168222016-08-05 17:44:54 +00006382 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006383 // 1.2.2 OpenMP Language Terminology
6384 // Structured block - An executable statement with a single entry at the
6385 // top and a single exit at the bottom.
6386 // The point of exit cannot be a branch out of the structured block.
6387 // longjmp() and throw() must not violate the entry/exit criteria.
6388 CS->getCapturedDecl()->setNothrow();
6389
Reid Kleckner87a31802018-03-12 21:43:02 +00006390 setFunctionHasBranchProtectedScope();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006391
Alexey Bataev25e5b442015-09-15 12:52:43 +00006392 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
6393 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006394}
6395
Alexey Bataev68446b72014-07-18 07:47:19 +00006396StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
6397 SourceLocation EndLoc) {
6398 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
6399}
6400
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00006401StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
6402 SourceLocation EndLoc) {
6403 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
6404}
6405
Alexey Bataev2df347a2014-07-18 10:17:07 +00006406StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
6407 SourceLocation EndLoc) {
6408 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
6409}
6410
Alexey Bataev169d96a2017-07-18 20:17:46 +00006411StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses,
6412 Stmt *AStmt,
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006413 SourceLocation StartLoc,
6414 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006415 if (!AStmt)
6416 return StmtError();
6417
6418 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006419
Reid Kleckner87a31802018-03-12 21:43:02 +00006420 setFunctionHasBranchProtectedScope();
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006421
Alexey Bataev169d96a2017-07-18 20:17:46 +00006422 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses,
Alexey Bataev3b1b8952017-07-25 15:53:26 +00006423 AStmt,
6424 DSAStack->getTaskgroupReductionRef());
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006425}
6426
Alexey Bataev6125da92014-07-21 11:26:11 +00006427StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
6428 SourceLocation StartLoc,
6429 SourceLocation EndLoc) {
6430 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
6431 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
6432}
6433
Alexey Bataev346265e2015-09-25 10:37:12 +00006434StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
6435 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00006436 SourceLocation StartLoc,
6437 SourceLocation EndLoc) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006438 const OMPClause *DependFound = nullptr;
6439 const OMPClause *DependSourceClause = nullptr;
6440 const OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00006441 bool ErrorFound = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00006442 const OMPThreadsClause *TC = nullptr;
6443 const OMPSIMDClause *SC = nullptr;
6444 for (const OMPClause *C : Clauses) {
Alexey Bataeveb482352015-12-18 05:05:56 +00006445 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
6446 DependFound = C;
6447 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
6448 if (DependSourceClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006449 Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
Alexey Bataeveb482352015-12-18 05:05:56 +00006450 << getOpenMPDirectiveName(OMPD_ordered)
6451 << getOpenMPClauseName(OMPC_depend) << 2;
6452 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006453 } else {
Alexey Bataeveb482352015-12-18 05:05:56 +00006454 DependSourceClause = C;
Alexey Bataeve3727102018-04-18 15:57:46 +00006455 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006456 if (DependSinkClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006457 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006458 << 0;
6459 ErrorFound = true;
6460 }
6461 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
6462 if (DependSourceClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006463 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006464 << 1;
6465 ErrorFound = true;
6466 }
6467 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00006468 }
Alexey Bataeve3727102018-04-18 15:57:46 +00006469 } else if (C->getClauseKind() == OMPC_threads) {
Alexey Bataev346265e2015-09-25 10:37:12 +00006470 TC = cast<OMPThreadsClause>(C);
Alexey Bataeve3727102018-04-18 15:57:46 +00006471 } else if (C->getClauseKind() == OMPC_simd) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006472 SC = cast<OMPSIMDClause>(C);
Alexey Bataeve3727102018-04-18 15:57:46 +00006473 }
Alexey Bataev346265e2015-09-25 10:37:12 +00006474 }
Alexey Bataeveb482352015-12-18 05:05:56 +00006475 if (!ErrorFound && !SC &&
6476 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006477 // OpenMP [2.8.1,simd Construct, Restrictions]
6478 // An ordered construct with the simd clause is the only OpenMP construct
6479 // that can appear in the simd region.
6480 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00006481 ErrorFound = true;
6482 } else if (DependFound && (TC || SC)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006483 Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd)
Alexey Bataeveb482352015-12-18 05:05:56 +00006484 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
6485 ErrorFound = true;
Alexey Bataevf138fda2018-08-13 19:04:24 +00006486 } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006487 Diag(DependFound->getBeginLoc(),
Alexey Bataeveb482352015-12-18 05:05:56 +00006488 diag::err_omp_ordered_directive_without_param);
6489 ErrorFound = true;
6490 } else if (TC || Clauses.empty()) {
Alexey Bataevf138fda2018-08-13 19:04:24 +00006491 if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006492 SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc;
Alexey Bataeveb482352015-12-18 05:05:56 +00006493 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
6494 << (TC != nullptr);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006495 Diag(Param->getBeginLoc(), diag::note_omp_ordered_param);
Alexey Bataeveb482352015-12-18 05:05:56 +00006496 ErrorFound = true;
6497 }
6498 }
6499 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006500 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00006501
6502 if (AStmt) {
6503 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6504
Reid Kleckner87a31802018-03-12 21:43:02 +00006505 setFunctionHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006506 }
Alexey Bataev346265e2015-09-25 10:37:12 +00006507
6508 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00006509}
6510
Alexey Bataev1d160b12015-03-13 12:27:31 +00006511namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006512/// Helper class for checking expression in 'omp atomic [update]'
Alexey Bataev1d160b12015-03-13 12:27:31 +00006513/// construct.
6514class OpenMPAtomicUpdateChecker {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006515 /// Error results for atomic update expressions.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006516 enum ExprAnalysisErrorCode {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006517 /// A statement is not an expression statement.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006518 NotAnExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006519 /// Expression is not builtin binary or unary operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006520 NotABinaryOrUnaryExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006521 /// Unary operation is not post-/pre- increment/decrement operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006522 NotAnUnaryIncDecExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006523 /// An expression is not of scalar type.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006524 NotAScalarType,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006525 /// A binary operation is not an assignment operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006526 NotAnAssignmentOp,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006527 /// RHS part of the binary operation is not a binary expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006528 NotABinaryExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006529 /// RHS part is not additive/multiplicative/shift/biwise binary
Alexey Bataev1d160b12015-03-13 12:27:31 +00006530 /// expression.
6531 NotABinaryOperator,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006532 /// RHS binary operation does not have reference to the updated LHS
Alexey Bataev1d160b12015-03-13 12:27:31 +00006533 /// part.
6534 NotAnUpdateExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006535 /// No errors is found.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006536 NoError
6537 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006538 /// Reference to Sema.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006539 Sema &SemaRef;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006540 /// A location for note diagnostics (when error is found).
Alexey Bataev1d160b12015-03-13 12:27:31 +00006541 SourceLocation NoteLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006542 /// 'x' lvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006543 Expr *X;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006544 /// 'expr' rvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006545 Expr *E;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006546 /// Helper expression of the form
Alexey Bataevb4505a72015-03-30 05:20:59 +00006547 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6548 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6549 Expr *UpdateExpr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006550 /// Is 'x' a LHS in a RHS part of full update expression. It is
Alexey Bataevb4505a72015-03-30 05:20:59 +00006551 /// important for non-associative operations.
6552 bool IsXLHSInRHSPart;
6553 BinaryOperatorKind Op;
6554 SourceLocation OpLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006555 /// true if the source expression is a postfix unary operation, false
Alexey Bataevb78ca832015-04-01 03:33:17 +00006556 /// if it is a prefix unary operation.
6557 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006558
6559public:
6560 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00006561 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00006562 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006563 /// Check specified statement that it is suitable for 'atomic update'
Alexey Bataev1d160b12015-03-13 12:27:31 +00006564 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00006565 /// expression. If DiagId and NoteId == 0, then only check is performed
6566 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006567 /// \param DiagId Diagnostic which should be emitted if error is found.
6568 /// \param NoteId Diagnostic note for the main error message.
6569 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00006570 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006571 /// Return the 'x' lvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006572 Expr *getX() const { return X; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006573 /// Return the 'expr' rvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006574 Expr *getExpr() const { return E; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006575 /// Return the update expression used in calculation of the updated
Alexey Bataevb4505a72015-03-30 05:20:59 +00006576 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6577 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6578 Expr *getUpdateExpr() const { return UpdateExpr; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006579 /// Return true if 'x' is LHS in RHS part of full update expression,
Alexey Bataevb4505a72015-03-30 05:20:59 +00006580 /// false otherwise.
6581 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
6582
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006583 /// true if the source expression is a postfix unary operation, false
Alexey Bataevb78ca832015-04-01 03:33:17 +00006584 /// if it is a prefix unary operation.
6585 bool isPostfixUpdate() const { return IsPostfixUpdate; }
6586
Alexey Bataev1d160b12015-03-13 12:27:31 +00006587private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00006588 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
6589 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00006590};
6591} // namespace
6592
6593bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
6594 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
6595 ExprAnalysisErrorCode ErrorFound = NoError;
6596 SourceLocation ErrorLoc, NoteLoc;
6597 SourceRange ErrorRange, NoteRange;
6598 // Allowed constructs are:
6599 // x = x binop expr;
6600 // x = expr binop x;
6601 if (AtomicBinOp->getOpcode() == BO_Assign) {
6602 X = AtomicBinOp->getLHS();
Alexey Bataeve3727102018-04-18 15:57:46 +00006603 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006604 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
6605 if (AtomicInnerBinOp->isMultiplicativeOp() ||
6606 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
6607 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006608 Op = AtomicInnerBinOp->getOpcode();
6609 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataeve3727102018-04-18 15:57:46 +00006610 Expr *LHS = AtomicInnerBinOp->getLHS();
6611 Expr *RHS = AtomicInnerBinOp->getRHS();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006612 llvm::FoldingSetNodeID XId, LHSId, RHSId;
6613 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
6614 /*Canonical=*/true);
6615 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
6616 /*Canonical=*/true);
6617 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
6618 /*Canonical=*/true);
6619 if (XId == LHSId) {
6620 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006621 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006622 } else if (XId == RHSId) {
6623 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006624 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006625 } else {
6626 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6627 ErrorRange = AtomicInnerBinOp->getSourceRange();
6628 NoteLoc = X->getExprLoc();
6629 NoteRange = X->getSourceRange();
6630 ErrorFound = NotAnUpdateExpression;
6631 }
6632 } else {
6633 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6634 ErrorRange = AtomicInnerBinOp->getSourceRange();
6635 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
6636 NoteRange = SourceRange(NoteLoc, NoteLoc);
6637 ErrorFound = NotABinaryOperator;
6638 }
6639 } else {
6640 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
6641 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
6642 ErrorFound = NotABinaryExpression;
6643 }
6644 } else {
6645 ErrorLoc = AtomicBinOp->getExprLoc();
6646 ErrorRange = AtomicBinOp->getSourceRange();
6647 NoteLoc = AtomicBinOp->getOperatorLoc();
6648 NoteRange = SourceRange(NoteLoc, NoteLoc);
6649 ErrorFound = NotAnAssignmentOp;
6650 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006651 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006652 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6653 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6654 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006655 }
6656 if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006657 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006658 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006659}
6660
6661bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
6662 unsigned NoteId) {
6663 ExprAnalysisErrorCode ErrorFound = NoError;
6664 SourceLocation ErrorLoc, NoteLoc;
6665 SourceRange ErrorRange, NoteRange;
6666 // Allowed constructs are:
6667 // x++;
6668 // x--;
6669 // ++x;
6670 // --x;
6671 // x binop= expr;
6672 // x = x binop expr;
6673 // x = expr binop x;
6674 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
6675 AtomicBody = AtomicBody->IgnoreParenImpCasts();
6676 if (AtomicBody->getType()->isScalarType() ||
6677 AtomicBody->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006678 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006679 AtomicBody->IgnoreParenImpCasts())) {
6680 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00006681 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006682 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00006683 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006684 E = AtomicCompAssignOp->getRHS();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006685 X = AtomicCompAssignOp->getLHS()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006686 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006687 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
6688 AtomicBody->IgnoreParenImpCasts())) {
6689 // Check for Binary Operation
David Majnemer9d168222016-08-05 17:44:54 +00006690 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
Alexey Bataevb4505a72015-03-30 05:20:59 +00006691 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006692 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
David Majnemer9d168222016-08-05 17:44:54 +00006693 AtomicBody->IgnoreParenImpCasts())) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006694 // Check for Unary Operation
6695 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006696 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006697 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
6698 OpLoc = AtomicUnaryOp->getOperatorLoc();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006699 X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006700 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
6701 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006702 } else {
6703 ErrorFound = NotAnUnaryIncDecExpression;
6704 ErrorLoc = AtomicUnaryOp->getExprLoc();
6705 ErrorRange = AtomicUnaryOp->getSourceRange();
6706 NoteLoc = AtomicUnaryOp->getOperatorLoc();
6707 NoteRange = SourceRange(NoteLoc, NoteLoc);
6708 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006709 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006710 ErrorFound = NotABinaryOrUnaryExpression;
6711 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
6712 NoteRange = ErrorRange = AtomicBody->getSourceRange();
6713 }
6714 } else {
6715 ErrorFound = NotAScalarType;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006716 NoteLoc = ErrorLoc = AtomicBody->getBeginLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006717 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6718 }
6719 } else {
6720 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006721 NoteLoc = ErrorLoc = S->getBeginLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006722 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6723 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006724 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006725 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6726 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6727 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006728 }
6729 if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006730 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006731 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006732 // Build an update expression of form 'OpaqueValueExpr(x) binop
6733 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
6734 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
6735 auto *OVEX = new (SemaRef.getASTContext())
6736 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
6737 auto *OVEExpr = new (SemaRef.getASTContext())
6738 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
Alexey Bataeve3727102018-04-18 15:57:46 +00006739 ExprResult Update =
Alexey Bataevb4505a72015-03-30 05:20:59 +00006740 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
6741 IsXLHSInRHSPart ? OVEExpr : OVEX);
6742 if (Update.isInvalid())
6743 return true;
6744 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
6745 Sema::AA_Casting);
6746 if (Update.isInvalid())
6747 return true;
6748 UpdateExpr = Update.get();
6749 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00006750 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006751}
6752
Alexey Bataev0162e452014-07-22 10:10:35 +00006753StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
6754 Stmt *AStmt,
6755 SourceLocation StartLoc,
6756 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006757 if (!AStmt)
6758 return StmtError();
6759
David Majnemer9d168222016-08-05 17:44:54 +00006760 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00006761 // 1.2.2 OpenMP Language Terminology
6762 // Structured block - An executable statement with a single entry at the
6763 // top and a single exit at the bottom.
6764 // The point of exit cannot be a branch out of the structured block.
6765 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00006766 OpenMPClauseKind AtomicKind = OMPC_unknown;
6767 SourceLocation AtomicKindLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00006768 for (const OMPClause *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00006769 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00006770 C->getClauseKind() == OMPC_update ||
6771 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00006772 if (AtomicKind != OMPC_unknown) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006773 Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00006774 << SourceRange(C->getBeginLoc(), C->getEndLoc());
Alexey Bataevdea47612014-07-23 07:46:59 +00006775 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
6776 << getOpenMPClauseName(AtomicKind);
6777 } else {
6778 AtomicKind = C->getClauseKind();
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006779 AtomicKindLoc = C->getBeginLoc();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006780 }
6781 }
6782 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006783
Alexey Bataeve3727102018-04-18 15:57:46 +00006784 Stmt *Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00006785 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
6786 Body = EWC->getSubExpr();
6787
Alexey Bataev62cec442014-11-18 10:14:22 +00006788 Expr *X = nullptr;
6789 Expr *V = nullptr;
6790 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006791 Expr *UE = nullptr;
6792 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006793 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00006794 // OpenMP [2.12.6, atomic Construct]
6795 // In the next expressions:
6796 // * x and v (as applicable) are both l-value expressions with scalar type.
6797 // * During the execution of an atomic region, multiple syntactic
6798 // occurrences of x must designate the same storage location.
6799 // * Neither of v and expr (as applicable) may access the storage location
6800 // designated by x.
6801 // * Neither of x and expr (as applicable) may access the storage location
6802 // designated by v.
6803 // * expr is an expression with scalar type.
6804 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
6805 // * binop, binop=, ++, and -- are not overloaded operators.
6806 // * The expression x binop expr must be numerically equivalent to x binop
6807 // (expr). This requirement is satisfied if the operators in expr have
6808 // precedence greater than binop, or by using parentheses around expr or
6809 // subexpressions of expr.
6810 // * The expression expr binop x must be numerically equivalent to (expr)
6811 // binop x. This requirement is satisfied if the operators in expr have
6812 // precedence equal to or greater than binop, or by using parentheses around
6813 // expr or subexpressions of expr.
6814 // * For forms that allow multiple occurrences of x, the number of times
6815 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00006816 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006817 enum {
6818 NotAnExpression,
6819 NotAnAssignmentOp,
6820 NotAScalarType,
6821 NotAnLValue,
6822 NoError
6823 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00006824 SourceLocation ErrorLoc, NoteLoc;
6825 SourceRange ErrorRange, NoteRange;
6826 // If clause is read:
6827 // v = x;
Alexey Bataeve3727102018-04-18 15:57:46 +00006828 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
6829 const auto *AtomicBinOp =
Alexey Bataev62cec442014-11-18 10:14:22 +00006830 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6831 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6832 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6833 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
6834 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6835 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
6836 if (!X->isLValue() || !V->isLValue()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006837 const Expr *NotLValueExpr = X->isLValue() ? V : X;
Alexey Bataev62cec442014-11-18 10:14:22 +00006838 ErrorFound = NotAnLValue;
6839 ErrorLoc = AtomicBinOp->getExprLoc();
6840 ErrorRange = AtomicBinOp->getSourceRange();
6841 NoteLoc = NotLValueExpr->getExprLoc();
6842 NoteRange = NotLValueExpr->getSourceRange();
6843 }
6844 } else if (!X->isInstantiationDependent() ||
6845 !V->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006846 const Expr *NotScalarExpr =
Alexey Bataev62cec442014-11-18 10:14:22 +00006847 (X->isInstantiationDependent() || X->getType()->isScalarType())
6848 ? V
6849 : X;
6850 ErrorFound = NotAScalarType;
6851 ErrorLoc = AtomicBinOp->getExprLoc();
6852 ErrorRange = AtomicBinOp->getSourceRange();
6853 NoteLoc = NotScalarExpr->getExprLoc();
6854 NoteRange = NotScalarExpr->getSourceRange();
6855 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006856 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00006857 ErrorFound = NotAnAssignmentOp;
6858 ErrorLoc = AtomicBody->getExprLoc();
6859 ErrorRange = AtomicBody->getSourceRange();
6860 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6861 : AtomicBody->getExprLoc();
6862 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6863 : AtomicBody->getSourceRange();
6864 }
6865 } else {
6866 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006867 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataev62cec442014-11-18 10:14:22 +00006868 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006869 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006870 if (ErrorFound != NoError) {
6871 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
6872 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006873 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6874 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00006875 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00006876 }
6877 if (CurContext->isDependentContext())
Alexey Bataev62cec442014-11-18 10:14:22 +00006878 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00006879 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006880 enum {
6881 NotAnExpression,
6882 NotAnAssignmentOp,
6883 NotAScalarType,
6884 NotAnLValue,
6885 NoError
6886 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006887 SourceLocation ErrorLoc, NoteLoc;
6888 SourceRange ErrorRange, NoteRange;
6889 // If clause is write:
6890 // x = expr;
Alexey Bataeve3727102018-04-18 15:57:46 +00006891 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
6892 const auto *AtomicBinOp =
Alexey Bataevf33eba62014-11-28 07:21:40 +00006893 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6894 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00006895 X = AtomicBinOp->getLHS();
6896 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006897 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6898 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
6899 if (!X->isLValue()) {
6900 ErrorFound = NotAnLValue;
6901 ErrorLoc = AtomicBinOp->getExprLoc();
6902 ErrorRange = AtomicBinOp->getSourceRange();
6903 NoteLoc = X->getExprLoc();
6904 NoteRange = X->getSourceRange();
6905 }
6906 } else if (!X->isInstantiationDependent() ||
6907 !E->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006908 const Expr *NotScalarExpr =
Alexey Bataevf33eba62014-11-28 07:21:40 +00006909 (X->isInstantiationDependent() || X->getType()->isScalarType())
6910 ? E
6911 : X;
6912 ErrorFound = NotAScalarType;
6913 ErrorLoc = AtomicBinOp->getExprLoc();
6914 ErrorRange = AtomicBinOp->getSourceRange();
6915 NoteLoc = NotScalarExpr->getExprLoc();
6916 NoteRange = NotScalarExpr->getSourceRange();
6917 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006918 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00006919 ErrorFound = NotAnAssignmentOp;
6920 ErrorLoc = AtomicBody->getExprLoc();
6921 ErrorRange = AtomicBody->getSourceRange();
6922 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6923 : AtomicBody->getExprLoc();
6924 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6925 : AtomicBody->getSourceRange();
6926 }
6927 } else {
6928 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006929 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006930 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006931 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00006932 if (ErrorFound != NoError) {
6933 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
6934 << ErrorRange;
6935 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6936 << NoteRange;
6937 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00006938 }
6939 if (CurContext->isDependentContext())
Alexey Bataevf33eba62014-11-28 07:21:40 +00006940 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006941 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006942 // If clause is update:
6943 // x++;
6944 // x--;
6945 // ++x;
6946 // --x;
6947 // x binop= expr;
6948 // x = x binop expr;
6949 // x = expr binop x;
6950 OpenMPAtomicUpdateChecker Checker(*this);
6951 if (Checker.checkStatement(
6952 Body, (AtomicKind == OMPC_update)
6953 ? diag::err_omp_atomic_update_not_expression_statement
6954 : diag::err_omp_atomic_not_expression_statement,
6955 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00006956 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006957 if (!CurContext->isDependentContext()) {
6958 E = Checker.getExpr();
6959 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006960 UE = Checker.getUpdateExpr();
6961 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00006962 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006963 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006964 enum {
6965 NotAnAssignmentOp,
6966 NotACompoundStatement,
6967 NotTwoSubstatements,
6968 NotASpecificExpression,
6969 NoError
6970 } ErrorFound = NoError;
6971 SourceLocation ErrorLoc, NoteLoc;
6972 SourceRange ErrorRange, NoteRange;
Alexey Bataeve3727102018-04-18 15:57:46 +00006973 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006974 // If clause is a capture:
6975 // v = x++;
6976 // v = x--;
6977 // v = ++x;
6978 // v = --x;
6979 // v = x binop= expr;
6980 // v = x = x binop expr;
6981 // v = x = expr binop x;
Alexey Bataeve3727102018-04-18 15:57:46 +00006982 const auto *AtomicBinOp =
Alexey Bataevb78ca832015-04-01 03:33:17 +00006983 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6984 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6985 V = AtomicBinOp->getLHS();
6986 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6987 OpenMPAtomicUpdateChecker Checker(*this);
6988 if (Checker.checkStatement(
6989 Body, diag::err_omp_atomic_capture_not_expression_statement,
6990 diag::note_omp_atomic_update))
6991 return StmtError();
6992 E = Checker.getExpr();
6993 X = Checker.getX();
6994 UE = Checker.getUpdateExpr();
6995 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6996 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00006997 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006998 ErrorLoc = AtomicBody->getExprLoc();
6999 ErrorRange = AtomicBody->getSourceRange();
7000 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
7001 : AtomicBody->getExprLoc();
7002 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
7003 : AtomicBody->getSourceRange();
7004 ErrorFound = NotAnAssignmentOp;
7005 }
7006 if (ErrorFound != NoError) {
7007 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
7008 << ErrorRange;
7009 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
7010 return StmtError();
Alexey Bataevb78ca832015-04-01 03:33:17 +00007011 }
Alexey Bataeve3727102018-04-18 15:57:46 +00007012 if (CurContext->isDependentContext())
7013 UE = V = E = X = nullptr;
Alexey Bataevb78ca832015-04-01 03:33:17 +00007014 } else {
7015 // If clause is a capture:
7016 // { v = x; x = expr; }
7017 // { v = x; x++; }
7018 // { v = x; x--; }
7019 // { v = x; ++x; }
7020 // { v = x; --x; }
7021 // { v = x; x binop= expr; }
7022 // { v = x; x = x binop expr; }
7023 // { v = x; x = expr binop x; }
7024 // { x++; v = x; }
7025 // { x--; v = x; }
7026 // { ++x; v = x; }
7027 // { --x; v = x; }
7028 // { x binop= expr; v = x; }
7029 // { x = x binop expr; v = x; }
7030 // { x = expr binop x; v = x; }
7031 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
7032 // Check that this is { expr1; expr2; }
7033 if (CS->size() == 2) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007034 Stmt *First = CS->body_front();
7035 Stmt *Second = CS->body_back();
Alexey Bataevb78ca832015-04-01 03:33:17 +00007036 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
7037 First = EWC->getSubExpr()->IgnoreParenImpCasts();
7038 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
7039 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
7040 // Need to find what subexpression is 'v' and what is 'x'.
7041 OpenMPAtomicUpdateChecker Checker(*this);
7042 bool IsUpdateExprFound = !Checker.checkStatement(Second);
7043 BinaryOperator *BinOp = nullptr;
7044 if (IsUpdateExprFound) {
7045 BinOp = dyn_cast<BinaryOperator>(First);
7046 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
7047 }
7048 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
7049 // { v = x; x++; }
7050 // { v = x; x--; }
7051 // { v = x; ++x; }
7052 // { v = x; --x; }
7053 // { v = x; x binop= expr; }
7054 // { v = x; x = x binop expr; }
7055 // { v = x; x = expr binop x; }
7056 // Check that the first expression has form v = x.
Alexey Bataeve3727102018-04-18 15:57:46 +00007057 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataevb78ca832015-04-01 03:33:17 +00007058 llvm::FoldingSetNodeID XId, PossibleXId;
7059 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
7060 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
7061 IsUpdateExprFound = XId == PossibleXId;
7062 if (IsUpdateExprFound) {
7063 V = BinOp->getLHS();
7064 X = Checker.getX();
7065 E = Checker.getExpr();
7066 UE = Checker.getUpdateExpr();
7067 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00007068 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00007069 }
7070 }
7071 if (!IsUpdateExprFound) {
7072 IsUpdateExprFound = !Checker.checkStatement(First);
7073 BinOp = nullptr;
7074 if (IsUpdateExprFound) {
7075 BinOp = dyn_cast<BinaryOperator>(Second);
7076 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
7077 }
7078 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
7079 // { x++; v = x; }
7080 // { x--; v = x; }
7081 // { ++x; v = x; }
7082 // { --x; v = x; }
7083 // { x binop= expr; v = x; }
7084 // { x = x binop expr; v = x; }
7085 // { x = expr binop x; v = x; }
7086 // Check that the second expression has form v = x.
Alexey Bataeve3727102018-04-18 15:57:46 +00007087 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataevb78ca832015-04-01 03:33:17 +00007088 llvm::FoldingSetNodeID XId, PossibleXId;
7089 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
7090 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
7091 IsUpdateExprFound = XId == PossibleXId;
7092 if (IsUpdateExprFound) {
7093 V = BinOp->getLHS();
7094 X = Checker.getX();
7095 E = Checker.getExpr();
7096 UE = Checker.getUpdateExpr();
7097 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00007098 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00007099 }
7100 }
7101 }
7102 if (!IsUpdateExprFound) {
7103 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00007104 auto *FirstExpr = dyn_cast<Expr>(First);
7105 auto *SecondExpr = dyn_cast<Expr>(Second);
7106 if (!FirstExpr || !SecondExpr ||
7107 !(FirstExpr->isInstantiationDependent() ||
7108 SecondExpr->isInstantiationDependent())) {
7109 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
7110 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00007111 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00007112 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007113 : First->getBeginLoc();
Alexey Bataev5a195472015-09-04 12:55:50 +00007114 NoteRange = ErrorRange = FirstBinOp
7115 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00007116 : SourceRange(ErrorLoc, ErrorLoc);
7117 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00007118 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
7119 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
7120 ErrorFound = NotAnAssignmentOp;
7121 NoteLoc = ErrorLoc = SecondBinOp
7122 ? SecondBinOp->getOperatorLoc()
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007123 : Second->getBeginLoc();
Alexey Bataev5a195472015-09-04 12:55:50 +00007124 NoteRange = ErrorRange =
7125 SecondBinOp ? SecondBinOp->getSourceRange()
7126 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00007127 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +00007128 Expr *PossibleXRHSInFirst =
Alexey Bataev5a195472015-09-04 12:55:50 +00007129 FirstBinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00007130 Expr *PossibleXLHSInSecond =
Alexey Bataev5a195472015-09-04 12:55:50 +00007131 SecondBinOp->getLHS()->IgnoreParenImpCasts();
7132 llvm::FoldingSetNodeID X1Id, X2Id;
7133 PossibleXRHSInFirst->Profile(X1Id, Context,
7134 /*Canonical=*/true);
7135 PossibleXLHSInSecond->Profile(X2Id, Context,
7136 /*Canonical=*/true);
7137 IsUpdateExprFound = X1Id == X2Id;
7138 if (IsUpdateExprFound) {
7139 V = FirstBinOp->getLHS();
7140 X = SecondBinOp->getLHS();
7141 E = SecondBinOp->getRHS();
7142 UE = nullptr;
7143 IsXLHSInRHSPart = false;
7144 IsPostfixUpdate = true;
7145 } else {
7146 ErrorFound = NotASpecificExpression;
7147 ErrorLoc = FirstBinOp->getExprLoc();
7148 ErrorRange = FirstBinOp->getSourceRange();
7149 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
7150 NoteRange = SecondBinOp->getRHS()->getSourceRange();
7151 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00007152 }
7153 }
7154 }
7155 }
7156 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007157 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevb78ca832015-04-01 03:33:17 +00007158 NoteRange = ErrorRange =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007159 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
Alexey Bataevb78ca832015-04-01 03:33:17 +00007160 ErrorFound = NotTwoSubstatements;
7161 }
7162 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007163 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevb78ca832015-04-01 03:33:17 +00007164 NoteRange = ErrorRange =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007165 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
Alexey Bataevb78ca832015-04-01 03:33:17 +00007166 ErrorFound = NotACompoundStatement;
7167 }
7168 if (ErrorFound != NoError) {
7169 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
7170 << ErrorRange;
7171 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
7172 return StmtError();
Alexey Bataevb78ca832015-04-01 03:33:17 +00007173 }
Alexey Bataeve3727102018-04-18 15:57:46 +00007174 if (CurContext->isDependentContext())
7175 UE = V = E = X = nullptr;
Alexey Bataev459dec02014-07-24 06:46:57 +00007176 }
Alexey Bataevdea47612014-07-23 07:46:59 +00007177 }
Alexey Bataev0162e452014-07-22 10:10:35 +00007178
Reid Kleckner87a31802018-03-12 21:43:02 +00007179 setFunctionHasBranchProtectedScope();
Alexey Bataev0162e452014-07-22 10:10:35 +00007180
Alexey Bataev62cec442014-11-18 10:14:22 +00007181 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00007182 X, V, E, UE, IsXLHSInRHSPart,
7183 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00007184}
7185
Alexey Bataev0bd520b2014-09-19 08:19:49 +00007186StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
7187 Stmt *AStmt,
7188 SourceLocation StartLoc,
7189 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007190 if (!AStmt)
7191 return StmtError();
7192
Alexey Bataeve3727102018-04-18 15:57:46 +00007193 auto *CS = cast<CapturedStmt>(AStmt);
Samuel Antao4af1b7b2015-12-02 17:44:43 +00007194 // 1.2.2 OpenMP Language Terminology
7195 // Structured block - An executable statement with a single entry at the
7196 // top and a single exit at the bottom.
7197 // The point of exit cannot be a branch out of the structured block.
7198 // longjmp() and throw() must not violate the entry/exit criteria.
7199 CS->getCapturedDecl()->setNothrow();
Alexey Bataev8451efa2018-01-15 19:06:12 +00007200 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target);
7201 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7202 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7203 // 1.2.2 OpenMP Language Terminology
7204 // Structured block - An executable statement with a single entry at the
7205 // top and a single exit at the bottom.
7206 // The point of exit cannot be a branch out of the structured block.
7207 // longjmp() and throw() must not violate the entry/exit criteria.
7208 CS->getCapturedDecl()->setNothrow();
7209 }
Alexey Bataev0bd520b2014-09-19 08:19:49 +00007210
Alexey Bataev13314bf2014-10-09 04:18:56 +00007211 // OpenMP [2.16, Nesting of Regions]
7212 // If specified, a teams construct must be contained within a target
7213 // construct. That target construct must contain no statements or directives
7214 // outside of the teams construct.
7215 if (DSAStack->hasInnerTeamsRegion()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007216 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true);
Alexey Bataev13314bf2014-10-09 04:18:56 +00007217 bool OMPTeamsFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00007218 if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00007219 auto I = CS->body_begin();
7220 while (I != CS->body_end()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007221 const auto *OED = dyn_cast<OMPExecutableDirective>(*I);
Kelvin Li620ba602019-02-05 16:43:00 +00007222 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind()) ||
7223 OMPTeamsFound) {
7224
Alexey Bataev13314bf2014-10-09 04:18:56 +00007225 OMPTeamsFound = false;
7226 break;
7227 }
7228 ++I;
7229 }
7230 assert(I != CS->body_end() && "Not found statement");
7231 S = *I;
Kelvin Li3834dce2016-06-27 19:15:43 +00007232 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +00007233 const auto *OED = dyn_cast<OMPExecutableDirective>(S);
Kelvin Li3834dce2016-06-27 19:15:43 +00007234 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
Alexey Bataev13314bf2014-10-09 04:18:56 +00007235 }
7236 if (!OMPTeamsFound) {
7237 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
7238 Diag(DSAStack->getInnerTeamsRegionLoc(),
7239 diag::note_omp_nested_teams_construct_here);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007240 Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here)
Alexey Bataev13314bf2014-10-09 04:18:56 +00007241 << isa<OMPExecutableDirective>(S);
7242 return StmtError();
7243 }
7244 }
7245
Reid Kleckner87a31802018-03-12 21:43:02 +00007246 setFunctionHasBranchProtectedScope();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00007247
7248 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
7249}
7250
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00007251StmtResult
7252Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
7253 Stmt *AStmt, SourceLocation StartLoc,
7254 SourceLocation EndLoc) {
7255 if (!AStmt)
7256 return StmtError();
7257
Alexey Bataeve3727102018-04-18 15:57:46 +00007258 auto *CS = cast<CapturedStmt>(AStmt);
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00007259 // 1.2.2 OpenMP Language Terminology
7260 // Structured block - An executable statement with a single entry at the
7261 // top and a single exit at the bottom.
7262 // The point of exit cannot be a branch out of the structured block.
7263 // longjmp() and throw() must not violate the entry/exit criteria.
7264 CS->getCapturedDecl()->setNothrow();
Alexey Bataev8451efa2018-01-15 19:06:12 +00007265 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel);
7266 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7267 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7268 // 1.2.2 OpenMP Language Terminology
7269 // Structured block - An executable statement with a single entry at the
7270 // top and a single exit at the bottom.
7271 // The point of exit cannot be a branch out of the structured block.
7272 // longjmp() and throw() must not violate the entry/exit criteria.
7273 CS->getCapturedDecl()->setNothrow();
7274 }
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00007275
Reid Kleckner87a31802018-03-12 21:43:02 +00007276 setFunctionHasBranchProtectedScope();
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00007277
7278 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
7279 AStmt);
7280}
7281
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007282StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
7283 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007284 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007285 if (!AStmt)
7286 return StmtError();
7287
Alexey Bataeve3727102018-04-18 15:57:46 +00007288 auto *CS = cast<CapturedStmt>(AStmt);
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007289 // 1.2.2 OpenMP Language Terminology
7290 // Structured block - An executable statement with a single entry at the
7291 // top and a single exit at the bottom.
7292 // The point of exit cannot be a branch out of the structured block.
7293 // longjmp() and throw() must not violate the entry/exit criteria.
7294 CS->getCapturedDecl()->setNothrow();
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00007295 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
7296 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7297 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7298 // 1.2.2 OpenMP Language Terminology
7299 // Structured block - An executable statement with a single entry at the
7300 // top and a single exit at the bottom.
7301 // The point of exit cannot be a branch out of the structured block.
7302 // longjmp() and throw() must not violate the entry/exit criteria.
7303 CS->getCapturedDecl()->setNothrow();
7304 }
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007305
7306 OMPLoopDirective::HelperExprs B;
7307 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7308 // define the nested loops number.
7309 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007310 checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00007311 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007312 VarsWithImplicitDSA, B);
7313 if (NestedLoopCount == 0)
7314 return StmtError();
7315
7316 assert((CurContext->isDependentContext() || B.builtAll()) &&
7317 "omp target parallel for loop exprs were not built");
7318
7319 if (!CurContext->isDependentContext()) {
7320 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007321 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007322 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007323 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007324 B.NumIterations, *this, CurScope,
7325 DSAStack))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007326 return StmtError();
7327 }
7328 }
7329
Reid Kleckner87a31802018-03-12 21:43:02 +00007330 setFunctionHasBranchProtectedScope();
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007331 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
7332 NestedLoopCount, Clauses, AStmt,
7333 B, DSAStack->isCancelRegion());
7334}
7335
Alexey Bataev95b64a92017-05-30 16:00:04 +00007336/// Check for existence of a map clause in the list of clauses.
7337static bool hasClauses(ArrayRef<OMPClause *> Clauses,
7338 const OpenMPClauseKind K) {
7339 return llvm::any_of(
7340 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; });
7341}
Samuel Antaodf67fc42016-01-19 19:15:56 +00007342
Alexey Bataev95b64a92017-05-30 16:00:04 +00007343template <typename... Params>
7344static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K,
7345 const Params... ClauseTypes) {
7346 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...);
Samuel Antaodf67fc42016-01-19 19:15:56 +00007347}
7348
Michael Wong65f367f2015-07-21 13:44:28 +00007349StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
7350 Stmt *AStmt,
7351 SourceLocation StartLoc,
7352 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007353 if (!AStmt)
7354 return StmtError();
7355
7356 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7357
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00007358 // OpenMP [2.10.1, Restrictions, p. 97]
7359 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00007360 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr)) {
7361 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
7362 << "'map' or 'use_device_ptr'"
David Majnemer9d168222016-08-05 17:44:54 +00007363 << getOpenMPDirectiveName(OMPD_target_data);
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00007364 return StmtError();
7365 }
7366
Reid Kleckner87a31802018-03-12 21:43:02 +00007367 setFunctionHasBranchProtectedScope();
Michael Wong65f367f2015-07-21 13:44:28 +00007368
7369 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
7370 AStmt);
7371}
7372
Samuel Antaodf67fc42016-01-19 19:15:56 +00007373StmtResult
7374Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
7375 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00007376 SourceLocation EndLoc, Stmt *AStmt) {
7377 if (!AStmt)
7378 return StmtError();
7379
Alexey Bataeve3727102018-04-18 15:57:46 +00007380 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00007381 // 1.2.2 OpenMP Language Terminology
7382 // Structured block - An executable statement with a single entry at the
7383 // top and a single exit at the bottom.
7384 // The point of exit cannot be a branch out of the structured block.
7385 // longjmp() and throw() must not violate the entry/exit criteria.
7386 CS->getCapturedDecl()->setNothrow();
7387 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data);
7388 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7389 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7390 // 1.2.2 OpenMP Language Terminology
7391 // Structured block - An executable statement with a single entry at the
7392 // top and a single exit at the bottom.
7393 // The point of exit cannot be a branch out of the structured block.
7394 // longjmp() and throw() must not violate the entry/exit criteria.
7395 CS->getCapturedDecl()->setNothrow();
7396 }
7397
Samuel Antaodf67fc42016-01-19 19:15:56 +00007398 // OpenMP [2.10.2, Restrictions, p. 99]
7399 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00007400 if (!hasClauses(Clauses, OMPC_map)) {
7401 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
7402 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data);
Samuel Antaodf67fc42016-01-19 19:15:56 +00007403 return StmtError();
7404 }
7405
Alexey Bataev7828b252017-11-21 17:08:48 +00007406 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
7407 AStmt);
Samuel Antaodf67fc42016-01-19 19:15:56 +00007408}
7409
Samuel Antao72590762016-01-19 20:04:50 +00007410StmtResult
7411Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
7412 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00007413 SourceLocation EndLoc, Stmt *AStmt) {
7414 if (!AStmt)
7415 return StmtError();
7416
Alexey Bataeve3727102018-04-18 15:57:46 +00007417 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00007418 // 1.2.2 OpenMP Language Terminology
7419 // Structured block - An executable statement with a single entry at the
7420 // top and a single exit at the bottom.
7421 // The point of exit cannot be a branch out of the structured block.
7422 // longjmp() and throw() must not violate the entry/exit criteria.
7423 CS->getCapturedDecl()->setNothrow();
7424 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data);
7425 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7426 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7427 // 1.2.2 OpenMP Language Terminology
7428 // Structured block - An executable statement with a single entry at the
7429 // top and a single exit at the bottom.
7430 // The point of exit cannot be a branch out of the structured block.
7431 // longjmp() and throw() must not violate the entry/exit criteria.
7432 CS->getCapturedDecl()->setNothrow();
7433 }
7434
Samuel Antao72590762016-01-19 20:04:50 +00007435 // OpenMP [2.10.3, Restrictions, p. 102]
7436 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00007437 if (!hasClauses(Clauses, OMPC_map)) {
7438 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
7439 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data);
Samuel Antao72590762016-01-19 20:04:50 +00007440 return StmtError();
7441 }
7442
Alexey Bataev7828b252017-11-21 17:08:48 +00007443 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
7444 AStmt);
Samuel Antao72590762016-01-19 20:04:50 +00007445}
7446
Samuel Antao686c70c2016-05-26 17:30:50 +00007447StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
7448 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00007449 SourceLocation EndLoc,
7450 Stmt *AStmt) {
7451 if (!AStmt)
7452 return StmtError();
7453
Alexey Bataeve3727102018-04-18 15:57:46 +00007454 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00007455 // 1.2.2 OpenMP Language Terminology
7456 // Structured block - An executable statement with a single entry at the
7457 // top and a single exit at the bottom.
7458 // The point of exit cannot be a branch out of the structured block.
7459 // longjmp() and throw() must not violate the entry/exit criteria.
7460 CS->getCapturedDecl()->setNothrow();
7461 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update);
7462 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7463 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7464 // 1.2.2 OpenMP Language Terminology
7465 // Structured block - An executable statement with a single entry at the
7466 // top and a single exit at the bottom.
7467 // The point of exit cannot be a branch out of the structured block.
7468 // longjmp() and throw() must not violate the entry/exit criteria.
7469 CS->getCapturedDecl()->setNothrow();
7470 }
7471
Alexey Bataev95b64a92017-05-30 16:00:04 +00007472 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) {
Samuel Antao686c70c2016-05-26 17:30:50 +00007473 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
7474 return StmtError();
7475 }
Alexey Bataev7828b252017-11-21 17:08:48 +00007476 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses,
7477 AStmt);
Samuel Antao686c70c2016-05-26 17:30:50 +00007478}
7479
Alexey Bataev13314bf2014-10-09 04:18:56 +00007480StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
7481 Stmt *AStmt, SourceLocation StartLoc,
7482 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007483 if (!AStmt)
7484 return StmtError();
7485
Alexey Bataeve3727102018-04-18 15:57:46 +00007486 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev13314bf2014-10-09 04:18:56 +00007487 // 1.2.2 OpenMP Language Terminology
7488 // Structured block - An executable statement with a single entry at the
7489 // top and a single exit at the bottom.
7490 // The point of exit cannot be a branch out of the structured block.
7491 // longjmp() and throw() must not violate the entry/exit criteria.
7492 CS->getCapturedDecl()->setNothrow();
7493
Reid Kleckner87a31802018-03-12 21:43:02 +00007494 setFunctionHasBranchProtectedScope();
Alexey Bataev13314bf2014-10-09 04:18:56 +00007495
Alexey Bataevceabd412017-11-30 18:01:54 +00007496 DSAStack->setParentTeamsRegionLoc(StartLoc);
7497
Alexey Bataev13314bf2014-10-09 04:18:56 +00007498 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
7499}
7500
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007501StmtResult
7502Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
7503 SourceLocation EndLoc,
7504 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007505 if (DSAStack->isParentNowaitRegion()) {
7506 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
7507 return StmtError();
7508 }
7509 if (DSAStack->isParentOrderedRegion()) {
7510 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
7511 return StmtError();
7512 }
7513 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
7514 CancelRegion);
7515}
7516
Alexey Bataev87933c72015-09-18 08:07:34 +00007517StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
7518 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00007519 SourceLocation EndLoc,
7520 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev80909872015-07-02 11:25:17 +00007521 if (DSAStack->isParentNowaitRegion()) {
7522 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
7523 return StmtError();
7524 }
7525 if (DSAStack->isParentOrderedRegion()) {
7526 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
7527 return StmtError();
7528 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00007529 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00007530 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
7531 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00007532}
7533
Alexey Bataev382967a2015-12-08 12:06:20 +00007534static bool checkGrainsizeNumTasksClauses(Sema &S,
7535 ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007536 const OMPClause *PrevClause = nullptr;
Alexey Bataev382967a2015-12-08 12:06:20 +00007537 bool ErrorFound = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00007538 for (const OMPClause *C : Clauses) {
Alexey Bataev382967a2015-12-08 12:06:20 +00007539 if (C->getClauseKind() == OMPC_grainsize ||
7540 C->getClauseKind() == OMPC_num_tasks) {
7541 if (!PrevClause)
7542 PrevClause = C;
7543 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007544 S.Diag(C->getBeginLoc(),
Alexey Bataev382967a2015-12-08 12:06:20 +00007545 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
7546 << getOpenMPClauseName(C->getClauseKind())
7547 << getOpenMPClauseName(PrevClause->getClauseKind());
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007548 S.Diag(PrevClause->getBeginLoc(),
Alexey Bataev382967a2015-12-08 12:06:20 +00007549 diag::note_omp_previous_grainsize_num_tasks)
7550 << getOpenMPClauseName(PrevClause->getClauseKind());
7551 ErrorFound = true;
7552 }
7553 }
7554 }
7555 return ErrorFound;
7556}
7557
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007558static bool checkReductionClauseWithNogroup(Sema &S,
7559 ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007560 const OMPClause *ReductionClause = nullptr;
7561 const OMPClause *NogroupClause = nullptr;
7562 for (const OMPClause *C : Clauses) {
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007563 if (C->getClauseKind() == OMPC_reduction) {
7564 ReductionClause = C;
7565 if (NogroupClause)
7566 break;
7567 continue;
7568 }
7569 if (C->getClauseKind() == OMPC_nogroup) {
7570 NogroupClause = C;
7571 if (ReductionClause)
7572 break;
7573 continue;
7574 }
7575 }
7576 if (ReductionClause && NogroupClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007577 S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup)
7578 << SourceRange(NogroupClause->getBeginLoc(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007579 NogroupClause->getEndLoc());
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007580 return true;
7581 }
7582 return false;
7583}
7584
Alexey Bataev49f6e782015-12-01 04:18:41 +00007585StmtResult Sema::ActOnOpenMPTaskLoopDirective(
7586 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007587 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00007588 if (!AStmt)
7589 return StmtError();
7590
7591 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7592 OMPLoopDirective::HelperExprs B;
7593 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7594 // define the nested loops number.
7595 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007596 checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007597 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00007598 VarsWithImplicitDSA, B);
7599 if (NestedLoopCount == 0)
7600 return StmtError();
7601
7602 assert((CurContext->isDependentContext() || B.builtAll()) &&
7603 "omp for loop exprs were not built");
7604
Alexey Bataev382967a2015-12-08 12:06:20 +00007605 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7606 // The grainsize clause and num_tasks clause are mutually exclusive and may
7607 // not appear on the same taskloop directive.
7608 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7609 return StmtError();
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007610 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7611 // If a reduction clause is present on the taskloop directive, the nogroup
7612 // clause must not be specified.
7613 if (checkReductionClauseWithNogroup(*this, Clauses))
7614 return StmtError();
Alexey Bataev382967a2015-12-08 12:06:20 +00007615
Reid Kleckner87a31802018-03-12 21:43:02 +00007616 setFunctionHasBranchProtectedScope();
Alexey Bataev49f6e782015-12-01 04:18:41 +00007617 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
7618 NestedLoopCount, Clauses, AStmt, B);
7619}
7620
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007621StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
7622 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007623 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007624 if (!AStmt)
7625 return StmtError();
7626
7627 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7628 OMPLoopDirective::HelperExprs B;
7629 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7630 // define the nested loops number.
7631 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007632 checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007633 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
7634 VarsWithImplicitDSA, B);
7635 if (NestedLoopCount == 0)
7636 return StmtError();
7637
7638 assert((CurContext->isDependentContext() || B.builtAll()) &&
7639 "omp for loop exprs were not built");
7640
Alexey Bataev5a3af132016-03-29 08:58:54 +00007641 if (!CurContext->isDependentContext()) {
7642 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007643 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007644 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007645 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007646 B.NumIterations, *this, CurScope,
7647 DSAStack))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007648 return StmtError();
7649 }
7650 }
7651
Alexey Bataev382967a2015-12-08 12:06:20 +00007652 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7653 // The grainsize clause and num_tasks clause are mutually exclusive and may
7654 // not appear on the same taskloop directive.
7655 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7656 return StmtError();
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007657 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7658 // If a reduction clause is present on the taskloop directive, the nogroup
7659 // clause must not be specified.
7660 if (checkReductionClauseWithNogroup(*this, Clauses))
7661 return StmtError();
Alexey Bataev438388c2017-11-22 18:34:02 +00007662 if (checkSimdlenSafelenSpecified(*this, Clauses))
7663 return StmtError();
Alexey Bataev382967a2015-12-08 12:06:20 +00007664
Reid Kleckner87a31802018-03-12 21:43:02 +00007665 setFunctionHasBranchProtectedScope();
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007666 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
7667 NestedLoopCount, Clauses, AStmt, B);
7668}
7669
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007670StmtResult Sema::ActOnOpenMPDistributeDirective(
7671 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007672 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007673 if (!AStmt)
7674 return StmtError();
7675
7676 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7677 OMPLoopDirective::HelperExprs B;
7678 // In presence of clause 'collapse' with number of loops, it will
7679 // define the nested loops number.
7680 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007681 checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007682 nullptr /*ordered not a clause on distribute*/, AStmt,
7683 *this, *DSAStack, VarsWithImplicitDSA, B);
7684 if (NestedLoopCount == 0)
7685 return StmtError();
7686
7687 assert((CurContext->isDependentContext() || B.builtAll()) &&
7688 "omp for loop exprs were not built");
7689
Reid Kleckner87a31802018-03-12 21:43:02 +00007690 setFunctionHasBranchProtectedScope();
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007691 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
7692 NestedLoopCount, Clauses, AStmt, B);
7693}
7694
Carlo Bertolli9925f152016-06-27 14:55:37 +00007695StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
7696 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007697 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Carlo Bertolli9925f152016-06-27 14:55:37 +00007698 if (!AStmt)
7699 return StmtError();
7700
Alexey Bataeve3727102018-04-18 15:57:46 +00007701 auto *CS = cast<CapturedStmt>(AStmt);
Carlo Bertolli9925f152016-06-27 14:55:37 +00007702 // 1.2.2 OpenMP Language Terminology
7703 // Structured block - An executable statement with a single entry at the
7704 // top and a single exit at the bottom.
7705 // The point of exit cannot be a branch out of the structured block.
7706 // longjmp() and throw() must not violate the entry/exit criteria.
7707 CS->getCapturedDecl()->setNothrow();
Alexey Bataev7f96c372017-11-22 17:19:31 +00007708 for (int ThisCaptureLevel =
7709 getOpenMPCaptureLevels(OMPD_distribute_parallel_for);
7710 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7711 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7712 // 1.2.2 OpenMP Language Terminology
7713 // Structured block - An executable statement with a single entry at the
7714 // top and a single exit at the bottom.
7715 // The point of exit cannot be a branch out of the structured block.
7716 // longjmp() and throw() must not violate the entry/exit criteria.
7717 CS->getCapturedDecl()->setNothrow();
7718 }
Carlo Bertolli9925f152016-06-27 14:55:37 +00007719
7720 OMPLoopDirective::HelperExprs B;
7721 // In presence of clause 'collapse' with number of loops, it will
7722 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007723 unsigned NestedLoopCount = checkOpenMPLoop(
Carlo Bertolli9925f152016-06-27 14:55:37 +00007724 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataev7f96c372017-11-22 17:19:31 +00007725 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Carlo Bertolli9925f152016-06-27 14:55:37 +00007726 VarsWithImplicitDSA, B);
7727 if (NestedLoopCount == 0)
7728 return StmtError();
7729
7730 assert((CurContext->isDependentContext() || B.builtAll()) &&
7731 "omp for loop exprs were not built");
7732
Reid Kleckner87a31802018-03-12 21:43:02 +00007733 setFunctionHasBranchProtectedScope();
Carlo Bertolli9925f152016-06-27 14:55:37 +00007734 return OMPDistributeParallelForDirective::Create(
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00007735 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7736 DSAStack->isCancelRegion());
Carlo Bertolli9925f152016-06-27 14:55:37 +00007737}
7738
Kelvin Li4a39add2016-07-05 05:00:15 +00007739StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
7740 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007741 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li4a39add2016-07-05 05:00:15 +00007742 if (!AStmt)
7743 return StmtError();
7744
Alexey Bataeve3727102018-04-18 15:57:46 +00007745 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li4a39add2016-07-05 05:00:15 +00007746 // 1.2.2 OpenMP Language Terminology
7747 // Structured block - An executable statement with a single entry at the
7748 // top and a single exit at the bottom.
7749 // The point of exit cannot be a branch out of the structured block.
7750 // longjmp() and throw() must not violate the entry/exit criteria.
7751 CS->getCapturedDecl()->setNothrow();
Alexey Bataev974acd62017-11-27 19:38:52 +00007752 for (int ThisCaptureLevel =
7753 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd);
7754 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7755 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7756 // 1.2.2 OpenMP Language Terminology
7757 // Structured block - An executable statement with a single entry at the
7758 // top and a single exit at the bottom.
7759 // The point of exit cannot be a branch out of the structured block.
7760 // longjmp() and throw() must not violate the entry/exit criteria.
7761 CS->getCapturedDecl()->setNothrow();
7762 }
Kelvin Li4a39add2016-07-05 05:00:15 +00007763
7764 OMPLoopDirective::HelperExprs B;
7765 // In presence of clause 'collapse' with number of loops, it will
7766 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007767 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li4a39add2016-07-05 05:00:15 +00007768 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev974acd62017-11-27 19:38:52 +00007769 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li4a39add2016-07-05 05:00:15 +00007770 VarsWithImplicitDSA, B);
7771 if (NestedLoopCount == 0)
7772 return StmtError();
7773
7774 assert((CurContext->isDependentContext() || B.builtAll()) &&
7775 "omp for loop exprs were not built");
7776
Alexey Bataev438388c2017-11-22 18:34:02 +00007777 if (!CurContext->isDependentContext()) {
7778 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007779 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00007780 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7781 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7782 B.NumIterations, *this, CurScope,
7783 DSAStack))
7784 return StmtError();
7785 }
7786 }
7787
Kelvin Lic5609492016-07-15 04:39:07 +00007788 if (checkSimdlenSafelenSpecified(*this, Clauses))
7789 return StmtError();
7790
Reid Kleckner87a31802018-03-12 21:43:02 +00007791 setFunctionHasBranchProtectedScope();
Kelvin Li4a39add2016-07-05 05:00:15 +00007792 return OMPDistributeParallelForSimdDirective::Create(
7793 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7794}
7795
Kelvin Li787f3fc2016-07-06 04:45:38 +00007796StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
7797 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007798 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li787f3fc2016-07-06 04:45:38 +00007799 if (!AStmt)
7800 return StmtError();
7801
Alexey Bataeve3727102018-04-18 15:57:46 +00007802 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li787f3fc2016-07-06 04:45:38 +00007803 // 1.2.2 OpenMP Language Terminology
7804 // Structured block - An executable statement with a single entry at the
7805 // top and a single exit at the bottom.
7806 // The point of exit cannot be a branch out of the structured block.
7807 // longjmp() and throw() must not violate the entry/exit criteria.
7808 CS->getCapturedDecl()->setNothrow();
Alexey Bataev617db5f2017-12-04 15:38:33 +00007809 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd);
7810 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7811 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7812 // 1.2.2 OpenMP Language Terminology
7813 // Structured block - An executable statement with a single entry at the
7814 // top and a single exit at the bottom.
7815 // The point of exit cannot be a branch out of the structured block.
7816 // longjmp() and throw() must not violate the entry/exit criteria.
7817 CS->getCapturedDecl()->setNothrow();
7818 }
Kelvin Li787f3fc2016-07-06 04:45:38 +00007819
7820 OMPLoopDirective::HelperExprs B;
7821 // In presence of clause 'collapse' with number of loops, it will
7822 // define the nested loops number.
7823 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007824 checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev617db5f2017-12-04 15:38:33 +00007825 nullptr /*ordered not a clause on distribute*/, CS, *this,
7826 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li787f3fc2016-07-06 04:45:38 +00007827 if (NestedLoopCount == 0)
7828 return StmtError();
7829
7830 assert((CurContext->isDependentContext() || B.builtAll()) &&
7831 "omp for loop exprs were not built");
7832
Alexey Bataev438388c2017-11-22 18:34:02 +00007833 if (!CurContext->isDependentContext()) {
7834 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007835 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00007836 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7837 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7838 B.NumIterations, *this, CurScope,
7839 DSAStack))
7840 return StmtError();
7841 }
7842 }
7843
Kelvin Lic5609492016-07-15 04:39:07 +00007844 if (checkSimdlenSafelenSpecified(*this, Clauses))
7845 return StmtError();
7846
Reid Kleckner87a31802018-03-12 21:43:02 +00007847 setFunctionHasBranchProtectedScope();
Kelvin Li787f3fc2016-07-06 04:45:38 +00007848 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
7849 NestedLoopCount, Clauses, AStmt, B);
7850}
7851
Kelvin Lia579b912016-07-14 02:54:56 +00007852StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
7853 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007854 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Lia579b912016-07-14 02:54:56 +00007855 if (!AStmt)
7856 return StmtError();
7857
Alexey Bataeve3727102018-04-18 15:57:46 +00007858 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Lia579b912016-07-14 02:54:56 +00007859 // 1.2.2 OpenMP Language Terminology
7860 // Structured block - An executable statement with a single entry at the
7861 // top and a single exit at the bottom.
7862 // The point of exit cannot be a branch out of the structured block.
7863 // longjmp() and throw() must not violate the entry/exit criteria.
7864 CS->getCapturedDecl()->setNothrow();
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007865 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
7866 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7867 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7868 // 1.2.2 OpenMP Language Terminology
7869 // Structured block - An executable statement with a single entry at the
7870 // top and a single exit at the bottom.
7871 // The point of exit cannot be a branch out of the structured block.
7872 // longjmp() and throw() must not violate the entry/exit criteria.
7873 CS->getCapturedDecl()->setNothrow();
7874 }
Kelvin Lia579b912016-07-14 02:54:56 +00007875
7876 OMPLoopDirective::HelperExprs B;
7877 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7878 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007879 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Lia579b912016-07-14 02:54:56 +00007880 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007881 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Kelvin Lia579b912016-07-14 02:54:56 +00007882 VarsWithImplicitDSA, B);
7883 if (NestedLoopCount == 0)
7884 return StmtError();
7885
7886 assert((CurContext->isDependentContext() || B.builtAll()) &&
7887 "omp target parallel for simd loop exprs were not built");
7888
7889 if (!CurContext->isDependentContext()) {
7890 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007891 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007892 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Lia579b912016-07-14 02:54:56 +00007893 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7894 B.NumIterations, *this, CurScope,
7895 DSAStack))
7896 return StmtError();
7897 }
7898 }
Kelvin Lic5609492016-07-15 04:39:07 +00007899 if (checkSimdlenSafelenSpecified(*this, Clauses))
Kelvin Lia579b912016-07-14 02:54:56 +00007900 return StmtError();
7901
Reid Kleckner87a31802018-03-12 21:43:02 +00007902 setFunctionHasBranchProtectedScope();
Kelvin Lia579b912016-07-14 02:54:56 +00007903 return OMPTargetParallelForSimdDirective::Create(
7904 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7905}
7906
Kelvin Li986330c2016-07-20 22:57:10 +00007907StmtResult Sema::ActOnOpenMPTargetSimdDirective(
7908 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007909 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li986330c2016-07-20 22:57:10 +00007910 if (!AStmt)
7911 return StmtError();
7912
Alexey Bataeve3727102018-04-18 15:57:46 +00007913 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li986330c2016-07-20 22:57:10 +00007914 // 1.2.2 OpenMP Language Terminology
7915 // Structured block - An executable statement with a single entry at the
7916 // top and a single exit at the bottom.
7917 // The point of exit cannot be a branch out of the structured block.
7918 // longjmp() and throw() must not violate the entry/exit criteria.
7919 CS->getCapturedDecl()->setNothrow();
Alexey Bataevf8365372017-11-17 17:57:25 +00007920 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd);
7921 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7922 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7923 // 1.2.2 OpenMP Language Terminology
7924 // Structured block - An executable statement with a single entry at the
7925 // top and a single exit at the bottom.
7926 // The point of exit cannot be a branch out of the structured block.
7927 // longjmp() and throw() must not violate the entry/exit criteria.
7928 CS->getCapturedDecl()->setNothrow();
7929 }
7930
Kelvin Li986330c2016-07-20 22:57:10 +00007931 OMPLoopDirective::HelperExprs B;
7932 // In presence of clause 'collapse' with number of loops, it will define the
7933 // nested loops number.
David Majnemer9d168222016-08-05 17:44:54 +00007934 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007935 checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
Alexey Bataevf8365372017-11-17 17:57:25 +00007936 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Kelvin Li986330c2016-07-20 22:57:10 +00007937 VarsWithImplicitDSA, B);
7938 if (NestedLoopCount == 0)
7939 return StmtError();
7940
7941 assert((CurContext->isDependentContext() || B.builtAll()) &&
7942 "omp target simd loop exprs were not built");
7943
7944 if (!CurContext->isDependentContext()) {
7945 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007946 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007947 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Li986330c2016-07-20 22:57:10 +00007948 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7949 B.NumIterations, *this, CurScope,
7950 DSAStack))
7951 return StmtError();
7952 }
7953 }
7954
7955 if (checkSimdlenSafelenSpecified(*this, Clauses))
7956 return StmtError();
7957
Reid Kleckner87a31802018-03-12 21:43:02 +00007958 setFunctionHasBranchProtectedScope();
Kelvin Li986330c2016-07-20 22:57:10 +00007959 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
7960 NestedLoopCount, Clauses, AStmt, B);
7961}
7962
Kelvin Li02532872016-08-05 14:37:37 +00007963StmtResult Sema::ActOnOpenMPTeamsDistributeDirective(
7964 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007965 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li02532872016-08-05 14:37:37 +00007966 if (!AStmt)
7967 return StmtError();
7968
Alexey Bataeve3727102018-04-18 15:57:46 +00007969 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li02532872016-08-05 14:37:37 +00007970 // 1.2.2 OpenMP Language Terminology
7971 // Structured block - An executable statement with a single entry at the
7972 // top and a single exit at the bottom.
7973 // The point of exit cannot be a branch out of the structured block.
7974 // longjmp() and throw() must not violate the entry/exit criteria.
7975 CS->getCapturedDecl()->setNothrow();
Alexey Bataev95c6dd42017-11-29 15:14:16 +00007976 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute);
7977 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7978 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7979 // 1.2.2 OpenMP Language Terminology
7980 // Structured block - An executable statement with a single entry at the
7981 // top and a single exit at the bottom.
7982 // The point of exit cannot be a branch out of the structured block.
7983 // longjmp() and throw() must not violate the entry/exit criteria.
7984 CS->getCapturedDecl()->setNothrow();
7985 }
Kelvin Li02532872016-08-05 14:37:37 +00007986
7987 OMPLoopDirective::HelperExprs B;
7988 // In presence of clause 'collapse' with number of loops, it will
7989 // define the nested loops number.
7990 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007991 checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses),
Alexey Bataev95c6dd42017-11-29 15:14:16 +00007992 nullptr /*ordered not a clause on distribute*/, CS, *this,
7993 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li02532872016-08-05 14:37:37 +00007994 if (NestedLoopCount == 0)
7995 return StmtError();
7996
7997 assert((CurContext->isDependentContext() || B.builtAll()) &&
7998 "omp teams distribute loop exprs were not built");
7999
Reid Kleckner87a31802018-03-12 21:43:02 +00008000 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00008001
8002 DSAStack->setParentTeamsRegionLoc(StartLoc);
8003
David Majnemer9d168222016-08-05 17:44:54 +00008004 return OMPTeamsDistributeDirective::Create(
8005 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Kelvin Li02532872016-08-05 14:37:37 +00008006}
8007
Kelvin Li4e325f72016-10-25 12:50:55 +00008008StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective(
8009 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00008010 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li4e325f72016-10-25 12:50:55 +00008011 if (!AStmt)
8012 return StmtError();
8013
Alexey Bataeve3727102018-04-18 15:57:46 +00008014 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li4e325f72016-10-25 12:50:55 +00008015 // 1.2.2 OpenMP Language Terminology
8016 // Structured block - An executable statement with a single entry at the
8017 // top and a single exit at the bottom.
8018 // The point of exit cannot be a branch out of the structured block.
8019 // longjmp() and throw() must not violate the entry/exit criteria.
8020 CS->getCapturedDecl()->setNothrow();
Alexey Bataev999277a2017-12-06 14:31:09 +00008021 for (int ThisCaptureLevel =
8022 getOpenMPCaptureLevels(OMPD_teams_distribute_simd);
8023 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8024 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8025 // 1.2.2 OpenMP Language Terminology
8026 // Structured block - An executable statement with a single entry at the
8027 // top and a single exit at the bottom.
8028 // The point of exit cannot be a branch out of the structured block.
8029 // longjmp() and throw() must not violate the entry/exit criteria.
8030 CS->getCapturedDecl()->setNothrow();
8031 }
8032
Kelvin Li4e325f72016-10-25 12:50:55 +00008033
8034 OMPLoopDirective::HelperExprs B;
8035 // In presence of clause 'collapse' with number of loops, it will
8036 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00008037 unsigned NestedLoopCount = checkOpenMPLoop(
Samuel Antao4c8035b2016-12-12 18:00:20 +00008038 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev999277a2017-12-06 14:31:09 +00008039 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Samuel Antao4c8035b2016-12-12 18:00:20 +00008040 VarsWithImplicitDSA, B);
Kelvin Li4e325f72016-10-25 12:50:55 +00008041
8042 if (NestedLoopCount == 0)
8043 return StmtError();
8044
8045 assert((CurContext->isDependentContext() || B.builtAll()) &&
8046 "omp teams distribute simd loop exprs were not built");
8047
8048 if (!CurContext->isDependentContext()) {
8049 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00008050 for (OMPClause *C : Clauses) {
Kelvin Li4e325f72016-10-25 12:50:55 +00008051 if (auto *LC = dyn_cast<OMPLinearClause>(C))
8052 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8053 B.NumIterations, *this, CurScope,
8054 DSAStack))
8055 return StmtError();
8056 }
8057 }
8058
8059 if (checkSimdlenSafelenSpecified(*this, Clauses))
8060 return StmtError();
8061
Reid Kleckner87a31802018-03-12 21:43:02 +00008062 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00008063
8064 DSAStack->setParentTeamsRegionLoc(StartLoc);
8065
Kelvin Li4e325f72016-10-25 12:50:55 +00008066 return OMPTeamsDistributeSimdDirective::Create(
8067 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
8068}
8069
Kelvin Li579e41c2016-11-30 23:51:03 +00008070StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective(
8071 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00008072 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li579e41c2016-11-30 23:51:03 +00008073 if (!AStmt)
8074 return StmtError();
8075
Alexey Bataeve3727102018-04-18 15:57:46 +00008076 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li579e41c2016-11-30 23:51:03 +00008077 // 1.2.2 OpenMP Language Terminology
8078 // Structured block - An executable statement with a single entry at the
8079 // top and a single exit at the bottom.
8080 // The point of exit cannot be a branch out of the structured block.
8081 // longjmp() and throw() must not violate the entry/exit criteria.
8082 CS->getCapturedDecl()->setNothrow();
8083
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00008084 for (int ThisCaptureLevel =
8085 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd);
8086 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8087 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8088 // 1.2.2 OpenMP Language Terminology
8089 // Structured block - An executable statement with a single entry at the
8090 // top and a single exit at the bottom.
8091 // The point of exit cannot be a branch out of the structured block.
8092 // longjmp() and throw() must not violate the entry/exit criteria.
8093 CS->getCapturedDecl()->setNothrow();
8094 }
8095
Kelvin Li579e41c2016-11-30 23:51:03 +00008096 OMPLoopDirective::HelperExprs B;
8097 // In presence of clause 'collapse' with number of loops, it will
8098 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00008099 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li579e41c2016-11-30 23:51:03 +00008100 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00008101 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li579e41c2016-11-30 23:51:03 +00008102 VarsWithImplicitDSA, B);
8103
8104 if (NestedLoopCount == 0)
8105 return StmtError();
8106
8107 assert((CurContext->isDependentContext() || B.builtAll()) &&
8108 "omp for loop exprs were not built");
8109
8110 if (!CurContext->isDependentContext()) {
8111 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00008112 for (OMPClause *C : Clauses) {
Kelvin Li579e41c2016-11-30 23:51:03 +00008113 if (auto *LC = dyn_cast<OMPLinearClause>(C))
8114 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8115 B.NumIterations, *this, CurScope,
8116 DSAStack))
8117 return StmtError();
8118 }
8119 }
8120
8121 if (checkSimdlenSafelenSpecified(*this, Clauses))
8122 return StmtError();
8123
Reid Kleckner87a31802018-03-12 21:43:02 +00008124 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00008125
8126 DSAStack->setParentTeamsRegionLoc(StartLoc);
8127
Kelvin Li579e41c2016-11-30 23:51:03 +00008128 return OMPTeamsDistributeParallelForSimdDirective::Create(
8129 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
8130}
8131
Kelvin Li7ade93f2016-12-09 03:24:30 +00008132StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective(
8133 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00008134 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li7ade93f2016-12-09 03:24:30 +00008135 if (!AStmt)
8136 return StmtError();
8137
Alexey Bataeve3727102018-04-18 15:57:46 +00008138 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li7ade93f2016-12-09 03:24:30 +00008139 // 1.2.2 OpenMP Language Terminology
8140 // Structured block - An executable statement with a single entry at the
8141 // top and a single exit at the bottom.
8142 // The point of exit cannot be a branch out of the structured block.
8143 // longjmp() and throw() must not violate the entry/exit criteria.
8144 CS->getCapturedDecl()->setNothrow();
8145
Carlo Bertolli62fae152017-11-20 20:46:39 +00008146 for (int ThisCaptureLevel =
8147 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for);
8148 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8149 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8150 // 1.2.2 OpenMP Language Terminology
8151 // Structured block - An executable statement with a single entry at the
8152 // top and a single exit at the bottom.
8153 // The point of exit cannot be a branch out of the structured block.
8154 // longjmp() and throw() must not violate the entry/exit criteria.
8155 CS->getCapturedDecl()->setNothrow();
8156 }
8157
Kelvin Li7ade93f2016-12-09 03:24:30 +00008158 OMPLoopDirective::HelperExprs B;
8159 // In presence of clause 'collapse' with number of loops, it will
8160 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00008161 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li7ade93f2016-12-09 03:24:30 +00008162 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
Carlo Bertolli62fae152017-11-20 20:46:39 +00008163 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li7ade93f2016-12-09 03:24:30 +00008164 VarsWithImplicitDSA, B);
8165
8166 if (NestedLoopCount == 0)
8167 return StmtError();
8168
8169 assert((CurContext->isDependentContext() || B.builtAll()) &&
8170 "omp for loop exprs were not built");
8171
Reid Kleckner87a31802018-03-12 21:43:02 +00008172 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00008173
8174 DSAStack->setParentTeamsRegionLoc(StartLoc);
8175
Kelvin Li7ade93f2016-12-09 03:24:30 +00008176 return OMPTeamsDistributeParallelForDirective::Create(
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00008177 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
8178 DSAStack->isCancelRegion());
Kelvin Li7ade93f2016-12-09 03:24:30 +00008179}
8180
Kelvin Libf594a52016-12-17 05:48:59 +00008181StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses,
8182 Stmt *AStmt,
8183 SourceLocation StartLoc,
8184 SourceLocation EndLoc) {
8185 if (!AStmt)
8186 return StmtError();
8187
Alexey Bataeve3727102018-04-18 15:57:46 +00008188 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Libf594a52016-12-17 05:48:59 +00008189 // 1.2.2 OpenMP Language Terminology
8190 // Structured block - An executable statement with a single entry at the
8191 // top and a single exit at the bottom.
8192 // The point of exit cannot be a branch out of the structured block.
8193 // longjmp() and throw() must not violate the entry/exit criteria.
8194 CS->getCapturedDecl()->setNothrow();
8195
Alexey Bataevf9fc42e2017-11-22 14:25:55 +00008196 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams);
8197 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8198 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8199 // 1.2.2 OpenMP Language Terminology
8200 // Structured block - An executable statement with a single entry at the
8201 // top and a single exit at the bottom.
8202 // The point of exit cannot be a branch out of the structured block.
8203 // longjmp() and throw() must not violate the entry/exit criteria.
8204 CS->getCapturedDecl()->setNothrow();
8205 }
Reid Kleckner87a31802018-03-12 21:43:02 +00008206 setFunctionHasBranchProtectedScope();
Kelvin Libf594a52016-12-17 05:48:59 +00008207
8208 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses,
8209 AStmt);
8210}
8211
Kelvin Li83c451e2016-12-25 04:52:54 +00008212StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective(
8213 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00008214 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li83c451e2016-12-25 04:52:54 +00008215 if (!AStmt)
8216 return StmtError();
8217
Alexey Bataeve3727102018-04-18 15:57:46 +00008218 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li83c451e2016-12-25 04:52:54 +00008219 // 1.2.2 OpenMP Language Terminology
8220 // Structured block - An executable statement with a single entry at the
8221 // top and a single exit at the bottom.
8222 // The point of exit cannot be a branch out of the structured block.
8223 // longjmp() and throw() must not violate the entry/exit criteria.
8224 CS->getCapturedDecl()->setNothrow();
Alexey Bataevdfa430f2017-12-08 15:03:50 +00008225 for (int ThisCaptureLevel =
8226 getOpenMPCaptureLevels(OMPD_target_teams_distribute);
8227 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8228 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8229 // 1.2.2 OpenMP Language Terminology
8230 // Structured block - An executable statement with a single entry at the
8231 // top and a single exit at the bottom.
8232 // The point of exit cannot be a branch out of the structured block.
8233 // longjmp() and throw() must not violate the entry/exit criteria.
8234 CS->getCapturedDecl()->setNothrow();
8235 }
Kelvin Li83c451e2016-12-25 04:52:54 +00008236
8237 OMPLoopDirective::HelperExprs B;
8238 // In presence of clause 'collapse' with number of loops, it will
8239 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00008240 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataevdfa430f2017-12-08 15:03:50 +00008241 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses),
8242 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li83c451e2016-12-25 04:52:54 +00008243 VarsWithImplicitDSA, B);
8244 if (NestedLoopCount == 0)
8245 return StmtError();
8246
8247 assert((CurContext->isDependentContext() || B.builtAll()) &&
8248 "omp target teams distribute loop exprs were not built");
8249
Reid Kleckner87a31802018-03-12 21:43:02 +00008250 setFunctionHasBranchProtectedScope();
Kelvin Li83c451e2016-12-25 04:52:54 +00008251 return OMPTargetTeamsDistributeDirective::Create(
8252 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
8253}
8254
Kelvin Li80e8f562016-12-29 22:16:30 +00008255StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective(
8256 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00008257 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li80e8f562016-12-29 22:16:30 +00008258 if (!AStmt)
8259 return StmtError();
8260
Alexey Bataeve3727102018-04-18 15:57:46 +00008261 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li80e8f562016-12-29 22:16:30 +00008262 // 1.2.2 OpenMP Language Terminology
8263 // Structured block - An executable statement with a single entry at the
8264 // top and a single exit at the bottom.
8265 // The point of exit cannot be a branch out of the structured block.
8266 // longjmp() and throw() must not violate the entry/exit criteria.
8267 CS->getCapturedDecl()->setNothrow();
Carlo Bertolli52978c32018-01-03 21:12:44 +00008268 for (int ThisCaptureLevel =
8269 getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for);
8270 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8271 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8272 // 1.2.2 OpenMP Language Terminology
8273 // Structured block - An executable statement with a single entry at the
8274 // top and a single exit at the bottom.
8275 // The point of exit cannot be a branch out of the structured block.
8276 // longjmp() and throw() must not violate the entry/exit criteria.
8277 CS->getCapturedDecl()->setNothrow();
8278 }
8279
Kelvin Li80e8f562016-12-29 22:16:30 +00008280 OMPLoopDirective::HelperExprs B;
8281 // In presence of clause 'collapse' with number of loops, it will
8282 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00008283 unsigned NestedLoopCount = checkOpenMPLoop(
Carlo Bertolli52978c32018-01-03 21:12:44 +00008284 OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
8285 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li80e8f562016-12-29 22:16:30 +00008286 VarsWithImplicitDSA, B);
8287 if (NestedLoopCount == 0)
8288 return StmtError();
8289
8290 assert((CurContext->isDependentContext() || B.builtAll()) &&
8291 "omp target teams distribute parallel for loop exprs were not built");
8292
Alexey Bataev647dd842018-01-15 20:59:40 +00008293 if (!CurContext->isDependentContext()) {
8294 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00008295 for (OMPClause *C : Clauses) {
Alexey Bataev647dd842018-01-15 20:59:40 +00008296 if (auto *LC = dyn_cast<OMPLinearClause>(C))
8297 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8298 B.NumIterations, *this, CurScope,
8299 DSAStack))
8300 return StmtError();
8301 }
8302 }
8303
Reid Kleckner87a31802018-03-12 21:43:02 +00008304 setFunctionHasBranchProtectedScope();
Kelvin Li80e8f562016-12-29 22:16:30 +00008305 return OMPTargetTeamsDistributeParallelForDirective::Create(
Alexey Bataev16e79882017-11-22 21:12:03 +00008306 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
8307 DSAStack->isCancelRegion());
Kelvin Li80e8f562016-12-29 22:16:30 +00008308}
8309
Kelvin Li1851df52017-01-03 05:23:48 +00008310StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
8311 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00008312 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li1851df52017-01-03 05:23:48 +00008313 if (!AStmt)
8314 return StmtError();
8315
Alexey Bataeve3727102018-04-18 15:57:46 +00008316 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li1851df52017-01-03 05:23:48 +00008317 // 1.2.2 OpenMP Language Terminology
8318 // Structured block - An executable statement with a single entry at the
8319 // top and a single exit at the bottom.
8320 // The point of exit cannot be a branch out of the structured block.
8321 // longjmp() and throw() must not violate the entry/exit criteria.
8322 CS->getCapturedDecl()->setNothrow();
Alexey Bataev647dd842018-01-15 20:59:40 +00008323 for (int ThisCaptureLevel = getOpenMPCaptureLevels(
8324 OMPD_target_teams_distribute_parallel_for_simd);
8325 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8326 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8327 // 1.2.2 OpenMP Language Terminology
8328 // Structured block - An executable statement with a single entry at the
8329 // top and a single exit at the bottom.
8330 // The point of exit cannot be a branch out of the structured block.
8331 // longjmp() and throw() must not violate the entry/exit criteria.
8332 CS->getCapturedDecl()->setNothrow();
8333 }
Kelvin Li1851df52017-01-03 05:23:48 +00008334
8335 OMPLoopDirective::HelperExprs B;
8336 // In presence of clause 'collapse' with number of loops, it will
8337 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00008338 unsigned NestedLoopCount =
8339 checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd,
Alexey Bataev647dd842018-01-15 20:59:40 +00008340 getCollapseNumberExpr(Clauses),
8341 nullptr /*ordered not a clause on distribute*/, CS, *this,
8342 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li1851df52017-01-03 05:23:48 +00008343 if (NestedLoopCount == 0)
8344 return StmtError();
8345
8346 assert((CurContext->isDependentContext() || B.builtAll()) &&
8347 "omp target teams distribute parallel for simd loop exprs were not "
8348 "built");
8349
8350 if (!CurContext->isDependentContext()) {
8351 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00008352 for (OMPClause *C : Clauses) {
Kelvin Li1851df52017-01-03 05:23:48 +00008353 if (auto *LC = dyn_cast<OMPLinearClause>(C))
8354 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8355 B.NumIterations, *this, CurScope,
8356 DSAStack))
8357 return StmtError();
8358 }
8359 }
8360
Alexey Bataev438388c2017-11-22 18:34:02 +00008361 if (checkSimdlenSafelenSpecified(*this, Clauses))
8362 return StmtError();
8363
Reid Kleckner87a31802018-03-12 21:43:02 +00008364 setFunctionHasBranchProtectedScope();
Kelvin Li1851df52017-01-03 05:23:48 +00008365 return OMPTargetTeamsDistributeParallelForSimdDirective::Create(
8366 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
8367}
8368
Kelvin Lida681182017-01-10 18:08:18 +00008369StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective(
8370 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00008371 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Lida681182017-01-10 18:08:18 +00008372 if (!AStmt)
8373 return StmtError();
8374
8375 auto *CS = cast<CapturedStmt>(AStmt);
8376 // 1.2.2 OpenMP Language Terminology
8377 // Structured block - An executable statement with a single entry at the
8378 // top and a single exit at the bottom.
8379 // The point of exit cannot be a branch out of the structured block.
8380 // longjmp() and throw() must not violate the entry/exit criteria.
8381 CS->getCapturedDecl()->setNothrow();
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00008382 for (int ThisCaptureLevel =
8383 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd);
8384 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8385 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8386 // 1.2.2 OpenMP Language Terminology
8387 // Structured block - An executable statement with a single entry at the
8388 // top and a single exit at the bottom.
8389 // The point of exit cannot be a branch out of the structured block.
8390 // longjmp() and throw() must not violate the entry/exit criteria.
8391 CS->getCapturedDecl()->setNothrow();
8392 }
Kelvin Lida681182017-01-10 18:08:18 +00008393
8394 OMPLoopDirective::HelperExprs B;
8395 // In presence of clause 'collapse' with number of loops, it will
8396 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00008397 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Lida681182017-01-10 18:08:18 +00008398 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00008399 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Lida681182017-01-10 18:08:18 +00008400 VarsWithImplicitDSA, B);
8401 if (NestedLoopCount == 0)
8402 return StmtError();
8403
8404 assert((CurContext->isDependentContext() || B.builtAll()) &&
8405 "omp target teams distribute simd loop exprs were not built");
8406
Alexey Bataev438388c2017-11-22 18:34:02 +00008407 if (!CurContext->isDependentContext()) {
8408 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00008409 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00008410 if (auto *LC = dyn_cast<OMPLinearClause>(C))
8411 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8412 B.NumIterations, *this, CurScope,
8413 DSAStack))
8414 return StmtError();
8415 }
8416 }
8417
8418 if (checkSimdlenSafelenSpecified(*this, Clauses))
8419 return StmtError();
8420
Reid Kleckner87a31802018-03-12 21:43:02 +00008421 setFunctionHasBranchProtectedScope();
Kelvin Lida681182017-01-10 18:08:18 +00008422 return OMPTargetTeamsDistributeSimdDirective::Create(
8423 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
8424}
8425
Alexey Bataeved09d242014-05-28 05:53:51 +00008426OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008427 SourceLocation StartLoc,
8428 SourceLocation LParenLoc,
8429 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008430 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008431 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00008432 case OMPC_final:
8433 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
8434 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00008435 case OMPC_num_threads:
8436 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
8437 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00008438 case OMPC_safelen:
8439 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
8440 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00008441 case OMPC_simdlen:
8442 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
8443 break;
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00008444 case OMPC_allocator:
8445 Res = ActOnOpenMPAllocatorClause(Expr, StartLoc, LParenLoc, EndLoc);
8446 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00008447 case OMPC_collapse:
8448 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
8449 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00008450 case OMPC_ordered:
8451 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
8452 break;
Michael Wonge710d542015-08-07 16:16:36 +00008453 case OMPC_device:
8454 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
8455 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00008456 case OMPC_num_teams:
8457 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
8458 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008459 case OMPC_thread_limit:
8460 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
8461 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00008462 case OMPC_priority:
8463 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
8464 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008465 case OMPC_grainsize:
8466 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
8467 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00008468 case OMPC_num_tasks:
8469 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
8470 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00008471 case OMPC_hint:
8472 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
8473 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008474 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008475 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008476 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008477 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008478 case OMPC_private:
8479 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00008480 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008481 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00008482 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008483 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008484 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00008485 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008486 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008487 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008488 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00008489 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008490 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008491 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008492 case OMPC_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +00008493 case OMPC_allocate:
Alexey Bataev6125da92014-07-21 11:26:11 +00008494 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008495 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008496 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008497 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008498 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008499 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008500 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00008501 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008502 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008503 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00008504 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008505 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008506 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008507 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008508 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00008509 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00008510 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00008511 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00008512 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00008513 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00008514 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00008515 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00008516 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00008517 case OMPC_atomic_default_mem_order:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008518 llvm_unreachable("Clause is not allowed.");
8519 }
8520 return Res;
8521}
8522
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008523// An OpenMP directive such as 'target parallel' has two captured regions:
8524// for the 'target' and 'parallel' respectively. This function returns
8525// the region in which to capture expressions associated with a clause.
8526// A return value of OMPD_unknown signifies that the expression should not
8527// be captured.
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008528static OpenMPDirectiveKind getOpenMPCaptureRegionForClause(
8529 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
8530 OpenMPDirectiveKind NameModifier = OMPD_unknown) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008531 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008532 switch (CKind) {
8533 case OMPC_if:
8534 switch (DKind) {
8535 case OMPD_target_parallel:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008536 case OMPD_target_parallel_for:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00008537 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008538 // If this clause applies to the nested 'parallel' region, capture within
8539 // the 'target' region, otherwise do not capture.
8540 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
8541 CaptureRegion = OMPD_target;
8542 break;
Carlo Bertolli52978c32018-01-03 21:12:44 +00008543 case OMPD_target_teams_distribute_parallel_for:
8544 case OMPD_target_teams_distribute_parallel_for_simd:
8545 // If this clause applies to the nested 'parallel' region, capture within
8546 // the 'teams' region, otherwise do not capture.
8547 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
8548 CaptureRegion = OMPD_teams;
8549 break;
Carlo Bertolli62fae152017-11-20 20:46:39 +00008550 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008551 case OMPD_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008552 CaptureRegion = OMPD_teams;
8553 break;
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008554 case OMPD_target_update:
Alexey Bataevfab20e42017-12-27 18:49:38 +00008555 case OMPD_target_enter_data:
8556 case OMPD_target_exit_data:
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008557 CaptureRegion = OMPD_task;
8558 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008559 case OMPD_cancel:
8560 case OMPD_parallel:
8561 case OMPD_parallel_sections:
8562 case OMPD_parallel_for:
8563 case OMPD_parallel_for_simd:
8564 case OMPD_target:
8565 case OMPD_target_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008566 case OMPD_target_teams:
8567 case OMPD_target_teams_distribute:
8568 case OMPD_target_teams_distribute_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008569 case OMPD_distribute_parallel_for:
8570 case OMPD_distribute_parallel_for_simd:
8571 case OMPD_task:
8572 case OMPD_taskloop:
8573 case OMPD_taskloop_simd:
8574 case OMPD_target_data:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008575 // Do not capture if-clause expressions.
8576 break;
8577 case OMPD_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +00008578 case OMPD_allocate:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008579 case OMPD_taskyield:
8580 case OMPD_barrier:
8581 case OMPD_taskwait:
8582 case OMPD_cancellation_point:
8583 case OMPD_flush:
8584 case OMPD_declare_reduction:
Michael Kruse251e1482019-02-01 20:25:04 +00008585 case OMPD_declare_mapper:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008586 case OMPD_declare_simd:
8587 case OMPD_declare_target:
8588 case OMPD_end_declare_target:
8589 case OMPD_teams:
8590 case OMPD_simd:
8591 case OMPD_for:
8592 case OMPD_for_simd:
8593 case OMPD_sections:
8594 case OMPD_section:
8595 case OMPD_single:
8596 case OMPD_master:
8597 case OMPD_critical:
8598 case OMPD_taskgroup:
8599 case OMPD_distribute:
8600 case OMPD_ordered:
8601 case OMPD_atomic:
8602 case OMPD_distribute_simd:
8603 case OMPD_teams_distribute:
8604 case OMPD_teams_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008605 case OMPD_requires:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008606 llvm_unreachable("Unexpected OpenMP directive with if-clause");
8607 case OMPD_unknown:
8608 llvm_unreachable("Unknown OpenMP directive");
8609 }
8610 break;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008611 case OMPC_num_threads:
8612 switch (DKind) {
8613 case OMPD_target_parallel:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008614 case OMPD_target_parallel_for:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00008615 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008616 CaptureRegion = OMPD_target;
8617 break;
Carlo Bertolli62fae152017-11-20 20:46:39 +00008618 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008619 case OMPD_teams_distribute_parallel_for_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008620 case OMPD_target_teams_distribute_parallel_for:
8621 case OMPD_target_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008622 CaptureRegion = OMPD_teams;
8623 break;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008624 case OMPD_parallel:
8625 case OMPD_parallel_sections:
8626 case OMPD_parallel_for:
8627 case OMPD_parallel_for_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008628 case OMPD_distribute_parallel_for:
8629 case OMPD_distribute_parallel_for_simd:
8630 // Do not capture num_threads-clause expressions.
8631 break;
8632 case OMPD_target_data:
8633 case OMPD_target_enter_data:
8634 case OMPD_target_exit_data:
8635 case OMPD_target_update:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008636 case OMPD_target:
8637 case OMPD_target_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008638 case OMPD_target_teams:
8639 case OMPD_target_teams_distribute:
8640 case OMPD_target_teams_distribute_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008641 case OMPD_cancel:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008642 case OMPD_task:
8643 case OMPD_taskloop:
8644 case OMPD_taskloop_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008645 case OMPD_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +00008646 case OMPD_allocate:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008647 case OMPD_taskyield:
8648 case OMPD_barrier:
8649 case OMPD_taskwait:
8650 case OMPD_cancellation_point:
8651 case OMPD_flush:
8652 case OMPD_declare_reduction:
Michael Kruse251e1482019-02-01 20:25:04 +00008653 case OMPD_declare_mapper:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008654 case OMPD_declare_simd:
8655 case OMPD_declare_target:
8656 case OMPD_end_declare_target:
8657 case OMPD_teams:
8658 case OMPD_simd:
8659 case OMPD_for:
8660 case OMPD_for_simd:
8661 case OMPD_sections:
8662 case OMPD_section:
8663 case OMPD_single:
8664 case OMPD_master:
8665 case OMPD_critical:
8666 case OMPD_taskgroup:
8667 case OMPD_distribute:
8668 case OMPD_ordered:
8669 case OMPD_atomic:
8670 case OMPD_distribute_simd:
8671 case OMPD_teams_distribute:
8672 case OMPD_teams_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008673 case OMPD_requires:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008674 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause");
8675 case OMPD_unknown:
8676 llvm_unreachable("Unknown OpenMP directive");
8677 }
8678 break;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008679 case OMPC_num_teams:
8680 switch (DKind) {
8681 case OMPD_target_teams:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008682 case OMPD_target_teams_distribute:
8683 case OMPD_target_teams_distribute_simd:
8684 case OMPD_target_teams_distribute_parallel_for:
8685 case OMPD_target_teams_distribute_parallel_for_simd:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008686 CaptureRegion = OMPD_target;
8687 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008688 case OMPD_teams_distribute_parallel_for:
8689 case OMPD_teams_distribute_parallel_for_simd:
8690 case OMPD_teams:
8691 case OMPD_teams_distribute:
8692 case OMPD_teams_distribute_simd:
8693 // Do not capture num_teams-clause expressions.
8694 break;
8695 case OMPD_distribute_parallel_for:
8696 case OMPD_distribute_parallel_for_simd:
8697 case OMPD_task:
8698 case OMPD_taskloop:
8699 case OMPD_taskloop_simd:
8700 case OMPD_target_data:
8701 case OMPD_target_enter_data:
8702 case OMPD_target_exit_data:
8703 case OMPD_target_update:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008704 case OMPD_cancel:
8705 case OMPD_parallel:
8706 case OMPD_parallel_sections:
8707 case OMPD_parallel_for:
8708 case OMPD_parallel_for_simd:
8709 case OMPD_target:
8710 case OMPD_target_simd:
8711 case OMPD_target_parallel:
8712 case OMPD_target_parallel_for:
8713 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008714 case OMPD_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +00008715 case OMPD_allocate:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008716 case OMPD_taskyield:
8717 case OMPD_barrier:
8718 case OMPD_taskwait:
8719 case OMPD_cancellation_point:
8720 case OMPD_flush:
8721 case OMPD_declare_reduction:
Michael Kruse251e1482019-02-01 20:25:04 +00008722 case OMPD_declare_mapper:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008723 case OMPD_declare_simd:
8724 case OMPD_declare_target:
8725 case OMPD_end_declare_target:
8726 case OMPD_simd:
8727 case OMPD_for:
8728 case OMPD_for_simd:
8729 case OMPD_sections:
8730 case OMPD_section:
8731 case OMPD_single:
8732 case OMPD_master:
8733 case OMPD_critical:
8734 case OMPD_taskgroup:
8735 case OMPD_distribute:
8736 case OMPD_ordered:
8737 case OMPD_atomic:
8738 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008739 case OMPD_requires:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008740 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
8741 case OMPD_unknown:
8742 llvm_unreachable("Unknown OpenMP directive");
8743 }
8744 break;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008745 case OMPC_thread_limit:
8746 switch (DKind) {
8747 case OMPD_target_teams:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008748 case OMPD_target_teams_distribute:
8749 case OMPD_target_teams_distribute_simd:
8750 case OMPD_target_teams_distribute_parallel_for:
8751 case OMPD_target_teams_distribute_parallel_for_simd:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008752 CaptureRegion = OMPD_target;
8753 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008754 case OMPD_teams_distribute_parallel_for:
8755 case OMPD_teams_distribute_parallel_for_simd:
8756 case OMPD_teams:
8757 case OMPD_teams_distribute:
8758 case OMPD_teams_distribute_simd:
8759 // Do not capture thread_limit-clause expressions.
8760 break;
8761 case OMPD_distribute_parallel_for:
8762 case OMPD_distribute_parallel_for_simd:
8763 case OMPD_task:
8764 case OMPD_taskloop:
8765 case OMPD_taskloop_simd:
8766 case OMPD_target_data:
8767 case OMPD_target_enter_data:
8768 case OMPD_target_exit_data:
8769 case OMPD_target_update:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008770 case OMPD_cancel:
8771 case OMPD_parallel:
8772 case OMPD_parallel_sections:
8773 case OMPD_parallel_for:
8774 case OMPD_parallel_for_simd:
8775 case OMPD_target:
8776 case OMPD_target_simd:
8777 case OMPD_target_parallel:
8778 case OMPD_target_parallel_for:
8779 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008780 case OMPD_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +00008781 case OMPD_allocate:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008782 case OMPD_taskyield:
8783 case OMPD_barrier:
8784 case OMPD_taskwait:
8785 case OMPD_cancellation_point:
8786 case OMPD_flush:
8787 case OMPD_declare_reduction:
Michael Kruse251e1482019-02-01 20:25:04 +00008788 case OMPD_declare_mapper:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008789 case OMPD_declare_simd:
8790 case OMPD_declare_target:
8791 case OMPD_end_declare_target:
8792 case OMPD_simd:
8793 case OMPD_for:
8794 case OMPD_for_simd:
8795 case OMPD_sections:
8796 case OMPD_section:
8797 case OMPD_single:
8798 case OMPD_master:
8799 case OMPD_critical:
8800 case OMPD_taskgroup:
8801 case OMPD_distribute:
8802 case OMPD_ordered:
8803 case OMPD_atomic:
8804 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008805 case OMPD_requires:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008806 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause");
8807 case OMPD_unknown:
8808 llvm_unreachable("Unknown OpenMP directive");
8809 }
8810 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008811 case OMPC_schedule:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008812 switch (DKind) {
Alexey Bataev2ba67042017-11-28 21:11:44 +00008813 case OMPD_parallel_for:
8814 case OMPD_parallel_for_simd:
Alexey Bataev7f96c372017-11-22 17:19:31 +00008815 case OMPD_distribute_parallel_for:
Alexey Bataev974acd62017-11-27 19:38:52 +00008816 case OMPD_distribute_parallel_for_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008817 case OMPD_teams_distribute_parallel_for:
8818 case OMPD_teams_distribute_parallel_for_simd:
8819 case OMPD_target_parallel_for:
8820 case OMPD_target_parallel_for_simd:
8821 case OMPD_target_teams_distribute_parallel_for:
8822 case OMPD_target_teams_distribute_parallel_for_simd:
Alexey Bataev7f96c372017-11-22 17:19:31 +00008823 CaptureRegion = OMPD_parallel;
8824 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008825 case OMPD_for:
8826 case OMPD_for_simd:
8827 // Do not capture schedule-clause expressions.
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008828 break;
8829 case OMPD_task:
8830 case OMPD_taskloop:
8831 case OMPD_taskloop_simd:
8832 case OMPD_target_data:
8833 case OMPD_target_enter_data:
8834 case OMPD_target_exit_data:
8835 case OMPD_target_update:
8836 case OMPD_teams:
8837 case OMPD_teams_distribute:
8838 case OMPD_teams_distribute_simd:
8839 case OMPD_target_teams_distribute:
8840 case OMPD_target_teams_distribute_simd:
8841 case OMPD_target:
8842 case OMPD_target_simd:
8843 case OMPD_target_parallel:
8844 case OMPD_cancel:
8845 case OMPD_parallel:
8846 case OMPD_parallel_sections:
8847 case OMPD_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +00008848 case OMPD_allocate:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008849 case OMPD_taskyield:
8850 case OMPD_barrier:
8851 case OMPD_taskwait:
8852 case OMPD_cancellation_point:
8853 case OMPD_flush:
8854 case OMPD_declare_reduction:
Michael Kruse251e1482019-02-01 20:25:04 +00008855 case OMPD_declare_mapper:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008856 case OMPD_declare_simd:
8857 case OMPD_declare_target:
8858 case OMPD_end_declare_target:
8859 case OMPD_simd:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008860 case OMPD_sections:
8861 case OMPD_section:
8862 case OMPD_single:
8863 case OMPD_master:
8864 case OMPD_critical:
8865 case OMPD_taskgroup:
8866 case OMPD_distribute:
8867 case OMPD_ordered:
8868 case OMPD_atomic:
8869 case OMPD_distribute_simd:
8870 case OMPD_target_teams:
Kelvin Li1408f912018-09-26 04:28:39 +00008871 case OMPD_requires:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008872 llvm_unreachable("Unexpected OpenMP directive with schedule clause");
8873 case OMPD_unknown:
8874 llvm_unreachable("Unknown OpenMP directive");
8875 }
8876 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008877 case OMPC_dist_schedule:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008878 switch (DKind) {
8879 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008880 case OMPD_teams_distribute_parallel_for_simd:
8881 case OMPD_teams_distribute:
8882 case OMPD_teams_distribute_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008883 case OMPD_target_teams_distribute_parallel_for:
8884 case OMPD_target_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008885 case OMPD_target_teams_distribute:
8886 case OMPD_target_teams_distribute_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008887 CaptureRegion = OMPD_teams;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008888 break;
8889 case OMPD_distribute_parallel_for:
8890 case OMPD_distribute_parallel_for_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008891 case OMPD_distribute:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008892 case OMPD_distribute_simd:
8893 // Do not capture thread_limit-clause expressions.
8894 break;
8895 case OMPD_parallel_for:
8896 case OMPD_parallel_for_simd:
8897 case OMPD_target_parallel_for_simd:
8898 case OMPD_target_parallel_for:
8899 case OMPD_task:
8900 case OMPD_taskloop:
8901 case OMPD_taskloop_simd:
8902 case OMPD_target_data:
8903 case OMPD_target_enter_data:
8904 case OMPD_target_exit_data:
8905 case OMPD_target_update:
8906 case OMPD_teams:
8907 case OMPD_target:
8908 case OMPD_target_simd:
8909 case OMPD_target_parallel:
8910 case OMPD_cancel:
8911 case OMPD_parallel:
8912 case OMPD_parallel_sections:
8913 case OMPD_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +00008914 case OMPD_allocate:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008915 case OMPD_taskyield:
8916 case OMPD_barrier:
8917 case OMPD_taskwait:
8918 case OMPD_cancellation_point:
8919 case OMPD_flush:
8920 case OMPD_declare_reduction:
Michael Kruse251e1482019-02-01 20:25:04 +00008921 case OMPD_declare_mapper:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008922 case OMPD_declare_simd:
8923 case OMPD_declare_target:
8924 case OMPD_end_declare_target:
8925 case OMPD_simd:
8926 case OMPD_for:
8927 case OMPD_for_simd:
8928 case OMPD_sections:
8929 case OMPD_section:
8930 case OMPD_single:
8931 case OMPD_master:
8932 case OMPD_critical:
8933 case OMPD_taskgroup:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008934 case OMPD_ordered:
8935 case OMPD_atomic:
8936 case OMPD_target_teams:
Kelvin Li1408f912018-09-26 04:28:39 +00008937 case OMPD_requires:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008938 llvm_unreachable("Unexpected OpenMP directive with schedule clause");
8939 case OMPD_unknown:
8940 llvm_unreachable("Unknown OpenMP directive");
8941 }
8942 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008943 case OMPC_device:
8944 switch (DKind) {
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008945 case OMPD_target_update:
Alexey Bataevfab20e42017-12-27 18:49:38 +00008946 case OMPD_target_enter_data:
8947 case OMPD_target_exit_data:
Alexey Bataev8451efa2018-01-15 19:06:12 +00008948 case OMPD_target:
Alexey Bataevf41c88f2018-01-16 15:05:16 +00008949 case OMPD_target_simd:
Alexey Bataev0c869ef2018-01-16 15:57:07 +00008950 case OMPD_target_teams:
Alexey Bataev54d5c7d2018-01-16 16:27:49 +00008951 case OMPD_target_parallel:
Alexey Bataev79df7562018-01-16 16:46:46 +00008952 case OMPD_target_teams_distribute:
Alexey Bataev8d16a432018-01-16 17:22:50 +00008953 case OMPD_target_teams_distribute_simd:
Alexey Bataev8ed895512018-01-16 17:41:04 +00008954 case OMPD_target_parallel_for:
Alexey Bataevd60d1ba2018-01-16 17:55:15 +00008955 case OMPD_target_parallel_for_simd:
Alexey Bataev9f9fb0b2018-01-16 19:02:33 +00008956 case OMPD_target_teams_distribute_parallel_for:
Alexey Bataev9350fc32018-01-16 19:18:24 +00008957 case OMPD_target_teams_distribute_parallel_for_simd:
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008958 CaptureRegion = OMPD_task;
8959 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008960 case OMPD_target_data:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008961 // Do not capture device-clause expressions.
8962 break;
8963 case OMPD_teams_distribute_parallel_for:
8964 case OMPD_teams_distribute_parallel_for_simd:
8965 case OMPD_teams:
8966 case OMPD_teams_distribute:
8967 case OMPD_teams_distribute_simd:
8968 case OMPD_distribute_parallel_for:
8969 case OMPD_distribute_parallel_for_simd:
8970 case OMPD_task:
8971 case OMPD_taskloop:
8972 case OMPD_taskloop_simd:
8973 case OMPD_cancel:
8974 case OMPD_parallel:
8975 case OMPD_parallel_sections:
8976 case OMPD_parallel_for:
8977 case OMPD_parallel_for_simd:
8978 case OMPD_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +00008979 case OMPD_allocate:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008980 case OMPD_taskyield:
8981 case OMPD_barrier:
8982 case OMPD_taskwait:
8983 case OMPD_cancellation_point:
8984 case OMPD_flush:
8985 case OMPD_declare_reduction:
Michael Kruse251e1482019-02-01 20:25:04 +00008986 case OMPD_declare_mapper:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008987 case OMPD_declare_simd:
8988 case OMPD_declare_target:
8989 case OMPD_end_declare_target:
8990 case OMPD_simd:
8991 case OMPD_for:
8992 case OMPD_for_simd:
8993 case OMPD_sections:
8994 case OMPD_section:
8995 case OMPD_single:
8996 case OMPD_master:
8997 case OMPD_critical:
8998 case OMPD_taskgroup:
8999 case OMPD_distribute:
9000 case OMPD_ordered:
9001 case OMPD_atomic:
9002 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00009003 case OMPD_requires:
Alexey Bataev2ba67042017-11-28 21:11:44 +00009004 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
9005 case OMPD_unknown:
9006 llvm_unreachable("Unknown OpenMP directive");
9007 }
9008 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00009009 case OMPC_firstprivate:
9010 case OMPC_lastprivate:
9011 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00009012 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00009013 case OMPC_in_reduction:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00009014 case OMPC_linear:
9015 case OMPC_default:
9016 case OMPC_proc_bind:
9017 case OMPC_final:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00009018 case OMPC_safelen:
9019 case OMPC_simdlen:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00009020 case OMPC_allocator:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00009021 case OMPC_collapse:
9022 case OMPC_private:
9023 case OMPC_shared:
9024 case OMPC_aligned:
9025 case OMPC_copyin:
9026 case OMPC_copyprivate:
9027 case OMPC_ordered:
9028 case OMPC_nowait:
9029 case OMPC_untied:
9030 case OMPC_mergeable:
9031 case OMPC_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +00009032 case OMPC_allocate:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00009033 case OMPC_flush:
9034 case OMPC_read:
9035 case OMPC_write:
9036 case OMPC_update:
9037 case OMPC_capture:
9038 case OMPC_seq_cst:
9039 case OMPC_depend:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00009040 case OMPC_threads:
9041 case OMPC_simd:
9042 case OMPC_map:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00009043 case OMPC_priority:
9044 case OMPC_grainsize:
9045 case OMPC_nogroup:
9046 case OMPC_num_tasks:
9047 case OMPC_hint:
9048 case OMPC_defaultmap:
9049 case OMPC_unknown:
9050 case OMPC_uniform:
9051 case OMPC_to:
9052 case OMPC_from:
9053 case OMPC_use_device_ptr:
9054 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00009055 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00009056 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009057 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009058 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009059 case OMPC_atomic_default_mem_order:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00009060 llvm_unreachable("Unexpected OpenMP clause.");
9061 }
9062 return CaptureRegion;
9063}
9064
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009065OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
9066 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009067 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009068 SourceLocation NameModifierLoc,
9069 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009070 SourceLocation EndLoc) {
9071 Expr *ValExpr = Condition;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00009072 Stmt *HelperValStmt = nullptr;
9073 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009074 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
9075 !Condition->isInstantiationDependent() &&
9076 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00009077 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009078 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009079 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009080
Alexey Bataev8e769ee2017-12-22 21:01:52 +00009081 ValExpr = Val.get();
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00009082
9083 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
9084 CaptureRegion =
9085 getOpenMPCaptureRegionForClause(DKind, OMPC_if, NameModifier);
Alexey Bataev2ba67042017-11-28 21:11:44 +00009086 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00009087 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00009088 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00009089 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
9090 HelperValStmt = buildPreInits(Context, Captures);
9091 }
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009092 }
9093
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00009094 return new (Context)
9095 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
9096 LParenLoc, NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009097}
9098
Alexey Bataev3778b602014-07-17 07:32:53 +00009099OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
9100 SourceLocation StartLoc,
9101 SourceLocation LParenLoc,
9102 SourceLocation EndLoc) {
9103 Expr *ValExpr = Condition;
9104 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
9105 !Condition->isInstantiationDependent() &&
9106 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00009107 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataev3778b602014-07-17 07:32:53 +00009108 if (Val.isInvalid())
9109 return nullptr;
9110
Richard Smith03a4aa32016-06-23 19:02:52 +00009111 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataev3778b602014-07-17 07:32:53 +00009112 }
9113
9114 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
9115}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00009116ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
9117 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00009118 if (!Op)
9119 return ExprError();
9120
9121 class IntConvertDiagnoser : public ICEConvertDiagnoser {
9122 public:
9123 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00009124 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00009125 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
9126 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00009127 return S.Diag(Loc, diag::err_omp_not_integral) << T;
9128 }
Alexey Bataeved09d242014-05-28 05:53:51 +00009129 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
9130 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00009131 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
9132 }
Alexey Bataeved09d242014-05-28 05:53:51 +00009133 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
9134 QualType T,
9135 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00009136 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
9137 }
Alexey Bataeved09d242014-05-28 05:53:51 +00009138 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
9139 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00009140 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00009141 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00009142 }
Alexey Bataeved09d242014-05-28 05:53:51 +00009143 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
9144 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00009145 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
9146 }
Alexey Bataeved09d242014-05-28 05:53:51 +00009147 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
9148 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00009149 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00009150 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00009151 }
Alexey Bataeved09d242014-05-28 05:53:51 +00009152 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
9153 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00009154 llvm_unreachable("conversion functions are permitted");
9155 }
9156 } ConvertDiagnoser;
9157 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
9158}
9159
Alexey Bataeve3727102018-04-18 15:57:46 +00009160static bool isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00009161 OpenMPClauseKind CKind,
9162 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009163 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
9164 !ValExpr->isInstantiationDependent()) {
9165 SourceLocation Loc = ValExpr->getExprLoc();
9166 ExprResult Value =
9167 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
9168 if (Value.isInvalid())
9169 return false;
9170
9171 ValExpr = Value.get();
9172 // The expression must evaluate to a non-negative integer value.
9173 llvm::APSInt Result;
9174 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00009175 Result.isSigned() &&
9176 !((!StrictlyPositive && Result.isNonNegative()) ||
9177 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009178 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00009179 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
9180 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009181 return false;
9182 }
9183 }
9184 return true;
9185}
9186
Alexey Bataev568a8332014-03-06 06:15:19 +00009187OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
9188 SourceLocation StartLoc,
9189 SourceLocation LParenLoc,
9190 SourceLocation EndLoc) {
9191 Expr *ValExpr = NumThreads;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00009192 Stmt *HelperValStmt = nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00009193
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009194 // OpenMP [2.5, Restrictions]
9195 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +00009196 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
Alexey Bataeva0569352015-12-01 10:17:31 +00009197 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009198 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00009199
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00009200 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +00009201 OpenMPDirectiveKind CaptureRegion =
9202 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads);
9203 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00009204 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00009205 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00009206 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
9207 HelperValStmt = buildPreInits(Context, Captures);
9208 }
9209
9210 return new (Context) OMPNumThreadsClause(
9211 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00009212}
9213
Alexey Bataev62c87d22014-03-21 04:51:18 +00009214ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009215 OpenMPClauseKind CKind,
9216 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00009217 if (!E)
9218 return ExprError();
9219 if (E->isValueDependent() || E->isTypeDependent() ||
9220 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00009221 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00009222 llvm::APSInt Result;
9223 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
9224 if (ICE.isInvalid())
9225 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009226 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
9227 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00009228 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009229 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
9230 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00009231 return ExprError();
9232 }
Alexander Musman09184fe2014-09-30 05:29:28 +00009233 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
9234 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
9235 << E->getSourceRange();
9236 return ExprError();
9237 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009238 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
9239 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00009240 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009241 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00009242 return ICE;
9243}
9244
9245OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
9246 SourceLocation LParenLoc,
9247 SourceLocation EndLoc) {
9248 // OpenMP [2.8.1, simd construct, Description]
9249 // The parameter of the safelen clause must be a constant
9250 // positive integer expression.
9251 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
9252 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009253 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00009254 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00009255 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00009256}
9257
Alexey Bataev66b15b52015-08-21 11:14:16 +00009258OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
9259 SourceLocation LParenLoc,
9260 SourceLocation EndLoc) {
9261 // OpenMP [2.8.1, simd construct, Description]
9262 // The parameter of the simdlen clause must be a constant
9263 // positive integer expression.
9264 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
9265 if (Simdlen.isInvalid())
9266 return nullptr;
9267 return new (Context)
9268 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
9269}
9270
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00009271/// Tries to find omp_allocator_handle_t type.
9272static bool FindOMPAllocatorHandleT(Sema &S, SourceLocation Loc,
9273 QualType &OMPAllocatorHandleT) {
9274 if (!OMPAllocatorHandleT.isNull())
9275 return true;
9276 DeclarationName OMPAllocatorHandleTName =
9277 &S.getASTContext().Idents.get("omp_allocator_handle_t");
9278 auto *TD = dyn_cast_or_null<TypeDecl>(S.LookupSingleName(
9279 S.TUScope, OMPAllocatorHandleTName, Loc, Sema::LookupAnyName));
9280 if (!TD) {
9281 S.Diag(Loc, diag::err_implied_omp_allocator_handle_t_not_found);
9282 return false;
9283 }
9284 OMPAllocatorHandleT = S.getASTContext().getTypeDeclType(TD);
9285 return true;
9286}
9287
9288OMPClause *Sema::ActOnOpenMPAllocatorClause(Expr *A, SourceLocation StartLoc,
9289 SourceLocation LParenLoc,
9290 SourceLocation EndLoc) {
9291 // OpenMP [2.11.3, allocate Directive, Description]
9292 // allocator is an expression of omp_allocator_handle_t type.
9293 if (!FindOMPAllocatorHandleT(*this, A->getExprLoc(), OMPAllocatorHandleT))
9294 return nullptr;
9295
9296 ExprResult Allocator = DefaultLvalueConversion(A);
9297 if (Allocator.isInvalid())
9298 return nullptr;
9299 Allocator = PerformImplicitConversion(Allocator.get(), OMPAllocatorHandleT,
9300 Sema::AA_Initializing,
9301 /*AllowExplicit=*/true);
9302 if (Allocator.isInvalid())
9303 return nullptr;
9304 return new (Context)
9305 OMPAllocatorClause(Allocator.get(), StartLoc, LParenLoc, EndLoc);
9306}
9307
Alexander Musman64d33f12014-06-04 07:53:32 +00009308OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
9309 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00009310 SourceLocation LParenLoc,
9311 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00009312 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00009313 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00009314 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00009315 // The parameter of the collapse clause must be a constant
9316 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00009317 ExprResult NumForLoopsResult =
9318 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
9319 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00009320 return nullptr;
9321 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00009322 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00009323}
9324
Alexey Bataev10e775f2015-07-30 11:36:16 +00009325OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
9326 SourceLocation EndLoc,
9327 SourceLocation LParenLoc,
9328 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00009329 // OpenMP [2.7.1, loop construct, Description]
9330 // OpenMP [2.8.1, simd construct, Description]
9331 // OpenMP [2.9.6, distribute construct, Description]
9332 // The parameter of the ordered clause must be a constant
9333 // positive integer expression if any.
9334 if (NumForLoops && LParenLoc.isValid()) {
9335 ExprResult NumForLoopsResult =
9336 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
9337 if (NumForLoopsResult.isInvalid())
9338 return nullptr;
9339 NumForLoops = NumForLoopsResult.get();
Alexey Bataeve3727102018-04-18 15:57:46 +00009340 } else {
Alexey Bataev346265e2015-09-25 10:37:12 +00009341 NumForLoops = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +00009342 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00009343 auto *Clause = OMPOrderedClause::Create(
9344 Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0,
9345 StartLoc, LParenLoc, EndLoc);
9346 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause);
9347 return Clause;
Alexey Bataev10e775f2015-07-30 11:36:16 +00009348}
9349
Alexey Bataeved09d242014-05-28 05:53:51 +00009350OMPClause *Sema::ActOnOpenMPSimpleClause(
9351 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
9352 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009353 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009354 switch (Kind) {
9355 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00009356 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00009357 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
9358 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009359 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009360 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00009361 Res = ActOnOpenMPProcBindClause(
9362 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
9363 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009364 break;
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009365 case OMPC_atomic_default_mem_order:
9366 Res = ActOnOpenMPAtomicDefaultMemOrderClause(
9367 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument),
9368 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
9369 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009370 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00009371 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00009372 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00009373 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009374 case OMPC_simdlen:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00009375 case OMPC_allocator:
Alexander Musman8bd31e62014-05-27 15:12:19 +00009376 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009377 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009378 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009379 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00009380 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00009381 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00009382 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00009383 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00009384 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00009385 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009386 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009387 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009388 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009389 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00009390 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009391 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009392 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009393 case OMPC_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +00009394 case OMPC_allocate:
Alexey Bataev6125da92014-07-21 11:26:11 +00009395 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009396 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00009397 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00009398 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00009399 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009400 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009401 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00009402 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00009403 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009404 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00009405 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009406 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009407 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009408 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009409 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00009410 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00009411 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009412 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00009413 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009414 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009415 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009416 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00009417 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00009418 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00009419 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00009420 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00009421 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00009422 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009423 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009424 case OMPC_dynamic_allocators:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009425 llvm_unreachable("Clause is not allowed.");
9426 }
9427 return Res;
9428}
9429
Alexey Bataev6402bca2015-12-28 07:25:51 +00009430static std::string
9431getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
9432 ArrayRef<unsigned> Exclude = llvm::None) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009433 SmallString<256> Buffer;
9434 llvm::raw_svector_ostream Out(Buffer);
Alexey Bataev6402bca2015-12-28 07:25:51 +00009435 unsigned Bound = Last >= 2 ? Last - 2 : 0;
9436 unsigned Skipped = Exclude.size();
9437 auto S = Exclude.begin(), E = Exclude.end();
Alexey Bataeve3727102018-04-18 15:57:46 +00009438 for (unsigned I = First; I < Last; ++I) {
9439 if (std::find(S, E, I) != E) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00009440 --Skipped;
9441 continue;
9442 }
Alexey Bataeve3727102018-04-18 15:57:46 +00009443 Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'";
9444 if (I == Bound - Skipped)
9445 Out << " or ";
9446 else if (I != Bound + 1 - Skipped)
9447 Out << ", ";
Alexey Bataev6402bca2015-12-28 07:25:51 +00009448 }
Alexey Bataeve3727102018-04-18 15:57:46 +00009449 return Out.str();
Alexey Bataev6402bca2015-12-28 07:25:51 +00009450}
9451
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009452OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
9453 SourceLocation KindKwLoc,
9454 SourceLocation StartLoc,
9455 SourceLocation LParenLoc,
9456 SourceLocation EndLoc) {
9457 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00009458 static_assert(OMPC_DEFAULT_unknown > 0,
9459 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009460 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009461 << getListOfPossibleValues(OMPC_default, /*First=*/0,
9462 /*Last=*/OMPC_DEFAULT_unknown)
9463 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009464 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009465 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00009466 switch (Kind) {
9467 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009468 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009469 break;
9470 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009471 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009472 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009473 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009474 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00009475 break;
9476 }
Alexey Bataeved09d242014-05-28 05:53:51 +00009477 return new (Context)
9478 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009479}
9480
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009481OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
9482 SourceLocation KindKwLoc,
9483 SourceLocation StartLoc,
9484 SourceLocation LParenLoc,
9485 SourceLocation EndLoc) {
9486 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009487 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009488 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
9489 /*Last=*/OMPC_PROC_BIND_unknown)
9490 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009491 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009492 }
Alexey Bataeved09d242014-05-28 05:53:51 +00009493 return new (Context)
9494 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009495}
9496
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009497OMPClause *Sema::ActOnOpenMPAtomicDefaultMemOrderClause(
9498 OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc,
9499 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
9500 if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) {
9501 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
9502 << getListOfPossibleValues(
9503 OMPC_atomic_default_mem_order, /*First=*/0,
9504 /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown)
9505 << getOpenMPClauseName(OMPC_atomic_default_mem_order);
9506 return nullptr;
9507 }
9508 return new (Context) OMPAtomicDefaultMemOrderClause(Kind, KindKwLoc, StartLoc,
9509 LParenLoc, EndLoc);
9510}
9511
Alexey Bataev56dafe82014-06-20 07:16:17 +00009512OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00009513 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00009514 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00009515 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00009516 SourceLocation EndLoc) {
9517 OMPClause *Res = nullptr;
9518 switch (Kind) {
9519 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00009520 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
9521 assert(Argument.size() == NumberOfElements &&
9522 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009523 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00009524 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
9525 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
9526 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
9527 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
9528 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009529 break;
9530 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00009531 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
9532 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
9533 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
9534 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009535 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00009536 case OMPC_dist_schedule:
9537 Res = ActOnOpenMPDistScheduleClause(
9538 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
9539 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
9540 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009541 case OMPC_defaultmap:
9542 enum { Modifier, DefaultmapKind };
9543 Res = ActOnOpenMPDefaultmapClause(
9544 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
9545 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
David Majnemer9d168222016-08-05 17:44:54 +00009546 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind],
9547 EndLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009548 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00009549 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009550 case OMPC_num_threads:
9551 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009552 case OMPC_simdlen:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00009553 case OMPC_allocator:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009554 case OMPC_collapse:
9555 case OMPC_default:
9556 case OMPC_proc_bind:
9557 case OMPC_private:
9558 case OMPC_firstprivate:
9559 case OMPC_lastprivate:
9560 case OMPC_shared:
9561 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00009562 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00009563 case OMPC_in_reduction:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009564 case OMPC_linear:
9565 case OMPC_aligned:
9566 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009567 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009568 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00009569 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009570 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009571 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009572 case OMPC_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +00009573 case OMPC_allocate:
Alexey Bataev6125da92014-07-21 11:26:11 +00009574 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009575 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00009576 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00009577 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00009578 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009579 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009580 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00009581 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00009582 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009583 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00009584 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009585 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009586 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009587 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009588 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00009589 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00009590 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009591 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009592 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009593 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00009594 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00009595 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00009596 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00009597 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00009598 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00009599 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009600 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009601 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009602 case OMPC_atomic_default_mem_order:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009603 llvm_unreachable("Clause is not allowed.");
9604 }
9605 return Res;
9606}
9607
Alexey Bataev6402bca2015-12-28 07:25:51 +00009608static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
9609 OpenMPScheduleClauseModifier M2,
9610 SourceLocation M1Loc, SourceLocation M2Loc) {
9611 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
9612 SmallVector<unsigned, 2> Excluded;
9613 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
9614 Excluded.push_back(M2);
9615 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
9616 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
9617 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
9618 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
9619 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
9620 << getListOfPossibleValues(OMPC_schedule,
9621 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
9622 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
9623 Excluded)
9624 << getOpenMPClauseName(OMPC_schedule);
9625 return true;
9626 }
9627 return false;
9628}
9629
Alexey Bataev56dafe82014-06-20 07:16:17 +00009630OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00009631 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00009632 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00009633 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
9634 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
9635 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
9636 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
9637 return nullptr;
9638 // OpenMP, 2.7.1, Loop Construct, Restrictions
9639 // Either the monotonic modifier or the nonmonotonic modifier can be specified
9640 // but not both.
9641 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
9642 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
9643 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
9644 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
9645 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
9646 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
9647 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
9648 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
9649 return nullptr;
9650 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00009651 if (Kind == OMPC_SCHEDULE_unknown) {
9652 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00009653 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
9654 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
9655 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
9656 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
9657 Exclude);
9658 } else {
9659 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
9660 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009661 }
9662 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
9663 << Values << getOpenMPClauseName(OMPC_schedule);
9664 return nullptr;
9665 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00009666 // OpenMP, 2.7.1, Loop Construct, Restrictions
9667 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
9668 // schedule(guided).
9669 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
9670 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
9671 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
9672 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
9673 diag::err_omp_schedule_nonmonotonic_static);
9674 return nullptr;
9675 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00009676 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00009677 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00009678 if (ChunkSize) {
9679 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
9680 !ChunkSize->isInstantiationDependent() &&
9681 !ChunkSize->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00009682 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
Alexey Bataev56dafe82014-06-20 07:16:17 +00009683 ExprResult Val =
9684 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
9685 if (Val.isInvalid())
9686 return nullptr;
9687
9688 ValExpr = Val.get();
9689
9690 // OpenMP [2.7.1, Restrictions]
9691 // chunk_size must be a loop invariant integer expression with a positive
9692 // value.
9693 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00009694 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
9695 if (Result.isSigned() && !Result.isStrictlyPositive()) {
9696 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00009697 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00009698 return nullptr;
9699 }
Alexey Bataev2ba67042017-11-28 21:11:44 +00009700 } else if (getOpenMPCaptureRegionForClause(
9701 DSAStack->getCurrentDirective(), OMPC_schedule) !=
9702 OMPD_unknown &&
Alexey Bataevb46cdea2016-06-15 11:20:48 +00009703 !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00009704 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00009705 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev5a3af132016-03-29 08:58:54 +00009706 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
9707 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009708 }
9709 }
9710 }
9711
Alexey Bataev6402bca2015-12-28 07:25:51 +00009712 return new (Context)
9713 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00009714 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009715}
9716
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009717OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
9718 SourceLocation StartLoc,
9719 SourceLocation EndLoc) {
9720 OMPClause *Res = nullptr;
9721 switch (Kind) {
9722 case OMPC_ordered:
9723 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
9724 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00009725 case OMPC_nowait:
9726 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
9727 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009728 case OMPC_untied:
9729 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
9730 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009731 case OMPC_mergeable:
9732 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
9733 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009734 case OMPC_read:
9735 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
9736 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00009737 case OMPC_write:
9738 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
9739 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00009740 case OMPC_update:
9741 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
9742 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00009743 case OMPC_capture:
9744 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
9745 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009746 case OMPC_seq_cst:
9747 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
9748 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00009749 case OMPC_threads:
9750 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
9751 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009752 case OMPC_simd:
9753 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
9754 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00009755 case OMPC_nogroup:
9756 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
9757 break;
Kelvin Li1408f912018-09-26 04:28:39 +00009758 case OMPC_unified_address:
9759 Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc);
9760 break;
Patrick Lyster4a370b92018-10-01 13:47:43 +00009761 case OMPC_unified_shared_memory:
9762 Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
9763 break;
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009764 case OMPC_reverse_offload:
9765 Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc);
9766 break;
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009767 case OMPC_dynamic_allocators:
9768 Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc);
9769 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009770 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00009771 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009772 case OMPC_num_threads:
9773 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009774 case OMPC_simdlen:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00009775 case OMPC_allocator:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009776 case OMPC_collapse:
9777 case OMPC_schedule:
9778 case OMPC_private:
9779 case OMPC_firstprivate:
9780 case OMPC_lastprivate:
9781 case OMPC_shared:
9782 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00009783 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00009784 case OMPC_in_reduction:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009785 case OMPC_linear:
9786 case OMPC_aligned:
9787 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009788 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009789 case OMPC_default:
9790 case OMPC_proc_bind:
9791 case OMPC_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +00009792 case OMPC_allocate:
Alexey Bataev6125da92014-07-21 11:26:11 +00009793 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009794 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00009795 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00009796 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009797 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009798 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009799 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009800 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00009801 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009802 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00009803 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009804 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009805 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009806 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00009807 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00009808 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00009809 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00009810 case OMPC_is_device_ptr:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009811 case OMPC_atomic_default_mem_order:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009812 llvm_unreachable("Clause is not allowed.");
9813 }
9814 return Res;
9815}
9816
Alexey Bataev236070f2014-06-20 11:19:47 +00009817OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
9818 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00009819 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00009820 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
9821}
9822
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009823OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
9824 SourceLocation EndLoc) {
9825 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
9826}
9827
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009828OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
9829 SourceLocation EndLoc) {
9830 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
9831}
9832
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009833OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
9834 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009835 return new (Context) OMPReadClause(StartLoc, EndLoc);
9836}
9837
Alexey Bataevdea47612014-07-23 07:46:59 +00009838OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
9839 SourceLocation EndLoc) {
9840 return new (Context) OMPWriteClause(StartLoc, EndLoc);
9841}
9842
Alexey Bataev67a4f222014-07-23 10:25:33 +00009843OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
9844 SourceLocation EndLoc) {
9845 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
9846}
9847
Alexey Bataev459dec02014-07-24 06:46:57 +00009848OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
9849 SourceLocation EndLoc) {
9850 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
9851}
9852
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009853OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
9854 SourceLocation EndLoc) {
9855 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
9856}
9857
Alexey Bataev346265e2015-09-25 10:37:12 +00009858OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
9859 SourceLocation EndLoc) {
9860 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
9861}
9862
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009863OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
9864 SourceLocation EndLoc) {
9865 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
9866}
9867
Alexey Bataevb825de12015-12-07 10:51:44 +00009868OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
9869 SourceLocation EndLoc) {
9870 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
9871}
9872
Kelvin Li1408f912018-09-26 04:28:39 +00009873OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc,
9874 SourceLocation EndLoc) {
9875 return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc);
9876}
9877
Patrick Lyster4a370b92018-10-01 13:47:43 +00009878OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc,
9879 SourceLocation EndLoc) {
9880 return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
9881}
9882
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009883OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc,
9884 SourceLocation EndLoc) {
9885 return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc);
9886}
9887
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009888OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc,
9889 SourceLocation EndLoc) {
9890 return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc);
9891}
9892
Alexey Bataevc5e02582014-06-16 07:08:35 +00009893OMPClause *Sema::ActOnOpenMPVarListClause(
9894 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
Michael Kruse4304e9d2019-02-19 16:38:20 +00009895 const OMPVarListLocTy &Locs, SourceLocation ColonLoc,
9896 CXXScopeSpec &ReductionOrMapperIdScopeSpec,
9897 DeclarationNameInfo &ReductionOrMapperId, OpenMPDependClauseKind DepKind,
Kelvin Lief579432018-12-18 22:18:41 +00009898 OpenMPLinearClauseKind LinKind,
9899 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers,
Michael Kruse4304e9d2019-02-19 16:38:20 +00009900 ArrayRef<SourceLocation> MapTypeModifiersLoc, OpenMPMapClauseKind MapType,
9901 bool IsMapTypeImplicit, SourceLocation DepLinMapLoc) {
9902 SourceLocation StartLoc = Locs.StartLoc;
9903 SourceLocation LParenLoc = Locs.LParenLoc;
9904 SourceLocation EndLoc = Locs.EndLoc;
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009905 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009906 switch (Kind) {
9907 case OMPC_private:
9908 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9909 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009910 case OMPC_firstprivate:
9911 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9912 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00009913 case OMPC_lastprivate:
9914 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9915 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00009916 case OMPC_shared:
9917 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
9918 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009919 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00009920 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
Michael Kruse4304e9d2019-02-19 16:38:20 +00009921 EndLoc, ReductionOrMapperIdScopeSpec,
9922 ReductionOrMapperId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00009923 break;
Alexey Bataev169d96a2017-07-18 20:17:46 +00009924 case OMPC_task_reduction:
9925 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
Michael Kruse4304e9d2019-02-19 16:38:20 +00009926 EndLoc, ReductionOrMapperIdScopeSpec,
9927 ReductionOrMapperId);
Alexey Bataev169d96a2017-07-18 20:17:46 +00009928 break;
Alexey Bataevfa312f32017-07-21 18:48:21 +00009929 case OMPC_in_reduction:
Michael Kruse4304e9d2019-02-19 16:38:20 +00009930 Res = ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9931 EndLoc, ReductionOrMapperIdScopeSpec,
9932 ReductionOrMapperId);
Alexey Bataevfa312f32017-07-21 18:48:21 +00009933 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00009934 case OMPC_linear:
9935 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00009936 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00009937 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009938 case OMPC_aligned:
9939 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
9940 ColonLoc, EndLoc);
9941 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009942 case OMPC_copyin:
9943 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
9944 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009945 case OMPC_copyprivate:
9946 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9947 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00009948 case OMPC_flush:
9949 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
9950 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009951 case OMPC_depend:
David Majnemer9d168222016-08-05 17:44:54 +00009952 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
Kelvin Li0bff7af2015-11-23 05:32:03 +00009953 StartLoc, LParenLoc, EndLoc);
9954 break;
9955 case OMPC_map:
Michael Kruse4304e9d2019-02-19 16:38:20 +00009956 Res = ActOnOpenMPMapClause(MapTypeModifiers, MapTypeModifiersLoc,
9957 ReductionOrMapperIdScopeSpec,
9958 ReductionOrMapperId, MapType, IsMapTypeImplicit,
9959 DepLinMapLoc, ColonLoc, VarList, Locs);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009960 break;
Samuel Antao661c0902016-05-26 17:39:58 +00009961 case OMPC_to:
Michael Kruse01f670d2019-02-22 22:29:42 +00009962 Res = ActOnOpenMPToClause(VarList, ReductionOrMapperIdScopeSpec,
9963 ReductionOrMapperId, Locs);
Samuel Antao661c0902016-05-26 17:39:58 +00009964 break;
Samuel Antaoec172c62016-05-26 17:49:04 +00009965 case OMPC_from:
Michael Kruse0336c752019-02-25 20:34:15 +00009966 Res = ActOnOpenMPFromClause(VarList, ReductionOrMapperIdScopeSpec,
9967 ReductionOrMapperId, Locs);
Samuel Antaoec172c62016-05-26 17:49:04 +00009968 break;
Carlo Bertolli2404b172016-07-13 15:37:16 +00009969 case OMPC_use_device_ptr:
Michael Kruse4304e9d2019-02-19 16:38:20 +00009970 Res = ActOnOpenMPUseDevicePtrClause(VarList, Locs);
Carlo Bertolli2404b172016-07-13 15:37:16 +00009971 break;
Carlo Bertolli70594e92016-07-13 17:16:49 +00009972 case OMPC_is_device_ptr:
Michael Kruse4304e9d2019-02-19 16:38:20 +00009973 Res = ActOnOpenMPIsDevicePtrClause(VarList, Locs);
Carlo Bertolli70594e92016-07-13 17:16:49 +00009974 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009975 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00009976 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00009977 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00009978 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009979 case OMPC_simdlen:
Alexey Bataev9cc10fc2019-03-12 18:52:33 +00009980 case OMPC_allocator:
Alexander Musman8bd31e62014-05-27 15:12:19 +00009981 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009982 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009983 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009984 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009985 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00009986 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009987 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009988 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009989 case OMPC_threadprivate:
Alexey Bataev25ed0c02019-03-07 17:54:44 +00009990 case OMPC_allocate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009991 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00009992 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00009993 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00009994 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009995 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00009996 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00009997 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009998 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009999 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +000010000 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +000010001 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000010002 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +000010003 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +000010004 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +000010005 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +000010006 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000010007 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000010008 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +000010009 case OMPC_uniform:
Kelvin Li1408f912018-09-26 04:28:39 +000010010 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +000010011 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +000010012 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +000010013 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +000010014 case OMPC_atomic_default_mem_order:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000010015 llvm_unreachable("Clause is not allowed.");
10016 }
10017 return Res;
10018}
10019
Alexey Bataev90c228f2016-02-08 09:29:13 +000010020ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +000010021 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +000010022 ExprResult Res = BuildDeclRefExpr(
10023 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
10024 if (!Res.isUsable())
10025 return ExprError();
10026 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
10027 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
10028 if (!Res.isUsable())
10029 return ExprError();
10030 }
10031 if (VK != VK_LValue && Res.get()->isGLValue()) {
10032 Res = DefaultLvalueConversion(Res.get());
10033 if (!Res.isUsable())
10034 return ExprError();
10035 }
10036 return Res;
10037}
10038
Alexey Bataev60da77e2016-02-29 05:54:20 +000010039static std::pair<ValueDecl *, bool>
10040getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
10041 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +000010042 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
10043 RefExpr->containsUnexpandedParameterPack())
10044 return std::make_pair(nullptr, true);
10045
Alexey Bataevd985eda2016-02-10 11:29:16 +000010046 // OpenMP [3.1, C/C++]
10047 // A list item is a variable name.
10048 // OpenMP [2.9.3.3, Restrictions, p.1]
10049 // A variable that is part of another variable (as an array or
10050 // structure element) cannot appear in a private clause.
10051 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +000010052 enum {
10053 NoArrayExpr = -1,
10054 ArraySubscript = 0,
10055 OMPArraySection = 1
10056 } IsArrayExpr = NoArrayExpr;
10057 if (AllowArraySection) {
10058 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010059 Expr *Base = ASE->getBase()->IgnoreParenImpCasts();
Alexey Bataev60da77e2016-02-29 05:54:20 +000010060 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
10061 Base = TempASE->getBase()->IgnoreParenImpCasts();
10062 RefExpr = Base;
10063 IsArrayExpr = ArraySubscript;
10064 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010065 Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
Alexey Bataev60da77e2016-02-29 05:54:20 +000010066 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
10067 Base = TempOASE->getBase()->IgnoreParenImpCasts();
10068 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
10069 Base = TempASE->getBase()->IgnoreParenImpCasts();
10070 RefExpr = Base;
10071 IsArrayExpr = OMPArraySection;
10072 }
10073 }
10074 ELoc = RefExpr->getExprLoc();
10075 ERange = RefExpr->getSourceRange();
10076 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +000010077 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
10078 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
10079 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
10080 (S.getCurrentThisType().isNull() || !ME ||
10081 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
10082 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010083 if (IsArrayExpr != NoArrayExpr) {
Alexey Bataev60da77e2016-02-29 05:54:20 +000010084 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
10085 << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +000010086 } else {
Alexey Bataev60da77e2016-02-29 05:54:20 +000010087 S.Diag(ELoc,
10088 AllowArraySection
10089 ? diag::err_omp_expected_var_name_member_expr_or_array_item
10090 : diag::err_omp_expected_var_name_member_expr)
10091 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
10092 }
Alexey Bataevd985eda2016-02-10 11:29:16 +000010093 return std::make_pair(nullptr, false);
10094 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +000010095 return std::make_pair(
10096 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false);
Alexey Bataevd985eda2016-02-10 11:29:16 +000010097}
10098
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000010099OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
10100 SourceLocation StartLoc,
10101 SourceLocation LParenLoc,
10102 SourceLocation EndLoc) {
10103 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +000010104 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeve3727102018-04-18 15:57:46 +000010105 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000010106 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +000010107 SourceLocation ELoc;
10108 SourceRange ERange;
10109 Expr *SimpleRefExpr = RefExpr;
10110 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +000010111 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000010112 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000010113 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +000010114 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000010115 }
Alexey Bataevd985eda2016-02-10 11:29:16 +000010116 ValueDecl *D = Res.first;
10117 if (!D)
10118 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000010119
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000010120 QualType Type = D->getType();
10121 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000010122
10123 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
10124 // A variable that appears in a private clause must not have an incomplete
10125 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000010126 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000010127 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010128 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000010129
Joel E. Dennye6234d1422019-01-04 22:11:31 +000010130 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
10131 // A variable that is privatized must not have a const-qualified type
10132 // unless it is of class type with a mutable member. This restriction does
10133 // not apply to the firstprivate clause.
10134 //
10135 // OpenMP 3.1 [2.9.3.3, private clause, Restrictions]
10136 // A variable that appears in a private clause must not have a
10137 // const-qualified type unless it is of class type with a mutable member.
Joel E. Dennyd2649292019-01-04 22:11:56 +000010138 if (rejectConstNotMutableType(*this, D, Type, OMPC_private, ELoc))
Joel E. Dennye6234d1422019-01-04 22:11:31 +000010139 continue;
10140
Alexey Bataev758e55e2013-09-06 18:03:48 +000010141 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
10142 // in a Construct]
10143 // Variables with the predetermined data-sharing attributes may not be
10144 // listed in data-sharing attributes clauses, except for the cases
10145 // listed below. For these exceptions only, listing a predetermined
10146 // variable in a data-sharing attribute clause is allowed and overrides
10147 // the variable's predetermined data-sharing attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +000010148 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010149 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +000010150 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
10151 << getOpenMPClauseName(OMPC_private);
Alexey Bataeve3727102018-04-18 15:57:46 +000010152 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010153 continue;
10154 }
10155
Alexey Bataeve3727102018-04-18 15:57:46 +000010156 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010157 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000010158 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Kelvin Libf594a52016-12-17 05:48:59 +000010159 isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010160 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
10161 << getOpenMPClauseName(OMPC_private) << Type
Kelvin Libf594a52016-12-17 05:48:59 +000010162 << getOpenMPDirectiveName(CurrDir);
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010163 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000010164 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010165 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000010166 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010167 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000010168 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010169 continue;
10170 }
10171
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010172 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
10173 // A list item cannot appear in both a map clause and a data-sharing
10174 // attribute clause on the same construct
Alexey Bataeve3727102018-04-18 15:57:46 +000010175 if (isOpenMPTargetExecutionDirective(CurrDir)) {
Samuel Antao6890b092016-07-28 14:25:09 +000010176 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +000010177 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +000010178 VD, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +000010179 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
10180 OpenMPClauseKind WhereFoundClauseKind) -> bool {
10181 ConflictKind = WhereFoundClauseKind;
10182 return true;
10183 })) {
10184 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010185 << getOpenMPClauseName(OMPC_private)
Samuel Antao6890b092016-07-28 14:25:09 +000010186 << getOpenMPClauseName(ConflictKind)
Kelvin Libf594a52016-12-17 05:48:59 +000010187 << getOpenMPDirectiveName(CurrDir);
Alexey Bataeve3727102018-04-18 15:57:46 +000010188 reportOriginalDsa(*this, DSAStack, D, DVar);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010189 continue;
10190 }
10191 }
10192
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010193 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
10194 // A variable of class type (or array thereof) that appears in a private
10195 // clause requires an accessible, unambiguous default constructor for the
10196 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +000010197 // Generate helper private variable and initialize it with the default
10198 // value. The address of the original variable is replaced by the address of
10199 // the new private variable in CodeGen. This new variable is not added to
10200 // IdResolver, so the code in the OpenMP region uses original variable for
10201 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010202 Type = Type.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010203 VarDecl *VDPrivate =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000010204 buildVarDecl(*this, ELoc, Type, D->getName(),
10205 D->hasAttrs() ? &D->getAttrs() : nullptr,
10206 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Richard Smith3beb7c62017-01-12 02:27:38 +000010207 ActOnUninitializedDecl(VDPrivate);
Alexey Bataev03b340a2014-10-21 03:16:40 +000010208 if (VDPrivate->isInvalidDecl())
10209 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +000010210 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000010211 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +000010212
Alexey Bataev90c228f2016-02-08 09:29:13 +000010213 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010214 if (!VD && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +000010215 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +000010216 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010217 Vars.push_back((VD || CurContext->isDependentContext())
10218 ? RefExpr->IgnoreParens()
10219 : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +000010220 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000010221 }
10222
Alexey Bataeved09d242014-05-28 05:53:51 +000010223 if (Vars.empty())
10224 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000010225
Alexey Bataev03b340a2014-10-21 03:16:40 +000010226 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
10227 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000010228}
10229
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010230namespace {
10231class DiagsUninitializedSeveretyRAII {
10232private:
10233 DiagnosticsEngine &Diags;
10234 SourceLocation SavedLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +000010235 bool IsIgnored = false;
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010236
10237public:
10238 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
10239 bool IsIgnored)
10240 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
10241 if (!IsIgnored) {
10242 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
10243 /*Map*/ diag::Severity::Ignored, Loc);
10244 }
10245 }
10246 ~DiagsUninitializedSeveretyRAII() {
10247 if (!IsIgnored)
10248 Diags.popMappings(SavedLoc);
10249 }
10250};
Alexander Kornienkoab9db512015-06-22 23:07:51 +000010251}
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010252
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010253OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
10254 SourceLocation StartLoc,
10255 SourceLocation LParenLoc,
10256 SourceLocation EndLoc) {
10257 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010258 SmallVector<Expr *, 8> PrivateCopies;
10259 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +000010260 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010261 bool IsImplicitClause =
10262 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
Alexey Bataeve3727102018-04-18 15:57:46 +000010263 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010264
Alexey Bataeve3727102018-04-18 15:57:46 +000010265 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000010266 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +000010267 SourceLocation ELoc;
10268 SourceRange ERange;
10269 Expr *SimpleRefExpr = RefExpr;
10270 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +000010271 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010272 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000010273 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010274 PrivateCopies.push_back(nullptr);
10275 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010276 }
Alexey Bataevd985eda2016-02-10 11:29:16 +000010277 ValueDecl *D = Res.first;
10278 if (!D)
10279 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010280
Alexey Bataev60da77e2016-02-29 05:54:20 +000010281 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +000010282 QualType Type = D->getType();
10283 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010284
10285 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
10286 // A variable that appears in a private clause must not have an incomplete
10287 // type or a reference type.
10288 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +000010289 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010290 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010291 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010292
10293 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
10294 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +000010295 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010296 // class type.
Alexey Bataeve3727102018-04-18 15:57:46 +000010297 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010298
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010299 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +000010300 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010301 if (!IsImplicitClause) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010302 DSAStackTy::DSAVarData DVar =
10303 DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +000010304 TopDVar = DVar;
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010305 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010306 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010307 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
10308 // A list item that specifies a given variable may not appear in more
10309 // than one clause on the same directive, except that a variable may be
10310 // specified in both firstprivate and lastprivate clauses.
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010311 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
10312 // A list item may appear in a firstprivate or lastprivate clause but not
10313 // both.
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010314 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevb358f992017-12-01 17:40:15 +000010315 (isOpenMPDistributeDirective(CurrDir) ||
10316 DVar.CKind != OMPC_lastprivate) &&
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010317 DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010318 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +000010319 << getOpenMPClauseName(DVar.CKind)
10320 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000010321 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010322 continue;
10323 }
10324
10325 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
10326 // in a Construct]
10327 // Variables with the predetermined data-sharing attributes may not be
10328 // listed in data-sharing attributes clauses, except for the cases
10329 // listed below. For these exceptions only, listing a predetermined
10330 // variable in a data-sharing attribute clause is allowed and overrides
10331 // the variable's predetermined data-sharing attributes.
10332 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
10333 // in a Construct, C/C++, p.2]
10334 // Variables with const-qualified type having no mutable member may be
10335 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +000010336 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010337 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
10338 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +000010339 << getOpenMPClauseName(DVar.CKind)
10340 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000010341 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010342 continue;
10343 }
10344
10345 // OpenMP [2.9.3.4, Restrictions, p.2]
10346 // A list item that is private within a parallel region must not appear
10347 // in a firstprivate clause on a worksharing construct if any of the
10348 // worksharing regions arising from the worksharing construct ever bind
10349 // to any of the parallel regions arising from the parallel construct.
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010350 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
10351 // A list item that is private within a teams region must not appear in a
10352 // firstprivate clause on a distribute construct if any of the distribute
10353 // regions arising from the distribute construct ever bind to any of the
10354 // teams regions arising from the teams construct.
10355 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
10356 // A list item that appears in a reduction clause of a teams construct
10357 // must not appear in a firstprivate clause on a distribute construct if
10358 // any of the distribute regions arising from the distribute construct
10359 // ever bind to any of the teams regions arising from the teams construct.
10360 if ((isOpenMPWorksharingDirective(CurrDir) ||
10361 isOpenMPDistributeDirective(CurrDir)) &&
Kelvin Li579e41c2016-11-30 23:51:03 +000010362 !isOpenMPParallelDirective(CurrDir) &&
10363 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +000010364 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010365 if (DVar.CKind != OMPC_shared &&
10366 (isOpenMPParallelDirective(DVar.DKind) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010367 isOpenMPTeamsDirective(DVar.DKind) ||
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010368 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +000010369 Diag(ELoc, diag::err_omp_required_access)
10370 << getOpenMPClauseName(OMPC_firstprivate)
10371 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +000010372 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +000010373 continue;
10374 }
10375 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010376 // OpenMP [2.9.3.4, Restrictions, p.3]
10377 // A list item that appears in a reduction clause of a parallel construct
10378 // must not appear in a firstprivate clause on a worksharing or task
10379 // construct if any of the worksharing or task regions arising from the
10380 // worksharing or task construct ever bind to any of the parallel regions
10381 // arising from the parallel construct.
10382 // OpenMP [2.9.3.4, Restrictions, p.4]
10383 // A list item that appears in a reduction clause in worksharing
10384 // construct must not appear in a firstprivate clause in a task construct
10385 // encountered during execution of any of the worksharing regions arising
10386 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +000010387 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010388 DVar = DSAStack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +000010389 D, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
10390 [](OpenMPDirectiveKind K) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010391 return isOpenMPParallelDirective(K) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010392 isOpenMPWorksharingDirective(K) ||
10393 isOpenMPTeamsDirective(K);
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010394 },
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010395 /*FromParent=*/true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010396 if (DVar.CKind == OMPC_reduction &&
10397 (isOpenMPParallelDirective(DVar.DKind) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010398 isOpenMPWorksharingDirective(DVar.DKind) ||
10399 isOpenMPTeamsDirective(DVar.DKind))) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010400 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
10401 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataeve3727102018-04-18 15:57:46 +000010402 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010403 continue;
10404 }
10405 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +000010406
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010407 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
10408 // A list item cannot appear in both a map clause and a data-sharing
10409 // attribute clause on the same construct
Alexey Bataevb358f992017-12-01 17:40:15 +000010410 if (isOpenMPTargetExecutionDirective(CurrDir)) {
Samuel Antao6890b092016-07-28 14:25:09 +000010411 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +000010412 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +000010413 VD, /*CurrentRegionOnly=*/true,
Alexey Bataeve3727102018-04-18 15:57:46 +000010414 [&ConflictKind](
10415 OMPClauseMappableExprCommon::MappableExprComponentListRef,
10416 OpenMPClauseKind WhereFoundClauseKind) {
Samuel Antao6890b092016-07-28 14:25:09 +000010417 ConflictKind = WhereFoundClauseKind;
10418 return true;
10419 })) {
10420 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010421 << getOpenMPClauseName(OMPC_firstprivate)
Samuel Antao6890b092016-07-28 14:25:09 +000010422 << getOpenMPClauseName(ConflictKind)
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010423 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000010424 reportOriginalDsa(*this, DSAStack, D, DVar);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010425 continue;
10426 }
10427 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010428 }
10429
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010430 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000010431 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +000010432 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010433 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
10434 << getOpenMPClauseName(OMPC_firstprivate) << Type
10435 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
10436 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +000010437 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010438 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +000010439 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010440 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +000010441 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010442 continue;
10443 }
10444
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010445 Type = Type.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010446 VarDecl *VDPrivate =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000010447 buildVarDecl(*this, ELoc, Type, D->getName(),
10448 D->hasAttrs() ? &D->getAttrs() : nullptr,
10449 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010450 // Generate helper private variable and initialize it with the value of the
10451 // original variable. The address of the original variable is replaced by
10452 // the address of the new private variable in the CodeGen. This new variable
10453 // is not added to IdResolver, so the code in the OpenMP region uses
10454 // original variable for proper diagnostics and variable capturing.
10455 Expr *VDInitRefExpr = nullptr;
10456 // For arrays generate initializer for single element and replace it by the
10457 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010458 if (Type->isArrayType()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010459 VarDecl *VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +000010460 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010461 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataeve3727102018-04-18 15:57:46 +000010462 Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010463 ElemType = ElemType.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010464 VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
10465 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +000010466 InitializedEntity Entity =
10467 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010468 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
10469
10470 InitializationSequence InitSeq(*this, Entity, Kind, Init);
10471 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
10472 if (Result.isInvalid())
10473 VDPrivate->setInvalidDecl();
10474 else
10475 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010476 // Remove temp variable declaration.
10477 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010478 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +000010479 VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
10480 ".firstprivate.temp");
Alexey Bataevd985eda2016-02-10 11:29:16 +000010481 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
10482 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +000010483 AddInitializerToDecl(VDPrivate,
10484 DefaultLvalueConversion(VDInitRefExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000010485 /*DirectInit=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010486 }
10487 if (VDPrivate->isInvalidDecl()) {
10488 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +000010489 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010490 diag::note_omp_task_predetermined_firstprivate_here);
10491 }
10492 continue;
10493 }
10494 CurContext->addDecl(VDPrivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000010495 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +000010496 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
10497 RefExpr->getExprLoc());
10498 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010499 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010500 if (TopDVar.CKind == OMPC_lastprivate) {
Alexey Bataev005248a2016-02-25 05:25:57 +000010501 Ref = TopDVar.PrivateCopy;
Alexey Bataeve3727102018-04-18 15:57:46 +000010502 } else {
Alexey Bataev61205072016-03-02 04:57:40 +000010503 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataeve3727102018-04-18 15:57:46 +000010504 if (!isOpenMPCapturedDecl(D))
Alexey Bataev005248a2016-02-25 05:25:57 +000010505 ExprCaptures.push_back(Ref->getDecl());
10506 }
Alexey Bataev417089f2016-02-17 13:19:37 +000010507 }
Alexey Bataevd985eda2016-02-10 11:29:16 +000010508 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010509 Vars.push_back((VD || CurContext->isDependentContext())
10510 ? RefExpr->IgnoreParens()
10511 : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010512 PrivateCopies.push_back(VDPrivateRefExpr);
10513 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010514 }
10515
Alexey Bataeved09d242014-05-28 05:53:51 +000010516 if (Vars.empty())
10517 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010518
10519 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +000010520 Vars, PrivateCopies, Inits,
10521 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010522}
10523
Alexander Musman1bb328c2014-06-04 13:06:39 +000010524OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
10525 SourceLocation StartLoc,
10526 SourceLocation LParenLoc,
10527 SourceLocation EndLoc) {
10528 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +000010529 SmallVector<Expr *, 8> SrcExprs;
10530 SmallVector<Expr *, 8> DstExprs;
10531 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +000010532 SmallVector<Decl *, 4> ExprCaptures;
10533 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeve3727102018-04-18 15:57:46 +000010534 for (Expr *RefExpr : VarList) {
Alexander Musman1bb328c2014-06-04 13:06:39 +000010535 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +000010536 SourceLocation ELoc;
10537 SourceRange ERange;
10538 Expr *SimpleRefExpr = RefExpr;
10539 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +000010540 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +000010541 // It will be analyzed later.
10542 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +000010543 SrcExprs.push_back(nullptr);
10544 DstExprs.push_back(nullptr);
10545 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010546 }
Alexey Bataev74caaf22016-02-20 04:09:36 +000010547 ValueDecl *D = Res.first;
10548 if (!D)
10549 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +000010550
Alexey Bataev74caaf22016-02-20 04:09:36 +000010551 QualType Type = D->getType();
10552 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010553
10554 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
10555 // A variable that appears in a lastprivate clause must not have an
10556 // incomplete type or a reference type.
10557 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +000010558 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +000010559 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010560 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +000010561
Joel E. Dennye6234d1422019-01-04 22:11:31 +000010562 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
10563 // A variable that is privatized must not have a const-qualified type
10564 // unless it is of class type with a mutable member. This restriction does
10565 // not apply to the firstprivate clause.
10566 //
10567 // OpenMP 3.1 [2.9.3.5, lastprivate clause, Restrictions]
10568 // A variable that appears in a lastprivate clause must not have a
10569 // const-qualified type unless it is of class type with a mutable member.
Joel E. Dennyd2649292019-01-04 22:11:56 +000010570 if (rejectConstNotMutableType(*this, D, Type, OMPC_lastprivate, ELoc))
Joel E. Dennye6234d1422019-01-04 22:11:31 +000010571 continue;
10572
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010573 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexander Musman1bb328c2014-06-04 13:06:39 +000010574 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
10575 // in a Construct]
10576 // Variables with the predetermined data-sharing attributes may not be
10577 // listed in data-sharing attributes clauses, except for the cases
10578 // listed below.
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010579 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
10580 // A list item may appear in a firstprivate or lastprivate clause but not
10581 // both.
Alexey Bataeve3727102018-04-18 15:57:46 +000010582 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010583 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
Alexey Bataevb358f992017-12-01 17:40:15 +000010584 (isOpenMPDistributeDirective(CurrDir) ||
10585 DVar.CKind != OMPC_firstprivate) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +000010586 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
10587 Diag(ELoc, diag::err_omp_wrong_dsa)
10588 << getOpenMPClauseName(DVar.CKind)
10589 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000010590 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010591 continue;
10592 }
10593
Alexey Bataevf29276e2014-06-18 04:14:57 +000010594 // OpenMP [2.14.3.5, Restrictions, p.2]
10595 // A list item that is private within a parallel region, or that appears in
10596 // the reduction clause of a parallel construct, must not appear in a
10597 // lastprivate clause on a worksharing construct if any of the corresponding
10598 // worksharing regions ever binds to any of the corresponding parallel
10599 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +000010600 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +000010601 if (isOpenMPWorksharingDirective(CurrDir) &&
Kelvin Li579e41c2016-11-30 23:51:03 +000010602 !isOpenMPParallelDirective(CurrDir) &&
10603 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +000010604 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +000010605 if (DVar.CKind != OMPC_shared) {
10606 Diag(ELoc, diag::err_omp_required_access)
10607 << getOpenMPClauseName(OMPC_lastprivate)
10608 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +000010609 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +000010610 continue;
10611 }
10612 }
Alexey Bataev74caaf22016-02-20 04:09:36 +000010613
Alexander Musman1bb328c2014-06-04 13:06:39 +000010614 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +000010615 // A variable of class type (or array thereof) that appears in a
10616 // lastprivate clause requires an accessible, unambiguous default
10617 // constructor for the class type, unless the list item is also specified
10618 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +000010619 // A variable of class type (or array thereof) that appears in a
10620 // lastprivate clause requires an accessible, unambiguous copy assignment
10621 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +000010622 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010623 VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(),
10624 Type.getUnqualifiedType(), ".lastprivate.src",
10625 D->hasAttrs() ? &D->getAttrs() : nullptr);
10626 DeclRefExpr *PseudoSrcExpr =
Alexey Bataev74caaf22016-02-20 04:09:36 +000010627 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataeve3727102018-04-18 15:57:46 +000010628 VarDecl *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +000010629 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +000010630 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000010631 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +000010632 // For arrays generate assignment operation for single element and replace
10633 // it by the original array element in CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +000010634 ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
10635 PseudoDstExpr, PseudoSrcExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +000010636 if (AssignmentOp.isInvalid())
10637 continue;
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000010638 AssignmentOp =
10639 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataev38e89532015-04-16 04:54:05 +000010640 if (AssignmentOp.isInvalid())
10641 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +000010642
Alexey Bataev74caaf22016-02-20 04:09:36 +000010643 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010644 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010645 if (TopDVar.CKind == OMPC_firstprivate) {
Alexey Bataev005248a2016-02-25 05:25:57 +000010646 Ref = TopDVar.PrivateCopy;
Alexey Bataeve3727102018-04-18 15:57:46 +000010647 } else {
Alexey Bataev61205072016-03-02 04:57:40 +000010648 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000010649 if (!isOpenMPCapturedDecl(D))
Alexey Bataev005248a2016-02-25 05:25:57 +000010650 ExprCaptures.push_back(Ref->getDecl());
10651 }
10652 if (TopDVar.CKind == OMPC_firstprivate ||
Alexey Bataeve3727102018-04-18 15:57:46 +000010653 (!isOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010654 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +000010655 ExprResult RefRes = DefaultLvalueConversion(Ref);
10656 if (!RefRes.isUsable())
10657 continue;
10658 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +000010659 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
10660 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +000010661 if (!PostUpdateRes.isUsable())
10662 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +000010663 ExprPostUpdates.push_back(
10664 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +000010665 }
10666 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010667 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010668 Vars.push_back((VD || CurContext->isDependentContext())
10669 ? RefExpr->IgnoreParens()
10670 : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +000010671 SrcExprs.push_back(PseudoSrcExpr);
10672 DstExprs.push_back(PseudoDstExpr);
10673 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +000010674 }
10675
10676 if (Vars.empty())
10677 return nullptr;
10678
10679 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +000010680 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +000010681 buildPreInits(Context, ExprCaptures),
10682 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +000010683}
10684
Alexey Bataev758e55e2013-09-06 18:03:48 +000010685OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
10686 SourceLocation StartLoc,
10687 SourceLocation LParenLoc,
10688 SourceLocation EndLoc) {
10689 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +000010690 for (Expr *RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010691 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +000010692 SourceLocation ELoc;
10693 SourceRange ERange;
10694 Expr *SimpleRefExpr = RefExpr;
10695 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010696 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +000010697 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000010698 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010699 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010700 ValueDecl *D = Res.first;
10701 if (!D)
10702 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +000010703
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010704 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010705 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
10706 // in a Construct]
10707 // Variables with the predetermined data-sharing attributes may not be
10708 // listed in data-sharing attributes clauses, except for the cases
10709 // listed below. For these exceptions only, listing a predetermined
10710 // variable in a data-sharing attribute clause is allowed and overrides
10711 // the variable's predetermined data-sharing attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +000010712 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataeved09d242014-05-28 05:53:51 +000010713 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
10714 DVar.RefExpr) {
10715 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
10716 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +000010717 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010718 continue;
10719 }
10720
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010721 DeclRefExpr *Ref = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000010722 if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +000010723 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010724 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010725 Vars.push_back((VD || !Ref || CurContext->isDependentContext())
10726 ? RefExpr->IgnoreParens()
10727 : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010728 }
10729
Alexey Bataeved09d242014-05-28 05:53:51 +000010730 if (Vars.empty())
10731 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +000010732
10733 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
10734}
10735
Alexey Bataevc5e02582014-06-16 07:08:35 +000010736namespace {
10737class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
10738 DSAStackTy *Stack;
10739
10740public:
10741 bool VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010742 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
10743 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataevc5e02582014-06-16 07:08:35 +000010744 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
10745 return false;
10746 if (DVar.CKind != OMPC_unknown)
10747 return true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010748 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +000010749 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) { return true; },
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010750 /*FromParent=*/true);
Alexey Bataeve3727102018-04-18 15:57:46 +000010751 return DVarPrivate.CKind != OMPC_unknown;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010752 }
10753 return false;
10754 }
10755 bool VisitStmt(Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010756 for (Stmt *Child : S->children()) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010757 if (Child && Visit(Child))
10758 return true;
10759 }
10760 return false;
10761 }
Alexey Bataev23b69422014-06-18 07:08:49 +000010762 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +000010763};
Alexey Bataev23b69422014-06-18 07:08:49 +000010764} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +000010765
Alexey Bataev60da77e2016-02-29 05:54:20 +000010766namespace {
10767// Transform MemberExpression for specified FieldDecl of current class to
10768// DeclRefExpr to specified OMPCapturedExprDecl.
10769class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
10770 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
Alexey Bataeve3727102018-04-18 15:57:46 +000010771 ValueDecl *Field = nullptr;
10772 DeclRefExpr *CapturedExpr = nullptr;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010773
10774public:
10775 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
10776 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
10777
10778 ExprResult TransformMemberExpr(MemberExpr *E) {
10779 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
10780 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +000010781 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +000010782 return CapturedExpr;
10783 }
10784 return BaseTransform::TransformMemberExpr(E);
10785 }
10786 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
10787};
10788} // namespace
10789
Alexey Bataev97d18bf2018-04-11 19:21:00 +000010790template <typename T, typename U>
Michael Kruse4304e9d2019-02-19 16:38:20 +000010791static T filterLookupForUDReductionAndMapper(
10792 SmallVectorImpl<U> &Lookups, const llvm::function_ref<T(ValueDecl *)> Gen) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010793 for (U &Set : Lookups) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010794 for (auto *D : Set) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010795 if (T Res = Gen(cast<ValueDecl>(D)))
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010796 return Res;
10797 }
10798 }
10799 return T();
10800}
10801
Alexey Bataev43b90b72018-09-12 16:31:59 +000010802static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) {
10803 assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case");
10804
10805 for (auto RD : D->redecls()) {
10806 // Don't bother with extra checks if we already know this one isn't visible.
10807 if (RD == D)
10808 continue;
10809
10810 auto ND = cast<NamedDecl>(RD);
10811 if (LookupResult::isVisible(SemaRef, ND))
10812 return ND;
10813 }
10814
10815 return nullptr;
10816}
10817
10818static void
Michael Kruse4304e9d2019-02-19 16:38:20 +000010819argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &Id,
Alexey Bataev43b90b72018-09-12 16:31:59 +000010820 SourceLocation Loc, QualType Ty,
10821 SmallVectorImpl<UnresolvedSet<8>> &Lookups) {
10822 // Find all of the associated namespaces and classes based on the
10823 // arguments we have.
10824 Sema::AssociatedNamespaceSet AssociatedNamespaces;
10825 Sema::AssociatedClassSet AssociatedClasses;
10826 OpaqueValueExpr OVE(Loc, Ty, VK_LValue);
10827 SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces,
10828 AssociatedClasses);
10829
10830 // C++ [basic.lookup.argdep]p3:
10831 // Let X be the lookup set produced by unqualified lookup (3.4.1)
10832 // and let Y be the lookup set produced by argument dependent
10833 // lookup (defined as follows). If X contains [...] then Y is
10834 // empty. Otherwise Y is the set of declarations found in the
10835 // namespaces associated with the argument types as described
10836 // below. The set of declarations found by the lookup of the name
10837 // is the union of X and Y.
10838 //
10839 // Here, we compute Y and add its members to the overloaded
10840 // candidate set.
10841 for (auto *NS : AssociatedNamespaces) {
10842 // When considering an associated namespace, the lookup is the
10843 // same as the lookup performed when the associated namespace is
10844 // used as a qualifier (3.4.3.2) except that:
10845 //
10846 // -- Any using-directives in the associated namespace are
10847 // ignored.
10848 //
10849 // -- Any namespace-scope friend functions declared in
10850 // associated classes are visible within their respective
10851 // namespaces even if they are not visible during an ordinary
10852 // lookup (11.4).
Michael Kruse4304e9d2019-02-19 16:38:20 +000010853 DeclContext::lookup_result R = NS->lookup(Id.getName());
Alexey Bataev43b90b72018-09-12 16:31:59 +000010854 for (auto *D : R) {
10855 auto *Underlying = D;
10856 if (auto *USD = dyn_cast<UsingShadowDecl>(D))
10857 Underlying = USD->getTargetDecl();
10858
Michael Kruse4304e9d2019-02-19 16:38:20 +000010859 if (!isa<OMPDeclareReductionDecl>(Underlying) &&
10860 !isa<OMPDeclareMapperDecl>(Underlying))
Alexey Bataev43b90b72018-09-12 16:31:59 +000010861 continue;
10862
10863 if (!SemaRef.isVisible(D)) {
10864 D = findAcceptableDecl(SemaRef, D);
10865 if (!D)
10866 continue;
10867 if (auto *USD = dyn_cast<UsingShadowDecl>(D))
10868 Underlying = USD->getTargetDecl();
10869 }
10870 Lookups.emplace_back();
10871 Lookups.back().addDecl(Underlying);
10872 }
10873 }
10874}
10875
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010876static ExprResult
10877buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
10878 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
10879 const DeclarationNameInfo &ReductionId, QualType Ty,
10880 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
10881 if (ReductionIdScopeSpec.isInvalid())
10882 return ExprError();
10883 SmallVector<UnresolvedSet<8>, 4> Lookups;
10884 if (S) {
10885 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
10886 Lookup.suppressDiagnostics();
10887 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010888 NamedDecl *D = Lookup.getRepresentativeDecl();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010889 do {
10890 S = S->getParent();
10891 } while (S && !S->isDeclScope(D));
10892 if (S)
10893 S = S->getParent();
Alexey Bataev43b90b72018-09-12 16:31:59 +000010894 Lookups.emplace_back();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010895 Lookups.back().append(Lookup.begin(), Lookup.end());
10896 Lookup.clear();
10897 }
10898 } else if (auto *ULE =
10899 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
10900 Lookups.push_back(UnresolvedSet<8>());
10901 Decl *PrevD = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000010902 for (NamedDecl *D : ULE->decls()) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010903 if (D == PrevD)
10904 Lookups.push_back(UnresolvedSet<8>());
10905 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
10906 Lookups.back().addDecl(DRD);
10907 PrevD = D;
10908 }
10909 }
Alexey Bataevfdc20352017-08-25 15:43:55 +000010910 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() ||
10911 Ty->isInstantiationDependentType() ||
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010912 Ty->containsUnexpandedParameterPack() ||
Michael Kruse4304e9d2019-02-19 16:38:20 +000010913 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010914 return !D->isInvalidDecl() &&
10915 (D->getType()->isDependentType() ||
10916 D->getType()->isInstantiationDependentType() ||
10917 D->getType()->containsUnexpandedParameterPack());
10918 })) {
10919 UnresolvedSet<8> ResSet;
Alexey Bataeve3727102018-04-18 15:57:46 +000010920 for (const UnresolvedSet<8> &Set : Lookups) {
Alexey Bataev43b90b72018-09-12 16:31:59 +000010921 if (Set.empty())
10922 continue;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010923 ResSet.append(Set.begin(), Set.end());
10924 // The last item marks the end of all declarations at the specified scope.
10925 ResSet.addDecl(Set[Set.size() - 1]);
10926 }
10927 return UnresolvedLookupExpr::Create(
10928 SemaRef.Context, /*NamingClass=*/nullptr,
10929 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
10930 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
10931 }
Alexey Bataev43b90b72018-09-12 16:31:59 +000010932 // Lookup inside the classes.
10933 // C++ [over.match.oper]p3:
10934 // For a unary operator @ with an operand of a type whose
10935 // cv-unqualified version is T1, and for a binary operator @ with
10936 // a left operand of a type whose cv-unqualified version is T1 and
10937 // a right operand of a type whose cv-unqualified version is T2,
10938 // three sets of candidate functions, designated member
10939 // candidates, non-member candidates and built-in candidates, are
10940 // constructed as follows:
10941 // -- If T1 is a complete class type or a class currently being
10942 // defined, the set of member candidates is the result of the
10943 // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise,
10944 // the set of member candidates is empty.
10945 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
10946 Lookup.suppressDiagnostics();
10947 if (const auto *TyRec = Ty->getAs<RecordType>()) {
10948 // Complete the type if it can be completed.
10949 // If the type is neither complete nor being defined, bail out now.
10950 if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() ||
10951 TyRec->getDecl()->getDefinition()) {
10952 Lookup.clear();
10953 SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl());
10954 if (Lookup.empty()) {
10955 Lookups.emplace_back();
10956 Lookups.back().append(Lookup.begin(), Lookup.end());
10957 }
10958 }
10959 }
10960 // Perform ADL.
10961 argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups);
Michael Kruse4304e9d2019-02-19 16:38:20 +000010962 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010963 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
10964 if (!D->isInvalidDecl() &&
10965 SemaRef.Context.hasSameType(D->getType(), Ty))
10966 return D;
10967 return nullptr;
10968 }))
James Y Knightb92d2902019-02-05 16:05:50 +000010969 return SemaRef.BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(),
10970 VK_LValue, Loc);
Michael Kruse4304e9d2019-02-19 16:38:20 +000010971 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010972 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(Loc, VD->getType(), Ty, Paths.front(),
10985 /*DiagID=*/0) !=
10986 Sema::AR_inaccessible) {
10987 SemaRef.BuildBasePathArray(Paths, BasePath);
James Y Knightb92d2902019-02-05 16:05:50 +000010988 return SemaRef.BuildDeclRefExpr(
10989 VD, VD->getType().getNonReferenceType(), VK_LValue, Loc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010990 }
10991 }
10992 }
10993 }
10994 if (ReductionIdScopeSpec.isSet()) {
10995 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
10996 return ExprError();
10997 }
10998 return ExprEmpty();
10999}
11000
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011001namespace {
11002/// Data for the reduction-based clauses.
11003struct ReductionData {
11004 /// List of original reduction items.
11005 SmallVector<Expr *, 8> Vars;
11006 /// List of private copies of the reduction items.
11007 SmallVector<Expr *, 8> Privates;
11008 /// LHS expressions for the reduction_op expressions.
11009 SmallVector<Expr *, 8> LHSs;
11010 /// RHS expressions for the reduction_op expressions.
11011 SmallVector<Expr *, 8> RHSs;
11012 /// Reduction operation expression.
11013 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev88202be2017-07-27 13:20:36 +000011014 /// Taskgroup descriptors for the corresponding reduction items in
11015 /// in_reduction clauses.
11016 SmallVector<Expr *, 8> TaskgroupDescriptors;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011017 /// List of captures for clause.
11018 SmallVector<Decl *, 4> ExprCaptures;
11019 /// List of postupdate expressions.
11020 SmallVector<Expr *, 4> ExprPostUpdates;
11021 ReductionData() = delete;
11022 /// Reserves required memory for the reduction data.
11023 ReductionData(unsigned Size) {
11024 Vars.reserve(Size);
11025 Privates.reserve(Size);
11026 LHSs.reserve(Size);
11027 RHSs.reserve(Size);
11028 ReductionOps.reserve(Size);
Alexey Bataev88202be2017-07-27 13:20:36 +000011029 TaskgroupDescriptors.reserve(Size);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011030 ExprCaptures.reserve(Size);
11031 ExprPostUpdates.reserve(Size);
11032 }
11033 /// Stores reduction item and reduction operation only (required for dependent
11034 /// reduction item).
11035 void push(Expr *Item, Expr *ReductionOp) {
11036 Vars.emplace_back(Item);
11037 Privates.emplace_back(nullptr);
11038 LHSs.emplace_back(nullptr);
11039 RHSs.emplace_back(nullptr);
11040 ReductionOps.emplace_back(ReductionOp);
Alexey Bataev88202be2017-07-27 13:20:36 +000011041 TaskgroupDescriptors.emplace_back(nullptr);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011042 }
11043 /// Stores reduction data.
Alexey Bataev88202be2017-07-27 13:20:36 +000011044 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp,
11045 Expr *TaskgroupDescriptor) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011046 Vars.emplace_back(Item);
11047 Privates.emplace_back(Private);
11048 LHSs.emplace_back(LHS);
11049 RHSs.emplace_back(RHS);
11050 ReductionOps.emplace_back(ReductionOp);
Alexey Bataev88202be2017-07-27 13:20:36 +000011051 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011052 }
11053};
11054} // namespace
11055
Alexey Bataeve3727102018-04-18 15:57:46 +000011056static bool checkOMPArraySectionConstantForReduction(
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000011057 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement,
11058 SmallVectorImpl<llvm::APSInt> &ArraySizes) {
11059 const Expr *Length = OASE->getLength();
11060 if (Length == nullptr) {
11061 // For array sections of the form [1:] or [:], we would need to analyze
11062 // the lower bound...
11063 if (OASE->getColonLoc().isValid())
11064 return false;
11065
11066 // This is an array subscript which has implicit length 1!
11067 SingleElement = true;
11068 ArraySizes.push_back(llvm::APSInt::get(1));
11069 } else {
Fangrui Song407659a2018-11-30 23:41:18 +000011070 Expr::EvalResult Result;
11071 if (!Length->EvaluateAsInt(Result, Context))
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000011072 return false;
11073
Fangrui Song407659a2018-11-30 23:41:18 +000011074 llvm::APSInt ConstantLengthValue = Result.Val.getInt();
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000011075 SingleElement = (ConstantLengthValue.getSExtValue() == 1);
11076 ArraySizes.push_back(ConstantLengthValue);
11077 }
11078
11079 // Get the base of this array section and walk up from there.
11080 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
11081
11082 // We require length = 1 for all array sections except the right-most to
11083 // guarantee that the memory region is contiguous and has no holes in it.
11084 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) {
11085 Length = TempOASE->getLength();
11086 if (Length == nullptr) {
11087 // For array sections of the form [1:] or [:], we would need to analyze
11088 // the lower bound...
11089 if (OASE->getColonLoc().isValid())
11090 return false;
11091
11092 // This is an array subscript which has implicit length 1!
11093 ArraySizes.push_back(llvm::APSInt::get(1));
11094 } else {
Fangrui Song407659a2018-11-30 23:41:18 +000011095 Expr::EvalResult Result;
11096 if (!Length->EvaluateAsInt(Result, Context))
11097 return false;
11098
11099 llvm::APSInt ConstantLengthValue = Result.Val.getInt();
11100 if (ConstantLengthValue.getSExtValue() != 1)
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000011101 return false;
11102
11103 ArraySizes.push_back(ConstantLengthValue);
11104 }
11105 Base = TempOASE->getBase()->IgnoreParenImpCasts();
11106 }
11107
11108 // If we have a single element, we don't need to add the implicit lengths.
11109 if (!SingleElement) {
11110 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) {
11111 // Has implicit length 1!
11112 ArraySizes.push_back(llvm::APSInt::get(1));
11113 Base = TempASE->getBase()->IgnoreParenImpCasts();
11114 }
11115 }
11116
11117 // This array section can be privatized as a single value or as a constant
11118 // sized array.
11119 return true;
11120}
11121
Alexey Bataeve3727102018-04-18 15:57:46 +000011122static bool actOnOMPReductionKindClause(
Alexey Bataev169d96a2017-07-18 20:17:46 +000011123 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind,
11124 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11125 SourceLocation ColonLoc, SourceLocation EndLoc,
11126 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011127 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011128 DeclarationName DN = ReductionId.getName();
11129 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator();
Alexey Bataevc5e02582014-06-16 07:08:35 +000011130 BinaryOperatorKind BOK = BO_Comma;
11131
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011132 ASTContext &Context = S.Context;
Alexey Bataevc5e02582014-06-16 07:08:35 +000011133 // OpenMP [2.14.3.6, reduction clause]
11134 // C
11135 // reduction-identifier is either an identifier or one of the following
11136 // operators: +, -, *, &, |, ^, && and ||
11137 // C++
11138 // reduction-identifier is either an id-expression or one of the following
11139 // operators: +, -, *, &, |, ^, && and ||
Alexey Bataevc5e02582014-06-16 07:08:35 +000011140 switch (OOK) {
11141 case OO_Plus:
11142 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011143 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +000011144 break;
11145 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011146 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +000011147 break;
11148 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011149 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +000011150 break;
11151 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011152 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +000011153 break;
11154 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011155 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +000011156 break;
11157 case OO_AmpAmp:
11158 BOK = BO_LAnd;
11159 break;
11160 case OO_PipePipe:
11161 BOK = BO_LOr;
11162 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011163 case OO_New:
11164 case OO_Delete:
11165 case OO_Array_New:
11166 case OO_Array_Delete:
11167 case OO_Slash:
11168 case OO_Percent:
11169 case OO_Tilde:
11170 case OO_Exclaim:
11171 case OO_Equal:
11172 case OO_Less:
11173 case OO_Greater:
11174 case OO_LessEqual:
11175 case OO_GreaterEqual:
11176 case OO_PlusEqual:
11177 case OO_MinusEqual:
11178 case OO_StarEqual:
11179 case OO_SlashEqual:
11180 case OO_PercentEqual:
11181 case OO_CaretEqual:
11182 case OO_AmpEqual:
11183 case OO_PipeEqual:
11184 case OO_LessLess:
11185 case OO_GreaterGreater:
11186 case OO_LessLessEqual:
11187 case OO_GreaterGreaterEqual:
11188 case OO_EqualEqual:
11189 case OO_ExclaimEqual:
Richard Smithd30b23d2017-12-01 02:13:10 +000011190 case OO_Spaceship:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011191 case OO_PlusPlus:
11192 case OO_MinusMinus:
11193 case OO_Comma:
11194 case OO_ArrowStar:
11195 case OO_Arrow:
11196 case OO_Call:
11197 case OO_Subscript:
11198 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +000011199 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011200 case NUM_OVERLOADED_OPERATORS:
11201 llvm_unreachable("Unexpected reduction identifier");
11202 case OO_None:
Alexey Bataeve3727102018-04-18 15:57:46 +000011203 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000011204 if (II->isStr("max"))
11205 BOK = BO_GT;
11206 else if (II->isStr("min"))
11207 BOK = BO_LT;
11208 }
11209 break;
11210 }
11211 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011212 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +000011213 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataev4d4624c2017-07-20 16:47:47 +000011214 else
11215 ReductionIdRange.setBegin(ReductionId.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +000011216 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +000011217
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011218 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
11219 bool FirstIter = true;
Alexey Bataeve3727102018-04-18 15:57:46 +000011220 for (Expr *RefExpr : VarList) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000011221 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +000011222 // OpenMP [2.1, C/C++]
11223 // A list item is a variable or array section, subject to the restrictions
11224 // specified in Section 2.4 on page 42 and in each of the sections
11225 // describing clauses and directives for which a list appears.
11226 // OpenMP [2.14.3.3, Restrictions, p.1]
11227 // A variable that is part of another variable (as an array or
11228 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011229 if (!FirstIter && IR != ER)
11230 ++IR;
11231 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +000011232 SourceLocation ELoc;
11233 SourceRange ERange;
11234 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011235 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
Alexey Bataev60da77e2016-02-29 05:54:20 +000011236 /*AllowArraySection=*/true);
11237 if (Res.second) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011238 // Try to find 'declare reduction' corresponding construct before using
11239 // builtin/overloaded operators.
11240 QualType Type = Context.DependentTy;
11241 CXXCastPath BasePath;
11242 ExprResult DeclareReductionRef = buildDeclareReductionRef(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011243 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011244 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011245 Expr *ReductionOp = nullptr;
11246 if (S.CurContext->isDependentContext() &&
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011247 (DeclareReductionRef.isUnset() ||
11248 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011249 ReductionOp = DeclareReductionRef.get();
11250 // It will be analyzed later.
11251 RD.push(RefExpr, ReductionOp);
Alexey Bataevc5e02582014-06-16 07:08:35 +000011252 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000011253 ValueDecl *D = Res.first;
11254 if (!D)
11255 continue;
11256
Alexey Bataev88202be2017-07-27 13:20:36 +000011257 Expr *TaskgroupDescriptor = nullptr;
Alexey Bataeva1764212015-09-30 09:22:36 +000011258 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +000011259 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
11260 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
Alexey Bataeve3727102018-04-18 15:57:46 +000011261 if (ASE) {
Alexey Bataev31300ed2016-02-04 11:27:03 +000011262 Type = ASE->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000011263 } else if (OASE) {
11264 QualType BaseType =
11265 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
11266 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
Alexey Bataeva1764212015-09-30 09:22:36 +000011267 Type = ATy->getElementType();
11268 else
11269 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +000011270 Type = Type.getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000011271 } else {
Alexey Bataev60da77e2016-02-29 05:54:20 +000011272 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
Alexey Bataeve3727102018-04-18 15:57:46 +000011273 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000011274 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +000011275
Alexey Bataevc5e02582014-06-16 07:08:35 +000011276 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
11277 // A variable that appears in a private clause must not have an incomplete
11278 // type or a reference type.
Joel E. Denny3cabf732018-06-28 19:54:49 +000011279 if (S.RequireCompleteType(ELoc, D->getType(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011280 diag::err_omp_reduction_incomplete_type))
Alexey Bataevc5e02582014-06-16 07:08:35 +000011281 continue;
11282 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +000011283 // A list item that appears in a reduction clause must not be
11284 // const-qualified.
Joel E. Dennyd2649292019-01-04 22:11:56 +000011285 if (rejectConstNotMutableType(S, D, Type, ClauseKind, ELoc,
11286 /*AcceptIfMutable*/ false, ASE || OASE))
Alexey Bataevc5e02582014-06-16 07:08:35 +000011287 continue;
Alexey Bataevbc529672018-09-28 19:33:14 +000011288
11289 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective();
Alexey Bataevc5e02582014-06-16 07:08:35 +000011290 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
11291 // If a list-item is a reference type then it must bind to the same object
11292 // for all threads of the team.
Alexey Bataevbc529672018-09-28 19:33:14 +000011293 if (!ASE && !OASE) {
11294 if (VD) {
11295 VarDecl *VDDef = VD->getDefinition();
11296 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) {
11297 DSARefChecker Check(Stack);
11298 if (Check.Visit(VDDef->getInit())) {
11299 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg)
11300 << getOpenMPClauseName(ClauseKind) << ERange;
11301 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
11302 continue;
11303 }
Alexey Bataeva1764212015-09-30 09:22:36 +000011304 }
Alexey Bataevc5e02582014-06-16 07:08:35 +000011305 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011306
Alexey Bataevbc529672018-09-28 19:33:14 +000011307 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
11308 // in a Construct]
11309 // Variables with the predetermined data-sharing attributes may not be
11310 // listed in data-sharing attributes clauses, except for the cases
11311 // listed below. For these exceptions only, listing a predetermined
11312 // variable in a data-sharing attribute clause is allowed and overrides
11313 // the variable's predetermined data-sharing attributes.
11314 // OpenMP [2.14.3.6, Restrictions, p.3]
11315 // Any number of reduction clauses can be specified on the directive,
11316 // but a list item can appear only once in the reduction clauses for that
11317 // directive.
11318 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false);
11319 if (DVar.CKind == OMPC_reduction) {
11320 S.Diag(ELoc, diag::err_omp_once_referenced)
11321 << getOpenMPClauseName(ClauseKind);
11322 if (DVar.RefExpr)
11323 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
11324 continue;
11325 }
11326 if (DVar.CKind != OMPC_unknown) {
11327 S.Diag(ELoc, diag::err_omp_wrong_dsa)
11328 << getOpenMPClauseName(DVar.CKind)
11329 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataeve3727102018-04-18 15:57:46 +000011330 reportOriginalDsa(S, Stack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011331 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +000011332 }
Alexey Bataevbc529672018-09-28 19:33:14 +000011333
11334 // OpenMP [2.14.3.6, Restrictions, p.1]
11335 // A list item that appears in a reduction clause of a worksharing
11336 // construct must be shared in the parallel regions to which any of the
11337 // worksharing regions arising from the worksharing construct bind.
11338 if (isOpenMPWorksharingDirective(CurrDir) &&
11339 !isOpenMPParallelDirective(CurrDir) &&
11340 !isOpenMPTeamsDirective(CurrDir)) {
11341 DVar = Stack->getImplicitDSA(D, true);
11342 if (DVar.CKind != OMPC_shared) {
11343 S.Diag(ELoc, diag::err_omp_required_access)
11344 << getOpenMPClauseName(OMPC_reduction)
11345 << getOpenMPClauseName(OMPC_shared);
11346 reportOriginalDsa(S, Stack, D, DVar);
11347 continue;
11348 }
11349 }
Alexey Bataevf29276e2014-06-18 04:14:57 +000011350 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011351
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011352 // Try to find 'declare reduction' corresponding construct before using
11353 // builtin/overloaded operators.
11354 CXXCastPath BasePath;
11355 ExprResult DeclareReductionRef = buildDeclareReductionRef(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011356 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011357 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
11358 if (DeclareReductionRef.isInvalid())
11359 continue;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011360 if (S.CurContext->isDependentContext() &&
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011361 (DeclareReductionRef.isUnset() ||
11362 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011363 RD.push(RefExpr, DeclareReductionRef.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011364 continue;
11365 }
11366 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
11367 // Not allowed reduction identifier is found.
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011368 S.Diag(ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011369 diag::err_omp_unknown_reduction_identifier)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011370 << Type << ReductionIdRange;
11371 continue;
11372 }
11373
11374 // OpenMP [2.14.3.6, reduction clause, Restrictions]
11375 // The type of a list item that appears in a reduction clause must be valid
11376 // for the reduction-identifier. For a max or min reduction in C, the type
11377 // of the list item must be an allowed arithmetic data type: char, int,
11378 // float, double, or _Bool, possibly modified with long, short, signed, or
11379 // unsigned. For a max or min reduction in C++, the type of the list item
11380 // must be an allowed arithmetic data type: char, wchar_t, int, float,
11381 // double, or bool, possibly modified with long, short, signed, or unsigned.
11382 if (DeclareReductionRef.isUnset()) {
11383 if ((BOK == BO_GT || BOK == BO_LT) &&
11384 !(Type->isScalarType() ||
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011385 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
11386 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
Alexey Bataev169d96a2017-07-18 20:17:46 +000011387 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011388 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011389 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
11390 VarDecl::DeclarationOnly;
11391 S.Diag(D->getLocation(),
11392 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011393 << D;
11394 }
11395 continue;
11396 }
11397 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011398 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) {
Alexey Bataev169d96a2017-07-18 20:17:46 +000011399 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg)
11400 << getOpenMPClauseName(ClauseKind);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011401 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011402 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
11403 VarDecl::DeclarationOnly;
11404 S.Diag(D->getLocation(),
11405 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011406 << D;
11407 }
11408 continue;
11409 }
11410 }
11411
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011412 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000011413 VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs",
11414 D->hasAttrs() ? &D->getAttrs() : nullptr);
11415 VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(),
11416 D->hasAttrs() ? &D->getAttrs() : nullptr);
11417 QualType PrivateTy = Type;
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000011418
11419 // Try if we can determine constant lengths for all array sections and avoid
11420 // the VLA.
11421 bool ConstantLengthOASE = false;
11422 if (OASE) {
11423 bool SingleElement;
11424 llvm::SmallVector<llvm::APSInt, 4> ArraySizes;
Alexey Bataeve3727102018-04-18 15:57:46 +000011425 ConstantLengthOASE = checkOMPArraySectionConstantForReduction(
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000011426 Context, OASE, SingleElement, ArraySizes);
11427
11428 // If we don't have a single element, we must emit a constant array type.
11429 if (ConstantLengthOASE && !SingleElement) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011430 for (llvm::APSInt &Size : ArraySizes)
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000011431 PrivateTy = Context.getConstantArrayType(
11432 PrivateTy, Size, ArrayType::Normal, /*IndexTypeQuals=*/0);
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000011433 }
11434 }
11435
11436 if ((OASE && !ConstantLengthOASE) ||
Jonas Hahnfeld96087f32017-11-02 13:30:42 +000011437 (!OASE && !ASE &&
Alexey Bataev60da77e2016-02-29 05:54:20 +000011438 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Jonas Hahnfeld87d44262017-11-18 21:00:46 +000011439 if (!Context.getTargetInfo().isVLASupported() &&
11440 S.shouldDiagnoseTargetSupportFromOpenMP()) {
11441 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE;
11442 S.Diag(ELoc, diag::note_vla_unsupported);
11443 continue;
11444 }
David Majnemer9d168222016-08-05 17:44:54 +000011445 // For arrays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011446 // Create pseudo array type for private copy. The size for this array will
11447 // be generated during codegen.
11448 // For array subscripts or single variables Private Ty is the same as Type
11449 // (type of the variable or single array element).
11450 PrivateTy = Context.getVariableArrayType(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011451 Type,
Alexey Bataevd070a582017-10-25 15:54:04 +000011452 new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue),
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011453 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +000011454 } else if (!ASE && !OASE &&
Alexey Bataeve3727102018-04-18 15:57:46 +000011455 Context.getAsArrayType(D->getType().getNonReferenceType())) {
Alexey Bataev60da77e2016-02-29 05:54:20 +000011456 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000011457 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011458 // Private copy.
Alexey Bataeve3727102018-04-18 15:57:46 +000011459 VarDecl *PrivateVD =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000011460 buildVarDecl(S, ELoc, PrivateTy, D->getName(),
11461 D->hasAttrs() ? &D->getAttrs() : nullptr,
11462 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011463 // Add initializer for private variable.
11464 Expr *Init = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000011465 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc);
11466 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011467 if (DeclareReductionRef.isUsable()) {
11468 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
11469 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
11470 if (DRD->getInitializer()) {
11471 Init = DRDRef;
11472 RHSVD->setInit(DRDRef);
11473 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011474 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011475 } else {
11476 switch (BOK) {
11477 case BO_Add:
11478 case BO_Xor:
11479 case BO_Or:
11480 case BO_LOr:
11481 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
11482 if (Type->isScalarType() || Type->isAnyComplexType())
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011483 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011484 break;
11485 case BO_Mul:
11486 case BO_LAnd:
11487 if (Type->isScalarType() || Type->isAnyComplexType()) {
11488 // '*' and '&&' reduction ops - initializer is '1'.
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011489 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +000011490 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011491 break;
11492 case BO_And: {
11493 // '&' reduction op - initializer is '~0'.
11494 QualType OrigType = Type;
11495 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
11496 Type = ComplexTy->getElementType();
11497 if (Type->isRealFloatingType()) {
11498 llvm::APFloat InitValue =
11499 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
11500 /*isIEEE=*/true);
11501 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
11502 Type, ELoc);
11503 } else if (Type->isScalarType()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011504 uint64_t Size = Context.getTypeSize(Type);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011505 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
11506 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
11507 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
11508 }
11509 if (Init && OrigType->isAnyComplexType()) {
11510 // Init = 0xFFFF + 0xFFFFi;
11511 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011512 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011513 }
11514 Type = OrigType;
11515 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011516 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011517 case BO_LT:
11518 case BO_GT: {
11519 // 'min' reduction op - initializer is 'Largest representable number in
11520 // the reduction list item type'.
11521 // 'max' reduction op - initializer is 'Least representable number in
11522 // the reduction list item type'.
11523 if (Type->isIntegerType() || Type->isPointerType()) {
11524 bool IsSigned = Type->hasSignedIntegerRepresentation();
Alexey Bataeve3727102018-04-18 15:57:46 +000011525 uint64_t Size = Context.getTypeSize(Type);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011526 QualType IntTy =
11527 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
11528 llvm::APInt InitValue =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011529 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
11530 : llvm::APInt::getMinValue(Size)
11531 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
11532 : llvm::APInt::getMaxValue(Size);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011533 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
11534 if (Type->isPointerType()) {
11535 // Cast to pointer type.
Alexey Bataeve3727102018-04-18 15:57:46 +000011536 ExprResult CastExpr = S.BuildCStyleCastExpr(
Alexey Bataevd070a582017-10-25 15:54:04 +000011537 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011538 if (CastExpr.isInvalid())
11539 continue;
11540 Init = CastExpr.get();
11541 }
11542 } else if (Type->isRealFloatingType()) {
11543 llvm::APFloat InitValue = llvm::APFloat::getLargest(
11544 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
11545 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
11546 Type, ELoc);
11547 }
11548 break;
11549 }
11550 case BO_PtrMemD:
11551 case BO_PtrMemI:
11552 case BO_MulAssign:
11553 case BO_Div:
11554 case BO_Rem:
11555 case BO_Sub:
11556 case BO_Shl:
11557 case BO_Shr:
11558 case BO_LE:
11559 case BO_GE:
11560 case BO_EQ:
11561 case BO_NE:
Richard Smithc70f1d62017-12-14 15:16:18 +000011562 case BO_Cmp:
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011563 case BO_AndAssign:
11564 case BO_XorAssign:
11565 case BO_OrAssign:
11566 case BO_Assign:
11567 case BO_AddAssign:
11568 case BO_SubAssign:
11569 case BO_DivAssign:
11570 case BO_RemAssign:
11571 case BO_ShlAssign:
11572 case BO_ShrAssign:
11573 case BO_Comma:
11574 llvm_unreachable("Unexpected reduction operation");
11575 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011576 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011577 if (Init && DeclareReductionRef.isUnset())
11578 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false);
11579 else if (!Init)
11580 S.ActOnUninitializedDecl(RHSVD);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011581 if (RHSVD->isInvalidDecl())
11582 continue;
11583 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011584 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible)
11585 << Type << ReductionIdRange;
11586 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
11587 VarDecl::DeclarationOnly;
11588 S.Diag(D->getLocation(),
11589 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +000011590 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011591 continue;
11592 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011593 // Store initializer for single element in private copy. Will be used during
11594 // codegen.
11595 PrivateVD->setInit(RHSVD->getInit());
11596 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataeve3727102018-04-18 15:57:46 +000011597 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011598 ExprResult ReductionOp;
11599 if (DeclareReductionRef.isUsable()) {
11600 QualType RedTy = DeclareReductionRef.get()->getType();
11601 QualType PtrRedTy = Context.getPointerType(RedTy);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011602 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
11603 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011604 if (!BasePath.empty()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011605 LHS = S.DefaultLvalueConversion(LHS.get());
11606 RHS = S.DefaultLvalueConversion(RHS.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011607 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
11608 CK_UncheckedDerivedToBase, LHS.get(),
11609 &BasePath, LHS.get()->getValueKind());
11610 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
11611 CK_UncheckedDerivedToBase, RHS.get(),
11612 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011613 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011614 FunctionProtoType::ExtProtoInfo EPI;
11615 QualType Params[] = {PtrRedTy, PtrRedTy};
11616 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
11617 auto *OVE = new (Context) OpaqueValueExpr(
11618 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011619 S.DefaultLvalueConversion(DeclareReductionRef.get()).get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011620 Expr *Args[] = {LHS.get(), RHS.get()};
Bruno Riccic5885cf2018-12-21 15:20:32 +000011621 ReductionOp =
11622 CallExpr::Create(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011623 } else {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011624 ReductionOp = S.BuildBinOp(
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011625 Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, LHSDRE, RHSDRE);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011626 if (ReductionOp.isUsable()) {
11627 if (BOK != BO_LT && BOK != BO_GT) {
11628 ReductionOp =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011629 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011630 BO_Assign, LHSDRE, ReductionOp.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011631 } else {
Alexey Bataevd070a582017-10-25 15:54:04 +000011632 auto *ConditionalOp = new (Context)
11633 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE,
11634 Type, VK_LValue, OK_Ordinary);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011635 ReductionOp =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011636 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011637 BO_Assign, LHSDRE, ConditionalOp);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011638 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +000011639 if (ReductionOp.isUsable())
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000011640 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get(),
11641 /*DiscardedValue*/ false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011642 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +000011643 if (!ReductionOp.isUsable())
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011644 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +000011645 }
11646
Alexey Bataevfa312f32017-07-21 18:48:21 +000011647 // OpenMP [2.15.4.6, Restrictions, p.2]
11648 // A list item that appears in an in_reduction clause of a task construct
11649 // must appear in a task_reduction clause of a construct associated with a
11650 // taskgroup region that includes the participating task in its taskgroup
11651 // set. The construct associated with the innermost region that meets this
11652 // condition must specify the same reduction-identifier as the in_reduction
11653 // clause.
11654 if (ClauseKind == OMPC_in_reduction) {
Alexey Bataevfa312f32017-07-21 18:48:21 +000011655 SourceRange ParentSR;
11656 BinaryOperatorKind ParentBOK;
11657 const Expr *ParentReductionOp;
Alexey Bataev88202be2017-07-27 13:20:36 +000011658 Expr *ParentBOKTD, *ParentReductionOpTD;
Alexey Bataevf189cb72017-07-24 14:52:13 +000011659 DSAStackTy::DSAVarData ParentBOKDSA =
Alexey Bataev88202be2017-07-27 13:20:36 +000011660 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK,
11661 ParentBOKTD);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011662 DSAStackTy::DSAVarData ParentReductionOpDSA =
Alexey Bataev88202be2017-07-27 13:20:36 +000011663 Stack->getTopMostTaskgroupReductionData(
11664 D, ParentSR, ParentReductionOp, ParentReductionOpTD);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011665 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown;
11666 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown;
11667 if (!IsParentBOK && !IsParentReductionOp) {
11668 S.Diag(ELoc, diag::err_omp_in_reduction_not_task_reduction);
11669 continue;
11670 }
Alexey Bataevfa312f32017-07-21 18:48:21 +000011671 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) ||
11672 (DeclareReductionRef.isUsable() && IsParentBOK) || BOK != ParentBOK ||
11673 IsParentReductionOp) {
11674 bool EmitError = true;
11675 if (IsParentReductionOp && DeclareReductionRef.isUsable()) {
11676 llvm::FoldingSetNodeID RedId, ParentRedId;
11677 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true);
11678 DeclareReductionRef.get()->Profile(RedId, Context,
11679 /*Canonical=*/true);
11680 EmitError = RedId != ParentRedId;
11681 }
11682 if (EmitError) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011683 S.Diag(ReductionId.getBeginLoc(),
Alexey Bataevfa312f32017-07-21 18:48:21 +000011684 diag::err_omp_reduction_identifier_mismatch)
11685 << ReductionIdRange << RefExpr->getSourceRange();
11686 S.Diag(ParentSR.getBegin(),
11687 diag::note_omp_previous_reduction_identifier)
Alexey Bataevf189cb72017-07-24 14:52:13 +000011688 << ParentSR
11689 << (IsParentBOK ? ParentBOKDSA.RefExpr
11690 : ParentReductionOpDSA.RefExpr)
11691 ->getSourceRange();
Alexey Bataevfa312f32017-07-21 18:48:21 +000011692 continue;
11693 }
11694 }
Alexey Bataev88202be2017-07-27 13:20:36 +000011695 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD;
11696 assert(TaskgroupDescriptor && "Taskgroup descriptor must be defined.");
Alexey Bataevfa312f32017-07-21 18:48:21 +000011697 }
11698
Alexey Bataev60da77e2016-02-29 05:54:20 +000011699 DeclRefExpr *Ref = nullptr;
11700 Expr *VarsExpr = RefExpr->IgnoreParens();
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011701 if (!VD && !S.CurContext->isDependentContext()) {
Alexey Bataev60da77e2016-02-29 05:54:20 +000011702 if (ASE || OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011703 TransformExprToCaptures RebuildToCapture(S, D);
Alexey Bataev60da77e2016-02-29 05:54:20 +000011704 VarsExpr =
11705 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
11706 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +000011707 } else {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011708 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +000011709 }
Alexey Bataeve3727102018-04-18 15:57:46 +000011710 if (!S.isOpenMPCapturedDecl(D)) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011711 RD.ExprCaptures.emplace_back(Ref->getDecl());
Alexey Bataev5a3af132016-03-29 08:58:54 +000011712 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011713 ExprResult RefRes = S.DefaultLvalueConversion(Ref);
Alexey Bataev5a3af132016-03-29 08:58:54 +000011714 if (!RefRes.isUsable())
11715 continue;
11716 ExprResult PostUpdateRes =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011717 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
11718 RefRes.get());
Alexey Bataev5a3af132016-03-29 08:58:54 +000011719 if (!PostUpdateRes.isUsable())
11720 continue;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011721 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
11722 Stack->getCurrentDirective() == OMPD_taskgroup) {
11723 S.Diag(RefExpr->getExprLoc(),
11724 diag::err_omp_reduction_non_addressable_expression)
Alexey Bataevbcd0ae02017-07-11 19:16:44 +000011725 << RefExpr->getSourceRange();
11726 continue;
11727 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011728 RD.ExprPostUpdates.emplace_back(
11729 S.IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +000011730 }
11731 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000011732 }
Alexey Bataev169d96a2017-07-18 20:17:46 +000011733 // All reduction items are still marked as reduction (to do not increase
11734 // code base size).
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011735 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011736 if (CurrDir == OMPD_taskgroup) {
11737 if (DeclareReductionRef.isUsable())
Alexey Bataev3b1b8952017-07-25 15:53:26 +000011738 Stack->addTaskgroupReductionData(D, ReductionIdRange,
11739 DeclareReductionRef.get());
Alexey Bataevf189cb72017-07-24 14:52:13 +000011740 else
Alexey Bataev3b1b8952017-07-25 15:53:26 +000011741 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011742 }
Alexey Bataev88202be2017-07-27 13:20:36 +000011743 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(),
11744 TaskgroupDescriptor);
Alexey Bataevc5e02582014-06-16 07:08:35 +000011745 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011746 return RD.Vars.empty();
11747}
Alexey Bataevc5e02582014-06-16 07:08:35 +000011748
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011749OMPClause *Sema::ActOnOpenMPReductionClause(
11750 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11751 SourceLocation ColonLoc, SourceLocation EndLoc,
11752 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11753 ArrayRef<Expr *> UnresolvedReductions) {
11754 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000011755 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList,
Alexey Bataev169d96a2017-07-18 20:17:46 +000011756 StartLoc, LParenLoc, ColonLoc, EndLoc,
11757 ReductionIdScopeSpec, ReductionId,
11758 UnresolvedReductions, RD))
Alexey Bataevc5e02582014-06-16 07:08:35 +000011759 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +000011760
Alexey Bataevc5e02582014-06-16 07:08:35 +000011761 return OMPReductionClause::Create(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011762 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11763 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
11764 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
11765 buildPreInits(Context, RD.ExprCaptures),
11766 buildPostUpdate(*this, RD.ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +000011767}
11768
Alexey Bataev169d96a2017-07-18 20:17:46 +000011769OMPClause *Sema::ActOnOpenMPTaskReductionClause(
11770 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11771 SourceLocation ColonLoc, SourceLocation EndLoc,
11772 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11773 ArrayRef<Expr *> UnresolvedReductions) {
11774 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000011775 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList,
11776 StartLoc, LParenLoc, ColonLoc, EndLoc,
11777 ReductionIdScopeSpec, ReductionId,
Alexey Bataev169d96a2017-07-18 20:17:46 +000011778 UnresolvedReductions, RD))
11779 return nullptr;
11780
11781 return OMPTaskReductionClause::Create(
11782 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11783 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
11784 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
11785 buildPreInits(Context, RD.ExprCaptures),
11786 buildPostUpdate(*this, RD.ExprPostUpdates));
11787}
11788
Alexey Bataevfa312f32017-07-21 18:48:21 +000011789OMPClause *Sema::ActOnOpenMPInReductionClause(
11790 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11791 SourceLocation ColonLoc, SourceLocation EndLoc,
11792 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11793 ArrayRef<Expr *> UnresolvedReductions) {
11794 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000011795 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList,
Alexey Bataevfa312f32017-07-21 18:48:21 +000011796 StartLoc, LParenLoc, ColonLoc, EndLoc,
11797 ReductionIdScopeSpec, ReductionId,
11798 UnresolvedReductions, RD))
11799 return nullptr;
11800
11801 return OMPInReductionClause::Create(
11802 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11803 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
Alexey Bataev88202be2017-07-27 13:20:36 +000011804 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors,
Alexey Bataevfa312f32017-07-21 18:48:21 +000011805 buildPreInits(Context, RD.ExprCaptures),
11806 buildPostUpdate(*this, RD.ExprPostUpdates));
11807}
11808
Alexey Bataevecba70f2016-04-12 11:02:11 +000011809bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
11810 SourceLocation LinLoc) {
11811 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
11812 LinKind == OMPC_LINEAR_unknown) {
11813 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
11814 return true;
11815 }
11816 return false;
11817}
11818
Alexey Bataeve3727102018-04-18 15:57:46 +000011819bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc,
Alexey Bataevecba70f2016-04-12 11:02:11 +000011820 OpenMPLinearClauseKind LinKind,
11821 QualType Type) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011822 const auto *VD = dyn_cast_or_null<VarDecl>(D);
Alexey Bataevecba70f2016-04-12 11:02:11 +000011823 // A variable must not have an incomplete type or a reference type.
11824 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
11825 return true;
11826 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
11827 !Type->isReferenceType()) {
11828 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
11829 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
11830 return true;
11831 }
11832 Type = Type.getNonReferenceType();
11833
Joel E. Dennybae586f2019-01-04 22:12:13 +000011834 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
11835 // A variable that is privatized must not have a const-qualified type
11836 // unless it is of class type with a mutable member. This restriction does
11837 // not apply to the firstprivate clause.
11838 if (rejectConstNotMutableType(*this, D, Type, OMPC_linear, ELoc))
Alexey Bataevecba70f2016-04-12 11:02:11 +000011839 return true;
Alexey Bataevecba70f2016-04-12 11:02:11 +000011840
11841 // A list item must be of integral or pointer type.
11842 Type = Type.getUnqualifiedType().getCanonicalType();
11843 const auto *Ty = Type.getTypePtrOrNull();
11844 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
11845 !Ty->isPointerType())) {
11846 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
11847 if (D) {
11848 bool IsDecl =
11849 !VD ||
11850 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
11851 Diag(D->getLocation(),
11852 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
11853 << D;
11854 }
11855 return true;
11856 }
11857 return false;
11858}
11859
Alexey Bataev182227b2015-08-20 10:54:39 +000011860OMPClause *Sema::ActOnOpenMPLinearClause(
11861 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
11862 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
11863 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +000011864 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011865 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +000011866 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +000011867 SmallVector<Decl *, 4> ExprCaptures;
11868 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +000011869 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +000011870 LinKind = OMPC_LINEAR_val;
Alexey Bataeve3727102018-04-18 15:57:46 +000011871 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000011872 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011873 SourceLocation ELoc;
11874 SourceRange ERange;
11875 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011876 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011877 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +000011878 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000011879 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011880 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +000011881 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +000011882 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011883 ValueDecl *D = Res.first;
11884 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +000011885 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +000011886
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011887 QualType Type = D->getType();
11888 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +000011889
11890 // OpenMP [2.14.3.7, linear clause]
11891 // A list-item cannot appear in more than one linear clause.
11892 // A list-item that appears in a linear clause cannot appear in any
11893 // other data-sharing attribute clause.
Alexey Bataeve3727102018-04-18 15:57:46 +000011894 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexander Musman8dba6642014-04-22 13:09:42 +000011895 if (DVar.RefExpr) {
11896 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
11897 << getOpenMPClauseName(OMPC_linear);
Alexey Bataeve3727102018-04-18 15:57:46 +000011898 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +000011899 continue;
11900 }
11901
Alexey Bataevecba70f2016-04-12 11:02:11 +000011902 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +000011903 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +000011904 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +000011905
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011906 // Build private copy of original var.
Alexey Bataeve3727102018-04-18 15:57:46 +000011907 VarDecl *Private =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000011908 buildVarDecl(*this, ELoc, Type, D->getName(),
11909 D->hasAttrs() ? &D->getAttrs() : nullptr,
11910 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011911 DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +000011912 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011913 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011914 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011915 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000011916 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev78849fb2016-03-09 09:49:00 +000011917 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011918 if (!isOpenMPCapturedDecl(D)) {
Alexey Bataev78849fb2016-03-09 09:49:00 +000011919 ExprCaptures.push_back(Ref->getDecl());
11920 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
11921 ExprResult RefRes = DefaultLvalueConversion(Ref);
11922 if (!RefRes.isUsable())
11923 continue;
11924 ExprResult PostUpdateRes =
11925 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
11926 SimpleRefExpr, RefRes.get());
11927 if (!PostUpdateRes.isUsable())
11928 continue;
11929 ExprPostUpdates.push_back(
11930 IgnoredValueConversions(PostUpdateRes.get()).get());
11931 }
11932 }
11933 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011934 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011935 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011936 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011937 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011938 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000011939 /*DirectInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011940 DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011941
11942 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000011943 Vars.push_back((VD || CurContext->isDependentContext())
11944 ? RefExpr->IgnoreParens()
11945 : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011946 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +000011947 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +000011948 }
11949
11950 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000011951 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +000011952
11953 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +000011954 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +000011955 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
11956 !Step->isInstantiationDependent() &&
11957 !Step->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011958 SourceLocation StepLoc = Step->getBeginLoc();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +000011959 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +000011960 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000011961 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +000011962 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +000011963
Alexander Musman3276a272015-03-21 10:12:56 +000011964 // Build var to save the step value.
11965 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +000011966 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +000011967 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +000011968 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +000011969 ExprResult CalcStep =
11970 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000011971 CalcStep = ActOnFinishFullExpr(CalcStep.get(), /*DiscardedValue*/ false);
Alexander Musman3276a272015-03-21 10:12:56 +000011972
Alexander Musman8dba6642014-04-22 13:09:42 +000011973 // Warn about zero linear step (it would be probably better specified as
11974 // making corresponding variables 'const').
11975 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +000011976 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
11977 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +000011978 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
11979 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +000011980 if (!IsConstant && CalcStep.isUsable()) {
11981 // Calculate the step beforehand instead of doing this on each iteration.
11982 // (This is not used if the number of iterations may be kfold-ed).
11983 CalcStepExpr = CalcStep.get();
11984 }
Alexander Musman8dba6642014-04-22 13:09:42 +000011985 }
11986
Alexey Bataev182227b2015-08-20 10:54:39 +000011987 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
11988 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +000011989 StepExpr, CalcStepExpr,
11990 buildPreInits(Context, ExprCaptures),
11991 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +000011992}
11993
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011994static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
11995 Expr *NumIterations, Sema &SemaRef,
11996 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +000011997 // Walk the vars and build update/final expressions for the CodeGen.
11998 SmallVector<Expr *, 8> Updates;
11999 SmallVector<Expr *, 8> Finals;
12000 Expr *Step = Clause.getStep();
12001 Expr *CalcStep = Clause.getCalcStep();
12002 // OpenMP [2.14.3.7, linear clause]
12003 // If linear-step is not specified it is assumed to be 1.
Alexey Bataeve3727102018-04-18 15:57:46 +000012004 if (!Step)
Alexander Musman3276a272015-03-21 10:12:56 +000012005 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000012006 else if (CalcStep)
Alexander Musman3276a272015-03-21 10:12:56 +000012007 Step = cast<BinaryOperator>(CalcStep)->getLHS();
12008 bool HasErrors = false;
12009 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +000012010 auto CurPrivate = Clause.privates().begin();
Alexey Bataeve3727102018-04-18 15:57:46 +000012011 OpenMPLinearClauseKind LinKind = Clause.getModifier();
12012 for (Expr *RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +000012013 SourceLocation ELoc;
12014 SourceRange ERange;
12015 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000012016 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange);
Alexey Bataev5dff95c2016-04-22 03:56:56 +000012017 ValueDecl *D = Res.first;
12018 if (Res.second || !D) {
12019 Updates.push_back(nullptr);
12020 Finals.push_back(nullptr);
12021 HasErrors = true;
12022 continue;
12023 }
Alexey Bataev5dff95c2016-04-22 03:56:56 +000012024 auto &&Info = Stack->isLoopControlVariable(D);
Alexey Bataev2b86f212017-11-29 21:31:48 +000012025 // OpenMP [2.15.11, distribute simd Construct]
12026 // A list item may not appear in a linear clause, unless it is the loop
12027 // iteration variable.
12028 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) &&
12029 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) {
12030 SemaRef.Diag(ELoc,
12031 diag::err_omp_linear_distribute_var_non_loop_iteration);
12032 Updates.push_back(nullptr);
12033 Finals.push_back(nullptr);
12034 HasErrors = true;
12035 continue;
12036 }
Alexander Musman3276a272015-03-21 10:12:56 +000012037 Expr *InitExpr = *CurInit;
12038
12039 // Build privatized reference to the current linear var.
David Majnemer9d168222016-08-05 17:44:54 +000012040 auto *DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000012041 Expr *CapturedRef;
12042 if (LinKind == OMPC_LINEAR_uval)
12043 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
12044 else
12045 CapturedRef =
12046 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
12047 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
12048 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +000012049
12050 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000012051 ExprResult Update;
Alexey Bataeve3727102018-04-18 15:57:46 +000012052 if (!Info.first)
Alexey Bataev5dff95c2016-04-22 03:56:56 +000012053 Update =
Alexey Bataeve3727102018-04-18 15:57:46 +000012054 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexey Bataev5dff95c2016-04-22 03:56:56 +000012055 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataeve3727102018-04-18 15:57:46 +000012056 else
Alexey Bataev5dff95c2016-04-22 03:56:56 +000012057 Update = *CurPrivate;
Stephen Kellyf2ceec42018-08-09 21:08:08 +000012058 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000012059 /*DiscardedValue*/ false);
Alexander Musman3276a272015-03-21 10:12:56 +000012060
12061 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000012062 ExprResult Final;
Alexey Bataeve3727102018-04-18 15:57:46 +000012063 if (!Info.first)
12064 Final =
12065 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
12066 InitExpr, NumIterations, Step, /*Subtract=*/false);
12067 else
Alexey Bataev5dff95c2016-04-22 03:56:56 +000012068 Final = *CurPrivate;
Stephen Kellyf2ceec42018-08-09 21:08:08 +000012069 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000012070 /*DiscardedValue*/ false);
Alexey Bataev5dff95c2016-04-22 03:56:56 +000012071
Alexander Musman3276a272015-03-21 10:12:56 +000012072 if (!Update.isUsable() || !Final.isUsable()) {
12073 Updates.push_back(nullptr);
12074 Finals.push_back(nullptr);
12075 HasErrors = true;
12076 } else {
12077 Updates.push_back(Update.get());
12078 Finals.push_back(Final.get());
12079 }
Richard Trieucc3949d2016-02-18 22:34:54 +000012080 ++CurInit;
12081 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +000012082 }
12083 Clause.setUpdates(Updates);
12084 Clause.setFinals(Finals);
12085 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +000012086}
12087
Alexander Musmanf0d76e72014-05-29 14:36:25 +000012088OMPClause *Sema::ActOnOpenMPAlignedClause(
12089 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
12090 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000012091 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +000012092 for (Expr *RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +000012093 assert(RefExpr && "NULL expr in OpenMP linear clause.");
12094 SourceLocation ELoc;
12095 SourceRange ERange;
12096 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000012097 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev1efd1662016-03-29 10:59:56 +000012098 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000012099 // It will be analyzed later.
12100 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000012101 }
Alexey Bataev1efd1662016-03-29 10:59:56 +000012102 ValueDecl *D = Res.first;
12103 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +000012104 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000012105
Alexey Bataev1efd1662016-03-29 10:59:56 +000012106 QualType QType = D->getType();
12107 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000012108
12109 // OpenMP [2.8.1, simd construct, Restrictions]
12110 // The type of list items appearing in the aligned clause must be
12111 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000012112 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +000012113 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +000012114 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000012115 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +000012116 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000012117 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +000012118 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +000012119 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +000012120 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +000012121 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +000012122 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000012123 continue;
12124 }
12125
12126 // OpenMP [2.8.1, simd construct, Restrictions]
12127 // A list-item cannot appear in more than one aligned clause.
Alexey Bataeve3727102018-04-18 15:57:46 +000012128 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +000012129 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000012130 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
12131 << getOpenMPClauseName(OMPC_aligned);
12132 continue;
12133 }
12134
Alexey Bataev1efd1662016-03-29 10:59:56 +000012135 DeclRefExpr *Ref = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000012136 if (!VD && isOpenMPCapturedDecl(D))
Alexey Bataev1efd1662016-03-29 10:59:56 +000012137 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
12138 Vars.push_back(DefaultFunctionArrayConversion(
12139 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
12140 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +000012141 }
12142
12143 // OpenMP [2.8.1, simd construct, Description]
12144 // The parameter of the aligned clause, alignment, must be a constant
12145 // positive integer expression.
12146 // If no optional parameter is specified, implementation-defined default
12147 // alignments for SIMD instructions on the target platforms are assumed.
12148 if (Alignment != nullptr) {
12149 ExprResult AlignResult =
12150 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
12151 if (AlignResult.isInvalid())
12152 return nullptr;
12153 Alignment = AlignResult.get();
12154 }
12155 if (Vars.empty())
12156 return nullptr;
12157
12158 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
12159 EndLoc, Vars, Alignment);
12160}
12161
Alexey Bataevd48bcd82014-03-31 03:36:38 +000012162OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
12163 SourceLocation StartLoc,
12164 SourceLocation LParenLoc,
12165 SourceLocation EndLoc) {
12166 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +000012167 SmallVector<Expr *, 8> SrcExprs;
12168 SmallVector<Expr *, 8> DstExprs;
12169 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeve3727102018-04-18 15:57:46 +000012170 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000012171 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
12172 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +000012173 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000012174 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000012175 SrcExprs.push_back(nullptr);
12176 DstExprs.push_back(nullptr);
12177 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000012178 continue;
12179 }
12180
Alexey Bataeved09d242014-05-28 05:53:51 +000012181 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000012182 // OpenMP [2.1, C/C++]
12183 // A list item is a variable name.
12184 // OpenMP [2.14.4.1, Restrictions, p.1]
12185 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeve3727102018-04-18 15:57:46 +000012186 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000012187 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000012188 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
12189 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000012190 continue;
12191 }
12192
12193 Decl *D = DE->getDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000012194 auto *VD = cast<VarDecl>(D);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000012195
12196 QualType Type = VD->getType();
12197 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
12198 // It will be analyzed later.
12199 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000012200 SrcExprs.push_back(nullptr);
12201 DstExprs.push_back(nullptr);
12202 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000012203 continue;
12204 }
12205
12206 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
12207 // A list item that appears in a copyin clause must be threadprivate.
12208 if (!DSAStack->isThreadPrivate(VD)) {
12209 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +000012210 << getOpenMPClauseName(OMPC_copyin)
12211 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000012212 continue;
12213 }
12214
12215 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
12216 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +000012217 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +000012218 // operator for the class type.
Alexey Bataeve3727102018-04-18 15:57:46 +000012219 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
12220 VarDecl *SrcVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000012221 buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000012222 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000012223 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +000012224 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +000012225 VarDecl *DstVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000012226 buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst",
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000012227 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000012228 DeclRefExpr *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +000012229 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +000012230 // For arrays generate assignment operation for single element and replace
12231 // it by the original array element in CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +000012232 ExprResult AssignmentOp =
12233 BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr,
12234 PseudoSrcExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000012235 if (AssignmentOp.isInvalid())
12236 continue;
12237 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000012238 /*DiscardedValue*/ false);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000012239 if (AssignmentOp.isInvalid())
12240 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000012241
12242 DSAStack->addDSA(VD, DE, OMPC_copyin);
12243 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000012244 SrcExprs.push_back(PseudoSrcExpr);
12245 DstExprs.push_back(PseudoDstExpr);
12246 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +000012247 }
12248
Alexey Bataeved09d242014-05-28 05:53:51 +000012249 if (Vars.empty())
12250 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000012251
Alexey Bataevf56f98c2015-04-16 05:39:01 +000012252 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
12253 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000012254}
12255
Alexey Bataevbae9a792014-06-27 10:37:06 +000012256OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
12257 SourceLocation StartLoc,
12258 SourceLocation LParenLoc,
12259 SourceLocation EndLoc) {
12260 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +000012261 SmallVector<Expr *, 8> SrcExprs;
12262 SmallVector<Expr *, 8> DstExprs;
12263 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeve3727102018-04-18 15:57:46 +000012264 for (Expr *RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +000012265 assert(RefExpr && "NULL expr in OpenMP linear clause.");
12266 SourceLocation ELoc;
12267 SourceRange ERange;
12268 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000012269 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataeve122da12016-03-17 10:50:17 +000012270 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000012271 // It will be analyzed later.
12272 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000012273 SrcExprs.push_back(nullptr);
12274 DstExprs.push_back(nullptr);
12275 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +000012276 }
Alexey Bataeve122da12016-03-17 10:50:17 +000012277 ValueDecl *D = Res.first;
12278 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +000012279 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000012280
Alexey Bataeve122da12016-03-17 10:50:17 +000012281 QualType Type = D->getType();
12282 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +000012283
12284 // OpenMP [2.14.4.2, Restrictions, p.2]
12285 // A list item that appears in a copyprivate clause may not appear in a
12286 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +000012287 if (!VD || !DSAStack->isThreadPrivate(VD)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012288 DSAStackTy::DSAVarData DVar =
12289 DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataeva63048e2015-03-23 06:18:07 +000012290 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
12291 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000012292 Diag(ELoc, diag::err_omp_wrong_dsa)
12293 << getOpenMPClauseName(DVar.CKind)
12294 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000012295 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000012296 continue;
12297 }
12298
12299 // OpenMP [2.11.4.2, Restrictions, p.1]
12300 // All list items that appear in a copyprivate clause must be either
12301 // threadprivate or private in the enclosing context.
12302 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +000012303 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +000012304 if (DVar.CKind == OMPC_shared) {
12305 Diag(ELoc, diag::err_omp_required_access)
12306 << getOpenMPClauseName(OMPC_copyprivate)
12307 << "threadprivate or private in the enclosing context";
Alexey Bataeve3727102018-04-18 15:57:46 +000012308 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000012309 continue;
12310 }
12311 }
12312 }
12313
Alexey Bataev7a3e5852015-05-19 08:19:24 +000012314 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000012315 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +000012316 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +000012317 << getOpenMPClauseName(OMPC_copyprivate) << Type
12318 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +000012319 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +000012320 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +000012321 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +000012322 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +000012323 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +000012324 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +000012325 continue;
12326 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +000012327
Alexey Bataevbae9a792014-06-27 10:37:06 +000012328 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
12329 // A variable of class type (or array thereof) that appears in a
12330 // copyin clause requires an accessible, unambiguous copy assignment
12331 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +000012332 Type = Context.getBaseElementType(Type.getNonReferenceType())
12333 .getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000012334 VarDecl *SrcVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000012335 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src",
Alexey Bataeve122da12016-03-17 10:50:17 +000012336 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000012337 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
12338 VarDecl *DstVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000012339 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst",
Alexey Bataeve122da12016-03-17 10:50:17 +000012340 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000012341 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
12342 ExprResult AssignmentOp = BuildBinOp(
12343 DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000012344 if (AssignmentOp.isInvalid())
12345 continue;
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000012346 AssignmentOp =
12347 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataeva63048e2015-03-23 06:18:07 +000012348 if (AssignmentOp.isInvalid())
12349 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000012350
12351 // No need to mark vars as copyprivate, they are already threadprivate or
12352 // implicitly private.
Alexey Bataeve3727102018-04-18 15:57:46 +000012353 assert(VD || isOpenMPCapturedDecl(D));
Alexey Bataeve122da12016-03-17 10:50:17 +000012354 Vars.push_back(
12355 VD ? RefExpr->IgnoreParens()
12356 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +000012357 SrcExprs.push_back(PseudoSrcExpr);
12358 DstExprs.push_back(PseudoDstExpr);
12359 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +000012360 }
12361
12362 if (Vars.empty())
12363 return nullptr;
12364
Alexey Bataeva63048e2015-03-23 06:18:07 +000012365 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
12366 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +000012367}
12368
Alexey Bataev6125da92014-07-21 11:26:11 +000012369OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
12370 SourceLocation StartLoc,
12371 SourceLocation LParenLoc,
12372 SourceLocation EndLoc) {
12373 if (VarList.empty())
12374 return nullptr;
12375
12376 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
12377}
Alexey Bataevdea47612014-07-23 07:46:59 +000012378
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012379OMPClause *
12380Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
12381 SourceLocation DepLoc, SourceLocation ColonLoc,
12382 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
12383 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +000012384 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012385 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +000012386 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000012387 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +000012388 return nullptr;
12389 }
12390 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012391 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
12392 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +000012393 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012394 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000012395 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
12396 /*Last=*/OMPC_DEPEND_unknown, Except)
12397 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012398 return nullptr;
12399 }
12400 SmallVector<Expr *, 8> Vars;
Alexey Bataev8b427062016-05-25 12:36:08 +000012401 DSAStackTy::OperatorOffsetTy OpsOffs;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012402 llvm::APSInt DepCounter(/*BitWidth=*/32);
12403 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
Alexey Bataevf138fda2018-08-13 19:04:24 +000012404 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) {
12405 if (const Expr *OrderedCountExpr =
12406 DSAStack->getParentOrderedRegionParam().first) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012407 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
12408 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012409 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012410 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012411 for (Expr *RefExpr : VarList) {
Alexey Bataev17daedf2018-02-15 22:42:57 +000012412 assert(RefExpr && "NULL expr in OpenMP shared clause.");
12413 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
12414 // It will be analyzed later.
12415 Vars.push_back(RefExpr);
12416 continue;
12417 }
12418
12419 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataeve3727102018-04-18 15:57:46 +000012420 Expr *SimpleExpr = RefExpr->IgnoreParenCasts();
Alexey Bataev17daedf2018-02-15 22:42:57 +000012421 if (DepKind == OMPC_DEPEND_sink) {
Alexey Bataevf138fda2018-08-13 19:04:24 +000012422 if (DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000012423 DepCounter >= TotalDepCount) {
12424 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
12425 continue;
12426 }
12427 ++DepCounter;
12428 // OpenMP [2.13.9, Summary]
12429 // depend(dependence-type : vec), where dependence-type is:
12430 // 'sink' and where vec is the iteration vector, which has the form:
12431 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
12432 // where n is the value specified by the ordered clause in the loop
12433 // directive, xi denotes the loop iteration variable of the i-th nested
12434 // loop associated with the loop directive, and di is a constant
12435 // non-negative integer.
12436 if (CurContext->isDependentContext()) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012437 // It will be analyzed later.
12438 Vars.push_back(RefExpr);
12439 continue;
12440 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000012441 SimpleExpr = SimpleExpr->IgnoreImplicit();
12442 OverloadedOperatorKind OOK = OO_None;
12443 SourceLocation OOLoc;
12444 Expr *LHS = SimpleExpr;
12445 Expr *RHS = nullptr;
12446 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
12447 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
12448 OOLoc = BO->getOperatorLoc();
12449 LHS = BO->getLHS()->IgnoreParenImpCasts();
12450 RHS = BO->getRHS()->IgnoreParenImpCasts();
12451 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
12452 OOK = OCE->getOperator();
12453 OOLoc = OCE->getOperatorLoc();
12454 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
12455 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
12456 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
12457 OOK = MCE->getMethodDecl()
12458 ->getNameInfo()
12459 .getName()
12460 .getCXXOverloadedOperator();
12461 OOLoc = MCE->getCallee()->getExprLoc();
12462 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
12463 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012464 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000012465 SourceLocation ELoc;
12466 SourceRange ERange;
Alexey Bataevbc529672018-09-28 19:33:14 +000012467 auto Res = getPrivateItem(*this, LHS, ELoc, ERange);
Alexey Bataev17daedf2018-02-15 22:42:57 +000012468 if (Res.second) {
12469 // It will be analyzed later.
12470 Vars.push_back(RefExpr);
12471 }
12472 ValueDecl *D = Res.first;
12473 if (!D)
12474 continue;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012475
Alexey Bataev17daedf2018-02-15 22:42:57 +000012476 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
12477 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
12478 continue;
12479 }
12480 if (RHS) {
12481 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
12482 RHS, OMPC_depend, /*StrictlyPositive=*/false);
12483 if (RHSRes.isInvalid())
12484 continue;
12485 }
12486 if (!CurContext->isDependentContext() &&
Alexey Bataevf138fda2018-08-13 19:04:24 +000012487 DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000012488 DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012489 const ValueDecl *VD =
Alexey Bataev17daedf2018-02-15 22:42:57 +000012490 DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue());
Alexey Bataeve3727102018-04-18 15:57:46 +000012491 if (VD)
Alexey Bataev17daedf2018-02-15 22:42:57 +000012492 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
12493 << 1 << VD;
Alexey Bataeve3727102018-04-18 15:57:46 +000012494 else
Alexey Bataev17daedf2018-02-15 22:42:57 +000012495 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0;
Alexey Bataev17daedf2018-02-15 22:42:57 +000012496 continue;
12497 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012498 OpsOffs.emplace_back(RHS, OOK);
Alexey Bataev17daedf2018-02-15 22:42:57 +000012499 } else {
12500 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
12501 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
12502 (ASE &&
12503 !ASE->getBase()->getType().getNonReferenceType()->isPointerType() &&
12504 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
12505 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
12506 << RefExpr->getSourceRange();
12507 continue;
12508 }
12509 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
12510 getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
12511 ExprResult Res =
12512 CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RefExpr->IgnoreParenImpCasts());
12513 getDiagnostics().setSuppressAllDiagnostics(Suppress);
12514 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr)) {
12515 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
12516 << RefExpr->getSourceRange();
12517 continue;
12518 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012519 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000012520 Vars.push_back(RefExpr->IgnoreParenImpCasts());
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012521 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000012522
12523 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
12524 TotalDepCount > VarList.size() &&
Alexey Bataevf138fda2018-08-13 19:04:24 +000012525 DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000012526 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) {
12527 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
12528 << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
12529 }
12530 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
12531 Vars.empty())
12532 return nullptr;
12533
Alexey Bataev8b427062016-05-25 12:36:08 +000012534 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataevf138fda2018-08-13 19:04:24 +000012535 DepKind, DepLoc, ColonLoc, Vars,
12536 TotalDepCount.getZExtValue());
Alexey Bataev17daedf2018-02-15 22:42:57 +000012537 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) &&
12538 DSAStack->isParentOrderedRegion())
Alexey Bataev8b427062016-05-25 12:36:08 +000012539 DSAStack->addDoacrossDependClause(C, OpsOffs);
12540 return C;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012541}
Michael Wonge710d542015-08-07 16:16:36 +000012542
12543OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
12544 SourceLocation LParenLoc,
12545 SourceLocation EndLoc) {
12546 Expr *ValExpr = Device;
Alexey Bataev931e19b2017-10-02 16:32:39 +000012547 Stmt *HelperValStmt = nullptr;
Michael Wonge710d542015-08-07 16:16:36 +000012548
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012549 // OpenMP [2.9.1, Restrictions]
12550 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000012551 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
Alexey Bataeva0569352015-12-01 10:17:31 +000012552 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012553 return nullptr;
12554
Alexey Bataev931e19b2017-10-02 16:32:39 +000012555 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000012556 OpenMPDirectiveKind CaptureRegion =
12557 getOpenMPCaptureRegionForClause(DKind, OMPC_device);
12558 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000012559 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000012560 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev931e19b2017-10-02 16:32:39 +000012561 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
12562 HelperValStmt = buildPreInits(Context, Captures);
12563 }
12564
Alexey Bataev8451efa2018-01-15 19:06:12 +000012565 return new (Context) OMPDeviceClause(ValExpr, HelperValStmt, CaptureRegion,
12566 StartLoc, LParenLoc, EndLoc);
Michael Wonge710d542015-08-07 16:16:36 +000012567}
Kelvin Li0bff7af2015-11-23 05:32:03 +000012568
Alexey Bataeve3727102018-04-18 15:57:46 +000012569static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
Alexey Bataev95c23e72018-02-27 21:31:11 +000012570 DSAStackTy *Stack, QualType QTy,
12571 bool FullCheck = true) {
Kelvin Li0bff7af2015-11-23 05:32:03 +000012572 NamedDecl *ND;
12573 if (QTy->isIncompleteType(&ND)) {
12574 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
12575 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +000012576 }
Alexey Bataev95c23e72018-02-27 21:31:11 +000012577 if (FullCheck && !SemaRef.CurContext->isDependentContext() &&
12578 !QTy.isTrivialType(SemaRef.Context))
12579 SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR;
Kelvin Li0bff7af2015-11-23 05:32:03 +000012580 return true;
12581}
12582
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000012583/// Return true if it can be proven that the provided array expression
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012584/// (array section or array subscript) does NOT specify the whole size of the
12585/// array whose base type is \a BaseQTy.
Alexey Bataeve3727102018-04-18 15:57:46 +000012586static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012587 const Expr *E,
12588 QualType BaseQTy) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012589 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012590
12591 // If this is an array subscript, it refers to the whole size if the size of
12592 // the dimension is constant and equals 1. Also, an array section assumes the
12593 // format of an array subscript if no colon is used.
12594 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012595 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012596 return ATy->getSize().getSExtValue() != 1;
12597 // Size can't be evaluated statically.
12598 return false;
12599 }
12600
12601 assert(OASE && "Expecting array section if not an array subscript.");
Alexey Bataeve3727102018-04-18 15:57:46 +000012602 const Expr *LowerBound = OASE->getLowerBound();
12603 const Expr *Length = OASE->getLength();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012604
12605 // If there is a lower bound that does not evaluates to zero, we are not
David Majnemer9d168222016-08-05 17:44:54 +000012606 // covering the whole dimension.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012607 if (LowerBound) {
Fangrui Song407659a2018-11-30 23:41:18 +000012608 Expr::EvalResult Result;
12609 if (!LowerBound->EvaluateAsInt(Result, SemaRef.getASTContext()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012610 return false; // Can't get the integer value as a constant.
Fangrui Song407659a2018-11-30 23:41:18 +000012611
12612 llvm::APSInt ConstLowerBound = Result.Val.getInt();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012613 if (ConstLowerBound.getSExtValue())
12614 return true;
12615 }
12616
12617 // If we don't have a length we covering the whole dimension.
12618 if (!Length)
12619 return false;
12620
12621 // If the base is a pointer, we don't have a way to get the size of the
12622 // pointee.
12623 if (BaseQTy->isPointerType())
12624 return false;
12625
12626 // We can only check if the length is the same as the size of the dimension
12627 // if we have a constant array.
Alexey Bataeve3727102018-04-18 15:57:46 +000012628 const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012629 if (!CATy)
12630 return false;
12631
Fangrui Song407659a2018-11-30 23:41:18 +000012632 Expr::EvalResult Result;
12633 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012634 return false; // Can't get the integer value as a constant.
12635
Fangrui Song407659a2018-11-30 23:41:18 +000012636 llvm::APSInt ConstLength = Result.Val.getInt();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012637 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
12638}
12639
12640// Return true if it can be proven that the provided array expression (array
12641// section or array subscript) does NOT specify a single element of the array
12642// whose base type is \a BaseQTy.
Alexey Bataeve3727102018-04-18 15:57:46 +000012643static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
David Majnemer9d168222016-08-05 17:44:54 +000012644 const Expr *E,
12645 QualType BaseQTy) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012646 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012647
12648 // An array subscript always refer to a single element. Also, an array section
12649 // assumes the format of an array subscript if no colon is used.
12650 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
12651 return false;
12652
12653 assert(OASE && "Expecting array section if not an array subscript.");
Alexey Bataeve3727102018-04-18 15:57:46 +000012654 const Expr *Length = OASE->getLength();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012655
12656 // If we don't have a length we have to check if the array has unitary size
12657 // for this dimension. Also, we should always expect a length if the base type
12658 // is pointer.
12659 if (!Length) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012660 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012661 return ATy->getSize().getSExtValue() != 1;
12662 // We cannot assume anything.
12663 return false;
12664 }
12665
12666 // Check if the length evaluates to 1.
Fangrui Song407659a2018-11-30 23:41:18 +000012667 Expr::EvalResult Result;
12668 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012669 return false; // Can't get the integer value as a constant.
12670
Fangrui Song407659a2018-11-30 23:41:18 +000012671 llvm::APSInt ConstLength = Result.Val.getInt();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012672 return ConstLength.getSExtValue() != 1;
12673}
12674
Samuel Antao661c0902016-05-26 17:39:58 +000012675// Return the expression of the base of the mappable expression or null if it
12676// cannot be determined and do all the necessary checks to see if the expression
12677// is valid as a standalone mappable expression. In the process, record all the
Samuel Antao90927002016-04-26 14:54:23 +000012678// components of the expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000012679static const Expr *checkMapClauseExpressionBase(
Samuel Antao90927002016-04-26 14:54:23 +000012680 Sema &SemaRef, Expr *E,
Samuel Antao661c0902016-05-26 17:39:58 +000012681 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012682 OpenMPClauseKind CKind, bool NoDiagnose) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012683 SourceLocation ELoc = E->getExprLoc();
12684 SourceRange ERange = E->getSourceRange();
12685
12686 // The base of elements of list in a map clause have to be either:
12687 // - a reference to variable or field.
12688 // - a member expression.
12689 // - an array expression.
12690 //
12691 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
12692 // reference to 'r'.
12693 //
12694 // If we have:
12695 //
12696 // struct SS {
12697 // Bla S;
12698 // foo() {
12699 // #pragma omp target map (S.Arr[:12]);
12700 // }
12701 // }
12702 //
12703 // We want to retrieve the member expression 'this->S';
12704
Alexey Bataeve3727102018-04-18 15:57:46 +000012705 const Expr *RelevantExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012706
Samuel Antao5de996e2016-01-22 20:21:36 +000012707 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
12708 // If a list item is an array section, it must specify contiguous storage.
12709 //
12710 // For this restriction it is sufficient that we make sure only references
12711 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012712 // exist except in the rightmost expression (unless they cover the whole
12713 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +000012714 //
12715 // r.ArrS[3:5].Arr[6:7]
12716 //
12717 // r.ArrS[3:5].x
12718 //
12719 // but these would be valid:
12720 // r.ArrS[3].Arr[6:7]
12721 //
12722 // r.ArrS[3].x
12723
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012724 bool AllowUnitySizeArraySection = true;
12725 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +000012726
Dmitry Polukhin644a9252016-03-11 07:58:34 +000012727 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012728 E = E->IgnoreParenImpCasts();
12729
12730 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
12731 if (!isa<VarDecl>(CurE->getDecl()))
Alexey Bataev27041fa2017-12-05 15:22:49 +000012732 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012733
12734 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012735
12736 // If we got a reference to a declaration, we should not expect any array
12737 // section before that.
12738 AllowUnitySizeArraySection = false;
12739 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000012740
12741 // Record the component.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012742 CurComponents.emplace_back(CurE, CurE->getDecl());
12743 } else if (auto *CurE = dyn_cast<MemberExpr>(E)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012744 Expr *BaseE = CurE->getBase()->IgnoreParenImpCasts();
Samuel Antao5de996e2016-01-22 20:21:36 +000012745
12746 if (isa<CXXThisExpr>(BaseE))
12747 // We found a base expression: this->Val.
12748 RelevantExpr = CurE;
12749 else
12750 E = BaseE;
12751
12752 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012753 if (!NoDiagnose) {
12754 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
12755 << CurE->getSourceRange();
12756 return nullptr;
12757 }
12758 if (RelevantExpr)
12759 return nullptr;
12760 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000012761 }
12762
12763 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
12764
12765 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
12766 // A bit-field cannot appear in a map clause.
12767 //
12768 if (FD->isBitField()) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012769 if (!NoDiagnose) {
12770 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
12771 << CurE->getSourceRange() << getOpenMPClauseName(CKind);
12772 return nullptr;
12773 }
12774 if (RelevantExpr)
12775 return nullptr;
12776 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000012777 }
12778
12779 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12780 // If the type of a list item is a reference to a type T then the type
12781 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012782 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000012783
12784 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
12785 // A list item cannot be a variable that is a member of a structure with
12786 // a union type.
12787 //
Alexey Bataeve3727102018-04-18 15:57:46 +000012788 if (CurType->isUnionType()) {
12789 if (!NoDiagnose) {
12790 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
12791 << CurE->getSourceRange();
12792 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012793 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012794 continue;
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012795 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012796
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012797 // If we got a member expression, we should not expect any array section
12798 // before that:
12799 //
12800 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
12801 // If a list item is an element of a structure, only the rightmost symbol
12802 // of the variable reference can be an array section.
12803 //
12804 AllowUnitySizeArraySection = false;
12805 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000012806
12807 // Record the component.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012808 CurComponents.emplace_back(CurE, FD);
12809 } else if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012810 E = CurE->getBase()->IgnoreParenImpCasts();
12811
12812 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012813 if (!NoDiagnose) {
12814 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
12815 << 0 << CurE->getSourceRange();
12816 return nullptr;
12817 }
12818 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000012819 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012820
12821 // If we got an array subscript that express the whole dimension we
12822 // can have any array expressions before. If it only expressing part of
12823 // the dimension, we can only have unitary-size array expressions.
Alexey Bataeve3727102018-04-18 15:57:46 +000012824 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012825 E->getType()))
12826 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000012827
Patrick Lystere13b1e32019-01-02 19:28:48 +000012828 if (const auto *TE = dyn_cast<CXXThisExpr>(E)) {
12829 Expr::EvalResult Result;
12830 if (CurE->getIdx()->EvaluateAsInt(Result, SemaRef.getASTContext())) {
12831 if (!Result.Val.getInt().isNullValue()) {
12832 SemaRef.Diag(CurE->getIdx()->getExprLoc(),
12833 diag::err_omp_invalid_map_this_expr);
12834 SemaRef.Diag(CurE->getIdx()->getExprLoc(),
12835 diag::note_omp_invalid_subscript_on_this_ptr_map);
12836 }
12837 }
12838 RelevantExpr = TE;
12839 }
12840
Samuel Antao90927002016-04-26 14:54:23 +000012841 // Record the component - we don't have any declaration associated.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012842 CurComponents.emplace_back(CurE, nullptr);
12843 } else if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012844 assert(!NoDiagnose && "Array sections cannot be implicitly mapped.");
Samuel Antao5de996e2016-01-22 20:21:36 +000012845 E = CurE->getBase()->IgnoreParenImpCasts();
12846
Alexey Bataev27041fa2017-12-05 15:22:49 +000012847 QualType CurType =
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012848 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
12849
Samuel Antao5de996e2016-01-22 20:21:36 +000012850 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12851 // If the type of a list item is a reference to a type T then the type
12852 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +000012853 if (CurType->isReferenceType())
12854 CurType = CurType->getPointeeType();
12855
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012856 bool IsPointer = CurType->isAnyPointerType();
12857
12858 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012859 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
12860 << 0 << CurE->getSourceRange();
Alexey Bataev27041fa2017-12-05 15:22:49 +000012861 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012862 }
12863
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012864 bool NotWhole =
Alexey Bataeve3727102018-04-18 15:57:46 +000012865 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012866 bool NotUnity =
Alexey Bataeve3727102018-04-18 15:57:46 +000012867 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012868
Samuel Antaodab51bb2016-07-18 23:22:11 +000012869 if (AllowWholeSizeArraySection) {
12870 // Any array section is currently allowed. Allowing a whole size array
12871 // section implies allowing a unity array section as well.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012872 //
12873 // If this array section refers to the whole dimension we can still
12874 // accept other array sections before this one, except if the base is a
12875 // pointer. Otherwise, only unitary sections are accepted.
12876 if (NotWhole || IsPointer)
12877 AllowWholeSizeArraySection = false;
Samuel Antaodab51bb2016-07-18 23:22:11 +000012878 } else if (AllowUnitySizeArraySection && NotUnity) {
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012879 // A unity or whole array section is not allowed and that is not
12880 // compatible with the properties of the current array section.
12881 SemaRef.Diag(
12882 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
12883 << CurE->getSourceRange();
Alexey Bataev27041fa2017-12-05 15:22:49 +000012884 return nullptr;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012885 }
Samuel Antao90927002016-04-26 14:54:23 +000012886
Patrick Lystere13b1e32019-01-02 19:28:48 +000012887 if (const auto *TE = dyn_cast<CXXThisExpr>(E)) {
12888 Expr::EvalResult ResultR;
12889 Expr::EvalResult ResultL;
12890 if (CurE->getLength()->EvaluateAsInt(ResultR,
12891 SemaRef.getASTContext())) {
12892 if (!ResultR.Val.getInt().isOneValue()) {
12893 SemaRef.Diag(CurE->getLength()->getExprLoc(),
12894 diag::err_omp_invalid_map_this_expr);
12895 SemaRef.Diag(CurE->getLength()->getExprLoc(),
12896 diag::note_omp_invalid_length_on_this_ptr_mapping);
12897 }
12898 }
12899 if (CurE->getLowerBound() && CurE->getLowerBound()->EvaluateAsInt(
12900 ResultL, SemaRef.getASTContext())) {
12901 if (!ResultL.Val.getInt().isNullValue()) {
12902 SemaRef.Diag(CurE->getLowerBound()->getExprLoc(),
12903 diag::err_omp_invalid_map_this_expr);
12904 SemaRef.Diag(CurE->getLowerBound()->getExprLoc(),
12905 diag::note_omp_invalid_lower_bound_on_this_ptr_mapping);
12906 }
12907 }
12908 RelevantExpr = TE;
12909 }
12910
Samuel Antao90927002016-04-26 14:54:23 +000012911 // Record the component - we don't have any declaration associated.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012912 CurComponents.emplace_back(CurE, nullptr);
12913 } else {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012914 if (!NoDiagnose) {
12915 // If nothing else worked, this is not a valid map clause expression.
12916 SemaRef.Diag(
12917 ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
12918 << ERange;
12919 }
Alexey Bataev27041fa2017-12-05 15:22:49 +000012920 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012921 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012922 }
12923
12924 return RelevantExpr;
12925}
12926
12927// Return true if expression E associated with value VD has conflicts with other
12928// map information.
Alexey Bataeve3727102018-04-18 15:57:46 +000012929static bool checkMapConflicts(
12930 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E,
Samuel Antao90927002016-04-26 14:54:23 +000012931 bool CurrentRegionOnly,
Samuel Antao661c0902016-05-26 17:39:58 +000012932 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
12933 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012934 assert(VD && E);
Samuel Antao5de996e2016-01-22 20:21:36 +000012935 SourceLocation ELoc = E->getExprLoc();
12936 SourceRange ERange = E->getSourceRange();
12937
12938 // In order to easily check the conflicts we need to match each component of
12939 // the expression under test with the components of the expressions that are
12940 // already in the stack.
12941
Samuel Antao5de996e2016-01-22 20:21:36 +000012942 assert(!CurComponents.empty() && "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000012943 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000012944 "Map clause expression with unexpected base!");
12945
12946 // Variables to help detecting enclosing problems in data environment nests.
12947 bool IsEnclosedByDataEnvironmentExpr = false;
Samuel Antao90927002016-04-26 14:54:23 +000012948 const Expr *EnclosingExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012949
Samuel Antao90927002016-04-26 14:54:23 +000012950 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
12951 VD, CurrentRegionOnly,
Alexey Bataeve3727102018-04-18 15:57:46 +000012952 [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc,
12953 ERange, CKind, &EnclosingExpr,
12954 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef
12955 StackComponents,
12956 OpenMPClauseKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012957 assert(!StackComponents.empty() &&
12958 "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000012959 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000012960 "Map clause expression with unexpected base!");
Fangrui Song16fe49a2018-04-18 19:32:01 +000012961 (void)VD;
Samuel Antao5de996e2016-01-22 20:21:36 +000012962
Samuel Antao90927002016-04-26 14:54:23 +000012963 // The whole expression in the stack.
Alexey Bataeve3727102018-04-18 15:57:46 +000012964 const Expr *RE = StackComponents.front().getAssociatedExpression();
Samuel Antao90927002016-04-26 14:54:23 +000012965
Samuel Antao5de996e2016-01-22 20:21:36 +000012966 // Expressions must start from the same base. Here we detect at which
12967 // point both expressions diverge from each other and see if we can
12968 // detect if the memory referred to both expressions is contiguous and
12969 // do not overlap.
12970 auto CI = CurComponents.rbegin();
12971 auto CE = CurComponents.rend();
12972 auto SI = StackComponents.rbegin();
12973 auto SE = StackComponents.rend();
12974 for (; CI != CE && SI != SE; ++CI, ++SI) {
12975
12976 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
12977 // At most one list item can be an array item derived from a given
12978 // variable in map clauses of the same construct.
Samuel Antao90927002016-04-26 14:54:23 +000012979 if (CurrentRegionOnly &&
12980 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
12981 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
12982 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
12983 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
12984 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
Samuel Antao5de996e2016-01-22 20:21:36 +000012985 diag::err_omp_multiple_array_items_in_map_clause)
Samuel Antao90927002016-04-26 14:54:23 +000012986 << CI->getAssociatedExpression()->getSourceRange();
12987 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
12988 diag::note_used_here)
12989 << SI->getAssociatedExpression()->getSourceRange();
Samuel Antao5de996e2016-01-22 20:21:36 +000012990 return true;
12991 }
12992
12993 // Do both expressions have the same kind?
Samuel Antao90927002016-04-26 14:54:23 +000012994 if (CI->getAssociatedExpression()->getStmtClass() !=
12995 SI->getAssociatedExpression()->getStmtClass())
Samuel Antao5de996e2016-01-22 20:21:36 +000012996 break;
12997
12998 // Are we dealing with different variables/fields?
Samuel Antao90927002016-04-26 14:54:23 +000012999 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
Samuel Antao5de996e2016-01-22 20:21:36 +000013000 break;
13001 }
Kelvin Li9f645ae2016-07-18 22:49:16 +000013002 // Check if the extra components of the expressions in the enclosing
13003 // data environment are redundant for the current base declaration.
13004 // If they are, the maps completely overlap, which is legal.
13005 for (; SI != SE; ++SI) {
13006 QualType Type;
Alexey Bataeve3727102018-04-18 15:57:46 +000013007 if (const auto *ASE =
David Majnemer9d168222016-08-05 17:44:54 +000013008 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
Kelvin Li9f645ae2016-07-18 22:49:16 +000013009 Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
Alexey Bataeve3727102018-04-18 15:57:46 +000013010 } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(
David Majnemer9d168222016-08-05 17:44:54 +000013011 SI->getAssociatedExpression())) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013012 const Expr *E = OASE->getBase()->IgnoreParenImpCasts();
Kelvin Li9f645ae2016-07-18 22:49:16 +000013013 Type =
13014 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
13015 }
13016 if (Type.isNull() || Type->isAnyPointerType() ||
Alexey Bataeve3727102018-04-18 15:57:46 +000013017 checkArrayExpressionDoesNotReferToWholeSize(
Kelvin Li9f645ae2016-07-18 22:49:16 +000013018 SemaRef, SI->getAssociatedExpression(), Type))
13019 break;
13020 }
Samuel Antao5de996e2016-01-22 20:21:36 +000013021
13022 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
13023 // List items of map clauses in the same construct must not share
13024 // original storage.
13025 //
13026 // If the expressions are exactly the same or one is a subset of the
13027 // other, it means they are sharing storage.
13028 if (CI == CE && SI == SE) {
13029 if (CurrentRegionOnly) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013030 if (CKind == OMPC_map) {
Samuel Antao661c0902016-05-26 17:39:58 +000013031 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +000013032 } else {
Samuel Antaoec172c62016-05-26 17:49:04 +000013033 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000013034 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
13035 << ERange;
13036 }
Samuel Antao5de996e2016-01-22 20:21:36 +000013037 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
13038 << RE->getSourceRange();
13039 return true;
Samuel Antao5de996e2016-01-22 20:21:36 +000013040 }
Alexey Bataeve3727102018-04-18 15:57:46 +000013041 // If we find the same expression in the enclosing data environment,
13042 // that is legal.
13043 IsEnclosedByDataEnvironmentExpr = true;
13044 return false;
Samuel Antao5de996e2016-01-22 20:21:36 +000013045 }
13046
Samuel Antao90927002016-04-26 14:54:23 +000013047 QualType DerivedType =
13048 std::prev(CI)->getAssociatedDeclaration()->getType();
13049 SourceLocation DerivedLoc =
13050 std::prev(CI)->getAssociatedExpression()->getExprLoc();
Samuel Antao5de996e2016-01-22 20:21:36 +000013051
13052 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
13053 // If the type of a list item is a reference to a type T then the type
13054 // will be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000013055 DerivedType = DerivedType.getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000013056
13057 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
13058 // A variable for which the type is pointer and an array section
13059 // derived from that variable must not appear as list items of map
13060 // clauses of the same construct.
13061 //
13062 // Also, cover one of the cases in:
13063 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
13064 // If any part of the original storage of a list item has corresponding
13065 // storage in the device data environment, all of the original storage
13066 // must have corresponding storage in the device data environment.
13067 //
13068 if (DerivedType->isAnyPointerType()) {
13069 if (CI == CE || SI == SE) {
13070 SemaRef.Diag(
13071 DerivedLoc,
13072 diag::err_omp_pointer_mapped_along_with_derived_section)
13073 << DerivedLoc;
Alexey Bataev2819260b2018-02-27 17:42:00 +000013074 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
13075 << RE->getSourceRange();
13076 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +000013077 }
13078 if (CI->getAssociatedExpression()->getStmtClass() !=
Alexey Bataev2819260b2018-02-27 17:42:00 +000013079 SI->getAssociatedExpression()->getStmtClass() ||
13080 CI->getAssociatedDeclaration()->getCanonicalDecl() ==
13081 SI->getAssociatedDeclaration()->getCanonicalDecl()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000013082 assert(CI != CE && SI != SE);
Alexey Bataev2819260b2018-02-27 17:42:00 +000013083 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced)
Samuel Antao5de996e2016-01-22 20:21:36 +000013084 << DerivedLoc;
Alexey Bataev2819260b2018-02-27 17:42:00 +000013085 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
13086 << RE->getSourceRange();
13087 return true;
Samuel Antao5de996e2016-01-22 20:21:36 +000013088 }
Samuel Antao5de996e2016-01-22 20:21:36 +000013089 }
13090
13091 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
13092 // List items of map clauses in the same construct must not share
13093 // original storage.
13094 //
13095 // An expression is a subset of the other.
13096 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013097 if (CKind == OMPC_map) {
Alexey Bataeve82445f2018-09-20 13:54:02 +000013098 if (CI != CE || SI != SE) {
13099 // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is
13100 // a pointer.
13101 auto Begin =
13102 CI != CE ? CurComponents.begin() : StackComponents.begin();
13103 auto End = CI != CE ? CurComponents.end() : StackComponents.end();
13104 auto It = Begin;
13105 while (It != End && !It->getAssociatedDeclaration())
13106 std::advance(It, 1);
13107 assert(It != End &&
13108 "Expected at least one component with the declaration.");
13109 if (It != Begin && It->getAssociatedDeclaration()
13110 ->getType()
13111 .getCanonicalType()
13112 ->isAnyPointerType()) {
13113 IsEnclosedByDataEnvironmentExpr = false;
13114 EnclosingExpr = nullptr;
13115 return false;
13116 }
13117 }
Samuel Antao661c0902016-05-26 17:39:58 +000013118 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +000013119 } else {
Samuel Antaoec172c62016-05-26 17:49:04 +000013120 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000013121 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
13122 << ERange;
13123 }
Samuel Antao5de996e2016-01-22 20:21:36 +000013124 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
13125 << RE->getSourceRange();
13126 return true;
13127 }
13128
13129 // The current expression uses the same base as other expression in the
Samuel Antao90927002016-04-26 14:54:23 +000013130 // data environment but does not contain it completely.
Samuel Antao5de996e2016-01-22 20:21:36 +000013131 if (!CurrentRegionOnly && SI != SE)
13132 EnclosingExpr = RE;
13133
13134 // The current expression is a subset of the expression in the data
13135 // environment.
13136 IsEnclosedByDataEnvironmentExpr |=
13137 (!CurrentRegionOnly && CI != CE && SI == SE);
13138
13139 return false;
13140 });
13141
13142 if (CurrentRegionOnly)
13143 return FoundError;
13144
13145 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
13146 // If any part of the original storage of a list item has corresponding
13147 // storage in the device data environment, all of the original storage must
13148 // have corresponding storage in the device data environment.
13149 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
13150 // If a list item is an element of a structure, and a different element of
13151 // the structure has a corresponding list item in the device data environment
13152 // prior to a task encountering the construct associated with the map clause,
Samuel Antao90927002016-04-26 14:54:23 +000013153 // then the list item must also have a corresponding list item in the device
Samuel Antao5de996e2016-01-22 20:21:36 +000013154 // data environment prior to the task encountering the construct.
13155 //
13156 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
13157 SemaRef.Diag(ELoc,
13158 diag::err_omp_original_storage_is_shared_and_does_not_contain)
13159 << ERange;
13160 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
13161 << EnclosingExpr->getSourceRange();
13162 return true;
13163 }
13164
13165 return FoundError;
13166}
13167
Michael Kruse4304e9d2019-02-19 16:38:20 +000013168// Look up the user-defined mapper given the mapper name and mapped type, and
13169// build a reference to it.
13170ExprResult buildUserDefinedMapperRef(Sema &SemaRef, Scope *S,
13171 CXXScopeSpec &MapperIdScopeSpec,
13172 const DeclarationNameInfo &MapperId,
13173 QualType Type, Expr *UnresolvedMapper) {
13174 if (MapperIdScopeSpec.isInvalid())
13175 return ExprError();
13176 // Find all user-defined mappers with the given MapperId.
13177 SmallVector<UnresolvedSet<8>, 4> Lookups;
13178 LookupResult Lookup(SemaRef, MapperId, Sema::LookupOMPMapperName);
13179 Lookup.suppressDiagnostics();
13180 if (S) {
13181 while (S && SemaRef.LookupParsedName(Lookup, S, &MapperIdScopeSpec)) {
13182 NamedDecl *D = Lookup.getRepresentativeDecl();
13183 while (S && !S->isDeclScope(D))
13184 S = S->getParent();
13185 if (S)
13186 S = S->getParent();
13187 Lookups.emplace_back();
13188 Lookups.back().append(Lookup.begin(), Lookup.end());
13189 Lookup.clear();
13190 }
13191 } else if (auto *ULE = cast_or_null<UnresolvedLookupExpr>(UnresolvedMapper)) {
13192 // Extract the user-defined mappers with the given MapperId.
13193 Lookups.push_back(UnresolvedSet<8>());
13194 for (NamedDecl *D : ULE->decls()) {
13195 auto *DMD = cast<OMPDeclareMapperDecl>(D);
13196 assert(DMD && "Expect valid OMPDeclareMapperDecl during instantiation.");
13197 Lookups.back().addDecl(DMD);
13198 }
13199 }
13200 // Defer the lookup for dependent types. The results will be passed through
13201 // UnresolvedMapper on instantiation.
13202 if (SemaRef.CurContext->isDependentContext() || Type->isDependentType() ||
13203 Type->isInstantiationDependentType() ||
13204 Type->containsUnexpandedParameterPack() ||
13205 filterLookupForUDReductionAndMapper<bool>(Lookups, [](ValueDecl *D) {
13206 return !D->isInvalidDecl() &&
13207 (D->getType()->isDependentType() ||
13208 D->getType()->isInstantiationDependentType() ||
13209 D->getType()->containsUnexpandedParameterPack());
13210 })) {
13211 UnresolvedSet<8> URS;
13212 for (const UnresolvedSet<8> &Set : Lookups) {
13213 if (Set.empty())
13214 continue;
13215 URS.append(Set.begin(), Set.end());
13216 }
13217 return UnresolvedLookupExpr::Create(
13218 SemaRef.Context, /*NamingClass=*/nullptr,
13219 MapperIdScopeSpec.getWithLocInContext(SemaRef.Context), MapperId,
13220 /*ADL=*/false, /*Overloaded=*/true, URS.begin(), URS.end());
13221 }
13222 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
13223 // The type must be of struct, union or class type in C and C++
13224 if (!Type->isStructureOrClassType() && !Type->isUnionType())
13225 return ExprEmpty();
13226 SourceLocation Loc = MapperId.getLoc();
13227 // Perform argument dependent lookup.
13228 if (SemaRef.getLangOpts().CPlusPlus && !MapperIdScopeSpec.isSet())
13229 argumentDependentLookup(SemaRef, MapperId, Loc, Type, Lookups);
13230 // Return the first user-defined mapper with the desired type.
13231 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
13232 Lookups, [&SemaRef, Type](ValueDecl *D) -> ValueDecl * {
13233 if (!D->isInvalidDecl() &&
13234 SemaRef.Context.hasSameType(D->getType(), Type))
13235 return D;
13236 return nullptr;
13237 }))
13238 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc);
13239 // Find the first user-defined mapper with a type derived from the desired
13240 // type.
13241 if (auto *VD = filterLookupForUDReductionAndMapper<ValueDecl *>(
13242 Lookups, [&SemaRef, Type, Loc](ValueDecl *D) -> ValueDecl * {
13243 if (!D->isInvalidDecl() &&
13244 SemaRef.IsDerivedFrom(Loc, Type, D->getType()) &&
13245 !Type.isMoreQualifiedThan(D->getType()))
13246 return D;
13247 return nullptr;
13248 })) {
13249 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
13250 /*DetectVirtual=*/false);
13251 if (SemaRef.IsDerivedFrom(Loc, Type, VD->getType(), Paths)) {
13252 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
13253 VD->getType().getUnqualifiedType()))) {
13254 if (SemaRef.CheckBaseClassAccess(
13255 Loc, VD->getType(), Type, Paths.front(),
13256 /*DiagID=*/0) != Sema::AR_inaccessible) {
13257 return SemaRef.BuildDeclRefExpr(VD, Type, VK_LValue, Loc);
13258 }
13259 }
13260 }
13261 }
13262 // Report error if a mapper is specified, but cannot be found.
13263 if (MapperIdScopeSpec.isSet() || MapperId.getAsString() != "default") {
13264 SemaRef.Diag(Loc, diag::err_omp_invalid_mapper)
13265 << Type << MapperId.getName();
13266 return ExprError();
13267 }
13268 return ExprEmpty();
13269}
13270
Samuel Antao661c0902016-05-26 17:39:58 +000013271namespace {
13272// Utility struct that gathers all the related lists associated with a mappable
13273// expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000013274struct MappableVarListInfo {
Samuel Antao661c0902016-05-26 17:39:58 +000013275 // The list of expressions.
13276 ArrayRef<Expr *> VarList;
13277 // The list of processed expressions.
13278 SmallVector<Expr *, 16> ProcessedVarList;
13279 // The mappble components for each expression.
13280 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
13281 // The base declaration of the variable.
13282 SmallVector<ValueDecl *, 16> VarBaseDeclarations;
Michael Kruse4304e9d2019-02-19 16:38:20 +000013283 // The reference to the user-defined mapper associated with every expression.
13284 SmallVector<Expr *, 16> UDMapperList;
Samuel Antao661c0902016-05-26 17:39:58 +000013285
13286 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
13287 // We have a list of components and base declarations for each entry in the
13288 // variable list.
13289 VarComponents.reserve(VarList.size());
13290 VarBaseDeclarations.reserve(VarList.size());
13291 }
13292};
13293}
13294
13295// Check the validity of the provided variable list for the provided clause kind
Michael Kruse4304e9d2019-02-19 16:38:20 +000013296// \a CKind. In the check process the valid expressions, mappable expression
13297// components, variables, and user-defined mappers are extracted and used to
13298// fill \a ProcessedVarList, \a VarComponents, \a VarBaseDeclarations, and \a
13299// UDMapperList in MVLI. \a MapType, \a IsMapTypeImplicit, \a MapperIdScopeSpec,
13300// and \a MapperId are expected to be valid if the clause kind is 'map'.
13301static void checkMappableExpressionList(
13302 Sema &SemaRef, DSAStackTy *DSAS, OpenMPClauseKind CKind,
13303 MappableVarListInfo &MVLI, SourceLocation StartLoc,
Michael Kruse01f670d2019-02-22 22:29:42 +000013304 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo MapperId,
13305 ArrayRef<Expr *> UnresolvedMappers,
Michael Kruse4304e9d2019-02-19 16:38:20 +000013306 OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
Michael Kruse01f670d2019-02-22 22:29:42 +000013307 bool IsMapTypeImplicit = false) {
Samuel Antaoec172c62016-05-26 17:49:04 +000013308 // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
13309 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
Samuel Antao661c0902016-05-26 17:39:58 +000013310 "Unexpected clause kind with mappable expressions!");
Michael Kruse01f670d2019-02-22 22:29:42 +000013311
13312 // If the identifier of user-defined mapper is not specified, it is "default".
13313 // We do not change the actual name in this clause to distinguish whether a
13314 // mapper is specified explicitly, i.e., it is not explicitly specified when
13315 // MapperId.getName() is empty.
13316 if (!MapperId.getName() || MapperId.getName().isEmpty()) {
13317 auto &DeclNames = SemaRef.getASTContext().DeclarationNames;
13318 MapperId.setName(DeclNames.getIdentifier(
13319 &SemaRef.getASTContext().Idents.get("default")));
13320 }
Michael Kruse4304e9d2019-02-19 16:38:20 +000013321
13322 // Iterators to find the current unresolved mapper expression.
13323 auto UMIt = UnresolvedMappers.begin(), UMEnd = UnresolvedMappers.end();
13324 bool UpdateUMIt = false;
13325 Expr *UnresolvedMapper = nullptr;
Kelvin Li0bff7af2015-11-23 05:32:03 +000013326
Samuel Antao90927002016-04-26 14:54:23 +000013327 // Keep track of the mappable components and base declarations in this clause.
13328 // Each entry in the list is going to have a list of components associated. We
13329 // record each set of the components so that we can build the clause later on.
13330 // In the end we should have the same amount of declarations and component
13331 // lists.
Samuel Antao90927002016-04-26 14:54:23 +000013332
Alexey Bataeve3727102018-04-18 15:57:46 +000013333 for (Expr *RE : MVLI.VarList) {
Samuel Antaoec172c62016-05-26 17:49:04 +000013334 assert(RE && "Null expr in omp to/from/map clause");
Kelvin Li0bff7af2015-11-23 05:32:03 +000013335 SourceLocation ELoc = RE->getExprLoc();
13336
Michael Kruse4304e9d2019-02-19 16:38:20 +000013337 // Find the current unresolved mapper expression.
13338 if (UpdateUMIt && UMIt != UMEnd) {
13339 UMIt++;
13340 assert(
13341 UMIt != UMEnd &&
13342 "Expect the size of UnresolvedMappers to match with that of VarList");
13343 }
13344 UpdateUMIt = true;
13345 if (UMIt != UMEnd)
13346 UnresolvedMapper = *UMIt;
13347
Alexey Bataeve3727102018-04-18 15:57:46 +000013348 const Expr *VE = RE->IgnoreParenLValueCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000013349
13350 if (VE->isValueDependent() || VE->isTypeDependent() ||
13351 VE->isInstantiationDependent() ||
13352 VE->containsUnexpandedParameterPack()) {
Michael Kruse0336c752019-02-25 20:34:15 +000013353 // Try to find the associated user-defined mapper.
13354 ExprResult ER = buildUserDefinedMapperRef(
13355 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
13356 VE->getType().getCanonicalType(), UnresolvedMapper);
13357 if (ER.isInvalid())
13358 continue;
13359 MVLI.UDMapperList.push_back(ER.get());
Samuel Antao5de996e2016-01-22 20:21:36 +000013360 // We can only analyze this information once the missing information is
13361 // resolved.
Samuel Antao661c0902016-05-26 17:39:58 +000013362 MVLI.ProcessedVarList.push_back(RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +000013363 continue;
13364 }
13365
Alexey Bataeve3727102018-04-18 15:57:46 +000013366 Expr *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000013367
Samuel Antao5de996e2016-01-22 20:21:36 +000013368 if (!RE->IgnoreParenImpCasts()->isLValue()) {
Samuel Antao661c0902016-05-26 17:39:58 +000013369 SemaRef.Diag(ELoc,
13370 diag::err_omp_expected_named_var_member_or_array_expression)
Samuel Antao5de996e2016-01-22 20:21:36 +000013371 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000013372 continue;
13373 }
13374
Samuel Antao90927002016-04-26 14:54:23 +000013375 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
13376 ValueDecl *CurDeclaration = nullptr;
13377
13378 // Obtain the array or member expression bases if required. Also, fill the
13379 // components array with all the components identified in the process.
Alexey Bataeve3727102018-04-18 15:57:46 +000013380 const Expr *BE = checkMapClauseExpressionBase(
13381 SemaRef, SimpleExpr, CurComponents, CKind, /*NoDiagnose=*/false);
Samuel Antao5de996e2016-01-22 20:21:36 +000013382 if (!BE)
13383 continue;
13384
Samuel Antao90927002016-04-26 14:54:23 +000013385 assert(!CurComponents.empty() &&
13386 "Invalid mappable expression information.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000013387
Patrick Lystere13b1e32019-01-02 19:28:48 +000013388 if (const auto *TE = dyn_cast<CXXThisExpr>(BE)) {
13389 // Add store "this" pointer to class in DSAStackTy for future checking
13390 DSAS->addMappedClassesQualTypes(TE->getType());
Michael Kruse0336c752019-02-25 20:34:15 +000013391 // Try to find the associated user-defined mapper.
13392 ExprResult ER = buildUserDefinedMapperRef(
13393 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
13394 VE->getType().getCanonicalType(), UnresolvedMapper);
13395 if (ER.isInvalid())
13396 continue;
13397 MVLI.UDMapperList.push_back(ER.get());
Patrick Lystere13b1e32019-01-02 19:28:48 +000013398 // Skip restriction checking for variable or field declarations
13399 MVLI.ProcessedVarList.push_back(RE);
13400 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13401 MVLI.VarComponents.back().append(CurComponents.begin(),
13402 CurComponents.end());
13403 MVLI.VarBaseDeclarations.push_back(nullptr);
13404 continue;
13405 }
13406
Samuel Antao90927002016-04-26 14:54:23 +000013407 // For the following checks, we rely on the base declaration which is
13408 // expected to be associated with the last component. The declaration is
13409 // expected to be a variable or a field (if 'this' is being mapped).
13410 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
13411 assert(CurDeclaration && "Null decl on map clause.");
13412 assert(
13413 CurDeclaration->isCanonicalDecl() &&
13414 "Expecting components to have associated only canonical declarations.");
13415
13416 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
Alexey Bataeve3727102018-04-18 15:57:46 +000013417 const auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
Samuel Antao5de996e2016-01-22 20:21:36 +000013418
13419 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000013420 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000013421
13422 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
Samuel Antao661c0902016-05-26 17:39:58 +000013423 // threadprivate variables cannot appear in a map clause.
13424 // OpenMP 4.5 [2.10.5, target update Construct]
13425 // threadprivate variables cannot appear in a from clause.
13426 if (VD && DSAS->isThreadPrivate(VD)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013427 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
Samuel Antao661c0902016-05-26 17:39:58 +000013428 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
13429 << getOpenMPClauseName(CKind);
Alexey Bataeve3727102018-04-18 15:57:46 +000013430 reportOriginalDsa(SemaRef, DSAS, VD, DVar);
Kelvin Li0bff7af2015-11-23 05:32:03 +000013431 continue;
13432 }
13433
Samuel Antao5de996e2016-01-22 20:21:36 +000013434 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
13435 // A list item cannot appear in both a map clause and a data-sharing
13436 // attribute clause on the same construct.
Kelvin Li0bff7af2015-11-23 05:32:03 +000013437
Samuel Antao5de996e2016-01-22 20:21:36 +000013438 // Check conflicts with other map clause expressions. We check the conflicts
13439 // with the current construct separately from the enclosing data
Samuel Antao661c0902016-05-26 17:39:58 +000013440 // environment, because the restrictions are different. We only have to
13441 // check conflicts across regions for the map clauses.
Alexey Bataeve3727102018-04-18 15:57:46 +000013442 if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
Samuel Antao661c0902016-05-26 17:39:58 +000013443 /*CurrentRegionOnly=*/true, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000013444 break;
Samuel Antao661c0902016-05-26 17:39:58 +000013445 if (CKind == OMPC_map &&
Alexey Bataeve3727102018-04-18 15:57:46 +000013446 checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
Samuel Antao661c0902016-05-26 17:39:58 +000013447 /*CurrentRegionOnly=*/false, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000013448 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000013449
Samuel Antao661c0902016-05-26 17:39:58 +000013450 // OpenMP 4.5 [2.10.5, target update Construct]
Samuel Antao5de996e2016-01-22 20:21:36 +000013451 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
13452 // If the type of a list item is a reference to a type T then the type will
13453 // be considered to be T for all purposes of this clause.
Alexey Bataev354df2e2018-05-02 18:44:10 +000013454 auto I = llvm::find_if(
13455 CurComponents,
13456 [](const OMPClauseMappableExprCommon::MappableComponent &MC) {
13457 return MC.getAssociatedDeclaration();
13458 });
13459 assert(I != CurComponents.end() && "Null decl on map clause.");
13460 QualType Type =
13461 I->getAssociatedDeclaration()->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000013462
Samuel Antao661c0902016-05-26 17:39:58 +000013463 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
13464 // A list item in a to or from clause must have a mappable type.
Samuel Antao5de996e2016-01-22 20:21:36 +000013465 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000013466 // A list item must have a mappable type.
Alexey Bataeve3727102018-04-18 15:57:46 +000013467 if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
Samuel Antao661c0902016-05-26 17:39:58 +000013468 DSAS, Type))
Kelvin Li0bff7af2015-11-23 05:32:03 +000013469 continue;
13470
Samuel Antao661c0902016-05-26 17:39:58 +000013471 if (CKind == OMPC_map) {
13472 // target enter data
13473 // OpenMP [2.10.2, Restrictions, p. 99]
13474 // A map-type must be specified in all map clauses and must be either
13475 // to or alloc.
13476 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
13477 if (DKind == OMPD_target_enter_data &&
13478 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
13479 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
13480 << (IsMapTypeImplicit ? 1 : 0)
13481 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
13482 << getOpenMPDirectiveName(DKind);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000013483 continue;
13484 }
Samuel Antao661c0902016-05-26 17:39:58 +000013485
13486 // target exit_data
13487 // OpenMP [2.10.3, Restrictions, p. 102]
13488 // A map-type must be specified in all map clauses and must be either
13489 // from, release, or delete.
13490 if (DKind == OMPD_target_exit_data &&
13491 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
13492 MapType == OMPC_MAP_delete)) {
13493 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
13494 << (IsMapTypeImplicit ? 1 : 0)
13495 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
13496 << getOpenMPDirectiveName(DKind);
13497 continue;
13498 }
13499
13500 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
13501 // A list item cannot appear in both a map clause and a data-sharing
13502 // attribute clause on the same construct
Alexey Bataeve3727102018-04-18 15:57:46 +000013503 if (VD && isOpenMPTargetExecutionDirective(DKind)) {
13504 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
Samuel Antao661c0902016-05-26 17:39:58 +000013505 if (isOpenMPPrivate(DVar.CKind)) {
Samuel Antao6890b092016-07-28 14:25:09 +000013506 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Samuel Antao661c0902016-05-26 17:39:58 +000013507 << getOpenMPClauseName(DVar.CKind)
Samuel Antao6890b092016-07-28 14:25:09 +000013508 << getOpenMPClauseName(OMPC_map)
Samuel Antao661c0902016-05-26 17:39:58 +000013509 << getOpenMPDirectiveName(DSAS->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000013510 reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar);
Samuel Antao661c0902016-05-26 17:39:58 +000013511 continue;
13512 }
13513 }
Michael Kruse01f670d2019-02-22 22:29:42 +000013514 }
Michael Kruse4304e9d2019-02-19 16:38:20 +000013515
Michael Kruse01f670d2019-02-22 22:29:42 +000013516 // Try to find the associated user-defined mapper.
Michael Kruse0336c752019-02-25 20:34:15 +000013517 ExprResult ER = buildUserDefinedMapperRef(
13518 SemaRef, DSAS->getCurScope(), MapperIdScopeSpec, MapperId,
13519 Type.getCanonicalType(), UnresolvedMapper);
13520 if (ER.isInvalid())
13521 continue;
13522 MVLI.UDMapperList.push_back(ER.get());
Carlo Bertollib74bfc82016-03-18 21:43:32 +000013523
Samuel Antao90927002016-04-26 14:54:23 +000013524 // Save the current expression.
Samuel Antao661c0902016-05-26 17:39:58 +000013525 MVLI.ProcessedVarList.push_back(RE);
Samuel Antao90927002016-04-26 14:54:23 +000013526
13527 // Store the components in the stack so that they can be used to check
13528 // against other clauses later on.
Samuel Antao6890b092016-07-28 14:25:09 +000013529 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents,
13530 /*WhereFoundClauseKind=*/OMPC_map);
Samuel Antao90927002016-04-26 14:54:23 +000013531
13532 // Save the components and declaration to create the clause. For purposes of
13533 // the clause creation, any component list that has has base 'this' uses
Samuel Antao686c70c2016-05-26 17:30:50 +000013534 // null as base declaration.
Samuel Antao661c0902016-05-26 17:39:58 +000013535 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13536 MVLI.VarComponents.back().append(CurComponents.begin(),
13537 CurComponents.end());
13538 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
13539 : CurDeclaration);
Kelvin Li0bff7af2015-11-23 05:32:03 +000013540 }
Samuel Antao661c0902016-05-26 17:39:58 +000013541}
13542
Michael Kruse4304e9d2019-02-19 16:38:20 +000013543OMPClause *Sema::ActOnOpenMPMapClause(
13544 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers,
13545 ArrayRef<SourceLocation> MapTypeModifiersLoc,
13546 CXXScopeSpec &MapperIdScopeSpec, DeclarationNameInfo &MapperId,
13547 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit, SourceLocation MapLoc,
13548 SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
13549 const OMPVarListLocTy &Locs, ArrayRef<Expr *> UnresolvedMappers) {
13550 OpenMPMapModifierKind Modifiers[] = {OMPC_MAP_MODIFIER_unknown,
13551 OMPC_MAP_MODIFIER_unknown,
13552 OMPC_MAP_MODIFIER_unknown};
Kelvin Lief579432018-12-18 22:18:41 +000013553 SourceLocation ModifiersLoc[OMPMapClause::NumberOfModifiers];
13554
13555 // Process map-type-modifiers, flag errors for duplicate modifiers.
13556 unsigned Count = 0;
13557 for (unsigned I = 0, E = MapTypeModifiers.size(); I < E; ++I) {
13558 if (MapTypeModifiers[I] != OMPC_MAP_MODIFIER_unknown &&
13559 llvm::find(Modifiers, MapTypeModifiers[I]) != std::end(Modifiers)) {
13560 Diag(MapTypeModifiersLoc[I], diag::err_omp_duplicate_map_type_modifier);
13561 continue;
13562 }
13563 assert(Count < OMPMapClause::NumberOfModifiers &&
Gheorghe-Teodor Berceaa3afcf22019-01-09 20:38:35 +000013564 "Modifiers exceed the allowed number of map type modifiers");
Kelvin Lief579432018-12-18 22:18:41 +000013565 Modifiers[Count] = MapTypeModifiers[I];
13566 ModifiersLoc[Count] = MapTypeModifiersLoc[I];
13567 ++Count;
13568 }
13569
Michael Kruse4304e9d2019-02-19 16:38:20 +000013570 MappableVarListInfo MVLI(VarList);
13571 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, Locs.StartLoc,
Michael Kruse01f670d2019-02-22 22:29:42 +000013572 MapperIdScopeSpec, MapperId, UnresolvedMappers,
13573 MapType, IsMapTypeImplicit);
Michael Kruse4304e9d2019-02-19 16:38:20 +000013574
Samuel Antao5de996e2016-01-22 20:21:36 +000013575 // We need to produce a map clause even if we don't have variables so that
13576 // other diagnostics related with non-existing map clauses are accurate.
Michael Kruse4304e9d2019-02-19 16:38:20 +000013577 return OMPMapClause::Create(Context, Locs, MVLI.ProcessedVarList,
13578 MVLI.VarBaseDeclarations, MVLI.VarComponents,
13579 MVLI.UDMapperList, Modifiers, ModifiersLoc,
13580 MapperIdScopeSpec.getWithLocInContext(Context),
13581 MapperId, MapType, IsMapTypeImplicit, MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000013582}
Kelvin Li099bb8c2015-11-24 20:50:12 +000013583
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013584QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
13585 TypeResult ParsedType) {
13586 assert(ParsedType.isUsable());
13587
13588 QualType ReductionType = GetTypeFromParser(ParsedType.get());
13589 if (ReductionType.isNull())
13590 return QualType();
13591
13592 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
13593 // A type name in a declare reduction directive cannot be a function type, an
13594 // array type, a reference type, or a type qualified with const, volatile or
13595 // restrict.
13596 if (ReductionType.hasQualifiers()) {
13597 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
13598 return QualType();
13599 }
13600
13601 if (ReductionType->isFunctionType()) {
13602 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
13603 return QualType();
13604 }
13605 if (ReductionType->isReferenceType()) {
13606 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
13607 return QualType();
13608 }
13609 if (ReductionType->isArrayType()) {
13610 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
13611 return QualType();
13612 }
13613 return ReductionType;
13614}
13615
13616Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
13617 Scope *S, DeclContext *DC, DeclarationName Name,
13618 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
13619 AccessSpecifier AS, Decl *PrevDeclInScope) {
13620 SmallVector<Decl *, 8> Decls;
13621 Decls.reserve(ReductionTypes.size());
13622
13623 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
Richard Smithbecb92d2017-10-10 22:33:17 +000013624 forRedeclarationInCurContext());
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013625 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
13626 // A reduction-identifier may not be re-declared in the current scope for the
13627 // same type or for a type that is compatible according to the base language
13628 // rules.
13629 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
13630 OMPDeclareReductionDecl *PrevDRD = nullptr;
13631 bool InCompoundScope = true;
13632 if (S != nullptr) {
13633 // Find previous declaration with the same name not referenced in other
13634 // declarations.
13635 FunctionScopeInfo *ParentFn = getEnclosingFunction();
13636 InCompoundScope =
13637 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
13638 LookupName(Lookup, S);
13639 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
13640 /*AllowInlineNamespace=*/false);
13641 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
Alexey Bataeve3727102018-04-18 15:57:46 +000013642 LookupResult::Filter Filter = Lookup.makeFilter();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013643 while (Filter.hasNext()) {
13644 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
13645 if (InCompoundScope) {
13646 auto I = UsedAsPrevious.find(PrevDecl);
13647 if (I == UsedAsPrevious.end())
13648 UsedAsPrevious[PrevDecl] = false;
Alexey Bataeve3727102018-04-18 15:57:46 +000013649 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope())
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013650 UsedAsPrevious[D] = true;
13651 }
13652 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
13653 PrevDecl->getLocation();
13654 }
13655 Filter.done();
13656 if (InCompoundScope) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013657 for (const auto &PrevData : UsedAsPrevious) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013658 if (!PrevData.second) {
13659 PrevDRD = PrevData.first;
13660 break;
13661 }
13662 }
13663 }
13664 } else if (PrevDeclInScope != nullptr) {
13665 auto *PrevDRDInScope = PrevDRD =
13666 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
13667 do {
13668 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
13669 PrevDRDInScope->getLocation();
13670 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
13671 } while (PrevDRDInScope != nullptr);
13672 }
Alexey Bataeve3727102018-04-18 15:57:46 +000013673 for (const auto &TyData : ReductionTypes) {
13674 const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013675 bool Invalid = false;
13676 if (I != PreviousRedeclTypes.end()) {
13677 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
13678 << TyData.first;
13679 Diag(I->second, diag::note_previous_definition);
13680 Invalid = true;
13681 }
13682 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
13683 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
13684 Name, TyData.first, PrevDRD);
13685 DC->addDecl(DRD);
13686 DRD->setAccess(AS);
13687 Decls.push_back(DRD);
13688 if (Invalid)
13689 DRD->setInvalidDecl();
13690 else
13691 PrevDRD = DRD;
13692 }
13693
13694 return DeclGroupPtrTy::make(
13695 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
13696}
13697
13698void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
13699 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13700
13701 // Enter new function scope.
13702 PushFunctionScope();
Reid Kleckner87a31802018-03-12 21:43:02 +000013703 setFunctionHasBranchProtectedScope();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013704 getCurFunction()->setHasOMPDeclareReductionCombiner();
13705
13706 if (S != nullptr)
13707 PushDeclContext(S, DRD);
13708 else
13709 CurContext = DRD;
13710
Faisal Valid143a0c2017-04-01 21:30:49 +000013711 PushExpressionEvaluationContext(
13712 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013713
13714 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013715 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
13716 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
13717 // uses semantics of argument handles by value, but it should be passed by
13718 // reference. C lang does not support references, so pass all parameters as
13719 // pointers.
13720 // Create 'T omp_in;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000013721 VarDecl *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013722 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013723 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
13724 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
13725 // uses semantics of argument handles by value, but it should be passed by
13726 // reference. C lang does not support references, so pass all parameters as
13727 // pointers.
13728 // Create 'T omp_out;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000013729 VarDecl *OmpOutParm =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013730 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
13731 if (S != nullptr) {
13732 PushOnScopeChains(OmpInParm, S);
13733 PushOnScopeChains(OmpOutParm, S);
13734 } else {
13735 DRD->addDecl(OmpInParm);
13736 DRD->addDecl(OmpOutParm);
13737 }
Alexey Bataeve6aa4692018-09-13 16:54:05 +000013738 Expr *InE =
13739 ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation());
13740 Expr *OutE =
13741 ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation());
13742 DRD->setCombinerData(InE, OutE);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013743}
13744
13745void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
13746 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13747 DiscardCleanupsInEvaluationContext();
13748 PopExpressionEvaluationContext();
13749
13750 PopDeclContext();
13751 PopFunctionScopeInfo();
13752
13753 if (Combiner != nullptr)
13754 DRD->setCombiner(Combiner);
13755 else
13756 DRD->setInvalidDecl();
13757}
13758
Alexey Bataev070f43a2017-09-06 14:49:58 +000013759VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013760 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13761
13762 // Enter new function scope.
13763 PushFunctionScope();
Reid Kleckner87a31802018-03-12 21:43:02 +000013764 setFunctionHasBranchProtectedScope();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013765
13766 if (S != nullptr)
13767 PushDeclContext(S, DRD);
13768 else
13769 CurContext = DRD;
13770
Faisal Valid143a0c2017-04-01 21:30:49 +000013771 PushExpressionEvaluationContext(
13772 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013773
13774 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013775 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
13776 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
13777 // uses semantics of argument handles by value, but it should be passed by
13778 // reference. C lang does not support references, so pass all parameters as
13779 // pointers.
13780 // Create 'T omp_priv;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000013781 VarDecl *OmpPrivParm =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013782 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013783 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
13784 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
13785 // uses semantics of argument handles by value, but it should be passed by
13786 // reference. C lang does not support references, so pass all parameters as
13787 // pointers.
13788 // Create 'T omp_orig;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000013789 VarDecl *OmpOrigParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013790 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013791 if (S != nullptr) {
13792 PushOnScopeChains(OmpPrivParm, S);
13793 PushOnScopeChains(OmpOrigParm, S);
13794 } else {
13795 DRD->addDecl(OmpPrivParm);
13796 DRD->addDecl(OmpOrigParm);
13797 }
Alexey Bataeve6aa4692018-09-13 16:54:05 +000013798 Expr *OrigE =
13799 ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation());
13800 Expr *PrivE =
13801 ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation());
13802 DRD->setInitializerData(OrigE, PrivE);
Alexey Bataev070f43a2017-09-06 14:49:58 +000013803 return OmpPrivParm;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013804}
13805
Alexey Bataev070f43a2017-09-06 14:49:58 +000013806void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer,
13807 VarDecl *OmpPrivParm) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013808 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13809 DiscardCleanupsInEvaluationContext();
13810 PopExpressionEvaluationContext();
13811
13812 PopDeclContext();
13813 PopFunctionScopeInfo();
13814
Alexey Bataev070f43a2017-09-06 14:49:58 +000013815 if (Initializer != nullptr) {
13816 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit);
13817 } else if (OmpPrivParm->hasInit()) {
13818 DRD->setInitializer(OmpPrivParm->getInit(),
13819 OmpPrivParm->isDirectInit()
13820 ? OMPDeclareReductionDecl::DirectInit
13821 : OMPDeclareReductionDecl::CopyInit);
13822 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013823 DRD->setInvalidDecl();
Alexey Bataev070f43a2017-09-06 14:49:58 +000013824 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013825}
13826
13827Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
13828 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013829 for (Decl *D : DeclReductions.get()) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013830 if (IsValid) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013831 if (S)
13832 PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S,
13833 /*AddToContext=*/false);
13834 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013835 D->setInvalidDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000013836 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013837 }
13838 return DeclReductions;
13839}
13840
Michael Kruse251e1482019-02-01 20:25:04 +000013841TypeResult Sema::ActOnOpenMPDeclareMapperVarDecl(Scope *S, Declarator &D) {
13842 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
13843 QualType T = TInfo->getType();
13844 if (D.isInvalidType())
13845 return true;
13846
13847 if (getLangOpts().CPlusPlus) {
13848 // Check that there are no default arguments (C++ only).
13849 CheckExtraCXXDefaultArguments(D);
13850 }
13851
13852 return CreateParsedType(T, TInfo);
13853}
13854
13855QualType Sema::ActOnOpenMPDeclareMapperType(SourceLocation TyLoc,
13856 TypeResult ParsedType) {
13857 assert(ParsedType.isUsable() && "Expect usable parsed mapper type");
13858
13859 QualType MapperType = GetTypeFromParser(ParsedType.get());
13860 assert(!MapperType.isNull() && "Expect valid mapper type");
13861
13862 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
13863 // The type must be of struct, union or class type in C and C++
13864 if (!MapperType->isStructureOrClassType() && !MapperType->isUnionType()) {
13865 Diag(TyLoc, diag::err_omp_mapper_wrong_type);
13866 return QualType();
13867 }
13868 return MapperType;
13869}
13870
13871OMPDeclareMapperDecl *Sema::ActOnOpenMPDeclareMapperDirectiveStart(
13872 Scope *S, DeclContext *DC, DeclarationName Name, QualType MapperType,
13873 SourceLocation StartLoc, DeclarationName VN, AccessSpecifier AS,
13874 Decl *PrevDeclInScope) {
13875 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPMapperName,
13876 forRedeclarationInCurContext());
13877 // [OpenMP 5.0], 2.19.7.3 declare mapper Directive, Restrictions
13878 // A mapper-identifier may not be redeclared in the current scope for the
13879 // same type or for a type that is compatible according to the base language
13880 // rules.
13881 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
13882 OMPDeclareMapperDecl *PrevDMD = nullptr;
13883 bool InCompoundScope = true;
13884 if (S != nullptr) {
13885 // Find previous declaration with the same name not referenced in other
13886 // declarations.
13887 FunctionScopeInfo *ParentFn = getEnclosingFunction();
13888 InCompoundScope =
13889 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
13890 LookupName(Lookup, S);
13891 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
13892 /*AllowInlineNamespace=*/false);
13893 llvm::DenseMap<OMPDeclareMapperDecl *, bool> UsedAsPrevious;
13894 LookupResult::Filter Filter = Lookup.makeFilter();
13895 while (Filter.hasNext()) {
13896 auto *PrevDecl = cast<OMPDeclareMapperDecl>(Filter.next());
13897 if (InCompoundScope) {
13898 auto I = UsedAsPrevious.find(PrevDecl);
13899 if (I == UsedAsPrevious.end())
13900 UsedAsPrevious[PrevDecl] = false;
13901 if (OMPDeclareMapperDecl *D = PrevDecl->getPrevDeclInScope())
13902 UsedAsPrevious[D] = true;
13903 }
13904 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
13905 PrevDecl->getLocation();
13906 }
13907 Filter.done();
13908 if (InCompoundScope) {
13909 for (const auto &PrevData : UsedAsPrevious) {
13910 if (!PrevData.second) {
13911 PrevDMD = PrevData.first;
13912 break;
13913 }
13914 }
13915 }
13916 } else if (PrevDeclInScope) {
13917 auto *PrevDMDInScope = PrevDMD =
13918 cast<OMPDeclareMapperDecl>(PrevDeclInScope);
13919 do {
13920 PreviousRedeclTypes[PrevDMDInScope->getType().getCanonicalType()] =
13921 PrevDMDInScope->getLocation();
13922 PrevDMDInScope = PrevDMDInScope->getPrevDeclInScope();
13923 } while (PrevDMDInScope != nullptr);
13924 }
13925 const auto I = PreviousRedeclTypes.find(MapperType.getCanonicalType());
13926 bool Invalid = false;
13927 if (I != PreviousRedeclTypes.end()) {
13928 Diag(StartLoc, diag::err_omp_declare_mapper_redefinition)
13929 << MapperType << Name;
13930 Diag(I->second, diag::note_previous_definition);
13931 Invalid = true;
13932 }
13933 auto *DMD = OMPDeclareMapperDecl::Create(Context, DC, StartLoc, Name,
13934 MapperType, VN, PrevDMD);
13935 DC->addDecl(DMD);
13936 DMD->setAccess(AS);
13937 if (Invalid)
13938 DMD->setInvalidDecl();
13939
13940 // Enter new function scope.
13941 PushFunctionScope();
13942 setFunctionHasBranchProtectedScope();
13943
13944 CurContext = DMD;
13945
13946 return DMD;
13947}
13948
13949void Sema::ActOnOpenMPDeclareMapperDirectiveVarDecl(OMPDeclareMapperDecl *DMD,
13950 Scope *S,
13951 QualType MapperType,
13952 SourceLocation StartLoc,
13953 DeclarationName VN) {
13954 VarDecl *VD = buildVarDecl(*this, StartLoc, MapperType, VN.getAsString());
13955 if (S)
13956 PushOnScopeChains(VD, S);
13957 else
13958 DMD->addDecl(VD);
13959 Expr *MapperVarRefExpr = buildDeclRefExpr(*this, VD, MapperType, StartLoc);
13960 DMD->setMapperVarRef(MapperVarRefExpr);
13961}
13962
13963Sema::DeclGroupPtrTy
13964Sema::ActOnOpenMPDeclareMapperDirectiveEnd(OMPDeclareMapperDecl *D, Scope *S,
13965 ArrayRef<OMPClause *> ClauseList) {
13966 PopDeclContext();
13967 PopFunctionScopeInfo();
13968
13969 if (D) {
13970 if (S)
13971 PushOnScopeChains(D, S, /*AddToContext=*/false);
13972 D->CreateClauses(Context, ClauseList);
13973 }
13974
13975 return DeclGroupPtrTy::make(DeclGroupRef(D));
13976}
13977
David Majnemer9d168222016-08-05 17:44:54 +000013978OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
Kelvin Li099bb8c2015-11-24 20:50:12 +000013979 SourceLocation StartLoc,
13980 SourceLocation LParenLoc,
13981 SourceLocation EndLoc) {
13982 Expr *ValExpr = NumTeams;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000013983 Stmt *HelperValStmt = nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000013984
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013985 // OpenMP [teams Constrcut, Restrictions]
13986 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000013987 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
Alexey Bataeva0569352015-12-01 10:17:31 +000013988 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013989 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000013990
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000013991 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000013992 OpenMPDirectiveKind CaptureRegion =
13993 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams);
13994 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000013995 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000013996 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000013997 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
13998 HelperValStmt = buildPreInits(Context, Captures);
13999 }
14000
14001 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion,
14002 StartLoc, LParenLoc, EndLoc);
Kelvin Li099bb8c2015-11-24 20:50:12 +000014003}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000014004
14005OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
14006 SourceLocation StartLoc,
14007 SourceLocation LParenLoc,
14008 SourceLocation EndLoc) {
14009 Expr *ValExpr = ThreadLimit;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000014010 Stmt *HelperValStmt = nullptr;
Kelvin Lia15fb1a2015-11-27 18:47:36 +000014011
14012 // OpenMP [teams Constrcut, Restrictions]
14013 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000014014 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
Alexey Bataeva0569352015-12-01 10:17:31 +000014015 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000014016 return nullptr;
14017
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000014018 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000014019 OpenMPDirectiveKind CaptureRegion =
14020 getOpenMPCaptureRegionForClause(DKind, OMPC_thread_limit);
14021 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000014022 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000014023 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000014024 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
14025 HelperValStmt = buildPreInits(Context, Captures);
14026 }
14027
14028 return new (Context) OMPThreadLimitClause(
14029 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
Kelvin Lia15fb1a2015-11-27 18:47:36 +000014030}
Alexey Bataeva0569352015-12-01 10:17:31 +000014031
14032OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
14033 SourceLocation StartLoc,
14034 SourceLocation LParenLoc,
14035 SourceLocation EndLoc) {
14036 Expr *ValExpr = Priority;
14037
14038 // OpenMP [2.9.1, task Constrcut]
14039 // The priority-value is a non-negative numerical scalar expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000014040 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
Alexey Bataeva0569352015-12-01 10:17:31 +000014041 /*StrictlyPositive=*/false))
14042 return nullptr;
14043
14044 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
14045}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000014046
14047OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
14048 SourceLocation StartLoc,
14049 SourceLocation LParenLoc,
14050 SourceLocation EndLoc) {
14051 Expr *ValExpr = Grainsize;
14052
14053 // OpenMP [2.9.2, taskloop Constrcut]
14054 // The parameter of the grainsize clause must be a positive integer
14055 // expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000014056 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000014057 /*StrictlyPositive=*/true))
14058 return nullptr;
14059
14060 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
14061}
Alexey Bataev382967a2015-12-08 12:06:20 +000014062
14063OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
14064 SourceLocation StartLoc,
14065 SourceLocation LParenLoc,
14066 SourceLocation EndLoc) {
14067 Expr *ValExpr = NumTasks;
14068
14069 // OpenMP [2.9.2, taskloop Constrcut]
14070 // The parameter of the num_tasks clause must be a positive integer
14071 // expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000014072 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
Alexey Bataev382967a2015-12-08 12:06:20 +000014073 /*StrictlyPositive=*/true))
14074 return nullptr;
14075
14076 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
14077}
14078
Alexey Bataev28c75412015-12-15 08:19:24 +000014079OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
14080 SourceLocation LParenLoc,
14081 SourceLocation EndLoc) {
14082 // OpenMP [2.13.2, critical construct, Description]
14083 // ... where hint-expression is an integer constant expression that evaluates
14084 // to a valid lock hint.
14085 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
14086 if (HintExpr.isInvalid())
14087 return nullptr;
14088 return new (Context)
14089 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
14090}
14091
Carlo Bertollib4adf552016-01-15 18:50:31 +000014092OMPClause *Sema::ActOnOpenMPDistScheduleClause(
14093 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
14094 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
14095 SourceLocation EndLoc) {
14096 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
14097 std::string Values;
14098 Values += "'";
14099 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
14100 Values += "'";
14101 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
14102 << Values << getOpenMPClauseName(OMPC_dist_schedule);
14103 return nullptr;
14104 }
14105 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000014106 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000014107 if (ChunkSize) {
14108 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
14109 !ChunkSize->isInstantiationDependent() &&
14110 !ChunkSize->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000014111 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
Carlo Bertollib4adf552016-01-15 18:50:31 +000014112 ExprResult Val =
14113 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
14114 if (Val.isInvalid())
14115 return nullptr;
14116
14117 ValExpr = Val.get();
14118
14119 // OpenMP [2.7.1, Restrictions]
14120 // chunk_size must be a loop invariant integer expression with a positive
14121 // value.
14122 llvm::APSInt Result;
14123 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
14124 if (Result.isSigned() && !Result.isStrictlyPositive()) {
14125 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
14126 << "dist_schedule" << ChunkSize->getSourceRange();
14127 return nullptr;
14128 }
Alexey Bataev2ba67042017-11-28 21:11:44 +000014129 } else if (getOpenMPCaptureRegionForClause(
14130 DSAStack->getCurrentDirective(), OMPC_dist_schedule) !=
14131 OMPD_unknown &&
Alexey Bataevb46cdea2016-06-15 11:20:48 +000014132 !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000014133 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000014134 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev5a3af132016-03-29 08:58:54 +000014135 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
14136 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000014137 }
14138 }
14139 }
14140
14141 return new (Context)
14142 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000014143 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000014144}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000014145
14146OMPClause *Sema::ActOnOpenMPDefaultmapClause(
14147 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
14148 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
14149 SourceLocation KindLoc, SourceLocation EndLoc) {
14150 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
David Majnemer9d168222016-08-05 17:44:54 +000014151 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) {
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000014152 std::string Value;
14153 SourceLocation Loc;
14154 Value += "'";
14155 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
14156 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000014157 OMPC_DEFAULTMAP_MODIFIER_tofrom);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000014158 Loc = MLoc;
14159 } else {
14160 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000014161 OMPC_DEFAULTMAP_scalar);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000014162 Loc = KindLoc;
14163 }
14164 Value += "'";
14165 Diag(Loc, diag::err_omp_unexpected_clause_value)
14166 << Value << getOpenMPClauseName(OMPC_defaultmap);
14167 return nullptr;
14168 }
Alexey Bataev2fd0cb22017-10-05 17:51:39 +000014169 DSAStack->setDefaultDMAToFromScalar(StartLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000014170
14171 return new (Context)
14172 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
14173}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000014174
14175bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
14176 DeclContext *CurLexicalContext = getCurLexicalContext();
14177 if (!CurLexicalContext->isFileContext() &&
14178 !CurLexicalContext->isExternCContext() &&
Alexey Bataev502ec492017-10-03 20:00:00 +000014179 !CurLexicalContext->isExternCXXContext() &&
14180 !isa<CXXRecordDecl>(CurLexicalContext) &&
14181 !isa<ClassTemplateDecl>(CurLexicalContext) &&
14182 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) &&
14183 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000014184 Diag(Loc, diag::err_omp_region_not_file_context);
14185 return false;
14186 }
Kelvin Libc38e632018-09-10 02:07:09 +000014187 ++DeclareTargetNestingLevel;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000014188 return true;
14189}
14190
14191void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
Kelvin Libc38e632018-09-10 02:07:09 +000014192 assert(DeclareTargetNestingLevel > 0 &&
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000014193 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
Kelvin Libc38e632018-09-10 02:07:09 +000014194 --DeclareTargetNestingLevel;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000014195}
14196
David Majnemer9d168222016-08-05 17:44:54 +000014197void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope,
14198 CXXScopeSpec &ScopeSpec,
14199 const DeclarationNameInfo &Id,
14200 OMPDeclareTargetDeclAttr::MapTypeTy MT,
14201 NamedDeclSetType &SameDirectiveDecls) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000014202 LookupResult Lookup(*this, Id, LookupOrdinaryName);
14203 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
14204
14205 if (Lookup.isAmbiguous())
14206 return;
14207 Lookup.suppressDiagnostics();
14208
14209 if (!Lookup.isSingleResult()) {
14210 if (TypoCorrection Corrected =
14211 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
14212 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
14213 CTK_ErrorRecovery)) {
14214 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
14215 << Id.getName());
14216 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
14217 return;
14218 }
14219
14220 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
14221 return;
14222 }
14223
14224 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
Alexey Bataev30a78212018-09-11 13:59:10 +000014225 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND) ||
14226 isa<FunctionTemplateDecl>(ND)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000014227 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
14228 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
Alexey Bataev30a78212018-09-11 13:59:10 +000014229 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
14230 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
14231 cast<ValueDecl>(ND));
14232 if (!Res) {
Alexey Bataeve3727102018-04-18 15:57:46 +000014233 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
Dmitry Polukhind69b5052016-05-09 14:59:13 +000014234 ND->addAttr(A);
14235 if (ASTMutationListener *ML = Context.getASTMutationListener())
14236 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
Kelvin Li1ce87c72017-12-12 20:08:12 +000014237 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Id.getLoc());
Alexey Bataev30a78212018-09-11 13:59:10 +000014238 } else if (*Res != MT) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000014239 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
14240 << Id.getName();
14241 }
Alexey Bataeve3727102018-04-18 15:57:46 +000014242 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000014243 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
Alexey Bataeve3727102018-04-18 15:57:46 +000014244 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +000014245}
14246
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000014247static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
14248 Sema &SemaRef, Decl *D) {
Alexey Bataev30a78212018-09-11 13:59:10 +000014249 if (!D || !isa<VarDecl>(D))
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000014250 return;
Alexey Bataev30a78212018-09-11 13:59:10 +000014251 auto *VD = cast<VarDecl>(D);
14252 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
14253 return;
14254 SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context);
14255 SemaRef.Diag(SL, diag::note_used_here) << SR;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000014256}
14257
14258static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
14259 Sema &SemaRef, DSAStackTy *Stack,
14260 ValueDecl *VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +000014261 return VD->hasAttr<OMPDeclareTargetDeclAttr>() ||
14262 checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(),
14263 /*FullCheck=*/false);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000014264}
14265
Kelvin Li1ce87c72017-12-12 20:08:12 +000014266void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D,
14267 SourceLocation IdLoc) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000014268 if (!D || D->isInvalidDecl())
14269 return;
14270 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
Stephen Kellyf2ceec42018-08-09 21:08:08 +000014271 SourceLocation SL = E ? E->getBeginLoc() : D->getLocation();
Alexey Bataeve3727102018-04-18 15:57:46 +000014272 if (auto *VD = dyn_cast<VarDecl>(D)) {
Alexey Bataevc1943e72018-07-09 19:58:08 +000014273 // Only global variables can be marked as declare target.
Alexey Bataev30a78212018-09-11 13:59:10 +000014274 if (!VD->isFileVarDecl() && !VD->isStaticLocal() &&
14275 !VD->isStaticDataMember())
Alexey Bataevc1943e72018-07-09 19:58:08 +000014276 return;
14277 // 2.10.6: threadprivate variable cannot appear in a declare target
14278 // directive.
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000014279 if (DSAStack->isThreadPrivate(VD)) {
14280 Diag(SL, diag::err_omp_threadprivate_in_target);
Alexey Bataeve3727102018-04-18 15:57:46 +000014281 reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000014282 return;
14283 }
14284 }
Alexey Bataev97b72212018-08-14 18:31:20 +000014285 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
14286 D = FTD->getTemplatedDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000014287 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Alexey Bataev30a78212018-09-11 13:59:10 +000014288 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
14289 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD);
14290 if (Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) {
Kelvin Li1ce87c72017-12-12 20:08:12 +000014291 assert(IdLoc.isValid() && "Source location is expected");
14292 Diag(IdLoc, diag::err_omp_function_in_link_clause);
14293 Diag(FD->getLocation(), diag::note_defined_here) << FD;
14294 return;
14295 }
14296 }
Alexey Bataev30a78212018-09-11 13:59:10 +000014297 if (auto *VD = dyn_cast<ValueDecl>(D)) {
14298 // Problem if any with var declared with incomplete type will be reported
14299 // as normal, so no need to check it here.
14300 if ((E || !VD->getType()->isIncompleteType()) &&
14301 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD))
14302 return;
14303 if (!E && !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) {
14304 // Checking declaration inside declare target region.
14305 if (isa<VarDecl>(D) || isa<FunctionDecl>(D) ||
14306 isa<FunctionTemplateDecl>(D)) {
14307 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
14308 Context, OMPDeclareTargetDeclAttr::MT_To);
14309 D->addAttr(A);
14310 if (ASTMutationListener *ML = Context.getASTMutationListener())
14311 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
14312 }
14313 return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000014314 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000014315 }
Alexey Bataev30a78212018-09-11 13:59:10 +000014316 if (!E)
14317 return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000014318 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
14319}
Samuel Antao661c0902016-05-26 17:39:58 +000014320
14321OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
Michael Kruse01f670d2019-02-22 22:29:42 +000014322 CXXScopeSpec &MapperIdScopeSpec,
14323 DeclarationNameInfo &MapperId,
14324 const OMPVarListLocTy &Locs,
14325 ArrayRef<Expr *> UnresolvedMappers) {
Samuel Antao661c0902016-05-26 17:39:58 +000014326 MappableVarListInfo MVLI(VarList);
Michael Kruse01f670d2019-02-22 22:29:42 +000014327 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, Locs.StartLoc,
14328 MapperIdScopeSpec, MapperId, UnresolvedMappers);
Samuel Antao661c0902016-05-26 17:39:58 +000014329 if (MVLI.ProcessedVarList.empty())
14330 return nullptr;
14331
Michael Kruse01f670d2019-02-22 22:29:42 +000014332 return OMPToClause::Create(
14333 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
14334 MVLI.VarComponents, MVLI.UDMapperList,
14335 MapperIdScopeSpec.getWithLocInContext(Context), MapperId);
Samuel Antao661c0902016-05-26 17:39:58 +000014336}
Samuel Antaoec172c62016-05-26 17:49:04 +000014337
14338OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
Michael Kruse0336c752019-02-25 20:34:15 +000014339 CXXScopeSpec &MapperIdScopeSpec,
14340 DeclarationNameInfo &MapperId,
14341 const OMPVarListLocTy &Locs,
14342 ArrayRef<Expr *> UnresolvedMappers) {
Samuel Antaoec172c62016-05-26 17:49:04 +000014343 MappableVarListInfo MVLI(VarList);
Michael Kruse01f670d2019-02-22 22:29:42 +000014344 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, Locs.StartLoc,
14345 MapperIdScopeSpec, MapperId, UnresolvedMappers);
Samuel Antaoec172c62016-05-26 17:49:04 +000014346 if (MVLI.ProcessedVarList.empty())
14347 return nullptr;
14348
Michael Kruse0336c752019-02-25 20:34:15 +000014349 return OMPFromClause::Create(
14350 Context, Locs, MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
14351 MVLI.VarComponents, MVLI.UDMapperList,
14352 MapperIdScopeSpec.getWithLocInContext(Context), MapperId);
Samuel Antaoec172c62016-05-26 17:49:04 +000014353}
Carlo Bertolli2404b172016-07-13 15:37:16 +000014354
14355OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
Michael Kruse4304e9d2019-02-19 16:38:20 +000014356 const OMPVarListLocTy &Locs) {
Samuel Antaocc10b852016-07-28 14:23:26 +000014357 MappableVarListInfo MVLI(VarList);
14358 SmallVector<Expr *, 8> PrivateCopies;
14359 SmallVector<Expr *, 8> Inits;
14360
Alexey Bataeve3727102018-04-18 15:57:46 +000014361 for (Expr *RefExpr : VarList) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000014362 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
14363 SourceLocation ELoc;
14364 SourceRange ERange;
14365 Expr *SimpleRefExpr = RefExpr;
14366 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
14367 if (Res.second) {
14368 // It will be analyzed later.
Samuel Antaocc10b852016-07-28 14:23:26 +000014369 MVLI.ProcessedVarList.push_back(RefExpr);
14370 PrivateCopies.push_back(nullptr);
14371 Inits.push_back(nullptr);
Carlo Bertolli2404b172016-07-13 15:37:16 +000014372 }
14373 ValueDecl *D = Res.first;
14374 if (!D)
14375 continue;
14376
14377 QualType Type = D->getType();
Samuel Antaocc10b852016-07-28 14:23:26 +000014378 Type = Type.getNonReferenceType().getUnqualifiedType();
14379
14380 auto *VD = dyn_cast<VarDecl>(D);
14381
14382 // Item should be a pointer or reference to pointer.
14383 if (!Type->isPointerType()) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000014384 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
14385 << 0 << RefExpr->getSourceRange();
14386 continue;
14387 }
Samuel Antaocc10b852016-07-28 14:23:26 +000014388
14389 // Build the private variable and the expression that refers to it.
Alexey Bataev63cc8e92018-03-20 14:45:59 +000014390 auto VDPrivate =
14391 buildVarDecl(*this, ELoc, Type, D->getName(),
14392 D->hasAttrs() ? &D->getAttrs() : nullptr,
14393 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Samuel Antaocc10b852016-07-28 14:23:26 +000014394 if (VDPrivate->isInvalidDecl())
14395 continue;
14396
14397 CurContext->addDecl(VDPrivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000014398 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Samuel Antaocc10b852016-07-28 14:23:26 +000014399 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
14400
14401 // Add temporary variable to initialize the private copy of the pointer.
Alexey Bataeve3727102018-04-18 15:57:46 +000014402 VarDecl *VDInit =
Samuel Antaocc10b852016-07-28 14:23:26 +000014403 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp");
Alexey Bataeve3727102018-04-18 15:57:46 +000014404 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr(
14405 *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc());
Samuel Antaocc10b852016-07-28 14:23:26 +000014406 AddInitializerToDecl(VDPrivate,
14407 DefaultLvalueConversion(VDInitRefExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000014408 /*DirectInit=*/false);
Samuel Antaocc10b852016-07-28 14:23:26 +000014409
14410 // If required, build a capture to implement the privatization initialized
14411 // with the current list item value.
14412 DeclRefExpr *Ref = nullptr;
14413 if (!VD)
14414 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
14415 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
14416 PrivateCopies.push_back(VDPrivateRefExpr);
14417 Inits.push_back(VDInitRefExpr);
14418
14419 // We need to add a data sharing attribute for this variable to make sure it
14420 // is correctly captured. A variable that shows up in a use_device_ptr has
14421 // similar properties of a first private variable.
14422 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
14423
14424 // Create a mappable component for the list item. List items in this clause
14425 // only need a component.
14426 MVLI.VarBaseDeclarations.push_back(D);
14427 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
14428 MVLI.VarComponents.back().push_back(
14429 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D));
Carlo Bertolli2404b172016-07-13 15:37:16 +000014430 }
14431
Samuel Antaocc10b852016-07-28 14:23:26 +000014432 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli2404b172016-07-13 15:37:16 +000014433 return nullptr;
14434
Samuel Antaocc10b852016-07-28 14:23:26 +000014435 return OMPUseDevicePtrClause::Create(
Michael Kruse4304e9d2019-02-19 16:38:20 +000014436 Context, Locs, MVLI.ProcessedVarList, PrivateCopies, Inits,
14437 MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli2404b172016-07-13 15:37:16 +000014438}
Carlo Bertolli70594e92016-07-13 17:16:49 +000014439
14440OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
Michael Kruse4304e9d2019-02-19 16:38:20 +000014441 const OMPVarListLocTy &Locs) {
Samuel Antao6890b092016-07-28 14:25:09 +000014442 MappableVarListInfo MVLI(VarList);
Alexey Bataeve3727102018-04-18 15:57:46 +000014443 for (Expr *RefExpr : VarList) {
Kelvin Li84376252016-12-14 15:39:58 +000014444 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause.");
Carlo Bertolli70594e92016-07-13 17:16:49 +000014445 SourceLocation ELoc;
14446 SourceRange ERange;
14447 Expr *SimpleRefExpr = RefExpr;
14448 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
14449 if (Res.second) {
14450 // It will be analyzed later.
Samuel Antao6890b092016-07-28 14:25:09 +000014451 MVLI.ProcessedVarList.push_back(RefExpr);
Carlo Bertolli70594e92016-07-13 17:16:49 +000014452 }
14453 ValueDecl *D = Res.first;
14454 if (!D)
14455 continue;
14456
14457 QualType Type = D->getType();
14458 // item should be a pointer or array or reference to pointer or array
14459 if (!Type.getNonReferenceType()->isPointerType() &&
14460 !Type.getNonReferenceType()->isArrayType()) {
14461 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
14462 << 0 << RefExpr->getSourceRange();
14463 continue;
14464 }
Samuel Antao6890b092016-07-28 14:25:09 +000014465
14466 // Check if the declaration in the clause does not show up in any data
14467 // sharing attribute.
Alexey Bataeve3727102018-04-18 15:57:46 +000014468 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Samuel Antao6890b092016-07-28 14:25:09 +000014469 if (isOpenMPPrivate(DVar.CKind)) {
14470 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
14471 << getOpenMPClauseName(DVar.CKind)
14472 << getOpenMPClauseName(OMPC_is_device_ptr)
14473 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000014474 reportOriginalDsa(*this, DSAStack, D, DVar);
Samuel Antao6890b092016-07-28 14:25:09 +000014475 continue;
14476 }
14477
Alexey Bataeve3727102018-04-18 15:57:46 +000014478 const Expr *ConflictExpr;
Samuel Antao6890b092016-07-28 14:25:09 +000014479 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +000014480 D, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +000014481 [&ConflictExpr](
14482 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
14483 OpenMPClauseKind) -> bool {
14484 ConflictExpr = R.front().getAssociatedExpression();
14485 return true;
14486 })) {
14487 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
14488 Diag(ConflictExpr->getExprLoc(), diag::note_used_here)
14489 << ConflictExpr->getSourceRange();
14490 continue;
14491 }
14492
14493 // Store the components in the stack so that they can be used to check
14494 // against other clauses later on.
14495 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D);
14496 DSAStack->addMappableExpressionComponents(
14497 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
14498
14499 // Record the expression we've just processed.
14500 MVLI.ProcessedVarList.push_back(SimpleRefExpr);
14501
14502 // Create a mappable component for the list item. List items in this clause
14503 // only need a component. We use a null declaration to signal fields in
14504 // 'this'.
14505 assert((isa<DeclRefExpr>(SimpleRefExpr) ||
14506 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
14507 "Unexpected device pointer expression!");
14508 MVLI.VarBaseDeclarations.push_back(
14509 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr);
14510 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
14511 MVLI.VarComponents.back().push_back(MC);
Carlo Bertolli70594e92016-07-13 17:16:49 +000014512 }
14513
Samuel Antao6890b092016-07-28 14:25:09 +000014514 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli70594e92016-07-13 17:16:49 +000014515 return nullptr;
14516
Michael Kruse4304e9d2019-02-19 16:38:20 +000014517 return OMPIsDevicePtrClause::Create(Context, Locs, MVLI.ProcessedVarList,
14518 MVLI.VarBaseDeclarations,
14519 MVLI.VarComponents);
Carlo Bertolli70594e92016-07-13 17:16:49 +000014520}