blob: 1166de752e395a5edd7b28acdd66c77758eb016f [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
Alexey Bataeva769e072013-03-22 06:34:35 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000010/// This file implements semantic analysis for OpenMP directives and
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000011/// clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000012///
13//===----------------------------------------------------------------------===//
14
Alexey Bataevb08f89f2015-08-14 12:25:37 +000015#include "TreeTransform.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000016#include "clang/AST/ASTContext.h"
Alexey Bataev97720002014-11-11 04:05:39 +000017#include "clang/AST/ASTMutationListener.h"
Alexey Bataeva839ddd2016-03-17 10:19:46 +000018#include "clang/AST/CXXInheritance.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000019#include "clang/AST/Decl.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000020#include "clang/AST/DeclCXX.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000021#include "clang/AST/DeclOpenMP.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000022#include "clang/AST/StmtCXX.h"
23#include "clang/AST/StmtOpenMP.h"
24#include "clang/AST/StmtVisitor.h"
Patrick Lystere13b1e32019-01-02 19:28:48 +000025#include "clang/AST/TypeOrdering.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000026#include "clang/Basic/OpenMPKinds.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000027#include "clang/Sema/Initialization.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000028#include "clang/Sema/Lookup.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000029#include "clang/Sema/Scope.h"
30#include "clang/Sema/ScopeInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000031#include "clang/Sema/SemaInternal.h"
Alexey Bataevfa312f32017-07-21 18:48:21 +000032#include "llvm/ADT/PointerEmbeddedInt.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000033using namespace clang;
34
Alexey Bataev758e55e2013-09-06 18:03:48 +000035//===----------------------------------------------------------------------===//
36// Stack of data-sharing attributes for variables
37//===----------------------------------------------------------------------===//
38
Alexey Bataeve3727102018-04-18 15:57:46 +000039static const Expr *checkMapClauseExpressionBase(
Alexey Bataevf47c4b42017-09-26 13:47:31 +000040 Sema &SemaRef, Expr *E,
41 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
Alexey Bataevb7a9b742017-12-05 19:20:09 +000042 OpenMPClauseKind CKind, bool NoDiagnose);
Alexey Bataevf47c4b42017-09-26 13:47:31 +000043
Alexey Bataev758e55e2013-09-06 18:03:48 +000044namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000045/// Default data sharing attributes, which can be applied to directive.
Alexey Bataev758e55e2013-09-06 18:03:48 +000046enum DefaultDataSharingAttributes {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000047 DSA_unspecified = 0, /// Data sharing attribute not specified.
48 DSA_none = 1 << 0, /// Default data sharing attribute 'none'.
49 DSA_shared = 1 << 1, /// Default data sharing attribute 'shared'.
Alexey Bataev2fd0cb22017-10-05 17:51:39 +000050};
51
52/// Attributes of the defaultmap clause.
53enum DefaultMapAttributes {
54 DMA_unspecified, /// Default mapping is not specified.
55 DMA_tofrom_scalar, /// Default mapping is 'tofrom:scalar'.
Alexey Bataev758e55e2013-09-06 18:03:48 +000056};
Alexey Bataev7ff55242014-06-19 09:13:45 +000057
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000058/// Stack for tracking declarations used in OpenMP directives and
Alexey Bataev758e55e2013-09-06 18:03:48 +000059/// clauses and their data-sharing attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +000060class DSAStackTy {
Alexey Bataev758e55e2013-09-06 18:03:48 +000061public:
Alexey Bataeve3727102018-04-18 15:57:46 +000062 struct DSAVarData {
Alexey Bataev7ace49d2016-05-17 08:55:33 +000063 OpenMPDirectiveKind DKind = OMPD_unknown;
64 OpenMPClauseKind CKind = OMPC_unknown;
Alexey Bataeve3727102018-04-18 15:57:46 +000065 const Expr *RefExpr = nullptr;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000066 DeclRefExpr *PrivateCopy = nullptr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000067 SourceLocation ImplicitDSALoc;
Alexey Bataev4d4624c2017-07-20 16:47:47 +000068 DSAVarData() = default;
Alexey Bataeve3727102018-04-18 15:57:46 +000069 DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
70 const Expr *RefExpr, DeclRefExpr *PrivateCopy,
71 SourceLocation ImplicitDSALoc)
Alexey Bataevf189cb72017-07-24 14:52:13 +000072 : DKind(DKind), CKind(CKind), RefExpr(RefExpr),
73 PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000074 };
Alexey Bataeve3727102018-04-18 15:57:46 +000075 using OperatorOffsetTy =
76 llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>;
Alexey Bataevf138fda2018-08-13 19:04:24 +000077 using DoacrossDependMapTy =
78 llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>;
Alexey Bataeved09d242014-05-28 05:53:51 +000079
Alexey Bataev758e55e2013-09-06 18:03:48 +000080private:
Alexey Bataeve3727102018-04-18 15:57:46 +000081 struct DSAInfo {
Alexey Bataev7ace49d2016-05-17 08:55:33 +000082 OpenMPClauseKind Attributes = OMPC_unknown;
83 /// Pointer to a reference expression and a flag which shows that the
84 /// variable is marked as lastprivate(true) or not (false).
Alexey Bataeve3727102018-04-18 15:57:46 +000085 llvm::PointerIntPair<const Expr *, 1, bool> RefExpr;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000086 DeclRefExpr *PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +000087 };
Alexey Bataeve3727102018-04-18 15:57:46 +000088 using DeclSAMapTy = llvm::SmallDenseMap<const ValueDecl *, DSAInfo, 8>;
89 using AlignedMapTy = llvm::SmallDenseMap<const ValueDecl *, const Expr *, 8>;
90 using LCDeclInfo = std::pair<unsigned, VarDecl *>;
91 using LoopControlVariablesMapTy =
92 llvm::SmallDenseMap<const ValueDecl *, LCDeclInfo, 8>;
Samuel Antao6890b092016-07-28 14:25:09 +000093 /// Struct that associates a component with the clause kind where they are
94 /// found.
95 struct MappedExprComponentTy {
96 OMPClauseMappableExprCommon::MappableExprComponentLists Components;
97 OpenMPClauseKind Kind = OMPC_unknown;
98 };
Alexey Bataeve3727102018-04-18 15:57:46 +000099 using MappedExprComponentsTy =
100 llvm::DenseMap<const ValueDecl *, MappedExprComponentTy>;
101 using CriticalsWithHintsTy =
102 llvm::StringMap<std::pair<const OMPCriticalDirective *, llvm::APSInt>>;
Alexey Bataevfa312f32017-07-21 18:48:21 +0000103 struct ReductionData {
Alexey Bataeve3727102018-04-18 15:57:46 +0000104 using BOKPtrType = llvm::PointerEmbeddedInt<BinaryOperatorKind, 16>;
Alexey Bataevfa312f32017-07-21 18:48:21 +0000105 SourceRange ReductionRange;
Alexey Bataevf87fa882017-07-21 19:26:22 +0000106 llvm::PointerUnion<const Expr *, BOKPtrType> ReductionOp;
Alexey Bataevfa312f32017-07-21 18:48:21 +0000107 ReductionData() = default;
108 void set(BinaryOperatorKind BO, SourceRange RR) {
109 ReductionRange = RR;
110 ReductionOp = BO;
111 }
112 void set(const Expr *RefExpr, SourceRange RR) {
113 ReductionRange = RR;
114 ReductionOp = RefExpr;
115 }
116 };
Alexey Bataeve3727102018-04-18 15:57:46 +0000117 using DeclReductionMapTy =
118 llvm::SmallDenseMap<const ValueDecl *, ReductionData, 4>;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000119
Alexey Bataeve3727102018-04-18 15:57:46 +0000120 struct SharingMapTy {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000121 DeclSAMapTy SharingMap;
Alexey Bataevfa312f32017-07-21 18:48:21 +0000122 DeclReductionMapTy ReductionMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000123 AlignedMapTy AlignedMap;
Samuel Antao90927002016-04-26 14:54:23 +0000124 MappedExprComponentsTy MappedExprComponents;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000125 LoopControlVariablesMapTy LCVMap;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000126 DefaultDataSharingAttributes DefaultAttr = DSA_unspecified;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000127 SourceLocation DefaultAttrLoc;
Alexey Bataev2fd0cb22017-10-05 17:51:39 +0000128 DefaultMapAttributes DefaultMapAttr = DMA_unspecified;
129 SourceLocation DefaultMapAttrLoc;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000130 OpenMPDirectiveKind Directive = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000131 DeclarationNameInfo DirectiveName;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000132 Scope *CurScope = nullptr;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000133 SourceLocation ConstructLoc;
Alexey Bataev8b427062016-05-25 12:36:08 +0000134 /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to
135 /// get the data (loop counters etc.) about enclosing loop-based construct.
136 /// This data is required during codegen.
137 DoacrossDependMapTy DoacrossDepends;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000138 /// first argument (Expr *) contains optional argument of the
Alexey Bataev346265e2015-09-25 10:37:12 +0000139 /// 'ordered' clause, the second one is true if the regions has 'ordered'
140 /// clause, false otherwise.
Alexey Bataevf138fda2018-08-13 19:04:24 +0000141 llvm::Optional<std::pair<const Expr *, OMPOrderedClause *>> OrderedRegion;
Alexey Bataev6ab5bb12018-10-29 15:01:58 +0000142 unsigned AssociatedLoops = 1;
143 const Decl *PossiblyLoopCounter = nullptr;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000144 bool NowaitRegion = false;
145 bool CancelRegion = false;
Alexey Bataev6ab5bb12018-10-29 15:01:58 +0000146 bool LoopStart = false;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000147 SourceLocation InnerTeamsRegionLoc;
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000148 /// Reference to the taskgroup task_reduction reference expression.
149 Expr *TaskgroupReductionRef = nullptr;
Patrick Lystere13b1e32019-01-02 19:28:48 +0000150 llvm::DenseSet<QualType> MappedClassesQualTypes;
Alexey Bataeved09d242014-05-28 05:53:51 +0000151 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000152 Scope *CurScope, SourceLocation Loc)
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000153 : Directive(DKind), DirectiveName(Name), CurScope(CurScope),
154 ConstructLoc(Loc) {}
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000155 SharingMapTy() = default;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000156 };
157
Alexey Bataeve3727102018-04-18 15:57:46 +0000158 using StackTy = SmallVector<SharingMapTy, 4>;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000159
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000160 /// Stack of used declaration and their data-sharing attributes.
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000161 DeclSAMapTy Threadprivates;
Alexey Bataev4b465392017-04-26 15:06:24 +0000162 const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr;
163 SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000164 /// true, if check for DSA must be from parent directive, false, if
Alexey Bataev39f915b82015-05-08 10:41:21 +0000165 /// from current directive.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000166 OpenMPClauseKind ClauseKindMode = OMPC_unknown;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000167 Sema &SemaRef;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000168 bool ForceCapturing = false;
Alexey Bataev60705422018-10-30 15:50:12 +0000169 /// true if all the vaiables in the target executable directives must be
170 /// captured by reference.
171 bool ForceCaptureByReferenceInTargetExecutable = false;
Alexey Bataev28c75412015-12-15 08:19:24 +0000172 CriticalsWithHintsTy Criticals;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000173
Alexey Bataeve3727102018-04-18 15:57:46 +0000174 using iterator = StackTy::const_reverse_iterator;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000175
Alexey Bataeve3727102018-04-18 15:57:46 +0000176 DSAVarData getDSA(iterator &Iter, ValueDecl *D) const;
Alexey Bataevec3da872014-01-31 05:15:34 +0000177
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000178 /// Checks if the variable is a local for OpenMP region.
Alexey Bataeve3727102018-04-18 15:57:46 +0000179 bool isOpenMPLocal(VarDecl *D, iterator Iter) const;
Alexey Bataeved09d242014-05-28 05:53:51 +0000180
Alexey Bataev4b465392017-04-26 15:06:24 +0000181 bool isStackEmpty() const {
182 return Stack.empty() ||
183 Stack.back().second != CurrentNonCapturingFunctionScope ||
184 Stack.back().first.empty();
185 }
186
Kelvin Li1408f912018-09-26 04:28:39 +0000187 /// Vector of previously declared requires directives
188 SmallVector<const OMPRequiresDecl *, 2> RequiresDecls;
189
Alexey Bataev758e55e2013-09-06 18:03:48 +0000190public:
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000191 explicit DSAStackTy(Sema &S) : SemaRef(S) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000192
Alexey Bataevaac108a2015-06-23 04:51:00 +0000193 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
Alexey Bataev3f82cfc2017-12-13 15:28:44 +0000194 OpenMPClauseKind getClauseParsingMode() const {
195 assert(isClauseParsingMode() && "Must be in clause parsing mode.");
196 return ClauseKindMode;
197 }
Alexey Bataevaac108a2015-06-23 04:51:00 +0000198 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000199
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000200 bool isForceVarCapturing() const { return ForceCapturing; }
201 void setForceVarCapturing(bool V) { ForceCapturing = V; }
202
Alexey Bataev60705422018-10-30 15:50:12 +0000203 void setForceCaptureByReferenceInTargetExecutable(bool V) {
204 ForceCaptureByReferenceInTargetExecutable = V;
205 }
206 bool isForceCaptureByReferenceInTargetExecutable() const {
207 return ForceCaptureByReferenceInTargetExecutable;
208 }
209
Alexey Bataev758e55e2013-09-06 18:03:48 +0000210 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000211 Scope *CurScope, SourceLocation Loc) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000212 if (Stack.empty() ||
213 Stack.back().second != CurrentNonCapturingFunctionScope)
214 Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope);
215 Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc);
216 Stack.back().first.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000217 }
218
219 void pop() {
Alexey Bataev4b465392017-04-26 15:06:24 +0000220 assert(!Stack.back().first.empty() &&
221 "Data-sharing attributes stack is empty!");
222 Stack.back().first.pop_back();
223 }
224
Alexey Bataev6ab5bb12018-10-29 15:01:58 +0000225 /// Marks that we're started loop parsing.
226 void loopInit() {
227 assert(isOpenMPLoopDirective(getCurrentDirective()) &&
228 "Expected loop-based directive.");
229 Stack.back().first.back().LoopStart = true;
230 }
231 /// Start capturing of the variables in the loop context.
232 void loopStart() {
233 assert(isOpenMPLoopDirective(getCurrentDirective()) &&
234 "Expected loop-based directive.");
235 Stack.back().first.back().LoopStart = false;
236 }
237 /// true, if variables are captured, false otherwise.
238 bool isLoopStarted() const {
239 assert(isOpenMPLoopDirective(getCurrentDirective()) &&
240 "Expected loop-based directive.");
241 return !Stack.back().first.back().LoopStart;
242 }
243 /// Marks (or clears) declaration as possibly loop counter.
244 void resetPossibleLoopCounter(const Decl *D = nullptr) {
245 Stack.back().first.back().PossiblyLoopCounter =
246 D ? D->getCanonicalDecl() : D;
247 }
248 /// Gets the possible loop counter decl.
249 const Decl *getPossiblyLoopCunter() const {
250 return Stack.back().first.back().PossiblyLoopCounter;
251 }
Alexey Bataev4b465392017-04-26 15:06:24 +0000252 /// Start new OpenMP region stack in new non-capturing function.
253 void pushFunction() {
254 const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction();
255 assert(!isa<CapturingScopeInfo>(CurFnScope));
256 CurrentNonCapturingFunctionScope = CurFnScope;
257 }
258 /// Pop region stack for non-capturing function.
259 void popFunction(const FunctionScopeInfo *OldFSI) {
260 if (!Stack.empty() && Stack.back().second == OldFSI) {
261 assert(Stack.back().first.empty());
262 Stack.pop_back();
263 }
264 CurrentNonCapturingFunctionScope = nullptr;
265 for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) {
266 if (!isa<CapturingScopeInfo>(FSI)) {
267 CurrentNonCapturingFunctionScope = FSI;
268 break;
269 }
270 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000271 }
272
Alexey Bataeve3727102018-04-18 15:57:46 +0000273 void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) {
Alexey Bataev43a919f2018-04-13 17:48:43 +0000274 Criticals.try_emplace(D->getDirectiveName().getAsString(), D, Hint);
Alexey Bataev28c75412015-12-15 08:19:24 +0000275 }
Alexey Bataeve3727102018-04-18 15:57:46 +0000276 const std::pair<const OMPCriticalDirective *, llvm::APSInt>
Alexey Bataev28c75412015-12-15 08:19:24 +0000277 getCriticalWithHint(const DeclarationNameInfo &Name) const {
278 auto I = Criticals.find(Name.getAsString());
279 if (I != Criticals.end())
280 return I->second;
281 return std::make_pair(nullptr, llvm::APSInt());
282 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000283 /// If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000284 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000285 /// for diagnostics.
Alexey Bataeve3727102018-04-18 15:57:46 +0000286 const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000287
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000288 /// Register specified variable as loop control variable.
Alexey Bataeve3727102018-04-18 15:57:46 +0000289 void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000290 /// Check if the specified variable is a loop control variable for
Alexey Bataev9c821032015-04-30 04:23:23 +0000291 /// current region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000292 /// \return The index of the loop control variable in the list of associated
293 /// for-loops (from outer to inner).
Alexey Bataeve3727102018-04-18 15:57:46 +0000294 const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000295 /// Check if the specified variable is a loop control variable for
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000296 /// parent region.
297 /// \return The index of the loop control variable in the list of associated
298 /// for-loops (from outer to inner).
Alexey Bataeve3727102018-04-18 15:57:46 +0000299 const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000300 /// Get the loop control variable for the I-th loop (or nullptr) in
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000301 /// parent directive.
Alexey Bataeve3727102018-04-18 15:57:46 +0000302 const ValueDecl *getParentLoopControlVariable(unsigned I) const;
Alexey Bataev9c821032015-04-30 04:23:23 +0000303
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000304 /// Adds explicit data sharing attribute to the specified declaration.
Alexey Bataeve3727102018-04-18 15:57:46 +0000305 void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
Alexey Bataev90c228f2016-02-08 09:29:13 +0000306 DeclRefExpr *PrivateCopy = nullptr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000307
Alexey Bataevfa312f32017-07-21 18:48:21 +0000308 /// Adds additional information for the reduction items with the reduction id
309 /// represented as an operator.
Alexey Bataeve3727102018-04-18 15:57:46 +0000310 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000311 BinaryOperatorKind BOK);
Alexey Bataevfa312f32017-07-21 18:48:21 +0000312 /// Adds additional information for the reduction items with the reduction id
313 /// represented as reduction identifier.
Alexey Bataeve3727102018-04-18 15:57:46 +0000314 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000315 const Expr *ReductionRef);
Alexey Bataevfa312f32017-07-21 18:48:21 +0000316 /// Returns the location and reduction operation from the innermost parent
317 /// region for the given \p D.
Alexey Bataeve3727102018-04-18 15:57:46 +0000318 const DSAVarData
319 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
320 BinaryOperatorKind &BOK,
321 Expr *&TaskgroupDescriptor) const;
Alexey Bataevfa312f32017-07-21 18:48:21 +0000322 /// Returns the location and reduction operation from the innermost parent
323 /// region for the given \p D.
Alexey Bataeve3727102018-04-18 15:57:46 +0000324 const DSAVarData
325 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
326 const Expr *&ReductionRef,
327 Expr *&TaskgroupDescriptor) const;
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000328 /// Return reduction reference expression for the current taskgroup.
329 Expr *getTaskgroupReductionRef() const {
330 assert(Stack.back().first.back().Directive == OMPD_taskgroup &&
331 "taskgroup reference expression requested for non taskgroup "
332 "directive.");
333 return Stack.back().first.back().TaskgroupReductionRef;
334 }
Alexey Bataev88202be2017-07-27 13:20:36 +0000335 /// Checks if the given \p VD declaration is actually a taskgroup reduction
336 /// descriptor variable at the \p Level of OpenMP regions.
Alexey Bataeve3727102018-04-18 15:57:46 +0000337 bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const {
Alexey Bataev88202be2017-07-27 13:20:36 +0000338 return Stack.back().first[Level].TaskgroupReductionRef &&
339 cast<DeclRefExpr>(Stack.back().first[Level].TaskgroupReductionRef)
340 ->getDecl() == VD;
341 }
Alexey Bataevfa312f32017-07-21 18:48:21 +0000342
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000343 /// Returns data sharing attributes from top of the stack for the
Alexey Bataev758e55e2013-09-06 18:03:48 +0000344 /// specified declaration.
Alexey Bataeve3727102018-04-18 15:57:46 +0000345 const DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000346 /// Returns data-sharing attributes for the specified declaration.
Alexey Bataeve3727102018-04-18 15:57:46 +0000347 const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000348 /// Checks if the specified variables has data-sharing attributes which
Alexey Bataevf29276e2014-06-18 04:14:57 +0000349 /// match specified \a CPred predicate in any directive which matches \a DPred
350 /// predicate.
Alexey Bataeve3727102018-04-18 15:57:46 +0000351 const DSAVarData
352 hasDSA(ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
353 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
354 bool FromParent) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000355 /// Checks if the specified variables has data-sharing attributes which
Alexey Bataevf29276e2014-06-18 04:14:57 +0000356 /// match specified \a CPred predicate in any innermost directive which
357 /// matches \a DPred predicate.
Alexey Bataeve3727102018-04-18 15:57:46 +0000358 const DSAVarData
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000359 hasInnermostDSA(ValueDecl *D,
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000360 const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
361 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +0000362 bool FromParent) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000363 /// Checks if the specified variables has explicit data-sharing
Alexey Bataevaac108a2015-06-23 04:51:00 +0000364 /// attributes which match specified \a CPred predicate at the specified
365 /// OpenMP region.
Alexey Bataeve3727102018-04-18 15:57:46 +0000366 bool hasExplicitDSA(const ValueDecl *D,
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000367 const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
Alexey Bataeve3727102018-04-18 15:57:46 +0000368 unsigned Level, bool NotLastprivate = false) const;
Samuel Antao4be30e92015-10-02 17:14:03 +0000369
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000370 /// Returns true if the directive at level \Level matches in the
Samuel Antao4be30e92015-10-02 17:14:03 +0000371 /// specified \a DPred predicate.
372 bool hasExplicitDirective(
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000373 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +0000374 unsigned Level) const;
Samuel Antao4be30e92015-10-02 17:14:03 +0000375
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000376 /// Finds a directive which matches specified \a DPred predicate.
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000377 bool hasDirective(
378 const llvm::function_ref<bool(
379 OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)>
380 DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +0000381 bool FromParent) const;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000382
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000383 /// Returns currently analyzed directive.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000384 OpenMPDirectiveKind getCurrentDirective() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000385 return isStackEmpty() ? OMPD_unknown : Stack.back().first.back().Directive;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000386 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000387 /// Returns directive kind at specified level.
Alexey Bataevdfa430f2017-12-08 15:03:50 +0000388 OpenMPDirectiveKind getDirective(unsigned Level) const {
389 assert(!isStackEmpty() && "No directive at specified level.");
390 return Stack.back().first[Level].Directive;
391 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000392 /// Returns parent directive.
Alexey Bataev549210e2014-06-24 04:39:47 +0000393 OpenMPDirectiveKind getParentDirective() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000394 if (isStackEmpty() || Stack.back().first.size() == 1)
395 return OMPD_unknown;
396 return std::next(Stack.back().first.rbegin())->Directive;
Alexey Bataev549210e2014-06-24 04:39:47 +0000397 }
Alexey Bataev6ab5bb12018-10-29 15:01:58 +0000398
Kelvin Li1408f912018-09-26 04:28:39 +0000399 /// Add requires decl to internal vector
400 void addRequiresDecl(OMPRequiresDecl *RD) {
401 RequiresDecls.push_back(RD);
402 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000403
Kelvin Li1408f912018-09-26 04:28:39 +0000404 /// Checks for a duplicate clause amongst previously declared requires
405 /// directives
406 bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const {
407 bool IsDuplicate = false;
408 for (OMPClause *CNew : ClauseList) {
409 for (const OMPRequiresDecl *D : RequiresDecls) {
410 for (const OMPClause *CPrev : D->clauselists()) {
411 if (CNew->getClauseKind() == CPrev->getClauseKind()) {
412 SemaRef.Diag(CNew->getBeginLoc(),
413 diag::err_omp_requires_clause_redeclaration)
414 << getOpenMPClauseName(CNew->getClauseKind());
415 SemaRef.Diag(CPrev->getBeginLoc(),
416 diag::note_omp_requires_previous_clause)
417 << getOpenMPClauseName(CPrev->getClauseKind());
418 IsDuplicate = true;
419 }
420 }
421 }
422 }
423 return IsDuplicate;
424 }
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +0000425
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000426 /// Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000427 void setDefaultDSANone(SourceLocation Loc) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000428 assert(!isStackEmpty());
429 Stack.back().first.back().DefaultAttr = DSA_none;
430 Stack.back().first.back().DefaultAttrLoc = Loc;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000431 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000432 /// Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000433 void setDefaultDSAShared(SourceLocation Loc) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000434 assert(!isStackEmpty());
435 Stack.back().first.back().DefaultAttr = DSA_shared;
436 Stack.back().first.back().DefaultAttrLoc = Loc;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000437 }
Alexey Bataev2fd0cb22017-10-05 17:51:39 +0000438 /// Set default data mapping attribute to 'tofrom:scalar'.
439 void setDefaultDMAToFromScalar(SourceLocation Loc) {
440 assert(!isStackEmpty());
441 Stack.back().first.back().DefaultMapAttr = DMA_tofrom_scalar;
442 Stack.back().first.back().DefaultMapAttrLoc = Loc;
443 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000444
445 DefaultDataSharingAttributes getDefaultDSA() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000446 return isStackEmpty() ? DSA_unspecified
447 : Stack.back().first.back().DefaultAttr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000448 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000449 SourceLocation getDefaultDSALocation() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000450 return isStackEmpty() ? SourceLocation()
451 : Stack.back().first.back().DefaultAttrLoc;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000452 }
Alexey Bataev2fd0cb22017-10-05 17:51:39 +0000453 DefaultMapAttributes getDefaultDMA() const {
454 return isStackEmpty() ? DMA_unspecified
455 : Stack.back().first.back().DefaultMapAttr;
456 }
457 DefaultMapAttributes getDefaultDMAAtLevel(unsigned Level) const {
458 return Stack.back().first[Level].DefaultMapAttr;
459 }
460 SourceLocation getDefaultDMALocation() const {
461 return isStackEmpty() ? SourceLocation()
462 : Stack.back().first.back().DefaultMapAttrLoc;
463 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000464
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000465 /// Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000466 bool isThreadPrivate(VarDecl *D) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000467 const DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000468 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000469 }
470
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000471 /// Marks current region as ordered (it has an 'ordered' clause).
Alexey Bataevf138fda2018-08-13 19:04:24 +0000472 void setOrderedRegion(bool IsOrdered, const Expr *Param,
473 OMPOrderedClause *Clause) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000474 assert(!isStackEmpty());
Alexey Bataevf138fda2018-08-13 19:04:24 +0000475 if (IsOrdered)
476 Stack.back().first.back().OrderedRegion.emplace(Param, Clause);
477 else
478 Stack.back().first.back().OrderedRegion.reset();
479 }
480 /// Returns true, if region is ordered (has associated 'ordered' clause),
481 /// false - otherwise.
482 bool isOrderedRegion() const {
483 if (isStackEmpty())
484 return false;
485 return Stack.back().first.rbegin()->OrderedRegion.hasValue();
486 }
487 /// Returns optional parameter for the ordered region.
488 std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const {
489 if (isStackEmpty() ||
490 !Stack.back().first.rbegin()->OrderedRegion.hasValue())
491 return std::make_pair(nullptr, nullptr);
492 return Stack.back().first.rbegin()->OrderedRegion.getValue();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000493 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000494 /// Returns true, if parent region is ordered (has associated
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000495 /// 'ordered' clause), false - otherwise.
496 bool isParentOrderedRegion() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000497 if (isStackEmpty() || Stack.back().first.size() == 1)
498 return false;
Alexey Bataevf138fda2018-08-13 19:04:24 +0000499 return std::next(Stack.back().first.rbegin())->OrderedRegion.hasValue();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000500 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000501 /// Returns optional parameter for the ordered region.
Alexey Bataevf138fda2018-08-13 19:04:24 +0000502 std::pair<const Expr *, OMPOrderedClause *>
503 getParentOrderedRegionParam() const {
504 if (isStackEmpty() || Stack.back().first.size() == 1 ||
505 !std::next(Stack.back().first.rbegin())->OrderedRegion.hasValue())
506 return std::make_pair(nullptr, nullptr);
507 return std::next(Stack.back().first.rbegin())->OrderedRegion.getValue();
Alexey Bataev346265e2015-09-25 10:37:12 +0000508 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000509 /// Marks current region as nowait (it has a 'nowait' clause).
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000510 void setNowaitRegion(bool IsNowait = true) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000511 assert(!isStackEmpty());
512 Stack.back().first.back().NowaitRegion = IsNowait;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000513 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000514 /// Returns true, if parent region is nowait (has associated
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000515 /// 'nowait' clause), false - otherwise.
516 bool isParentNowaitRegion() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000517 if (isStackEmpty() || Stack.back().first.size() == 1)
518 return false;
519 return std::next(Stack.back().first.rbegin())->NowaitRegion;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000520 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000521 /// Marks parent region as cancel region.
Alexey Bataev25e5b442015-09-15 12:52:43 +0000522 void setParentCancelRegion(bool Cancel = true) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000523 if (!isStackEmpty() && Stack.back().first.size() > 1) {
524 auto &StackElemRef = *std::next(Stack.back().first.rbegin());
525 StackElemRef.CancelRegion |= StackElemRef.CancelRegion || Cancel;
526 }
Alexey Bataev25e5b442015-09-15 12:52:43 +0000527 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000528 /// Return true if current region has inner cancel construct.
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000529 bool isCancelRegion() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000530 return isStackEmpty() ? false : Stack.back().first.back().CancelRegion;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000531 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000532
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000533 /// Set collapse value for the region.
Alexey Bataev4b465392017-04-26 15:06:24 +0000534 void setAssociatedLoops(unsigned Val) {
535 assert(!isStackEmpty());
536 Stack.back().first.back().AssociatedLoops = Val;
537 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000538 /// Return collapse value for region.
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000539 unsigned getAssociatedLoops() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000540 return isStackEmpty() ? 0 : Stack.back().first.back().AssociatedLoops;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000541 }
Alexey Bataev9c821032015-04-30 04:23:23 +0000542
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000543 /// Marks current target region as one with closely nested teams
Alexey Bataev13314bf2014-10-09 04:18:56 +0000544 /// region.
545 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000546 if (!isStackEmpty() && Stack.back().first.size() > 1) {
547 std::next(Stack.back().first.rbegin())->InnerTeamsRegionLoc =
548 TeamsRegionLoc;
549 }
Alexey Bataev13314bf2014-10-09 04:18:56 +0000550 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000551 /// Returns true, if current region has closely nested teams region.
Alexey Bataev13314bf2014-10-09 04:18:56 +0000552 bool hasInnerTeamsRegion() const {
553 return getInnerTeamsRegionLoc().isValid();
554 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000555 /// Returns location of the nested teams region (if any).
Alexey Bataev13314bf2014-10-09 04:18:56 +0000556 SourceLocation getInnerTeamsRegionLoc() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000557 return isStackEmpty() ? SourceLocation()
558 : Stack.back().first.back().InnerTeamsRegionLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000559 }
560
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000561 Scope *getCurScope() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000562 return isStackEmpty() ? nullptr : Stack.back().first.back().CurScope;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000563 }
Alexey Bataeve3727102018-04-18 15:57:46 +0000564 SourceLocation getConstructLoc() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000565 return isStackEmpty() ? SourceLocation()
566 : Stack.back().first.back().ConstructLoc;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000567 }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000568
Samuel Antao4c8035b2016-12-12 18:00:20 +0000569 /// Do the check specified in \a Check to all component lists and return true
570 /// if any issue is found.
Samuel Antao90927002016-04-26 14:54:23 +0000571 bool checkMappableExprComponentListsForDecl(
Alexey Bataeve3727102018-04-18 15:57:46 +0000572 const ValueDecl *VD, bool CurrentRegionOnly,
Samuel Antao6890b092016-07-28 14:25:09 +0000573 const llvm::function_ref<
574 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000575 OpenMPClauseKind)>
Alexey Bataeve3727102018-04-18 15:57:46 +0000576 Check) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000577 if (isStackEmpty())
578 return false;
579 auto SI = Stack.back().first.rbegin();
580 auto SE = Stack.back().first.rend();
Samuel Antao5de996e2016-01-22 20:21:36 +0000581
582 if (SI == SE)
583 return false;
584
Alexey Bataeve3727102018-04-18 15:57:46 +0000585 if (CurrentRegionOnly)
Samuel Antao5de996e2016-01-22 20:21:36 +0000586 SE = std::next(SI);
Alexey Bataeve3727102018-04-18 15:57:46 +0000587 else
588 std::advance(SI, 1);
Samuel Antao5de996e2016-01-22 20:21:36 +0000589
590 for (; SI != SE; ++SI) {
Samuel Antao90927002016-04-26 14:54:23 +0000591 auto MI = SI->MappedExprComponents.find(VD);
592 if (MI != SI->MappedExprComponents.end())
Alexey Bataeve3727102018-04-18 15:57:46 +0000593 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
594 MI->second.Components)
Samuel Antao6890b092016-07-28 14:25:09 +0000595 if (Check(L, MI->second.Kind))
Samuel Antao5de996e2016-01-22 20:21:36 +0000596 return true;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000597 }
Samuel Antao5de996e2016-01-22 20:21:36 +0000598 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000599 }
600
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +0000601 /// Do the check specified in \a Check to all component lists at a given level
602 /// and return true if any issue is found.
603 bool checkMappableExprComponentListsForDeclAtLevel(
Alexey Bataeve3727102018-04-18 15:57:46 +0000604 const ValueDecl *VD, unsigned Level,
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +0000605 const llvm::function_ref<
606 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000607 OpenMPClauseKind)>
Alexey Bataeve3727102018-04-18 15:57:46 +0000608 Check) const {
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +0000609 if (isStackEmpty())
610 return false;
611
612 auto StartI = Stack.back().first.begin();
613 auto EndI = Stack.back().first.end();
614 if (std::distance(StartI, EndI) <= (int)Level)
615 return false;
616 std::advance(StartI, Level);
617
618 auto MI = StartI->MappedExprComponents.find(VD);
619 if (MI != StartI->MappedExprComponents.end())
Alexey Bataeve3727102018-04-18 15:57:46 +0000620 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
621 MI->second.Components)
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +0000622 if (Check(L, MI->second.Kind))
623 return true;
624 return false;
625 }
626
Samuel Antao4c8035b2016-12-12 18:00:20 +0000627 /// Create a new mappable expression component list associated with a given
628 /// declaration and initialize it with the provided list of components.
Samuel Antao90927002016-04-26 14:54:23 +0000629 void addMappableExpressionComponents(
Alexey Bataeve3727102018-04-18 15:57:46 +0000630 const ValueDecl *VD,
Samuel Antao6890b092016-07-28 14:25:09 +0000631 OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
632 OpenMPClauseKind WhereFoundClauseKind) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000633 assert(!isStackEmpty() &&
Samuel Antao90927002016-04-26 14:54:23 +0000634 "Not expecting to retrieve components from a empty stack!");
Alexey Bataeve3727102018-04-18 15:57:46 +0000635 MappedExprComponentTy &MEC =
636 Stack.back().first.back().MappedExprComponents[VD];
Samuel Antao90927002016-04-26 14:54:23 +0000637 // Create new entry and append the new components there.
Samuel Antao6890b092016-07-28 14:25:09 +0000638 MEC.Components.resize(MEC.Components.size() + 1);
639 MEC.Components.back().append(Components.begin(), Components.end());
640 MEC.Kind = WhereFoundClauseKind;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000641 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000642
643 unsigned getNestingLevel() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000644 assert(!isStackEmpty());
645 return Stack.back().first.size() - 1;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000646 }
Alexey Bataeve3727102018-04-18 15:57:46 +0000647 void addDoacrossDependClause(OMPDependClause *C,
648 const OperatorOffsetTy &OpsOffs) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000649 assert(!isStackEmpty() && Stack.back().first.size() > 1);
Alexey Bataeve3727102018-04-18 15:57:46 +0000650 SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
Alexey Bataev4b465392017-04-26 15:06:24 +0000651 assert(isOpenMPWorksharingDirective(StackElem.Directive));
Alexey Bataeve3727102018-04-18 15:57:46 +0000652 StackElem.DoacrossDepends.try_emplace(C, OpsOffs);
Alexey Bataev8b427062016-05-25 12:36:08 +0000653 }
654 llvm::iterator_range<DoacrossDependMapTy::const_iterator>
655 getDoacrossDependClauses() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000656 assert(!isStackEmpty());
Alexey Bataeve3727102018-04-18 15:57:46 +0000657 const SharingMapTy &StackElem = Stack.back().first.back();
Alexey Bataev4b465392017-04-26 15:06:24 +0000658 if (isOpenMPWorksharingDirective(StackElem.Directive)) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000659 const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends;
Alexey Bataev8b427062016-05-25 12:36:08 +0000660 return llvm::make_range(Ref.begin(), Ref.end());
661 }
Alexey Bataev4b465392017-04-26 15:06:24 +0000662 return llvm::make_range(StackElem.DoacrossDepends.end(),
663 StackElem.DoacrossDepends.end());
Alexey Bataev8b427062016-05-25 12:36:08 +0000664 }
Patrick Lystere13b1e32019-01-02 19:28:48 +0000665
666 // Store types of classes which have been explicitly mapped
667 void addMappedClassesQualTypes(QualType QT) {
668 SharingMapTy &StackElem = Stack.back().first.back();
669 StackElem.MappedClassesQualTypes.insert(QT);
670 }
671
672 // Return set of mapped classes types
673 bool isClassPreviouslyMapped(QualType QT) const {
674 const SharingMapTy &StackElem = Stack.back().first.back();
675 return StackElem.MappedClassesQualTypes.count(QT) != 0;
676 }
677
Alexey Bataev758e55e2013-09-06 18:03:48 +0000678};
Alexey Bataev7e6803e2019-01-09 15:58:05 +0000679
680bool isImplicitTaskingRegion(OpenMPDirectiveKind DKind) {
681 return isOpenMPParallelDirective(DKind) || isOpenMPTeamsDirective(DKind);
682}
683
684bool isImplicitOrExplicitTaskingRegion(OpenMPDirectiveKind DKind) {
685 return isImplicitTaskingRegion(DKind) || isOpenMPTaskingDirective(DKind) || DKind == OMPD_unknown;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000686}
Alexey Bataeve3727102018-04-18 15:57:46 +0000687
Alexey Bataeved09d242014-05-28 05:53:51 +0000688} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000689
Alexey Bataeve3727102018-04-18 15:57:46 +0000690static const Expr *getExprAsWritten(const Expr *E) {
Bill Wendling7c44da22018-10-31 03:48:47 +0000691 if (const auto *FE = dyn_cast<FullExpr>(E))
692 E = FE->getSubExpr();
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000693
Alexey Bataeve3727102018-04-18 15:57:46 +0000694 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000695 E = MTE->GetTemporaryExpr();
696
Alexey Bataeve3727102018-04-18 15:57:46 +0000697 while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000698 E = Binder->getSubExpr();
699
Alexey Bataeve3727102018-04-18 15:57:46 +0000700 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000701 E = ICE->getSubExprAsWritten();
702 return E->IgnoreParens();
703}
704
Alexey Bataeve3727102018-04-18 15:57:46 +0000705static Expr *getExprAsWritten(Expr *E) {
706 return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E)));
707}
708
709static const ValueDecl *getCanonicalDecl(const ValueDecl *D) {
710 if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D))
711 if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000712 D = ME->getMemberDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +0000713 const auto *VD = dyn_cast<VarDecl>(D);
714 const auto *FD = dyn_cast<FieldDecl>(D);
David Majnemer9d168222016-08-05 17:44:54 +0000715 if (VD != nullptr) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000716 VD = VD->getCanonicalDecl();
717 D = VD;
718 } else {
719 assert(FD);
720 FD = FD->getCanonicalDecl();
721 D = FD;
722 }
723 return D;
724}
725
Alexey Bataeve3727102018-04-18 15:57:46 +0000726static ValueDecl *getCanonicalDecl(ValueDecl *D) {
727 return const_cast<ValueDecl *>(
728 getCanonicalDecl(const_cast<const ValueDecl *>(D)));
729}
730
731DSAStackTy::DSAVarData DSAStackTy::getDSA(iterator &Iter,
732 ValueDecl *D) const {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000733 D = getCanonicalDecl(D);
734 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000735 const auto *FD = dyn_cast<FieldDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000736 DSAVarData DVar;
Alexey Bataev4b465392017-04-26 15:06:24 +0000737 if (isStackEmpty() || Iter == Stack.back().first.rend()) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000738 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
739 // in a region but not in construct]
740 // File-scope or namespace-scope variables referenced in called routines
741 // in the region are shared unless they appear in a threadprivate
742 // directive.
Alexey Bataeve3727102018-04-18 15:57:46 +0000743 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000744 DVar.CKind = OMPC_shared;
745
746 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
747 // in a region but not in construct]
748 // Variables with static storage duration that are declared in called
749 // routines in the region are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000750 if (VD && VD->hasGlobalStorage())
751 DVar.CKind = OMPC_shared;
752
753 // Non-static data members are shared by default.
754 if (FD)
Alexey Bataev750a58b2014-03-18 12:19:12 +0000755 DVar.CKind = OMPC_shared;
756
Alexey Bataev758e55e2013-09-06 18:03:48 +0000757 return DVar;
758 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000759
Alexey Bataevec3da872014-01-31 05:15:34 +0000760 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
761 // in a Construct, C/C++, predetermined, p.1]
762 // Variables with automatic storage duration that are declared in a scope
763 // inside the construct are private.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000764 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
765 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000766 DVar.CKind = OMPC_private;
767 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000768 }
769
Alexey Bataeveffbdf12017-07-21 17:24:30 +0000770 DVar.DKind = Iter->Directive;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000771 // Explicitly specified attributes and local variables with predetermined
772 // attributes.
773 if (Iter->SharingMap.count(D)) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000774 const DSAInfo &Data = Iter->SharingMap.lookup(D);
775 DVar.RefExpr = Data.RefExpr.getPointer();
776 DVar.PrivateCopy = Data.PrivateCopy;
777 DVar.CKind = Data.Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000778 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000779 return DVar;
780 }
781
782 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
783 // in a Construct, C/C++, implicitly determined, p.1]
784 // In a parallel or task construct, the data-sharing attributes of these
785 // variables are determined by the default clause, if present.
786 switch (Iter->DefaultAttr) {
787 case DSA_shared:
788 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000789 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000790 return DVar;
791 case DSA_none:
792 return DVar;
793 case DSA_unspecified:
794 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
795 // in a Construct, implicitly determined, p.2]
796 // In a parallel construct, if no default clause is present, these
797 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000798 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000799 if (isOpenMPParallelDirective(DVar.DKind) ||
800 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000801 DVar.CKind = OMPC_shared;
802 return DVar;
803 }
804
805 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
806 // in a Construct, implicitly determined, p.4]
807 // In a task construct, if no default clause is present, a variable that in
808 // the enclosing context is determined to be shared by all implicit tasks
809 // bound to the current team is shared.
Alexey Bataev35aaee62016-04-13 13:36:48 +0000810 if (isOpenMPTaskingDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000811 DSAVarData DVarTemp;
Alexey Bataeve3727102018-04-18 15:57:46 +0000812 iterator I = Iter, E = Stack.back().first.rend();
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000813 do {
814 ++I;
Alexey Bataeved09d242014-05-28 05:53:51 +0000815 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
Alexey Bataev35aaee62016-04-13 13:36:48 +0000816 // Referenced in a Construct, implicitly determined, p.6]
Alexey Bataev758e55e2013-09-06 18:03:48 +0000817 // In a task construct, if no default clause is present, a variable
818 // whose data-sharing attribute is not determined by the rules above is
819 // firstprivate.
820 DVarTemp = getDSA(I, D);
821 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000822 DVar.RefExpr = nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000823 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000824 return DVar;
825 }
Alexey Bataev7e6803e2019-01-09 15:58:05 +0000826 } while (I != E && !isImplicitTaskingRegion(I->Directive));
Alexey Bataev758e55e2013-09-06 18:03:48 +0000827 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000828 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000829 return DVar;
830 }
831 }
832 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
833 // in a Construct, implicitly determined, p.3]
834 // For constructs other than task, if no default clause is present, these
835 // variables inherit their data-sharing attributes from the enclosing
836 // context.
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000837 return getDSA(++Iter, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000838}
839
Alexey Bataeve3727102018-04-18 15:57:46 +0000840const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D,
841 const Expr *NewDE) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000842 assert(!isStackEmpty() && "Data sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000843 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000844 SharingMapTy &StackElem = Stack.back().first.back();
Alexey Bataev4b465392017-04-26 15:06:24 +0000845 auto It = StackElem.AlignedMap.find(D);
846 if (It == StackElem.AlignedMap.end()) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000847 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
Alexey Bataev4b465392017-04-26 15:06:24 +0000848 StackElem.AlignedMap[D] = NewDE;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000849 return nullptr;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000850 }
Alexey Bataeve3727102018-04-18 15:57:46 +0000851 assert(It->second && "Unexpected nullptr expr in the aligned map");
852 return It->second;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000853}
854
Alexey Bataeve3727102018-04-18 15:57:46 +0000855void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000856 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000857 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000858 SharingMapTy &StackElem = Stack.back().first.back();
859 StackElem.LCVMap.try_emplace(
860 D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture));
Alexey Bataev9c821032015-04-30 04:23:23 +0000861}
862
Alexey Bataeve3727102018-04-18 15:57:46 +0000863const DSAStackTy::LCDeclInfo
864DSAStackTy::isLoopControlVariable(const ValueDecl *D) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000865 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000866 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000867 const SharingMapTy &StackElem = Stack.back().first.back();
Alexey Bataev4b465392017-04-26 15:06:24 +0000868 auto It = StackElem.LCVMap.find(D);
869 if (It != StackElem.LCVMap.end())
870 return It->second;
871 return {0, nullptr};
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000872}
873
Alexey Bataeve3727102018-04-18 15:57:46 +0000874const DSAStackTy::LCDeclInfo
875DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000876 assert(!isStackEmpty() && Stack.back().first.size() > 1 &&
877 "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000878 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000879 const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
Alexey Bataev4b465392017-04-26 15:06:24 +0000880 auto It = StackElem.LCVMap.find(D);
881 if (It != StackElem.LCVMap.end())
882 return It->second;
883 return {0, nullptr};
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000884}
885
Alexey Bataeve3727102018-04-18 15:57:46 +0000886const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000887 assert(!isStackEmpty() && Stack.back().first.size() > 1 &&
888 "Data-sharing attributes stack is empty");
Alexey Bataeve3727102018-04-18 15:57:46 +0000889 const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
Alexey Bataev4b465392017-04-26 15:06:24 +0000890 if (StackElem.LCVMap.size() < I)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000891 return nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +0000892 for (const auto &Pair : StackElem.LCVMap)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000893 if (Pair.second.first == I)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000894 return Pair.first;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000895 return nullptr;
Alexey Bataev9c821032015-04-30 04:23:23 +0000896}
897
Alexey Bataeve3727102018-04-18 15:57:46 +0000898void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
Alexey Bataev90c228f2016-02-08 09:29:13 +0000899 DeclRefExpr *PrivateCopy) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000900 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000901 if (A == OMPC_threadprivate) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000902 DSAInfo &Data = Threadprivates[D];
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000903 Data.Attributes = A;
904 Data.RefExpr.setPointer(E);
905 Data.PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000906 } else {
Alexey Bataev4b465392017-04-26 15:06:24 +0000907 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataeve3727102018-04-18 15:57:46 +0000908 DSAInfo &Data = Stack.back().first.back().SharingMap[D];
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000909 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
910 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
911 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
912 (isLoopControlVariable(D).first && A == OMPC_private));
913 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
914 Data.RefExpr.setInt(/*IntVal=*/true);
915 return;
916 }
917 const bool IsLastprivate =
918 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
919 Data.Attributes = A;
920 Data.RefExpr.setPointerAndInt(E, IsLastprivate);
921 Data.PrivateCopy = PrivateCopy;
922 if (PrivateCopy) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000923 DSAInfo &Data =
924 Stack.back().first.back().SharingMap[PrivateCopy->getDecl()];
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000925 Data.Attributes = A;
926 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
927 Data.PrivateCopy = nullptr;
928 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000929 }
930}
931
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000932/// Build a variable declaration for OpenMP loop iteration variable.
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000933static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev63cc8e92018-03-20 14:45:59 +0000934 StringRef Name, const AttrVec *Attrs = nullptr,
935 DeclRefExpr *OrigRef = nullptr) {
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000936 DeclContext *DC = SemaRef.CurContext;
937 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
938 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
Alexey Bataeve3727102018-04-18 15:57:46 +0000939 auto *Decl =
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000940 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
941 if (Attrs) {
942 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
943 I != E; ++I)
944 Decl->addAttr(*I);
945 }
946 Decl->setImplicit();
Alexey Bataev63cc8e92018-03-20 14:45:59 +0000947 if (OrigRef) {
948 Decl->addAttr(
949 OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef));
950 }
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000951 return Decl;
952}
953
954static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
955 SourceLocation Loc,
956 bool RefersToCapture = false) {
957 D->setReferenced();
958 D->markUsed(S.Context);
959 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
960 SourceLocation(), D, RefersToCapture, Loc, Ty,
961 VK_LValue);
962}
963
Alexey Bataeve3727102018-04-18 15:57:46 +0000964void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000965 BinaryOperatorKind BOK) {
Alexey Bataevfa312f32017-07-21 18:48:21 +0000966 D = getCanonicalDecl(D);
967 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataevfa312f32017-07-21 18:48:21 +0000968 assert(
Richard Trieu09f14112017-07-21 21:29:35 +0000969 Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000970 "Additional reduction info may be specified only for reduction items.");
Alexey Bataeve3727102018-04-18 15:57:46 +0000971 ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D];
Alexey Bataevfa312f32017-07-21 18:48:21 +0000972 assert(ReductionData.ReductionRange.isInvalid() &&
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000973 Stack.back().first.back().Directive == OMPD_taskgroup &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000974 "Additional reduction info may be specified only once for reduction "
975 "items.");
976 ReductionData.set(BOK, SR);
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000977 Expr *&TaskgroupReductionRef =
978 Stack.back().first.back().TaskgroupReductionRef;
979 if (!TaskgroupReductionRef) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000980 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
981 SemaRef.Context.VoidPtrTy, ".task_red.");
Alexey Bataevd070a582017-10-25 15:54:04 +0000982 TaskgroupReductionRef =
983 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000984 }
Alexey Bataevfa312f32017-07-21 18:48:21 +0000985}
986
Alexey Bataeve3727102018-04-18 15:57:46 +0000987void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000988 const Expr *ReductionRef) {
Alexey Bataevfa312f32017-07-21 18:48:21 +0000989 D = getCanonicalDecl(D);
990 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataevfa312f32017-07-21 18:48:21 +0000991 assert(
Richard Trieu09f14112017-07-21 21:29:35 +0000992 Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000993 "Additional reduction info may be specified only for reduction items.");
Alexey Bataeve3727102018-04-18 15:57:46 +0000994 ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D];
Alexey Bataevfa312f32017-07-21 18:48:21 +0000995 assert(ReductionData.ReductionRange.isInvalid() &&
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000996 Stack.back().first.back().Directive == OMPD_taskgroup &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000997 "Additional reduction info may be specified only once for reduction "
998 "items.");
999 ReductionData.set(ReductionRef, SR);
Alexey Bataev3b1b8952017-07-25 15:53:26 +00001000 Expr *&TaskgroupReductionRef =
1001 Stack.back().first.back().TaskgroupReductionRef;
1002 if (!TaskgroupReductionRef) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001003 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
1004 SemaRef.Context.VoidPtrTy, ".task_red.");
Alexey Bataevd070a582017-10-25 15:54:04 +00001005 TaskgroupReductionRef =
1006 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
Alexey Bataev3b1b8952017-07-25 15:53:26 +00001007 }
Alexey Bataevfa312f32017-07-21 18:48:21 +00001008}
1009
Alexey Bataeve3727102018-04-18 15:57:46 +00001010const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1011 const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK,
1012 Expr *&TaskgroupDescriptor) const {
Alexey Bataevfa312f32017-07-21 18:48:21 +00001013 D = getCanonicalDecl(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +00001014 assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1015 if (Stack.back().first.empty())
1016 return DSAVarData();
Alexey Bataeve3727102018-04-18 15:57:46 +00001017 for (iterator I = std::next(Stack.back().first.rbegin(), 1),
1018 E = Stack.back().first.rend();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001019 I != E; std::advance(I, 1)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001020 const DSAInfo &Data = I->SharingMap.lookup(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +00001021 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
Alexey Bataevfa312f32017-07-21 18:48:21 +00001022 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00001023 const ReductionData &ReductionData = I->ReductionMap.lookup(D);
Alexey Bataevfa312f32017-07-21 18:48:21 +00001024 if (!ReductionData.ReductionOp ||
1025 ReductionData.ReductionOp.is<const Expr *>())
Alexey Bataevf189cb72017-07-24 14:52:13 +00001026 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001027 SR = ReductionData.ReductionRange;
Alexey Bataevf87fa882017-07-21 19:26:22 +00001028 BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>();
Alexey Bataev88202be2017-07-27 13:20:36 +00001029 assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1030 "expression for the descriptor is not "
1031 "set.");
1032 TaskgroupDescriptor = I->TaskgroupReductionRef;
Alexey Bataevf189cb72017-07-24 14:52:13 +00001033 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
1034 Data.PrivateCopy, I->DefaultAttrLoc);
Alexey Bataevfa312f32017-07-21 18:48:21 +00001035 }
Alexey Bataevf189cb72017-07-24 14:52:13 +00001036 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001037}
1038
Alexey Bataeve3727102018-04-18 15:57:46 +00001039const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1040 const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef,
1041 Expr *&TaskgroupDescriptor) const {
Alexey Bataevfa312f32017-07-21 18:48:21 +00001042 D = getCanonicalDecl(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +00001043 assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1044 if (Stack.back().first.empty())
1045 return DSAVarData();
Alexey Bataeve3727102018-04-18 15:57:46 +00001046 for (iterator I = std::next(Stack.back().first.rbegin(), 1),
1047 E = Stack.back().first.rend();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001048 I != E; std::advance(I, 1)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001049 const DSAInfo &Data = I->SharingMap.lookup(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +00001050 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
Alexey Bataevfa312f32017-07-21 18:48:21 +00001051 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00001052 const ReductionData &ReductionData = I->ReductionMap.lookup(D);
Alexey Bataevfa312f32017-07-21 18:48:21 +00001053 if (!ReductionData.ReductionOp ||
1054 !ReductionData.ReductionOp.is<const Expr *>())
Alexey Bataevf189cb72017-07-24 14:52:13 +00001055 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001056 SR = ReductionData.ReductionRange;
1057 ReductionRef = ReductionData.ReductionOp.get<const Expr *>();
Alexey Bataev88202be2017-07-27 13:20:36 +00001058 assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1059 "expression for the descriptor is not "
1060 "set.");
1061 TaskgroupDescriptor = I->TaskgroupReductionRef;
Alexey Bataevf189cb72017-07-24 14:52:13 +00001062 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
1063 Data.PrivateCopy, I->DefaultAttrLoc);
Alexey Bataevfa312f32017-07-21 18:48:21 +00001064 }
Alexey Bataevf189cb72017-07-24 14:52:13 +00001065 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001066}
1067
Alexey Bataeve3727102018-04-18 15:57:46 +00001068bool DSAStackTy::isOpenMPLocal(VarDecl *D, iterator Iter) const {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001069 D = D->getCanonicalDecl();
Alexey Bataev852525d2018-03-02 17:17:12 +00001070 if (!isStackEmpty()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001071 iterator I = Iter, E = Stack.back().first.rend();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001072 Scope *TopScope = nullptr;
Alexey Bataev7e6803e2019-01-09 15:58:05 +00001073 while (I != E && !isImplicitOrExplicitTaskingRegion(I->Directive) &&
Alexey Bataev852525d2018-03-02 17:17:12 +00001074 !isOpenMPTargetExecutionDirective(I->Directive))
Alexey Bataevec3da872014-01-31 05:15:34 +00001075 ++I;
Alexey Bataeved09d242014-05-28 05:53:51 +00001076 if (I == E)
1077 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001078 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001079 Scope *CurScope = getCurScope();
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001080 while (CurScope != TopScope && !CurScope->isDeclScope(D))
Alexey Bataev758e55e2013-09-06 18:03:48 +00001081 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +00001082 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001083 }
Alexey Bataevec3da872014-01-31 05:15:34 +00001084 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001085}
1086
Joel E. Dennyd2649292019-01-04 22:11:56 +00001087static bool isConstNotMutableType(Sema &SemaRef, QualType Type,
1088 bool AcceptIfMutable = true,
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001089 bool *IsClassType = nullptr) {
1090 ASTContext &Context = SemaRef.getASTContext();
Joel E. Dennyd2649292019-01-04 22:11:56 +00001091 Type = Type.getNonReferenceType().getCanonicalType();
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001092 bool IsConstant = Type.isConstant(Context);
1093 Type = Context.getBaseElementType(Type);
Joel E. Dennyd2649292019-01-04 22:11:56 +00001094 const CXXRecordDecl *RD = AcceptIfMutable && SemaRef.getLangOpts().CPlusPlus
1095 ? Type->getAsCXXRecordDecl()
1096 : nullptr;
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001097 if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
1098 if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate())
1099 RD = CTD->getTemplatedDecl();
1100 if (IsClassType)
1101 *IsClassType = RD;
1102 return IsConstant && !(SemaRef.getLangOpts().CPlusPlus && RD &&
1103 RD->hasDefinition() && RD->hasMutableFields());
1104}
1105
Joel E. Dennyd2649292019-01-04 22:11:56 +00001106static bool rejectConstNotMutableType(Sema &SemaRef, const ValueDecl *D,
1107 QualType Type, OpenMPClauseKind CKind,
1108 SourceLocation ELoc,
1109 bool AcceptIfMutable = true,
1110 bool ListItemNotVar = false) {
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001111 ASTContext &Context = SemaRef.getASTContext();
1112 bool IsClassType;
Joel E. Dennyd2649292019-01-04 22:11:56 +00001113 if (isConstNotMutableType(SemaRef, Type, AcceptIfMutable, &IsClassType)) {
1114 unsigned Diag = ListItemNotVar
1115 ? diag::err_omp_const_list_item
1116 : IsClassType ? diag::err_omp_const_not_mutable_variable
1117 : diag::err_omp_const_variable;
1118 SemaRef.Diag(ELoc, Diag) << getOpenMPClauseName(CKind);
1119 if (!ListItemNotVar && D) {
1120 const VarDecl *VD = dyn_cast<VarDecl>(D);
1121 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
1122 VarDecl::DeclarationOnly;
1123 SemaRef.Diag(D->getLocation(),
1124 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1125 << D;
1126 }
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001127 return true;
1128 }
1129 return false;
1130}
1131
Alexey Bataeve3727102018-04-18 15:57:46 +00001132const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
1133 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001134 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001135 DSAVarData DVar;
1136
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001137 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001138 auto TI = Threadprivates.find(D);
1139 if (TI != Threadprivates.end()) {
1140 DVar.RefExpr = TI->getSecond().RefExpr.getPointer();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001141 DVar.CKind = OMPC_threadprivate;
1142 return DVar;
Alexey Bataeve3727102018-04-18 15:57:46 +00001143 }
1144 if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
Alexey Bataev817d7f32017-11-14 21:01:01 +00001145 DVar.RefExpr = buildDeclRefExpr(
1146 SemaRef, VD, D->getType().getNonReferenceType(),
1147 VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation());
1148 DVar.CKind = OMPC_threadprivate;
1149 addDSA(D, DVar.RefExpr, OMPC_threadprivate);
Alexey Bataev852525d2018-03-02 17:17:12 +00001150 return DVar;
1151 }
1152 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1153 // in a Construct, C/C++, predetermined, p.1]
1154 // Variables appearing in threadprivate directives are threadprivate.
1155 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
1156 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1157 SemaRef.getLangOpts().OpenMPUseTLS &&
1158 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
1159 (VD && VD->getStorageClass() == SC_Register &&
1160 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
1161 DVar.RefExpr = buildDeclRefExpr(
1162 SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation());
1163 DVar.CKind = OMPC_threadprivate;
1164 addDSA(D, DVar.RefExpr, OMPC_threadprivate);
1165 return DVar;
1166 }
1167 if (SemaRef.getLangOpts().OpenMPCUDAMode && VD &&
1168 VD->isLocalVarDeclOrParm() && !isStackEmpty() &&
1169 !isLoopControlVariable(D).first) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001170 iterator IterTarget =
Alexey Bataev852525d2018-03-02 17:17:12 +00001171 std::find_if(Stack.back().first.rbegin(), Stack.back().first.rend(),
1172 [](const SharingMapTy &Data) {
1173 return isOpenMPTargetExecutionDirective(Data.Directive);
1174 });
1175 if (IterTarget != Stack.back().first.rend()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001176 iterator ParentIterTarget = std::next(IterTarget, 1);
1177 for (iterator Iter = Stack.back().first.rbegin();
1178 Iter != ParentIterTarget; std::advance(Iter, 1)) {
Alexey Bataev852525d2018-03-02 17:17:12 +00001179 if (isOpenMPLocal(VD, Iter)) {
1180 DVar.RefExpr =
1181 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1182 D->getLocation());
1183 DVar.CKind = OMPC_threadprivate;
1184 return DVar;
1185 }
Alexey Bataev852525d2018-03-02 17:17:12 +00001186 }
1187 if (!isClauseParsingMode() || IterTarget != Stack.back().first.rbegin()) {
1188 auto DSAIter = IterTarget->SharingMap.find(D);
1189 if (DSAIter != IterTarget->SharingMap.end() &&
1190 isOpenMPPrivate(DSAIter->getSecond().Attributes)) {
1191 DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer();
1192 DVar.CKind = OMPC_threadprivate;
1193 return DVar;
Alexey Bataeve3727102018-04-18 15:57:46 +00001194 }
1195 iterator End = Stack.back().first.rend();
1196 if (!SemaRef.isOpenMPCapturedByRef(
1197 D, std::distance(ParentIterTarget, End))) {
Alexey Bataev852525d2018-03-02 17:17:12 +00001198 DVar.RefExpr =
1199 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1200 IterTarget->ConstructLoc);
1201 DVar.CKind = OMPC_threadprivate;
1202 return DVar;
1203 }
1204 }
1205 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001206 }
1207
Alexey Bataev4b465392017-04-26 15:06:24 +00001208 if (isStackEmpty())
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001209 // Not in OpenMP execution region and top scope was already checked.
1210 return DVar;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001211
Alexey Bataev758e55e2013-09-06 18:03:48 +00001212 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +00001213 // in a Construct, C/C++, predetermined, p.4]
1214 // Static data members are shared.
1215 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1216 // in a Construct, C/C++, predetermined, p.7]
1217 // Variables with static storage duration that are declared in a scope
1218 // inside the construct are shared.
Alexey Bataeve3727102018-04-18 15:57:46 +00001219 auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; };
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001220 if (VD && VD->isStaticDataMember()) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001221 DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent);
Alexey Bataevdffa93a2015-12-10 08:20:58 +00001222 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
Alexey Bataevec3da872014-01-31 05:15:34 +00001223 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001224
Alexey Bataevdffa93a2015-12-10 08:20:58 +00001225 DVar.CKind = OMPC_shared;
1226 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001227 }
1228
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001229 // The predetermined shared attribute for const-qualified types having no
1230 // mutable members was removed after OpenMP 3.1.
1231 if (SemaRef.LangOpts.OpenMP <= 31) {
1232 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1233 // in a Construct, C/C++, predetermined, p.6]
1234 // Variables with const qualified type having no mutable member are
1235 // shared.
Joel E. Dennyd2649292019-01-04 22:11:56 +00001236 if (isConstNotMutableType(SemaRef, D->getType())) {
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001237 // Variables with const-qualified type having no mutable member may be
1238 // listed in a firstprivate clause, even if they are static data members.
1239 DSAVarData DVarTemp = hasInnermostDSA(
1240 D,
1241 [](OpenMPClauseKind C) {
1242 return C == OMPC_firstprivate || C == OMPC_shared;
1243 },
1244 MatchesAlways, FromParent);
1245 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
1246 return DVarTemp;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001247
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001248 DVar.CKind = OMPC_shared;
1249 return DVar;
1250 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001251 }
1252
Alexey Bataev758e55e2013-09-06 18:03:48 +00001253 // Explicitly specified attributes and local variables with predetermined
1254 // attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +00001255 iterator I = Stack.back().first.rbegin();
1256 iterator EndI = Stack.back().first.rend();
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001257 if (FromParent && I != EndI)
1258 std::advance(I, 1);
Alexey Bataeve3727102018-04-18 15:57:46 +00001259 auto It = I->SharingMap.find(D);
1260 if (It != I->SharingMap.end()) {
1261 const DSAInfo &Data = It->getSecond();
1262 DVar.RefExpr = Data.RefExpr.getPointer();
1263 DVar.PrivateCopy = Data.PrivateCopy;
1264 DVar.CKind = Data.Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001265 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev4d4624c2017-07-20 16:47:47 +00001266 DVar.DKind = I->Directive;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001267 }
1268
1269 return DVar;
1270}
1271
Alexey Bataeve3727102018-04-18 15:57:46 +00001272const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1273 bool FromParent) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001274 if (isStackEmpty()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001275 iterator I;
Alexey Bataev4b465392017-04-26 15:06:24 +00001276 return getDSA(I, D);
1277 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001278 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +00001279 iterator StartI = Stack.back().first.rbegin();
1280 iterator EndI = Stack.back().first.rend();
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001281 if (FromParent && StartI != EndI)
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001282 std::advance(StartI, 1);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001283 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001284}
1285
Alexey Bataeve3727102018-04-18 15:57:46 +00001286const DSAStackTy::DSAVarData
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001287DSAStackTy::hasDSA(ValueDecl *D,
Alexey Bataev97d18bf2018-04-11 19:21:00 +00001288 const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1289 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +00001290 bool FromParent) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001291 if (isStackEmpty())
1292 return {};
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001293 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +00001294 iterator I = Stack.back().first.rbegin();
1295 iterator EndI = Stack.back().first.rend();
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001296 if (FromParent && I != EndI)
Alexey Bataev0e6fc1c2017-04-27 14:46:26 +00001297 std::advance(I, 1);
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001298 for (; I != EndI; std::advance(I, 1)) {
Alexey Bataev7e6803e2019-01-09 15:58:05 +00001299 if (!DPred(I->Directive) && !isImplicitOrExplicitTaskingRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +00001300 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00001301 iterator NewI = I;
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001302 DSAVarData DVar = getDSA(NewI, D);
1303 if (I == NewI && CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001304 return DVar;
Alexey Bataev60859c02017-04-27 15:10:33 +00001305 }
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001306 return {};
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001307}
1308
Alexey Bataeve3727102018-04-18 15:57:46 +00001309const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
Alexey Bataev97d18bf2018-04-11 19:21:00 +00001310 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1311 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +00001312 bool FromParent) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001313 if (isStackEmpty())
1314 return {};
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001315 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +00001316 iterator StartI = Stack.back().first.rbegin();
1317 iterator EndI = Stack.back().first.rend();
Alexey Bataeve3978122016-07-19 05:06:39 +00001318 if (FromParent && StartI != EndI)
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001319 std::advance(StartI, 1);
Alexey Bataeve3978122016-07-19 05:06:39 +00001320 if (StartI == EndI || !DPred(StartI->Directive))
Alexey Bataev4b465392017-04-26 15:06:24 +00001321 return {};
Alexey Bataeve3727102018-04-18 15:57:46 +00001322 iterator NewI = StartI;
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001323 DSAVarData DVar = getDSA(NewI, D);
1324 return (NewI == StartI && CPred(DVar.CKind)) ? DVar : DSAVarData();
Alexey Bataevc5e02582014-06-16 07:08:35 +00001325}
1326
Alexey Bataevaac108a2015-06-23 04:51:00 +00001327bool DSAStackTy::hasExplicitDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00001328 const ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1329 unsigned Level, bool NotLastprivate) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001330 if (isStackEmpty())
1331 return false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001332 D = getCanonicalDecl(D);
Alexey Bataev4b465392017-04-26 15:06:24 +00001333 auto StartI = Stack.back().first.begin();
1334 auto EndI = Stack.back().first.end();
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +00001335 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +00001336 return false;
1337 std::advance(StartI, Level);
Alexey Bataeve3727102018-04-18 15:57:46 +00001338 auto I = StartI->SharingMap.find(D);
Alexey Bataev92b33652018-11-21 19:41:10 +00001339 if ((I != StartI->SharingMap.end()) &&
Alexey Bataeve3727102018-04-18 15:57:46 +00001340 I->getSecond().RefExpr.getPointer() &&
1341 CPred(I->getSecond().Attributes) &&
Alexey Bataev92b33652018-11-21 19:41:10 +00001342 (!NotLastprivate || !I->getSecond().RefExpr.getInt()))
1343 return true;
1344 // Check predetermined rules for the loop control variables.
1345 auto LI = StartI->LCVMap.find(D);
1346 if (LI != StartI->LCVMap.end())
1347 return CPred(OMPC_private);
1348 return false;
Alexey Bataevaac108a2015-06-23 04:51:00 +00001349}
1350
Samuel Antao4be30e92015-10-02 17:14:03 +00001351bool DSAStackTy::hasExplicitDirective(
Alexey Bataeve3727102018-04-18 15:57:46 +00001352 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1353 unsigned Level) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001354 if (isStackEmpty())
1355 return false;
1356 auto StartI = Stack.back().first.begin();
1357 auto EndI = Stack.back().first.end();
Samuel Antao4be30e92015-10-02 17:14:03 +00001358 if (std::distance(StartI, EndI) <= (int)Level)
1359 return false;
1360 std::advance(StartI, Level);
1361 return DPred(StartI->Directive);
1362}
1363
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001364bool DSAStackTy::hasDirective(
1365 const llvm::function_ref<bool(OpenMPDirectiveKind,
1366 const DeclarationNameInfo &, SourceLocation)>
Alexey Bataev97d18bf2018-04-11 19:21:00 +00001367 DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +00001368 bool FromParent) const {
Samuel Antaof0d79752016-05-27 15:21:27 +00001369 // We look only in the enclosing region.
Alexey Bataev4b465392017-04-26 15:06:24 +00001370 if (isStackEmpty())
Samuel Antaof0d79752016-05-27 15:21:27 +00001371 return false;
Alexey Bataev4b465392017-04-26 15:06:24 +00001372 auto StartI = std::next(Stack.back().first.rbegin());
1373 auto EndI = Stack.back().first.rend();
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001374 if (FromParent && StartI != EndI)
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001375 StartI = std::next(StartI);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001376 for (auto I = StartI, EE = EndI; I != EE; ++I) {
1377 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
1378 return true;
1379 }
1380 return false;
1381}
1382
Alexey Bataev758e55e2013-09-06 18:03:48 +00001383void Sema::InitDataSharingAttributesStack() {
1384 VarDataSharingAttributesStack = new DSAStackTy(*this);
1385}
1386
1387#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
1388
Alexey Bataev4b465392017-04-26 15:06:24 +00001389void Sema::pushOpenMPFunctionRegion() {
1390 DSAStack->pushFunction();
1391}
1392
1393void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) {
1394 DSAStack->popFunction(OldFSI);
1395}
1396
Alexey Bataeve3727102018-04-18 15:57:46 +00001397bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level) const {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001398 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1399
Alexey Bataeve3727102018-04-18 15:57:46 +00001400 ASTContext &Ctx = getASTContext();
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001401 bool IsByRef = true;
1402
1403 // Find the directive that is associated with the provided scope.
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00001404 D = cast<ValueDecl>(D->getCanonicalDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00001405 QualType Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001406
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001407 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001408 // This table summarizes how a given variable should be passed to the device
1409 // given its type and the clauses where it appears. This table is based on
1410 // the description in OpenMP 4.5 [2.10.4, target Construct] and
1411 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
1412 //
1413 // =========================================================================
1414 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
1415 // | |(tofrom:scalar)| | pvt | | | |
1416 // =========================================================================
1417 // | scl | | | | - | | bycopy|
1418 // | scl | | - | x | - | - | bycopy|
1419 // | scl | | x | - | - | - | null |
1420 // | scl | x | | | - | | byref |
1421 // | scl | x | - | x | - | - | bycopy|
1422 // | scl | x | x | - | - | - | null |
1423 // | scl | | - | - | - | x | byref |
1424 // | scl | x | - | - | - | x | byref |
1425 //
1426 // | agg | n.a. | | | - | | byref |
1427 // | agg | n.a. | - | x | - | - | byref |
1428 // | agg | n.a. | x | - | - | - | null |
1429 // | agg | n.a. | - | - | - | x | byref |
1430 // | agg | n.a. | - | - | - | x[] | byref |
1431 //
1432 // | ptr | n.a. | | | - | | bycopy|
1433 // | ptr | n.a. | - | x | - | - | bycopy|
1434 // | ptr | n.a. | x | - | - | - | null |
1435 // | ptr | n.a. | - | - | - | x | byref |
1436 // | ptr | n.a. | - | - | - | x[] | bycopy|
1437 // | ptr | n.a. | - | - | x | | bycopy|
1438 // | ptr | n.a. | - | - | x | x | bycopy|
1439 // | ptr | n.a. | - | - | x | x[] | bycopy|
1440 // =========================================================================
1441 // Legend:
1442 // scl - scalar
1443 // ptr - pointer
1444 // agg - aggregate
1445 // x - applies
1446 // - - invalid in this combination
1447 // [] - mapped with an array section
1448 // byref - should be mapped by reference
1449 // byval - should be mapped by value
1450 // null - initialize a local variable to null on the device
1451 //
1452 // Observations:
1453 // - All scalar declarations that show up in a map clause have to be passed
1454 // by reference, because they may have been mapped in the enclosing data
1455 // environment.
1456 // - If the scalar value does not fit the size of uintptr, it has to be
1457 // passed by reference, regardless the result in the table above.
1458 // - For pointers mapped by value that have either an implicit map or an
1459 // array section, the runtime library may pass the NULL value to the
1460 // device instead of the value passed to it by the compiler.
1461
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001462 if (Ty->isReferenceType())
1463 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
Samuel Antao86ace552016-04-27 22:40:57 +00001464
1465 // Locate map clauses and see if the variable being captured is referred to
1466 // in any of those clauses. Here we only care about variables, not fields,
1467 // because fields are part of aggregates.
1468 bool IsVariableUsedInMapClause = false;
1469 bool IsVariableAssociatedWithSection = false;
1470
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +00001471 DSAStack->checkMappableExprComponentListsForDeclAtLevel(
Alexey Bataeve3727102018-04-18 15:57:46 +00001472 D, Level,
1473 [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D](
1474 OMPClauseMappableExprCommon::MappableExprComponentListRef
Samuel Antao6890b092016-07-28 14:25:09 +00001475 MapExprComponents,
1476 OpenMPClauseKind WhereFoundClauseKind) {
1477 // Only the map clause information influences how a variable is
1478 // captured. E.g. is_device_ptr does not require changing the default
Samuel Antao4c8035b2016-12-12 18:00:20 +00001479 // behavior.
Samuel Antao6890b092016-07-28 14:25:09 +00001480 if (WhereFoundClauseKind != OMPC_map)
1481 return false;
Samuel Antao86ace552016-04-27 22:40:57 +00001482
1483 auto EI = MapExprComponents.rbegin();
1484 auto EE = MapExprComponents.rend();
1485
1486 assert(EI != EE && "Invalid map expression!");
1487
1488 if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
1489 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
1490
1491 ++EI;
1492 if (EI == EE)
1493 return false;
1494
1495 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
1496 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
1497 isa<MemberExpr>(EI->getAssociatedExpression())) {
1498 IsVariableAssociatedWithSection = true;
1499 // There is nothing more we need to know about this variable.
1500 return true;
1501 }
1502
1503 // Keep looking for more map info.
1504 return false;
1505 });
1506
1507 if (IsVariableUsedInMapClause) {
1508 // If variable is identified in a map clause it is always captured by
1509 // reference except if it is a pointer that is dereferenced somehow.
1510 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
1511 } else {
Alexey Bataev3f96fe62017-12-13 17:31:39 +00001512 // By default, all the data that has a scalar type is mapped by copy
1513 // (except for reduction variables).
1514 IsByRef =
Alexey Bataev60705422018-10-30 15:50:12 +00001515 (DSAStack->isForceCaptureByReferenceInTargetExecutable() &&
1516 !Ty->isAnyPointerType()) ||
Alexey Bataev3f96fe62017-12-13 17:31:39 +00001517 !Ty->isScalarType() ||
1518 DSAStack->getDefaultDMAAtLevel(Level) == DMA_tofrom_scalar ||
1519 DSAStack->hasExplicitDSA(
1520 D, [](OpenMPClauseKind K) { return K == OMPC_reduction; }, Level);
Samuel Antao86ace552016-04-27 22:40:57 +00001521 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001522 }
1523
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001524 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00001525 IsByRef =
Alexey Bataev60705422018-10-30 15:50:12 +00001526 ((DSAStack->isForceCaptureByReferenceInTargetExecutable() &&
1527 !Ty->isAnyPointerType()) ||
1528 !DSAStack->hasExplicitDSA(
1529 D,
1530 [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; },
1531 Level, /*NotLastprivate=*/true)) &&
Alexey Bataev8e769ee2017-12-22 21:01:52 +00001532 // If the variable is artificial and must be captured by value - try to
1533 // capture by value.
Alexey Bataevd2202ca2017-12-27 17:58:32 +00001534 !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() &&
1535 !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue());
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001536 }
1537
Samuel Antao86ace552016-04-27 22:40:57 +00001538 // When passing data by copy, we need to make sure it fits the uintptr size
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001539 // and alignment, because the runtime library only deals with uintptr types.
1540 // If it does not fit the uintptr size, we need to pass the data by reference
1541 // instead.
1542 if (!IsByRef &&
1543 (Ctx.getTypeSizeInChars(Ty) >
1544 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001545 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001546 IsByRef = true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001547 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001548
1549 return IsByRef;
1550}
1551
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001552unsigned Sema::getOpenMPNestingLevel() const {
1553 assert(getLangOpts().OpenMP);
1554 return DSAStack->getNestingLevel();
1555}
1556
Jonas Hahnfeld87d44262017-11-18 21:00:46 +00001557bool Sema::isInOpenMPTargetExecutionDirective() const {
1558 return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) &&
1559 !DSAStack->isClauseParsingMode()) ||
1560 DSAStack->hasDirective(
1561 [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
1562 SourceLocation) -> bool {
1563 return isOpenMPTargetExecutionDirective(K);
1564 },
1565 false);
1566}
1567
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001568VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D) {
Alexey Bataevf841bd92014-12-16 07:00:22 +00001569 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001570 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +00001571
1572 // If we are attempting to capture a global variable in a directive with
1573 // 'target' we return true so that this global is also mapped to the device.
1574 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001575 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001576 if (VD && !VD->hasLocalStorage()) {
1577 if (isInOpenMPDeclareTargetContext() &&
1578 (getCurCapturedRegion() || getCurBlock() || getCurLambda())) {
1579 // Try to mark variable as declare target if it is used in capturing
1580 // regions.
Alexey Bataev97b72212018-08-14 18:31:20 +00001581 if (!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001582 checkDeclIsAllowedInOpenMPTarget(nullptr, VD);
Alexey Bataev92327c52018-03-26 16:40:55 +00001583 return nullptr;
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001584 } else if (isInOpenMPTargetExecutionDirective()) {
1585 // If the declaration is enclosed in a 'declare target' directive,
1586 // then it should not be captured.
1587 //
Alexey Bataev97b72212018-08-14 18:31:20 +00001588 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001589 return nullptr;
1590 return VD;
1591 }
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001592 }
Alexey Bataev60705422018-10-30 15:50:12 +00001593 // Capture variables captured by reference in lambdas for target-based
1594 // directives.
1595 if (VD && !DSAStack->isClauseParsingMode()) {
1596 if (const auto *RD = VD->getType()
1597 .getCanonicalType()
1598 .getNonReferenceType()
1599 ->getAsCXXRecordDecl()) {
1600 bool SavedForceCaptureByReferenceInTargetExecutable =
1601 DSAStack->isForceCaptureByReferenceInTargetExecutable();
1602 DSAStack->setForceCaptureByReferenceInTargetExecutable(/*V=*/true);
Alexey Bataevd1840e52018-11-16 21:13:33 +00001603 if (RD->isLambda()) {
1604 llvm::DenseMap<const VarDecl *, FieldDecl *> Captures;
1605 FieldDecl *ThisCapture;
1606 RD->getCaptureFields(Captures, ThisCapture);
Alexey Bataev60705422018-10-30 15:50:12 +00001607 for (const LambdaCapture &LC : RD->captures()) {
1608 if (LC.getCaptureKind() == LCK_ByRef) {
1609 VarDecl *VD = LC.getCapturedVar();
1610 DeclContext *VDC = VD->getDeclContext();
1611 if (!VDC->Encloses(CurContext))
1612 continue;
1613 DSAStackTy::DSAVarData DVarPrivate =
1614 DSAStack->getTopDSA(VD, /*FromParent=*/false);
1615 // Do not capture already captured variables.
1616 if (!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) &&
1617 DVarPrivate.CKind == OMPC_unknown &&
1618 !DSAStack->checkMappableExprComponentListsForDecl(
1619 D, /*CurrentRegionOnly=*/true,
1620 [](OMPClauseMappableExprCommon::
1621 MappableExprComponentListRef,
1622 OpenMPClauseKind) { return true; }))
1623 MarkVariableReferenced(LC.getLocation(), LC.getCapturedVar());
1624 } else if (LC.getCaptureKind() == LCK_This) {
Alexey Bataevd1840e52018-11-16 21:13:33 +00001625 QualType ThisTy = getCurrentThisType();
1626 if (!ThisTy.isNull() &&
1627 Context.typesAreCompatible(ThisTy, ThisCapture->getType()))
1628 CheckCXXThisCapture(LC.getLocation());
Alexey Bataev60705422018-10-30 15:50:12 +00001629 }
1630 }
Alexey Bataevd1840e52018-11-16 21:13:33 +00001631 }
Alexey Bataev60705422018-10-30 15:50:12 +00001632 DSAStack->setForceCaptureByReferenceInTargetExecutable(
1633 SavedForceCaptureByReferenceInTargetExecutable);
1634 }
1635 }
Samuel Antao4be30e92015-10-02 17:14:03 +00001636
Alexey Bataev48977c32015-08-04 08:10:48 +00001637 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
1638 (!DSAStack->isClauseParsingMode() ||
1639 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001640 auto &&Info = DSAStack->isLoopControlVariable(D);
1641 if (Info.first ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001642 (VD && VD->hasLocalStorage() &&
Alexey Bataev7e6803e2019-01-09 15:58:05 +00001643 isImplicitOrExplicitTaskingRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001644 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001645 return VD ? VD : Info.second;
Alexey Bataeve3727102018-04-18 15:57:46 +00001646 DSAStackTy::DSAVarData DVarPrivate =
1647 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001648 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
Alexey Bataev90c228f2016-02-08 09:29:13 +00001649 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00001650 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate,
1651 [](OpenMPDirectiveKind) { return true; },
1652 DSAStack->isClauseParsingMode());
Alexey Bataev90c228f2016-02-08 09:29:13 +00001653 if (DVarPrivate.CKind != OMPC_unknown)
1654 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001655 }
Alexey Bataev90c228f2016-02-08 09:29:13 +00001656 return nullptr;
Alexey Bataevf841bd92014-12-16 07:00:22 +00001657}
1658
Alexey Bataevdfa430f2017-12-08 15:03:50 +00001659void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex,
1660 unsigned Level) const {
1661 SmallVector<OpenMPDirectiveKind, 4> Regions;
1662 getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level));
1663 FunctionScopesIndex -= Regions.size();
1664}
1665
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00001666void Sema::startOpenMPLoop() {
1667 assert(LangOpts.OpenMP && "OpenMP must be enabled.");
1668 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective()))
1669 DSAStack->loopInit();
1670}
1671
Alexey Bataeve3727102018-04-18 15:57:46 +00001672bool Sema::isOpenMPPrivateDecl(const ValueDecl *D, unsigned Level) const {
Alexey Bataevaac108a2015-06-23 04:51:00 +00001673 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00001674 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
1675 if (DSAStack->getAssociatedLoops() > 0 &&
1676 !DSAStack->isLoopStarted()) {
1677 DSAStack->resetPossibleLoopCounter(D);
1678 DSAStack->loopStart();
1679 return true;
1680 }
1681 if ((DSAStack->getPossiblyLoopCunter() == D->getCanonicalDecl() ||
1682 DSAStack->isLoopControlVariable(D).first) &&
1683 !DSAStack->hasExplicitDSA(
1684 D, [](OpenMPClauseKind K) { return K != OMPC_private; }, Level) &&
1685 !isOpenMPSimdDirective(DSAStack->getCurrentDirective()))
1686 return true;
1687 }
Alexey Bataevaac108a2015-06-23 04:51:00 +00001688 return DSAStack->hasExplicitDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00001689 D, [](OpenMPClauseKind K) { return K == OMPC_private; }, Level) ||
Alexey Bataev3f82cfc2017-12-13 15:28:44 +00001690 (DSAStack->isClauseParsingMode() &&
1691 DSAStack->getClauseParsingMode() == OMPC_private) ||
Alexey Bataev88202be2017-07-27 13:20:36 +00001692 // Consider taskgroup reduction descriptor variable a private to avoid
1693 // possible capture in the region.
1694 (DSAStack->hasExplicitDirective(
1695 [](OpenMPDirectiveKind K) { return K == OMPD_taskgroup; },
1696 Level) &&
1697 DSAStack->isTaskgroupReductionRef(D, Level));
Alexey Bataevaac108a2015-06-23 04:51:00 +00001698}
1699
Alexey Bataeve3727102018-04-18 15:57:46 +00001700void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D,
1701 unsigned Level) {
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001702 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1703 D = getCanonicalDecl(D);
1704 OpenMPClauseKind OMPC = OMPC_unknown;
1705 for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) {
1706 const unsigned NewLevel = I - 1;
1707 if (DSAStack->hasExplicitDSA(D,
1708 [&OMPC](const OpenMPClauseKind K) {
1709 if (isOpenMPPrivate(K)) {
1710 OMPC = K;
1711 return true;
1712 }
1713 return false;
1714 },
1715 NewLevel))
1716 break;
1717 if (DSAStack->checkMappableExprComponentListsForDeclAtLevel(
1718 D, NewLevel,
1719 [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
1720 OpenMPClauseKind) { return true; })) {
1721 OMPC = OMPC_map;
1722 break;
1723 }
1724 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1725 NewLevel)) {
Alexey Bataev63cc8e92018-03-20 14:45:59 +00001726 OMPC = OMPC_map;
1727 if (D->getType()->isScalarType() &&
1728 DSAStack->getDefaultDMAAtLevel(NewLevel) !=
1729 DefaultMapAttributes::DMA_tofrom_scalar)
1730 OMPC = OMPC_firstprivate;
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001731 break;
1732 }
1733 }
1734 if (OMPC != OMPC_unknown)
1735 FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, OMPC));
1736}
1737
Alexey Bataeve3727102018-04-18 15:57:46 +00001738bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D,
1739 unsigned Level) const {
Samuel Antao4be30e92015-10-02 17:14:03 +00001740 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1741 // Return true if the current level is no longer enclosed in a target region.
1742
Alexey Bataeve3727102018-04-18 15:57:46 +00001743 const auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001744 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00001745 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1746 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +00001747}
1748
Alexey Bataeved09d242014-05-28 05:53:51 +00001749void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001750
1751void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
1752 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001753 Scope *CurScope, SourceLocation Loc) {
1754 DSAStack->push(DKind, DirName, CurScope, Loc);
Faisal Valid143a0c2017-04-01 21:30:49 +00001755 PushExpressionEvaluationContext(
1756 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001757}
1758
Alexey Bataevaac108a2015-06-23 04:51:00 +00001759void Sema::StartOpenMPClause(OpenMPClauseKind K) {
1760 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001761}
1762
Alexey Bataevaac108a2015-06-23 04:51:00 +00001763void Sema::EndOpenMPClause() {
1764 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001765}
1766
Alexey Bataev758e55e2013-09-06 18:03:48 +00001767void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001768 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
1769 // A variable of class type (or array thereof) that appears in a lastprivate
1770 // clause requires an accessible, unambiguous default constructor for the
1771 // class type, unless the list item is also specified in a firstprivate
1772 // clause.
Alexey Bataeve3727102018-04-18 15:57:46 +00001773 if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
1774 for (OMPClause *C : D->clauses()) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001775 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
1776 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeve3727102018-04-18 15:57:46 +00001777 for (Expr *DE : Clause->varlists()) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001778 if (DE->isValueDependent() || DE->isTypeDependent()) {
1779 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001780 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +00001781 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00001782 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
Alexey Bataeve3727102018-04-18 15:57:46 +00001783 auto *VD = cast<VarDecl>(DRE->getDecl());
Alexey Bataev005248a2016-02-25 05:25:57 +00001784 QualType Type = VD->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +00001785 const DSAStackTy::DSAVarData DVar =
1786 DSAStack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001787 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001788 // Generate helper private variable and initialize it with the
1789 // default value. The address of the original variable is replaced
1790 // by the address of the new private variable in CodeGen. This new
1791 // variable is not added to IdResolver, so the code in the OpenMP
1792 // region uses original variable for proper diagnostics.
Alexey Bataeve3727102018-04-18 15:57:46 +00001793 VarDecl *VDPrivate = buildVarDecl(
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001794 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev63cc8e92018-03-20 14:45:59 +00001795 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE);
Richard Smith3beb7c62017-01-12 02:27:38 +00001796 ActOnUninitializedDecl(VDPrivate);
Alexey Bataev38e89532015-04-16 04:54:05 +00001797 if (VDPrivate->isInvalidDecl())
1798 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001799 PrivateCopies.push_back(buildDeclRefExpr(
1800 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001801 } else {
1802 // The variable is also a firstprivate, so initialization sequence
1803 // for private copy is generated already.
1804 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001805 }
1806 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001807 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001808 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001809 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001810 }
1811 }
1812 }
1813
Alexey Bataev758e55e2013-09-06 18:03:48 +00001814 DSAStack->pop();
1815 DiscardCleanupsInEvaluationContext();
1816 PopExpressionEvaluationContext();
1817}
1818
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001819static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1820 Expr *NumIterations, Sema &SemaRef,
1821 Scope *S, DSAStackTy *Stack);
Alexander Musman3276a272015-03-21 10:12:56 +00001822
Alexey Bataeva769e072013-03-22 06:34:35 +00001823namespace {
1824
Alexey Bataeve3727102018-04-18 15:57:46 +00001825class VarDeclFilterCCC final : public CorrectionCandidateCallback {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001826private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001827 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001828
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001829public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001830 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001831 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001832 NamedDecl *ND = Candidate.getCorrectionDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +00001833 if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001834 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001835 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1836 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001837 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001838 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001839 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001840};
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001841
Alexey Bataeve3727102018-04-18 15:57:46 +00001842class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001843private:
1844 Sema &SemaRef;
1845
1846public:
1847 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
1848 bool ValidateCandidate(const TypoCorrection &Candidate) override {
1849 NamedDecl *ND = Candidate.getCorrectionDecl();
Kelvin Li59e3d192017-11-30 18:52:06 +00001850 if (ND && (isa<VarDecl>(ND) || isa<FunctionDecl>(ND))) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001851 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1852 SemaRef.getCurScope());
1853 }
1854 return false;
1855 }
1856};
1857
Alexey Bataeved09d242014-05-28 05:53:51 +00001858} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001859
1860ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1861 CXXScopeSpec &ScopeSpec,
1862 const DeclarationNameInfo &Id) {
1863 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1864 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1865
1866 if (Lookup.isAmbiguous())
1867 return ExprError();
1868
1869 VarDecl *VD;
1870 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001871 if (TypoCorrection Corrected = CorrectTypo(
1872 Id, LookupOrdinaryName, CurScope, nullptr,
1873 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001874 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001875 PDiag(Lookup.empty()
1876 ? diag::err_undeclared_var_use_suggest
1877 : diag::err_omp_expected_var_arg_suggest)
1878 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001879 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001880 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001881 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1882 : diag::err_omp_expected_var_arg)
1883 << Id.getName();
1884 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001885 }
Alexey Bataeve3727102018-04-18 15:57:46 +00001886 } else if (!(VD = Lookup.getAsSingle<VarDecl>())) {
1887 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
1888 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1889 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001890 }
1891 Lookup.suppressDiagnostics();
1892
1893 // OpenMP [2.9.2, Syntax, C/C++]
1894 // Variables must be file-scope, namespace-scope, or static block-scope.
1895 if (!VD->hasGlobalStorage()) {
1896 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001897 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1898 bool IsDecl =
1899 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001900 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001901 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1902 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001903 return ExprError();
1904 }
1905
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001906 VarDecl *CanonicalVD = VD->getCanonicalDecl();
George Burgess IV00f70bd2018-03-01 05:43:23 +00001907 NamedDecl *ND = CanonicalVD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001908 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1909 // A threadprivate directive for file-scope variables must appear outside
1910 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001911 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1912 !getCurLexicalContext()->isTranslationUnit()) {
1913 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001914 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1915 bool IsDecl =
1916 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1917 Diag(VD->getLocation(),
1918 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1919 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001920 return ExprError();
1921 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001922 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1923 // A threadprivate directive for static class member variables must appear
1924 // in the class definition, in the same scope in which the member
1925 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001926 if (CanonicalVD->isStaticDataMember() &&
1927 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1928 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001929 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1930 bool IsDecl =
1931 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1932 Diag(VD->getLocation(),
1933 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1934 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001935 return ExprError();
1936 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001937 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1938 // A threadprivate directive for namespace-scope variables must appear
1939 // outside any definition or declaration other than the namespace
1940 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001941 if (CanonicalVD->getDeclContext()->isNamespace() &&
1942 (!getCurLexicalContext()->isFileContext() ||
1943 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1944 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001945 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1946 bool IsDecl =
1947 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1948 Diag(VD->getLocation(),
1949 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1950 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001951 return ExprError();
1952 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001953 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1954 // A threadprivate directive for static block-scope variables must appear
1955 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001956 if (CanonicalVD->isStaticLocal() && CurScope &&
1957 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001958 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001959 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1960 bool IsDecl =
1961 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1962 Diag(VD->getLocation(),
1963 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1964 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001965 return ExprError();
1966 }
1967
1968 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1969 // A threadprivate directive must lexically precede all references to any
1970 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001971 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001972 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001973 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001974 return ExprError();
1975 }
1976
1977 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev376b4a42016-02-09 09:41:09 +00001978 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1979 SourceLocation(), VD,
1980 /*RefersToEnclosingVariableOrCapture=*/false,
1981 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001982}
1983
Alexey Bataeved09d242014-05-28 05:53:51 +00001984Sema::DeclGroupPtrTy
1985Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1986 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001987 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001988 CurContext->addDecl(D);
1989 return DeclGroupPtrTy::make(DeclGroupRef(D));
1990 }
David Blaikie0403cb12016-01-15 23:43:25 +00001991 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001992}
1993
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001994namespace {
Alexey Bataeve3727102018-04-18 15:57:46 +00001995class LocalVarRefChecker final
1996 : public ConstStmtVisitor<LocalVarRefChecker, bool> {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001997 Sema &SemaRef;
1998
1999public:
2000 bool VisitDeclRefExpr(const DeclRefExpr *E) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002001 if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002002 if (VD->hasLocalStorage()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002003 SemaRef.Diag(E->getBeginLoc(),
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002004 diag::err_omp_local_var_in_threadprivate_init)
2005 << E->getSourceRange();
2006 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
2007 << VD << VD->getSourceRange();
2008 return true;
2009 }
2010 }
2011 return false;
2012 }
2013 bool VisitStmt(const Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002014 for (const Stmt *Child : S->children()) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002015 if (Child && Visit(Child))
2016 return true;
2017 }
2018 return false;
2019 }
Alexey Bataev23b69422014-06-18 07:08:49 +00002020 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002021};
2022} // namespace
2023
Alexey Bataeved09d242014-05-28 05:53:51 +00002024OMPThreadPrivateDecl *
2025Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002026 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +00002027 for (Expr *RefExpr : VarList) {
2028 auto *DE = cast<DeclRefExpr>(RefExpr);
2029 auto *VD = cast<VarDecl>(DE->getDecl());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002030 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00002031
Alexey Bataev376b4a42016-02-09 09:41:09 +00002032 // Mark variable as used.
2033 VD->setReferenced();
2034 VD->markUsed(Context);
2035
Alexey Bataevf56f98c2015-04-16 05:39:01 +00002036 QualType QType = VD->getType();
2037 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
2038 // It will be analyzed later.
2039 Vars.push_back(DE);
2040 continue;
2041 }
2042
Alexey Bataeva769e072013-03-22 06:34:35 +00002043 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
2044 // A threadprivate variable must not have an incomplete type.
2045 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002046 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00002047 continue;
2048 }
2049
2050 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
2051 // A threadprivate variable must not have a reference type.
2052 if (VD->getType()->isReferenceType()) {
2053 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00002054 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
2055 bool IsDecl =
2056 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2057 Diag(VD->getLocation(),
2058 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2059 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00002060 continue;
2061 }
2062
Samuel Antaof8b50122015-07-13 22:54:53 +00002063 // Check if this is a TLS variable. If TLS is not being supported, produce
2064 // the corresponding diagnostic.
2065 if ((VD->getTLSKind() != VarDecl::TLS_None &&
2066 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
2067 getLangOpts().OpenMPUseTLS &&
2068 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00002069 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
2070 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00002071 Diag(ILoc, diag::err_omp_var_thread_local)
2072 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00002073 bool IsDecl =
2074 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2075 Diag(VD->getLocation(),
2076 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2077 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00002078 continue;
2079 }
2080
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002081 // Check if initial value of threadprivate variable reference variable with
2082 // local storage (it is not supported by runtime).
Alexey Bataeve3727102018-04-18 15:57:46 +00002083 if (const Expr *Init = VD->getAnyInitializer()) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002084 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002085 if (Checker.Visit(Init))
2086 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002087 }
2088
Alexey Bataeved09d242014-05-28 05:53:51 +00002089 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00002090 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00002091 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
2092 Context, SourceRange(Loc, Loc)));
Alexey Bataeve3727102018-04-18 15:57:46 +00002093 if (ASTMutationListener *ML = Context.getASTMutationListener())
Alexey Bataev97720002014-11-11 04:05:39 +00002094 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00002095 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002096 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00002097 if (!Vars.empty()) {
2098 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
2099 Vars);
2100 D->setAccess(AS_public);
2101 }
2102 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00002103}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002104
Kelvin Li1408f912018-09-26 04:28:39 +00002105Sema::DeclGroupPtrTy
2106Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc,
2107 ArrayRef<OMPClause *> ClauseList) {
2108 OMPRequiresDecl *D = nullptr;
2109 if (!CurContext->isFileContext()) {
2110 Diag(Loc, diag::err_omp_invalid_scope) << "requires";
2111 } else {
2112 D = CheckOMPRequiresDecl(Loc, ClauseList);
2113 if (D) {
2114 CurContext->addDecl(D);
2115 DSAStack->addRequiresDecl(D);
2116 }
2117 }
2118 return DeclGroupPtrTy::make(DeclGroupRef(D));
2119}
2120
2121OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc,
2122 ArrayRef<OMPClause *> ClauseList) {
2123 if (!DSAStack->hasDuplicateRequiresClause(ClauseList))
2124 return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc,
2125 ClauseList);
2126 return nullptr;
2127}
2128
Alexey Bataeve3727102018-04-18 15:57:46 +00002129static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
2130 const ValueDecl *D,
2131 const DSAStackTy::DSAVarData &DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00002132 bool IsLoopIterVar = false) {
2133 if (DVar.RefExpr) {
2134 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
2135 << getOpenMPClauseName(DVar.CKind);
2136 return;
2137 }
2138 enum {
2139 PDSA_StaticMemberShared,
2140 PDSA_StaticLocalVarShared,
2141 PDSA_LoopIterVarPrivate,
2142 PDSA_LoopIterVarLinear,
2143 PDSA_LoopIterVarLastprivate,
2144 PDSA_ConstVarShared,
2145 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002146 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00002147 PDSA_LocalVarPrivate,
2148 PDSA_Implicit
2149 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00002150 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002151 auto ReportLoc = D->getLocation();
2152 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002153 if (IsLoopIterVar) {
2154 if (DVar.CKind == OMPC_private)
2155 Reason = PDSA_LoopIterVarPrivate;
2156 else if (DVar.CKind == OMPC_lastprivate)
2157 Reason = PDSA_LoopIterVarLastprivate;
2158 else
2159 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev35aaee62016-04-13 13:36:48 +00002160 } else if (isOpenMPTaskingDirective(DVar.DKind) &&
2161 DVar.CKind == OMPC_firstprivate) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002162 Reason = PDSA_TaskVarFirstprivate;
2163 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002164 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00002165 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002166 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00002167 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002168 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00002169 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002170 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00002171 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002172 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00002173 ReportHint = true;
2174 Reason = PDSA_LocalVarPrivate;
2175 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00002176 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002177 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00002178 << Reason << ReportHint
2179 << getOpenMPDirectiveName(Stack->getCurrentDirective());
2180 } else if (DVar.ImplicitDSALoc.isValid()) {
2181 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
2182 << getOpenMPClauseName(DVar.CKind);
2183 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00002184}
2185
Alexey Bataev758e55e2013-09-06 18:03:48 +00002186namespace {
Alexey Bataeve3727102018-04-18 15:57:46 +00002187class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> {
Alexey Bataev758e55e2013-09-06 18:03:48 +00002188 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00002189 Sema &SemaRef;
Alexey Bataeve3727102018-04-18 15:57:46 +00002190 bool ErrorFound = false;
2191 CapturedStmt *CS = nullptr;
2192 llvm::SmallVector<Expr *, 4> ImplicitFirstprivate;
2193 llvm::SmallVector<Expr *, 4> ImplicitMap;
2194 Sema::VarsWithInheritedDSAType VarsWithInheritedDSA;
2195 llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations;
Alexey Bataeved09d242014-05-28 05:53:51 +00002196
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00002197 void VisitSubCaptures(OMPExecutableDirective *S) {
2198 // Check implicitly captured variables.
2199 if (!S->hasAssociatedStmt() || !S->getAssociatedStmt())
2200 return;
2201 for (const CapturedStmt::Capture &Cap :
2202 S->getInnermostCapturedStmt()->captures()) {
2203 if (!Cap.capturesVariable())
2204 continue;
2205 VarDecl *VD = Cap.getCapturedVar();
2206 // Do not try to map the variable if it or its sub-component was mapped
2207 // already.
2208 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) &&
2209 Stack->checkMappableExprComponentListsForDecl(
2210 VD, /*CurrentRegionOnly=*/true,
2211 [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
2212 OpenMPClauseKind) { return true; }))
2213 continue;
2214 DeclRefExpr *DRE = buildDeclRefExpr(
2215 SemaRef, VD, VD->getType().getNonLValueExprType(SemaRef.Context),
2216 Cap.getLocation(), /*RefersToCapture=*/true);
2217 Visit(DRE);
2218 }
2219 }
2220
Alexey Bataev758e55e2013-09-06 18:03:48 +00002221public:
2222 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00002223 if (E->isTypeDependent() || E->isValueDependent() ||
2224 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
2225 return;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002226 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00002227 VD = VD->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002228 // Skip internally declared variables.
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002229 if (VD->hasLocalStorage() && !CS->capturesVariable(VD))
Alexey Bataeved09d242014-05-28 05:53:51 +00002230 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002231
Alexey Bataeve3727102018-04-18 15:57:46 +00002232 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002233 // Check if the variable has explicit DSA set and stop analysis if it so.
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00002234 if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second)
David Majnemer9d168222016-08-05 17:44:54 +00002235 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002236
Alexey Bataevafe50572017-10-06 17:00:28 +00002237 // Skip internally declared static variables.
Alexey Bataev92327c52018-03-26 16:40:55 +00002238 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
Alexey Bataev97b72212018-08-14 18:31:20 +00002239 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
Alexey Bataev92327c52018-03-26 16:40:55 +00002240 if (VD->hasGlobalStorage() && !CS->capturesVariable(VD) &&
2241 (!Res || *Res != OMPDeclareTargetDeclAttr::MT_Link))
Alexey Bataevafe50572017-10-06 17:00:28 +00002242 return;
2243
Alexey Bataeve3727102018-04-18 15:57:46 +00002244 SourceLocation ELoc = E->getExprLoc();
2245 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002246 // The default(none) clause requires that each variable that is referenced
2247 // in the construct, and does not have a predetermined data-sharing
2248 // attribute, must have its data-sharing attribute explicitly determined
2249 // by being listed in a data-sharing attribute clause.
2250 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev7e6803e2019-01-09 15:58:05 +00002251 isImplicitOrExplicitTaskingRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00002252 VarsWithInheritedDSA.count(VD) == 0) {
2253 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002254 return;
2255 }
2256
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002257 if (isOpenMPTargetExecutionDirective(DKind) &&
2258 !Stack->isLoopControlVariable(VD).first) {
2259 if (!Stack->checkMappableExprComponentListsForDecl(
2260 VD, /*CurrentRegionOnly=*/true,
2261 [](OMPClauseMappableExprCommon::MappableExprComponentListRef
2262 StackComponents,
2263 OpenMPClauseKind) {
2264 // Variable is used if it has been marked as an array, array
2265 // section or the variable iself.
2266 return StackComponents.size() == 1 ||
2267 std::all_of(
2268 std::next(StackComponents.rbegin()),
2269 StackComponents.rend(),
2270 [](const OMPClauseMappableExprCommon::
2271 MappableComponent &MC) {
2272 return MC.getAssociatedDeclaration() ==
2273 nullptr &&
2274 (isa<OMPArraySectionExpr>(
2275 MC.getAssociatedExpression()) ||
2276 isa<ArraySubscriptExpr>(
2277 MC.getAssociatedExpression()));
2278 });
2279 })) {
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002280 bool IsFirstprivate = false;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002281 // By default lambdas are captured as firstprivates.
2282 if (const auto *RD =
2283 VD->getType().getNonReferenceType()->getAsCXXRecordDecl())
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002284 IsFirstprivate = RD->isLambda();
2285 IsFirstprivate =
2286 IsFirstprivate ||
2287 (VD->getType().getNonReferenceType()->isScalarType() &&
Alexey Bataev92327c52018-03-26 16:40:55 +00002288 Stack->getDefaultDMA() != DMA_tofrom_scalar && !Res);
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002289 if (IsFirstprivate)
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002290 ImplicitFirstprivate.emplace_back(E);
2291 else
2292 ImplicitMap.emplace_back(E);
2293 return;
2294 }
2295 }
2296
Alexey Bataev758e55e2013-09-06 18:03:48 +00002297 // OpenMP [2.9.3.6, Restrictions, p.2]
2298 // A list item that appears in a reduction clause of the innermost
2299 // enclosing worksharing or parallel construct may not be accessed in an
2300 // explicit task.
Alexey Bataev7ace49d2016-05-17 08:55:33 +00002301 DVar = Stack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00002302 VD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
2303 [](OpenMPDirectiveKind K) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00002304 return isOpenMPParallelDirective(K) ||
2305 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
2306 },
Alexey Bataeveffbdf12017-07-21 17:24:30 +00002307 /*FromParent=*/true);
Alexey Bataev35aaee62016-04-13 13:36:48 +00002308 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00002309 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00002310 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
Alexey Bataeve3727102018-04-18 15:57:46 +00002311 reportOriginalDsa(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002312 return;
2313 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002314
2315 // Define implicit data-sharing attributes for task.
Alexey Bataeve3727102018-04-18 15:57:46 +00002316 DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00002317 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
2318 !Stack->isLoopControlVariable(VD).first)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002319 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002320 }
2321 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002322 void VisitMemberExpr(MemberExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00002323 if (E->isTypeDependent() || E->isValueDependent() ||
2324 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
2325 return;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002326 auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002327 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
Patrick Lystere13b1e32019-01-02 19:28:48 +00002328 if (auto *TE = dyn_cast<CXXThisExpr>(E->getBase()->IgnoreParens())) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002329 if (!FD)
2330 return;
Alexey Bataeve3727102018-04-18 15:57:46 +00002331 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002332 // Check if the variable has explicit DSA set and stop analysis if it
2333 // so.
2334 if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second)
2335 return;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002336
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002337 if (isOpenMPTargetExecutionDirective(DKind) &&
2338 !Stack->isLoopControlVariable(FD).first &&
2339 !Stack->checkMappableExprComponentListsForDecl(
2340 FD, /*CurrentRegionOnly=*/true,
2341 [](OMPClauseMappableExprCommon::MappableExprComponentListRef
2342 StackComponents,
2343 OpenMPClauseKind) {
2344 return isa<CXXThisExpr>(
2345 cast<MemberExpr>(
2346 StackComponents.back().getAssociatedExpression())
2347 ->getBase()
2348 ->IgnoreParens());
2349 })) {
2350 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
2351 // A bit-field cannot appear in a map clause.
2352 //
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002353 if (FD->isBitField())
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002354 return;
Patrick Lystere13b1e32019-01-02 19:28:48 +00002355
2356 // Check to see if the member expression is referencing a class that
2357 // has already been explicitly mapped
2358 if (Stack->isClassPreviouslyMapped(TE->getType()))
2359 return;
2360
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002361 ImplicitMap.emplace_back(E);
2362 return;
2363 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002364
Alexey Bataeve3727102018-04-18 15:57:46 +00002365 SourceLocation ELoc = E->getExprLoc();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002366 // OpenMP [2.9.3.6, Restrictions, p.2]
2367 // A list item that appears in a reduction clause of the innermost
2368 // enclosing worksharing or parallel construct may not be accessed in
2369 // an explicit task.
2370 DVar = Stack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00002371 FD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
2372 [](OpenMPDirectiveKind K) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002373 return isOpenMPParallelDirective(K) ||
2374 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
2375 },
2376 /*FromParent=*/true);
2377 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
2378 ErrorFound = true;
2379 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
Alexey Bataeve3727102018-04-18 15:57:46 +00002380 reportOriginalDsa(SemaRef, Stack, FD, DVar);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002381 return;
2382 }
2383
2384 // Define implicit data-sharing attributes for task.
Alexey Bataeve3727102018-04-18 15:57:46 +00002385 DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002386 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
Alexey Bataevb40e05202018-10-24 18:53:12 +00002387 !Stack->isLoopControlVariable(FD).first) {
2388 // Check if there is a captured expression for the current field in the
2389 // region. Do not mark it as firstprivate unless there is no captured
2390 // expression.
2391 // TODO: try to make it firstprivate.
2392 if (DVar.CKind != OMPC_unknown)
2393 ImplicitFirstprivate.push_back(E);
2394 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002395 return;
2396 }
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002397 if (isOpenMPTargetExecutionDirective(DKind)) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002398 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
Alexey Bataeve3727102018-04-18 15:57:46 +00002399 if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map,
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002400 /*NoDiagnose=*/true))
Alexey Bataev27041fa2017-12-05 15:22:49 +00002401 return;
Alexey Bataeve3727102018-04-18 15:57:46 +00002402 const auto *VD = cast<ValueDecl>(
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002403 CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl());
2404 if (!Stack->checkMappableExprComponentListsForDecl(
2405 VD, /*CurrentRegionOnly=*/true,
2406 [&CurComponents](
2407 OMPClauseMappableExprCommon::MappableExprComponentListRef
2408 StackComponents,
2409 OpenMPClauseKind) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002410 auto CCI = CurComponents.rbegin();
Alexey Bataev5ec38932017-09-26 16:19:04 +00002411 auto CCE = CurComponents.rend();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002412 for (const auto &SC : llvm::reverse(StackComponents)) {
2413 // Do both expressions have the same kind?
2414 if (CCI->getAssociatedExpression()->getStmtClass() !=
2415 SC.getAssociatedExpression()->getStmtClass())
2416 if (!(isa<OMPArraySectionExpr>(
2417 SC.getAssociatedExpression()) &&
2418 isa<ArraySubscriptExpr>(
2419 CCI->getAssociatedExpression())))
2420 return false;
2421
Alexey Bataeve3727102018-04-18 15:57:46 +00002422 const Decl *CCD = CCI->getAssociatedDeclaration();
2423 const Decl *SCD = SC.getAssociatedDeclaration();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002424 CCD = CCD ? CCD->getCanonicalDecl() : nullptr;
2425 SCD = SCD ? SCD->getCanonicalDecl() : nullptr;
2426 if (SCD != CCD)
2427 return false;
2428 std::advance(CCI, 1);
Alexey Bataev5ec38932017-09-26 16:19:04 +00002429 if (CCI == CCE)
2430 break;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002431 }
2432 return true;
2433 })) {
2434 Visit(E->getBase());
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002435 }
Alexey Bataeve3727102018-04-18 15:57:46 +00002436 } else {
Alexey Bataev7fcacd82016-11-28 15:55:15 +00002437 Visit(E->getBase());
Alexey Bataeve3727102018-04-18 15:57:46 +00002438 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002439 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002440 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002441 for (OMPClause *C : S->clauses()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002442 // Skip analysis of arguments of implicitly defined firstprivate clause
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002443 // for task|target directives.
2444 // Skip analysis of arguments of implicitly defined map clause for target
2445 // directives.
2446 if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) &&
2447 C->isImplicit())) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002448 for (Stmt *CC : C->children()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002449 if (CC)
2450 Visit(CC);
2451 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002452 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002453 }
Alexey Bataevf07946e2018-10-29 20:17:42 +00002454 // Check implicitly captured variables.
2455 VisitSubCaptures(S);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002456 }
2457 void VisitStmt(Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002458 for (Stmt *C : S->children()) {
Alexey Bataev8fc7b5f2018-10-25 15:35:27 +00002459 if (C) {
Joel E. Denny0fdf5a92018-12-19 15:59:47 +00002460 // Check implicitly captured variables in the task-based directives to
2461 // check if they must be firstprivatized.
2462 Visit(C);
Alexey Bataev8fc7b5f2018-10-25 15:35:27 +00002463 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002464 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002465 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002466
Alexey Bataeve3727102018-04-18 15:57:46 +00002467 bool isErrorFound() const { return ErrorFound; }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002468 ArrayRef<Expr *> getImplicitFirstprivate() const {
2469 return ImplicitFirstprivate;
2470 }
2471 ArrayRef<Expr *> getImplicitMap() const { return ImplicitMap; }
Alexey Bataeve3727102018-04-18 15:57:46 +00002472 const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const {
Alexey Bataev4acb8592014-07-07 13:01:15 +00002473 return VarsWithInheritedDSA;
2474 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002475
Alexey Bataev7ff55242014-06-19 09:13:45 +00002476 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
2477 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00002478};
Alexey Bataeved09d242014-05-28 05:53:51 +00002479} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00002480
Alexey Bataevbae9a792014-06-27 10:37:06 +00002481void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00002482 switch (DKind) {
Kelvin Li70a12c52016-07-13 21:51:49 +00002483 case OMPD_parallel:
2484 case OMPD_parallel_for:
2485 case OMPD_parallel_for_simd:
2486 case OMPD_parallel_sections:
Carlo Bertolliba1487b2017-10-04 14:12:09 +00002487 case OMPD_teams:
Alexey Bataev999277a2017-12-06 14:31:09 +00002488 case OMPD_teams_distribute:
2489 case OMPD_teams_distribute_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002490 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Alexey Bataev2377fe92015-09-10 08:12:02 +00002491 QualType KmpInt32PtrTy =
2492 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002493 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002494 std::make_pair(".global_tid.", KmpInt32PtrTy),
2495 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2496 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00002497 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00002498 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2499 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00002500 break;
2501 }
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002502 case OMPD_target_teams:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00002503 case OMPD_target_parallel:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00002504 case OMPD_target_parallel_for:
Alexey Bataevdfa430f2017-12-08 15:03:50 +00002505 case OMPD_target_parallel_for_simd:
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00002506 case OMPD_target_teams_distribute:
2507 case OMPD_target_teams_distribute_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002508 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2509 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2510 QualType KmpInt32PtrTy =
2511 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2512 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00002513 FunctionProtoType::ExtProtoInfo EPI;
2514 EPI.Variadic = true;
2515 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2516 Sema::CapturedParamNameType Params[] = {
2517 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002518 std::make_pair(".part_id.", KmpInt32PtrTy),
2519 std::make_pair(".privates.", VoidPtrTy),
2520 std::make_pair(
2521 ".copy_fn.",
2522 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00002523 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2524 std::make_pair(StringRef(), QualType()) // __context with shared vars
2525 };
2526 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2527 Params);
Alexey Bataev0c869ef2018-01-16 15:57:07 +00002528 // Mark this captured region as inlined, because we don't use outlined
2529 // function directly.
2530 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2531 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002532 Context, AlwaysInlineAttr::Keyword_forceinline));
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002533 Sema::CapturedParamNameType ParamsTarget[] = {
2534 std::make_pair(StringRef(), QualType()) // __context with shared vars
2535 };
2536 // Start a captured region for 'target' with no implicit parameters.
2537 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2538 ParamsTarget);
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002539 Sema::CapturedParamNameType ParamsTeamsOrParallel[] = {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002540 std::make_pair(".global_tid.", KmpInt32PtrTy),
2541 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2542 std::make_pair(StringRef(), QualType()) // __context with shared vars
2543 };
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002544 // Start a captured region for 'teams' or 'parallel'. Both regions have
2545 // the same implicit parameters.
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002546 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002547 ParamsTeamsOrParallel);
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002548 break;
2549 }
Alexey Bataev8451efa2018-01-15 19:06:12 +00002550 case OMPD_target:
2551 case OMPD_target_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002552 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2553 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2554 QualType KmpInt32PtrTy =
2555 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2556 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00002557 FunctionProtoType::ExtProtoInfo EPI;
2558 EPI.Variadic = true;
2559 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2560 Sema::CapturedParamNameType Params[] = {
2561 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002562 std::make_pair(".part_id.", KmpInt32PtrTy),
2563 std::make_pair(".privates.", VoidPtrTy),
2564 std::make_pair(
2565 ".copy_fn.",
2566 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00002567 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2568 std::make_pair(StringRef(), QualType()) // __context with shared vars
2569 };
2570 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2571 Params);
2572 // Mark this captured region as inlined, because we don't use outlined
2573 // function directly.
2574 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2575 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002576 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev8451efa2018-01-15 19:06:12 +00002577 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2578 std::make_pair(StringRef(), QualType()));
2579 break;
2580 }
Kelvin Li70a12c52016-07-13 21:51:49 +00002581 case OMPD_simd:
2582 case OMPD_for:
2583 case OMPD_for_simd:
2584 case OMPD_sections:
2585 case OMPD_section:
2586 case OMPD_single:
2587 case OMPD_master:
2588 case OMPD_critical:
Kelvin Lia579b912016-07-14 02:54:56 +00002589 case OMPD_taskgroup:
2590 case OMPD_distribute:
Alexey Bataev46506272017-12-05 17:41:34 +00002591 case OMPD_distribute_simd:
Kelvin Li70a12c52016-07-13 21:51:49 +00002592 case OMPD_ordered:
2593 case OMPD_atomic:
Alexey Bataev8451efa2018-01-15 19:06:12 +00002594 case OMPD_target_data: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002595 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002596 std::make_pair(StringRef(), QualType()) // __context with shared vars
2597 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00002598 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2599 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002600 break;
2601 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002602 case OMPD_task: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002603 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2604 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2605 QualType KmpInt32PtrTy =
2606 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2607 QualType Args[] = {VoidPtrTy};
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002608 FunctionProtoType::ExtProtoInfo EPI;
2609 EPI.Variadic = true;
2610 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002611 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00002612 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002613 std::make_pair(".part_id.", KmpInt32PtrTy),
2614 std::make_pair(".privates.", VoidPtrTy),
2615 std::make_pair(
2616 ".copy_fn.",
2617 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev48591dd2016-04-20 04:01:36 +00002618 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002619 std::make_pair(StringRef(), QualType()) // __context with shared vars
2620 };
2621 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2622 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00002623 // Mark this captured region as inlined, because we don't use outlined
2624 // function directly.
2625 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2626 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002627 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002628 break;
2629 }
Alexey Bataev1e73ef32016-04-28 12:14:51 +00002630 case OMPD_taskloop:
2631 case OMPD_taskloop_simd: {
Alexey Bataev7292c292016-04-25 12:22:29 +00002632 QualType KmpInt32Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002633 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1)
2634 .withConst();
Alexey Bataev7292c292016-04-25 12:22:29 +00002635 QualType KmpUInt64Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002636 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0)
2637 .withConst();
Alexey Bataev7292c292016-04-25 12:22:29 +00002638 QualType KmpInt64Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002639 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1)
2640 .withConst();
2641 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2642 QualType KmpInt32PtrTy =
2643 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2644 QualType Args[] = {VoidPtrTy};
Alexey Bataev7292c292016-04-25 12:22:29 +00002645 FunctionProtoType::ExtProtoInfo EPI;
2646 EPI.Variadic = true;
2647 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev49f6e782015-12-01 04:18:41 +00002648 Sema::CapturedParamNameType Params[] = {
Alexey Bataev7292c292016-04-25 12:22:29 +00002649 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002650 std::make_pair(".part_id.", KmpInt32PtrTy),
2651 std::make_pair(".privates.", VoidPtrTy),
Alexey Bataev7292c292016-04-25 12:22:29 +00002652 std::make_pair(
2653 ".copy_fn.",
2654 Context.getPointerType(CopyFnType).withConst().withRestrict()),
2655 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2656 std::make_pair(".lb.", KmpUInt64Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002657 std::make_pair(".ub.", KmpUInt64Ty),
2658 std::make_pair(".st.", KmpInt64Ty),
Alexey Bataev7292c292016-04-25 12:22:29 +00002659 std::make_pair(".liter.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002660 std::make_pair(".reductions.", VoidPtrTy),
Alexey Bataev49f6e782015-12-01 04:18:41 +00002661 std::make_pair(StringRef(), QualType()) // __context with shared vars
2662 };
2663 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2664 Params);
Alexey Bataev7292c292016-04-25 12:22:29 +00002665 // Mark this captured region as inlined, because we don't use outlined
2666 // function directly.
2667 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2668 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002669 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev49f6e782015-12-01 04:18:41 +00002670 break;
2671 }
Kelvin Li4a39add2016-07-05 05:00:15 +00002672 case OMPD_distribute_parallel_for_simd:
Alexey Bataev647dd842018-01-15 20:59:40 +00002673 case OMPD_distribute_parallel_for: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002674 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli9925f152016-06-27 14:55:37 +00002675 QualType KmpInt32PtrTy =
2676 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2677 Sema::CapturedParamNameType Params[] = {
2678 std::make_pair(".global_tid.", KmpInt32PtrTy),
2679 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002680 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2681 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli9925f152016-06-27 14:55:37 +00002682 std::make_pair(StringRef(), QualType()) // __context with shared vars
2683 };
2684 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2685 Params);
2686 break;
2687 }
Alexey Bataev647dd842018-01-15 20:59:40 +00002688 case OMPD_target_teams_distribute_parallel_for:
2689 case OMPD_target_teams_distribute_parallel_for_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002690 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli52978c32018-01-03 21:12:44 +00002691 QualType KmpInt32PtrTy =
2692 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002693 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
Carlo Bertolli52978c32018-01-03 21:12:44 +00002694
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002695 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00002696 FunctionProtoType::ExtProtoInfo EPI;
2697 EPI.Variadic = true;
2698 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2699 Sema::CapturedParamNameType Params[] = {
2700 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002701 std::make_pair(".part_id.", KmpInt32PtrTy),
2702 std::make_pair(".privates.", VoidPtrTy),
2703 std::make_pair(
2704 ".copy_fn.",
2705 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00002706 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2707 std::make_pair(StringRef(), QualType()) // __context with shared vars
2708 };
2709 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2710 Params);
Alexey Bataev9f9fb0b2018-01-16 19:02:33 +00002711 // Mark this captured region as inlined, because we don't use outlined
2712 // function directly.
2713 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2714 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002715 Context, AlwaysInlineAttr::Keyword_forceinline));
Carlo Bertolli52978c32018-01-03 21:12:44 +00002716 Sema::CapturedParamNameType ParamsTarget[] = {
2717 std::make_pair(StringRef(), QualType()) // __context with shared vars
2718 };
2719 // Start a captured region for 'target' with no implicit parameters.
2720 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2721 ParamsTarget);
2722
2723 Sema::CapturedParamNameType ParamsTeams[] = {
2724 std::make_pair(".global_tid.", KmpInt32PtrTy),
2725 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2726 std::make_pair(StringRef(), QualType()) // __context with shared vars
2727 };
2728 // Start a captured region for 'target' with no implicit parameters.
2729 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2730 ParamsTeams);
2731
2732 Sema::CapturedParamNameType ParamsParallel[] = {
2733 std::make_pair(".global_tid.", KmpInt32PtrTy),
2734 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002735 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2736 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli52978c32018-01-03 21:12:44 +00002737 std::make_pair(StringRef(), QualType()) // __context with shared vars
2738 };
2739 // Start a captured region for 'teams' or 'parallel'. Both regions have
2740 // the same implicit parameters.
2741 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2742 ParamsParallel);
2743 break;
2744 }
2745
Alexey Bataev46506272017-12-05 17:41:34 +00002746 case OMPD_teams_distribute_parallel_for:
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00002747 case OMPD_teams_distribute_parallel_for_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002748 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli62fae152017-11-20 20:46:39 +00002749 QualType KmpInt32PtrTy =
2750 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2751
2752 Sema::CapturedParamNameType ParamsTeams[] = {
2753 std::make_pair(".global_tid.", KmpInt32PtrTy),
2754 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2755 std::make_pair(StringRef(), QualType()) // __context with shared vars
2756 };
2757 // Start a captured region for 'target' with no implicit parameters.
2758 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2759 ParamsTeams);
2760
2761 Sema::CapturedParamNameType ParamsParallel[] = {
2762 std::make_pair(".global_tid.", KmpInt32PtrTy),
2763 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002764 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2765 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli62fae152017-11-20 20:46:39 +00002766 std::make_pair(StringRef(), QualType()) // __context with shared vars
2767 };
2768 // Start a captured region for 'teams' or 'parallel'. Both regions have
2769 // the same implicit parameters.
2770 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2771 ParamsParallel);
2772 break;
2773 }
Alexey Bataev7828b252017-11-21 17:08:48 +00002774 case OMPD_target_update:
2775 case OMPD_target_enter_data:
2776 case OMPD_target_exit_data: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002777 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2778 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2779 QualType KmpInt32PtrTy =
2780 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2781 QualType Args[] = {VoidPtrTy};
Alexey Bataev7828b252017-11-21 17:08:48 +00002782 FunctionProtoType::ExtProtoInfo EPI;
2783 EPI.Variadic = true;
2784 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2785 Sema::CapturedParamNameType Params[] = {
2786 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002787 std::make_pair(".part_id.", KmpInt32PtrTy),
2788 std::make_pair(".privates.", VoidPtrTy),
2789 std::make_pair(
2790 ".copy_fn.",
2791 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev7828b252017-11-21 17:08:48 +00002792 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2793 std::make_pair(StringRef(), QualType()) // __context with shared vars
2794 };
2795 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2796 Params);
2797 // Mark this captured region as inlined, because we don't use outlined
2798 // function directly.
2799 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2800 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002801 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev7828b252017-11-21 17:08:48 +00002802 break;
2803 }
Alexey Bataev9959db52014-05-06 10:08:46 +00002804 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00002805 case OMPD_taskyield:
2806 case OMPD_barrier:
2807 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002808 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00002809 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00002810 case OMPD_flush:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002811 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00002812 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00002813 case OMPD_declare_target:
2814 case OMPD_end_declare_target:
Kelvin Li1408f912018-09-26 04:28:39 +00002815 case OMPD_requires:
Alexey Bataev9959db52014-05-06 10:08:46 +00002816 llvm_unreachable("OpenMP Directive is not allowed");
2817 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00002818 llvm_unreachable("Unknown OpenMP directive");
2819 }
2820}
2821
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002822int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) {
2823 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2824 getOpenMPCaptureRegions(CaptureRegions, DKind);
2825 return CaptureRegions.size();
2826}
2827
Alexey Bataev3392d762016-02-16 11:18:12 +00002828static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00002829 Expr *CaptureExpr, bool WithInit,
2830 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00002831 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00002832 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00002833 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00002834 QualType Ty = Init->getType();
2835 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002836 if (S.getLangOpts().CPlusPlus) {
Alexey Bataev4244be22016-02-11 05:35:55 +00002837 Ty = C.getLValueReferenceType(Ty);
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002838 } else {
Alexey Bataev4244be22016-02-11 05:35:55 +00002839 Ty = C.getPointerType(Ty);
2840 ExprResult Res =
2841 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
2842 if (!Res.isUsable())
2843 return nullptr;
2844 Init = Res.get();
2845 }
Alexey Bataev61205072016-03-02 04:57:40 +00002846 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00002847 }
Alexey Bataeva7206b92016-12-20 16:51:02 +00002848 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty,
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002849 CaptureExpr->getBeginLoc());
Alexey Bataev2bbf7212016-03-03 03:52:24 +00002850 if (!WithInit)
Alexey Bataeve3727102018-04-18 15:57:46 +00002851 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C));
Alexey Bataev4244be22016-02-11 05:35:55 +00002852 S.CurContext->addHiddenDecl(CED);
Richard Smith3beb7c62017-01-12 02:27:38 +00002853 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00002854 return CED;
2855}
2856
Alexey Bataev61205072016-03-02 04:57:40 +00002857static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
2858 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00002859 OMPCapturedExprDecl *CD;
Alexey Bataeve3727102018-04-18 15:57:46 +00002860 if (VarDecl *VD = S.isOpenMPCapturedDecl(D))
Alexey Bataevb7a34b62016-02-25 03:59:29 +00002861 CD = cast<OMPCapturedExprDecl>(VD);
Alexey Bataeve3727102018-04-18 15:57:46 +00002862 else
Alexey Bataev5a3af132016-03-29 08:58:54 +00002863 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
2864 /*AsExpression=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00002865 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00002866 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00002867}
2868
Alexey Bataev5a3af132016-03-29 08:58:54 +00002869static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002870 CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00002871 if (!Ref) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002872 OMPCapturedExprDecl *CD = buildCaptureDecl(
2873 S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr,
2874 /*WithInit=*/true, /*AsExpression=*/true);
Alexey Bataev5a3af132016-03-29 08:58:54 +00002875 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
2876 CaptureExpr->getExprLoc());
2877 }
2878 ExprResult Res = Ref;
2879 if (!S.getLangOpts().CPlusPlus &&
2880 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002881 Ref->getType()->isPointerType()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00002882 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002883 if (!Res.isUsable())
2884 return ExprError();
2885 }
2886 return S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00002887}
2888
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002889namespace {
2890// OpenMP directives parsed in this section are represented as a
2891// CapturedStatement with an associated statement. If a syntax error
2892// is detected during the parsing of the associated statement, the
2893// compiler must abort processing and close the CapturedStatement.
2894//
2895// Combined directives such as 'target parallel' have more than one
2896// nested CapturedStatements. This RAII ensures that we unwind out
2897// of all the nested CapturedStatements when an error is found.
2898class CaptureRegionUnwinderRAII {
2899private:
2900 Sema &S;
2901 bool &ErrorFound;
Alexey Bataeve3727102018-04-18 15:57:46 +00002902 OpenMPDirectiveKind DKind = OMPD_unknown;
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002903
2904public:
2905 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound,
2906 OpenMPDirectiveKind DKind)
2907 : S(S), ErrorFound(ErrorFound), DKind(DKind) {}
2908 ~CaptureRegionUnwinderRAII() {
2909 if (ErrorFound) {
2910 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind);
2911 while (--ThisCaptureLevel >= 0)
2912 S.ActOnCapturedRegionError();
2913 }
2914 }
2915};
2916} // namespace
2917
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002918StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
2919 ArrayRef<OMPClause *> Clauses) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002920 bool ErrorFound = false;
2921 CaptureRegionUnwinderRAII CaptureRegionUnwinder(
2922 *this, ErrorFound, DSAStack->getCurrentDirective());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002923 if (!S.isUsable()) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002924 ErrorFound = true;
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002925 return StmtError();
2926 }
Alexey Bataev993d2802015-12-28 06:23:08 +00002927
Alexey Bataev2ba67042017-11-28 21:11:44 +00002928 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2929 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective());
Alexey Bataev993d2802015-12-28 06:23:08 +00002930 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00002931 OMPScheduleClause *SC = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +00002932 SmallVector<const OMPLinearClause *, 4> LCs;
2933 SmallVector<const OMPClauseWithPreInit *, 4> PICs;
Alexey Bataev040d5402015-05-12 08:35:28 +00002934 // This is required for proper codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00002935 for (OMPClause *Clause : Clauses) {
Alexey Bataev88202be2017-07-27 13:20:36 +00002936 if (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) &&
2937 Clause->getClauseKind() == OMPC_in_reduction) {
2938 // Capture taskgroup task_reduction descriptors inside the tasking regions
2939 // with the corresponding in_reduction items.
2940 auto *IRC = cast<OMPInReductionClause>(Clause);
Alexey Bataeve3727102018-04-18 15:57:46 +00002941 for (Expr *E : IRC->taskgroup_descriptors())
Alexey Bataev88202be2017-07-27 13:20:36 +00002942 if (E)
2943 MarkDeclarationsReferencedInExpr(E);
2944 }
Alexey Bataev16dc7b62015-05-20 03:46:04 +00002945 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00002946 Clause->getClauseKind() == OMPC_copyprivate ||
2947 (getLangOpts().OpenMPUseTLS &&
2948 getASTContext().getTargetInfo().isTLSSupported() &&
2949 Clause->getClauseKind() == OMPC_copyin)) {
2950 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00002951 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeve3727102018-04-18 15:57:46 +00002952 for (Stmt *VarRef : Clause->children()) {
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002953 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00002954 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002955 }
2956 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00002957 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev2ba67042017-11-28 21:11:44 +00002958 } else if (CaptureRegions.size() > 1 ||
2959 CaptureRegions.back() != OMPD_unknown) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002960 if (auto *C = OMPClauseWithPreInit::get(Clause))
2961 PICs.push_back(C);
Alexey Bataev005248a2016-02-25 05:25:57 +00002962 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002963 if (Expr *E = C->getPostUpdateExpr())
Alexey Bataev005248a2016-02-25 05:25:57 +00002964 MarkDeclarationsReferencedInExpr(E);
2965 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002966 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002967 if (Clause->getClauseKind() == OMPC_schedule)
2968 SC = cast<OMPScheduleClause>(Clause);
2969 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00002970 OC = cast<OMPOrderedClause>(Clause);
2971 else if (Clause->getClauseKind() == OMPC_linear)
2972 LCs.push_back(cast<OMPLinearClause>(Clause));
2973 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002974 // OpenMP, 2.7.1 Loop Construct, Restrictions
2975 // The nonmonotonic modifier cannot be specified if an ordered clause is
2976 // specified.
2977 if (SC &&
2978 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
2979 SC->getSecondScheduleModifier() ==
2980 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
2981 OC) {
2982 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
2983 ? SC->getFirstScheduleModifierLoc()
2984 : SC->getSecondScheduleModifierLoc(),
2985 diag::err_omp_schedule_nonmonotonic_ordered)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00002986 << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
Alexey Bataev6402bca2015-12-28 07:25:51 +00002987 ErrorFound = true;
2988 }
Alexey Bataev993d2802015-12-28 06:23:08 +00002989 if (!LCs.empty() && OC && OC->getNumForLoops()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002990 for (const OMPLinearClause *C : LCs) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002991 Diag(C->getBeginLoc(), diag::err_omp_linear_ordered)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00002992 << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
Alexey Bataev993d2802015-12-28 06:23:08 +00002993 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002994 ErrorFound = true;
2995 }
Alexey Bataev113438c2015-12-30 12:06:23 +00002996 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
2997 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
2998 OC->getNumForLoops()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002999 Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd)
Alexey Bataev113438c2015-12-30 12:06:23 +00003000 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
3001 ErrorFound = true;
3002 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00003003 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00003004 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00003005 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003006 StmtResult SR = S;
Alexey Bataev2ba67042017-11-28 21:11:44 +00003007 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00003008 // Mark all variables in private list clauses as used in inner region.
3009 // Required for proper codegen of combined directives.
3010 // TODO: add processing for other clauses.
Alexey Bataev2ba67042017-11-28 21:11:44 +00003011 if (ThisCaptureRegion != OMPD_unknown) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003012 for (const clang::OMPClauseWithPreInit *C : PICs) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00003013 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion();
3014 // Find the particular capture region for the clause if the
3015 // directive is a combined one with multiple capture regions.
3016 // If the directive is not a combined one, the capture region
3017 // associated with the clause is OMPD_unknown and is generated
3018 // only once.
3019 if (CaptureRegion == ThisCaptureRegion ||
3020 CaptureRegion == OMPD_unknown) {
3021 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003022 for (Decl *D : DS->decls())
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00003023 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
3024 }
3025 }
3026 }
3027 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003028 SR = ActOnCapturedRegionEnd(SR.get());
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00003029 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003030 return SR;
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00003031}
3032
Jonas Hahnfeld64a9e3c2017-02-22 06:49:10 +00003033static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion,
3034 OpenMPDirectiveKind CancelRegion,
3035 SourceLocation StartLoc) {
3036 // CancelRegion is only needed for cancel and cancellation_point.
3037 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point)
3038 return false;
3039
3040 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for ||
3041 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup)
3042 return false;
3043
3044 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region)
3045 << getOpenMPDirectiveName(CancelRegion);
3046 return true;
3047}
3048
Alexey Bataeve3727102018-04-18 15:57:46 +00003049static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003050 OpenMPDirectiveKind CurrentRegion,
3051 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003052 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003053 SourceLocation StartLoc) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003054 if (Stack->getCurScope()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003055 OpenMPDirectiveKind ParentRegion = Stack->getParentDirective();
3056 OpenMPDirectiveKind OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003057 bool NestingProhibited = false;
3058 bool CloseNesting = true;
David Majnemer9d168222016-08-05 17:44:54 +00003059 bool OrphanSeen = false;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003060 enum {
3061 NoRecommend,
3062 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00003063 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003064 ShouldBeInTargetRegion,
3065 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003066 } Recommend = NoRecommend;
Kelvin Lifd8b5742016-07-01 14:30:25 +00003067 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003068 // OpenMP [2.16, Nesting of Regions]
3069 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003070 // OpenMP [2.8.1,simd Construct, Restrictions]
Kelvin Lifd8b5742016-07-01 14:30:25 +00003071 // An ordered construct with the simd clause is the only OpenMP
3072 // construct that can appear in the simd region.
David Majnemer9d168222016-08-05 17:44:54 +00003073 // Allowing a SIMD construct nested in another SIMD construct is an
Kelvin Lifd8b5742016-07-01 14:30:25 +00003074 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
3075 // message.
3076 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
3077 ? diag::err_omp_prohibited_region_simd
3078 : diag::warn_omp_nesting_simd);
3079 return CurrentRegion != OMPD_simd;
Alexey Bataev549210e2014-06-24 04:39:47 +00003080 }
Alexey Bataev0162e452014-07-22 10:10:35 +00003081 if (ParentRegion == OMPD_atomic) {
3082 // OpenMP [2.16, Nesting of Regions]
3083 // OpenMP constructs may not be nested inside an atomic region.
3084 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
3085 return true;
3086 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003087 if (CurrentRegion == OMPD_section) {
3088 // OpenMP [2.7.2, sections Construct, Restrictions]
3089 // Orphaned section directives are prohibited. That is, the section
3090 // directives must appear within the sections construct and must not be
3091 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003092 if (ParentRegion != OMPD_sections &&
3093 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003094 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
3095 << (ParentRegion != OMPD_unknown)
3096 << getOpenMPDirectiveName(ParentRegion);
3097 return true;
3098 }
3099 return false;
3100 }
Alexey Bataev185e88d2019-01-08 15:53:42 +00003101 // Allow some constructs (except teams and cancellation constructs) to be
3102 // orphaned (they could be used in functions, called from OpenMP regions
3103 // with the required preconditions).
Kelvin Libf594a52016-12-17 05:48:59 +00003104 if (ParentRegion == OMPD_unknown &&
Alexey Bataev185e88d2019-01-08 15:53:42 +00003105 !isOpenMPNestingTeamsDirective(CurrentRegion) &&
3106 CurrentRegion != OMPD_cancellation_point &&
3107 CurrentRegion != OMPD_cancel)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003108 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00003109 if (CurrentRegion == OMPD_cancellation_point ||
3110 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003111 // OpenMP [2.16, Nesting of Regions]
3112 // A cancellation point construct for which construct-type-clause is
3113 // taskgroup must be nested inside a task construct. A cancellation
3114 // point construct for which construct-type-clause is not taskgroup must
3115 // be closely nested inside an OpenMP construct that matches the type
3116 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00003117 // A cancel construct for which construct-type-clause is taskgroup must be
3118 // nested inside a task construct. A cancel construct for which
3119 // construct-type-clause is not taskgroup must be closely nested inside an
3120 // OpenMP construct that matches the type specified in
3121 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003122 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003123 !((CancelRegion == OMPD_parallel &&
3124 (ParentRegion == OMPD_parallel ||
3125 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00003126 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003127 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00003128 ParentRegion == OMPD_target_parallel_for ||
3129 ParentRegion == OMPD_distribute_parallel_for ||
Alexey Bataev16e79882017-11-22 21:12:03 +00003130 ParentRegion == OMPD_teams_distribute_parallel_for ||
3131 ParentRegion == OMPD_target_teams_distribute_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003132 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
3133 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00003134 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
3135 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev185e88d2019-01-08 15:53:42 +00003136 OrphanSeen = ParentRegion == OMPD_unknown;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003137 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00003138 // OpenMP [2.16, Nesting of Regions]
3139 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003140 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00003141 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003142 isOpenMPTaskingDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003143 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
3144 // OpenMP [2.16, Nesting of Regions]
3145 // A critical region may not be nested (closely or otherwise) inside a
3146 // critical region with the same name. Note that this restriction is not
3147 // sufficient to prevent deadlock.
3148 SourceLocation PreviousCriticalLoc;
David Majnemer9d168222016-08-05 17:44:54 +00003149 bool DeadLock = Stack->hasDirective(
3150 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
3151 const DeclarationNameInfo &DNI,
Alexey Bataeve3727102018-04-18 15:57:46 +00003152 SourceLocation Loc) {
David Majnemer9d168222016-08-05 17:44:54 +00003153 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
3154 PreviousCriticalLoc = Loc;
3155 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003156 }
3157 return false;
David Majnemer9d168222016-08-05 17:44:54 +00003158 },
3159 false /* skip top directive */);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003160 if (DeadLock) {
3161 SemaRef.Diag(StartLoc,
3162 diag::err_omp_prohibited_region_critical_same_name)
3163 << CurrentName.getName();
3164 if (PreviousCriticalLoc.isValid())
3165 SemaRef.Diag(PreviousCriticalLoc,
3166 diag::note_omp_previous_critical_region);
3167 return true;
3168 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003169 } else if (CurrentRegion == OMPD_barrier) {
3170 // OpenMP [2.16, Nesting of Regions]
3171 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003172 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003173 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3174 isOpenMPTaskingDirective(ParentRegion) ||
3175 ParentRegion == OMPD_master ||
3176 ParentRegion == OMPD_critical ||
3177 ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00003178 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Kelvin Li579e41c2016-11-30 23:51:03 +00003179 !isOpenMPParallelDirective(CurrentRegion) &&
3180 !isOpenMPTeamsDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003181 // OpenMP [2.16, Nesting of Regions]
3182 // A worksharing region may not be closely nested inside a worksharing,
3183 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003184 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3185 isOpenMPTaskingDirective(ParentRegion) ||
3186 ParentRegion == OMPD_master ||
3187 ParentRegion == OMPD_critical ||
3188 ParentRegion == OMPD_ordered;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003189 Recommend = ShouldBeInParallelRegion;
3190 } else if (CurrentRegion == OMPD_ordered) {
3191 // OpenMP [2.16, Nesting of Regions]
3192 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00003193 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003194 // An ordered region must be closely nested inside a loop region (or
3195 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003196 // OpenMP [2.8.1,simd Construct, Restrictions]
3197 // An ordered construct with the simd clause is the only OpenMP construct
3198 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003199 NestingProhibited = ParentRegion == OMPD_critical ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003200 isOpenMPTaskingDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003201 !(isOpenMPSimdDirective(ParentRegion) ||
3202 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003203 Recommend = ShouldBeInOrderedRegion;
Kelvin Libf594a52016-12-17 05:48:59 +00003204 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00003205 // OpenMP [2.16, Nesting of Regions]
3206 // If specified, a teams construct must be contained within a target
3207 // construct.
3208 NestingProhibited = ParentRegion != OMPD_target;
Kelvin Li2b51f722016-07-26 04:32:50 +00003209 OrphanSeen = ParentRegion == OMPD_unknown;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003210 Recommend = ShouldBeInTargetRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003211 }
Kelvin Libf594a52016-12-17 05:48:59 +00003212 if (!NestingProhibited &&
3213 !isOpenMPTargetExecutionDirective(CurrentRegion) &&
3214 !isOpenMPTargetDataManagementDirective(CurrentRegion) &&
3215 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00003216 // OpenMP [2.16, Nesting of Regions]
3217 // distribute, parallel, parallel sections, parallel workshare, and the
3218 // parallel loop and parallel loop SIMD constructs are the only OpenMP
3219 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003220 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
3221 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00003222 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003223 }
David Majnemer9d168222016-08-05 17:44:54 +00003224 if (!NestingProhibited &&
Kelvin Li02532872016-08-05 14:37:37 +00003225 isOpenMPNestingDistributeDirective(CurrentRegion)) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003226 // OpenMP 4.5 [2.17 Nesting of Regions]
3227 // The region associated with the distribute construct must be strictly
3228 // nested inside a teams region
Kelvin Libf594a52016-12-17 05:48:59 +00003229 NestingProhibited =
3230 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003231 Recommend = ShouldBeInTeamsRegion;
3232 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003233 if (!NestingProhibited &&
3234 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
3235 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
3236 // OpenMP 4.5 [2.17 Nesting of Regions]
3237 // If a target, target update, target data, target enter data, or
3238 // target exit data construct is encountered during execution of a
3239 // target region, the behavior is unspecified.
3240 NestingProhibited = Stack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +00003241 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
Alexey Bataeve3727102018-04-18 15:57:46 +00003242 SourceLocation) {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003243 if (isOpenMPTargetExecutionDirective(K)) {
3244 OffendingRegion = K;
3245 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003246 }
3247 return false;
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003248 },
3249 false /* don't skip top directive */);
3250 CloseNesting = false;
3251 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003252 if (NestingProhibited) {
Kelvin Li2b51f722016-07-26 04:32:50 +00003253 if (OrphanSeen) {
3254 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
3255 << getOpenMPDirectiveName(CurrentRegion) << Recommend;
3256 } else {
3257 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
3258 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
3259 << Recommend << getOpenMPDirectiveName(CurrentRegion);
3260 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003261 return true;
3262 }
3263 }
3264 return false;
3265}
3266
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003267static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
3268 ArrayRef<OMPClause *> Clauses,
3269 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
3270 bool ErrorFound = false;
3271 unsigned NamedModifiersNumber = 0;
3272 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
3273 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00003274 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00003275 for (const OMPClause *C : Clauses) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003276 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
3277 // At most one if clause without a directive-name-modifier can appear on
3278 // the directive.
3279 OpenMPDirectiveKind CurNM = IC->getNameModifier();
3280 if (FoundNameModifiers[CurNM]) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003281 S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003282 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
3283 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
3284 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003285 } else if (CurNM != OMPD_unknown) {
3286 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003287 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003288 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003289 FoundNameModifiers[CurNM] = IC;
3290 if (CurNM == OMPD_unknown)
3291 continue;
3292 // Check if the specified name modifier is allowed for the current
3293 // directive.
3294 // At most one if clause with the particular directive-name-modifier can
3295 // appear on the directive.
3296 bool MatchFound = false;
3297 for (auto NM : AllowedNameModifiers) {
3298 if (CurNM == NM) {
3299 MatchFound = true;
3300 break;
3301 }
3302 }
3303 if (!MatchFound) {
3304 S.Diag(IC->getNameModifierLoc(),
3305 diag::err_omp_wrong_if_directive_name_modifier)
3306 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
3307 ErrorFound = true;
3308 }
3309 }
3310 }
3311 // If any if clause on the directive includes a directive-name-modifier then
3312 // all if clauses on the directive must include a directive-name-modifier.
3313 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
3314 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003315 S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003316 diag::err_omp_no_more_if_clause);
3317 } else {
3318 std::string Values;
3319 std::string Sep(", ");
3320 unsigned AllowedCnt = 0;
3321 unsigned TotalAllowedNum =
3322 AllowedNameModifiers.size() - NamedModifiersNumber;
3323 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3324 ++Cnt) {
3325 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
3326 if (!FoundNameModifiers[NM]) {
3327 Values += "'";
3328 Values += getOpenMPDirectiveName(NM);
3329 Values += "'";
3330 if (AllowedCnt + 2 == TotalAllowedNum)
3331 Values += " or ";
3332 else if (AllowedCnt + 1 != TotalAllowedNum)
3333 Values += Sep;
3334 ++AllowedCnt;
3335 }
3336 }
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003337 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003338 diag::err_omp_unnamed_if_clause)
3339 << (TotalAllowedNum > 1) << Values;
3340 }
Alexey Bataeve3727102018-04-18 15:57:46 +00003341 for (SourceLocation Loc : NameModifierLoc) {
Alexey Bataevecb156a2015-09-15 17:23:56 +00003342 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3343 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003344 ErrorFound = true;
3345 }
3346 return ErrorFound;
3347}
3348
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003349StmtResult Sema::ActOnOpenMPExecutableDirective(
3350 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
3351 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3352 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003353 StmtResult Res = StmtError();
Jonas Hahnfeld64a9e3c2017-02-22 06:49:10 +00003354 // First check CancelRegion which is then used in checkNestingOfRegions.
3355 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) ||
3356 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003357 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00003358 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003359
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003360 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataeve3727102018-04-18 15:57:46 +00003361 VarsWithInheritedDSAType VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003362 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00003363 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00003364 if (AStmt && !CurContext->isDependentContext()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00003365 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
3366
3367 // Check default data sharing attributes for referenced variables.
3368 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
Arpith Chacko Jacob1f46b702017-01-23 15:38:49 +00003369 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind);
3370 Stmt *S = AStmt;
3371 while (--ThisCaptureLevel >= 0)
3372 S = cast<CapturedStmt>(S)->getCapturedStmt();
3373 DSAChecker.Visit(S);
Alexey Bataev68446b72014-07-18 07:47:19 +00003374 if (DSAChecker.isErrorFound())
3375 return StmtError();
3376 // Generate list of implicitly defined firstprivate variables.
3377 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00003378
Alexey Bataev88202be2017-07-27 13:20:36 +00003379 SmallVector<Expr *, 4> ImplicitFirstprivates(
3380 DSAChecker.getImplicitFirstprivate().begin(),
3381 DSAChecker.getImplicitFirstprivate().end());
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003382 SmallVector<Expr *, 4> ImplicitMaps(DSAChecker.getImplicitMap().begin(),
3383 DSAChecker.getImplicitMap().end());
Alexey Bataev88202be2017-07-27 13:20:36 +00003384 // Mark taskgroup task_reduction descriptors as implicitly firstprivate.
Alexey Bataeve3727102018-04-18 15:57:46 +00003385 for (OMPClause *C : Clauses) {
Alexey Bataev88202be2017-07-27 13:20:36 +00003386 if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003387 for (Expr *E : IRC->taskgroup_descriptors())
Alexey Bataev88202be2017-07-27 13:20:36 +00003388 if (E)
3389 ImplicitFirstprivates.emplace_back(E);
3390 }
3391 }
3392 if (!ImplicitFirstprivates.empty()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00003393 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
Alexey Bataev88202be2017-07-27 13:20:36 +00003394 ImplicitFirstprivates, SourceLocation(), SourceLocation(),
3395 SourceLocation())) {
Alexey Bataev68446b72014-07-18 07:47:19 +00003396 ClausesWithImplicit.push_back(Implicit);
3397 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
Alexey Bataev88202be2017-07-27 13:20:36 +00003398 ImplicitFirstprivates.size();
Alexey Bataeve3727102018-04-18 15:57:46 +00003399 } else {
Alexey Bataev68446b72014-07-18 07:47:19 +00003400 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003401 }
Alexey Bataev68446b72014-07-18 07:47:19 +00003402 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003403 if (!ImplicitMaps.empty()) {
3404 if (OMPClause *Implicit = ActOnOpenMPMapClause(
Kelvin Lief579432018-12-18 22:18:41 +00003405 llvm::None, llvm::None, OMPC_MAP_tofrom,
3406 /*IsMapTypeImplicit=*/true, SourceLocation(), SourceLocation(),
3407 ImplicitMaps, SourceLocation(), SourceLocation(),
3408 SourceLocation())) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003409 ClausesWithImplicit.emplace_back(Implicit);
3410 ErrorFound |=
3411 cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMaps.size();
Alexey Bataeve3727102018-04-18 15:57:46 +00003412 } else {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003413 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003414 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003415 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003416 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003417
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003418 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003419 switch (Kind) {
3420 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00003421 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3422 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003423 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003424 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003425 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003426 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3427 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003428 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003429 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003430 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3431 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003432 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003433 case OMPD_for_simd:
3434 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3435 EndLoc, VarsWithInheritedDSA);
3436 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003437 case OMPD_sections:
3438 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3439 EndLoc);
3440 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003441 case OMPD_section:
3442 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003443 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003444 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3445 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003446 case OMPD_single:
3447 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3448 EndLoc);
3449 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003450 case OMPD_master:
3451 assert(ClausesWithImplicit.empty() &&
3452 "No clauses are allowed for 'omp master' directive");
3453 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3454 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003455 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003456 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3457 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003458 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003459 case OMPD_parallel_for:
3460 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3461 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003462 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003463 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003464 case OMPD_parallel_for_simd:
3465 Res = ActOnOpenMPParallelForSimdDirective(
3466 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003467 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003468 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003469 case OMPD_parallel_sections:
3470 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3471 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003472 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003473 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003474 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003475 Res =
3476 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003477 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003478 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003479 case OMPD_taskyield:
3480 assert(ClausesWithImplicit.empty() &&
3481 "No clauses are allowed for 'omp taskyield' directive");
3482 assert(AStmt == nullptr &&
3483 "No associated statement allowed for 'omp taskyield' directive");
3484 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3485 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003486 case OMPD_barrier:
3487 assert(ClausesWithImplicit.empty() &&
3488 "No clauses are allowed for 'omp barrier' directive");
3489 assert(AStmt == nullptr &&
3490 "No associated statement allowed for 'omp barrier' directive");
3491 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3492 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003493 case OMPD_taskwait:
3494 assert(ClausesWithImplicit.empty() &&
3495 "No clauses are allowed for 'omp taskwait' directive");
3496 assert(AStmt == nullptr &&
3497 "No associated statement allowed for 'omp taskwait' directive");
3498 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3499 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003500 case OMPD_taskgroup:
Alexey Bataev169d96a2017-07-18 20:17:46 +00003501 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc,
3502 EndLoc);
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003503 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003504 case OMPD_flush:
3505 assert(AStmt == nullptr &&
3506 "No associated statement allowed for 'omp flush' directive");
3507 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3508 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003509 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003510 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3511 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003512 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003513 case OMPD_atomic:
3514 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3515 EndLoc);
3516 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003517 case OMPD_teams:
3518 Res =
3519 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3520 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003521 case OMPD_target:
3522 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3523 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003524 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003525 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003526 case OMPD_target_parallel:
3527 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3528 StartLoc, EndLoc);
3529 AllowedNameModifiers.push_back(OMPD_target);
3530 AllowedNameModifiers.push_back(OMPD_parallel);
3531 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003532 case OMPD_target_parallel_for:
3533 Res = ActOnOpenMPTargetParallelForDirective(
3534 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3535 AllowedNameModifiers.push_back(OMPD_target);
3536 AllowedNameModifiers.push_back(OMPD_parallel);
3537 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003538 case OMPD_cancellation_point:
3539 assert(ClausesWithImplicit.empty() &&
3540 "No clauses are allowed for 'omp cancellation point' directive");
3541 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3542 "cancellation point' directive");
3543 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3544 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003545 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003546 assert(AStmt == nullptr &&
3547 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003548 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3549 CancelRegion);
3550 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003551 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003552 case OMPD_target_data:
3553 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3554 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003555 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003556 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003557 case OMPD_target_enter_data:
3558 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00003559 EndLoc, AStmt);
Samuel Antaodf67fc42016-01-19 19:15:56 +00003560 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3561 break;
Samuel Antao72590762016-01-19 20:04:50 +00003562 case OMPD_target_exit_data:
3563 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00003564 EndLoc, AStmt);
Samuel Antao72590762016-01-19 20:04:50 +00003565 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3566 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003567 case OMPD_taskloop:
3568 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3569 EndLoc, VarsWithInheritedDSA);
3570 AllowedNameModifiers.push_back(OMPD_taskloop);
3571 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003572 case OMPD_taskloop_simd:
3573 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3574 EndLoc, VarsWithInheritedDSA);
3575 AllowedNameModifiers.push_back(OMPD_taskloop);
3576 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003577 case OMPD_distribute:
3578 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3579 EndLoc, VarsWithInheritedDSA);
3580 break;
Samuel Antao686c70c2016-05-26 17:30:50 +00003581 case OMPD_target_update:
Alexey Bataev7828b252017-11-21 17:08:48 +00003582 Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc,
3583 EndLoc, AStmt);
Samuel Antao686c70c2016-05-26 17:30:50 +00003584 AllowedNameModifiers.push_back(OMPD_target_update);
3585 break;
Carlo Bertolli9925f152016-06-27 14:55:37 +00003586 case OMPD_distribute_parallel_for:
3587 Res = ActOnOpenMPDistributeParallelForDirective(
3588 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3589 AllowedNameModifiers.push_back(OMPD_parallel);
3590 break;
Kelvin Li4a39add2016-07-05 05:00:15 +00003591 case OMPD_distribute_parallel_for_simd:
3592 Res = ActOnOpenMPDistributeParallelForSimdDirective(
3593 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3594 AllowedNameModifiers.push_back(OMPD_parallel);
3595 break;
Kelvin Li787f3fc2016-07-06 04:45:38 +00003596 case OMPD_distribute_simd:
3597 Res = ActOnOpenMPDistributeSimdDirective(
3598 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3599 break;
Kelvin Lia579b912016-07-14 02:54:56 +00003600 case OMPD_target_parallel_for_simd:
3601 Res = ActOnOpenMPTargetParallelForSimdDirective(
3602 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3603 AllowedNameModifiers.push_back(OMPD_target);
3604 AllowedNameModifiers.push_back(OMPD_parallel);
3605 break;
Kelvin Li986330c2016-07-20 22:57:10 +00003606 case OMPD_target_simd:
3607 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3608 EndLoc, VarsWithInheritedDSA);
3609 AllowedNameModifiers.push_back(OMPD_target);
3610 break;
Kelvin Li02532872016-08-05 14:37:37 +00003611 case OMPD_teams_distribute:
David Majnemer9d168222016-08-05 17:44:54 +00003612 Res = ActOnOpenMPTeamsDistributeDirective(
3613 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Kelvin Li02532872016-08-05 14:37:37 +00003614 break;
Kelvin Li4e325f72016-10-25 12:50:55 +00003615 case OMPD_teams_distribute_simd:
3616 Res = ActOnOpenMPTeamsDistributeSimdDirective(
3617 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3618 break;
Kelvin Li579e41c2016-11-30 23:51:03 +00003619 case OMPD_teams_distribute_parallel_for_simd:
3620 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective(
3621 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3622 AllowedNameModifiers.push_back(OMPD_parallel);
3623 break;
Kelvin Li7ade93f2016-12-09 03:24:30 +00003624 case OMPD_teams_distribute_parallel_for:
3625 Res = ActOnOpenMPTeamsDistributeParallelForDirective(
3626 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3627 AllowedNameModifiers.push_back(OMPD_parallel);
3628 break;
Kelvin Libf594a52016-12-17 05:48:59 +00003629 case OMPD_target_teams:
3630 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc,
3631 EndLoc);
3632 AllowedNameModifiers.push_back(OMPD_target);
3633 break;
Kelvin Li83c451e2016-12-25 04:52:54 +00003634 case OMPD_target_teams_distribute:
3635 Res = ActOnOpenMPTargetTeamsDistributeDirective(
3636 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3637 AllowedNameModifiers.push_back(OMPD_target);
3638 break;
Kelvin Li80e8f562016-12-29 22:16:30 +00003639 case OMPD_target_teams_distribute_parallel_for:
3640 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective(
3641 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3642 AllowedNameModifiers.push_back(OMPD_target);
3643 AllowedNameModifiers.push_back(OMPD_parallel);
3644 break;
Kelvin Li1851df52017-01-03 05:23:48 +00003645 case OMPD_target_teams_distribute_parallel_for_simd:
3646 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
3647 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3648 AllowedNameModifiers.push_back(OMPD_target);
3649 AllowedNameModifiers.push_back(OMPD_parallel);
3650 break;
Kelvin Lida681182017-01-10 18:08:18 +00003651 case OMPD_target_teams_distribute_simd:
3652 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective(
3653 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3654 AllowedNameModifiers.push_back(OMPD_target);
3655 break;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00003656 case OMPD_declare_target:
3657 case OMPD_end_declare_target:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003658 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003659 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003660 case OMPD_declare_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00003661 case OMPD_requires:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003662 llvm_unreachable("OpenMP Directive is not allowed");
3663 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003664 llvm_unreachable("Unknown OpenMP directive");
3665 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003666
Alexey Bataeve3727102018-04-18 15:57:46 +00003667 for (const auto &P : VarsWithInheritedDSA) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003668 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3669 << P.first << P.second->getSourceRange();
3670 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003671 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3672
3673 if (!AllowedNameModifiers.empty())
3674 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3675 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003676
Alexey Bataeved09d242014-05-28 05:53:51 +00003677 if (ErrorFound)
3678 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003679 return Res;
3680}
3681
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003682Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3683 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
Alexey Bataevd93d3762016-04-12 09:35:56 +00003684 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
Alexey Bataevecba70f2016-04-12 11:02:11 +00003685 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3686 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003687 assert(Aligneds.size() == Alignments.size());
Alexey Bataevecba70f2016-04-12 11:02:11 +00003688 assert(Linears.size() == LinModifiers.size());
3689 assert(Linears.size() == Steps.size());
Alexey Bataev587e1de2016-03-30 10:43:55 +00003690 if (!DG || DG.get().isNull())
3691 return DeclGroupPtrTy();
3692
3693 if (!DG.get().isSingleDecl()) {
Alexey Bataev20dfd772016-04-04 10:12:15 +00003694 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003695 return DG;
3696 }
Alexey Bataeve3727102018-04-18 15:57:46 +00003697 Decl *ADecl = DG.get().getSingleDecl();
Alexey Bataev587e1de2016-03-30 10:43:55 +00003698 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3699 ADecl = FTD->getTemplatedDecl();
3700
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003701 auto *FD = dyn_cast<FunctionDecl>(ADecl);
3702 if (!FD) {
3703 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003704 return DeclGroupPtrTy();
3705 }
3706
Alexey Bataev2af33e32016-04-07 12:45:37 +00003707 // OpenMP [2.8.2, declare simd construct, Description]
3708 // The parameter of the simdlen clause must be a constant positive integer
3709 // expression.
3710 ExprResult SL;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003711 if (Simdlen)
Alexey Bataev2af33e32016-04-07 12:45:37 +00003712 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003713 // OpenMP [2.8.2, declare simd construct, Description]
3714 // The special this pointer can be used as if was one of the arguments to the
3715 // function in any of the linear, aligned, or uniform clauses.
3716 // The uniform clause declares one or more arguments to have an invariant
3717 // value for all concurrent invocations of the function in the execution of a
3718 // single SIMD loop.
Alexey Bataeve3727102018-04-18 15:57:46 +00003719 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs;
3720 const Expr *UniformedLinearThis = nullptr;
3721 for (const Expr *E : Uniforms) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003722 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00003723 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3724 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003725 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3726 FD->getParamDecl(PVD->getFunctionScopeIndex())
Alexey Bataevecba70f2016-04-12 11:02:11 +00003727 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
Alexey Bataev43a919f2018-04-13 17:48:43 +00003728 UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003729 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003730 }
3731 if (isa<CXXThisExpr>(E)) {
3732 UniformedLinearThis = E;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003733 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003734 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003735 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3736 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
Alexey Bataev2af33e32016-04-07 12:45:37 +00003737 }
Alexey Bataevd93d3762016-04-12 09:35:56 +00003738 // OpenMP [2.8.2, declare simd construct, Description]
3739 // The aligned clause declares that the object to which each list item points
3740 // is aligned to the number of bytes expressed in the optional parameter of
3741 // the aligned clause.
3742 // The special this pointer can be used as if was one of the arguments to the
3743 // function in any of the linear, aligned, or uniform clauses.
3744 // The type of list items appearing in the aligned clause must be array,
3745 // pointer, reference to array, or reference to pointer.
Alexey Bataeve3727102018-04-18 15:57:46 +00003746 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs;
3747 const Expr *AlignedThis = nullptr;
3748 for (const Expr *E : Aligneds) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003749 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00003750 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3751 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3752 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevd93d3762016-04-12 09:35:56 +00003753 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3754 FD->getParamDecl(PVD->getFunctionScopeIndex())
3755 ->getCanonicalDecl() == CanonPVD) {
3756 // OpenMP [2.8.1, simd construct, Restrictions]
3757 // A list-item cannot appear in more than one aligned clause.
3758 if (AlignedArgs.count(CanonPVD) > 0) {
3759 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3760 << 1 << E->getSourceRange();
3761 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3762 diag::note_omp_explicit_dsa)
3763 << getOpenMPClauseName(OMPC_aligned);
3764 continue;
3765 }
3766 AlignedArgs[CanonPVD] = E;
3767 QualType QTy = PVD->getType()
3768 .getNonReferenceType()
3769 .getUnqualifiedType()
3770 .getCanonicalType();
3771 const Type *Ty = QTy.getTypePtrOrNull();
3772 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3773 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3774 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3775 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3776 }
3777 continue;
3778 }
3779 }
3780 if (isa<CXXThisExpr>(E)) {
3781 if (AlignedThis) {
3782 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3783 << 2 << E->getSourceRange();
3784 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3785 << getOpenMPClauseName(OMPC_aligned);
3786 }
3787 AlignedThis = E;
3788 continue;
3789 }
3790 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3791 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3792 }
3793 // The optional parameter of the aligned clause, alignment, must be a constant
3794 // positive integer expression. If no optional parameter is specified,
3795 // implementation-defined default alignments for SIMD instructions on the
3796 // target platforms are assumed.
Alexey Bataeve3727102018-04-18 15:57:46 +00003797 SmallVector<const Expr *, 4> NewAligns;
3798 for (Expr *E : Alignments) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003799 ExprResult Align;
3800 if (E)
3801 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3802 NewAligns.push_back(Align.get());
3803 }
Alexey Bataevecba70f2016-04-12 11:02:11 +00003804 // OpenMP [2.8.2, declare simd construct, Description]
3805 // The linear clause declares one or more list items to be private to a SIMD
3806 // lane and to have a linear relationship with respect to the iteration space
3807 // of a loop.
3808 // The special this pointer can be used as if was one of the arguments to the
3809 // function in any of the linear, aligned, or uniform clauses.
3810 // When a linear-step expression is specified in a linear clause it must be
3811 // either a constant integer expression or an integer-typed parameter that is
3812 // specified in a uniform clause on the directive.
Alexey Bataeve3727102018-04-18 15:57:46 +00003813 llvm::DenseMap<const Decl *, const Expr *> LinearArgs;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003814 const bool IsUniformedThis = UniformedLinearThis != nullptr;
3815 auto MI = LinModifiers.begin();
Alexey Bataeve3727102018-04-18 15:57:46 +00003816 for (const Expr *E : Linears) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003817 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3818 ++MI;
3819 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00003820 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3821 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3822 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevecba70f2016-04-12 11:02:11 +00003823 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3824 FD->getParamDecl(PVD->getFunctionScopeIndex())
3825 ->getCanonicalDecl() == CanonPVD) {
3826 // OpenMP [2.15.3.7, linear Clause, Restrictions]
3827 // A list-item cannot appear in more than one linear clause.
3828 if (LinearArgs.count(CanonPVD) > 0) {
3829 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3830 << getOpenMPClauseName(OMPC_linear)
3831 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3832 Diag(LinearArgs[CanonPVD]->getExprLoc(),
3833 diag::note_omp_explicit_dsa)
3834 << getOpenMPClauseName(OMPC_linear);
3835 continue;
3836 }
3837 // Each argument can appear in at most one uniform or linear clause.
3838 if (UniformedArgs.count(CanonPVD) > 0) {
3839 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3840 << getOpenMPClauseName(OMPC_linear)
3841 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3842 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3843 diag::note_omp_explicit_dsa)
3844 << getOpenMPClauseName(OMPC_uniform);
3845 continue;
3846 }
3847 LinearArgs[CanonPVD] = E;
3848 if (E->isValueDependent() || E->isTypeDependent() ||
3849 E->isInstantiationDependent() ||
3850 E->containsUnexpandedParameterPack())
3851 continue;
3852 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3853 PVD->getOriginalType());
3854 continue;
3855 }
3856 }
3857 if (isa<CXXThisExpr>(E)) {
3858 if (UniformedLinearThis) {
3859 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3860 << getOpenMPClauseName(OMPC_linear)
3861 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3862 << E->getSourceRange();
3863 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3864 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3865 : OMPC_linear);
3866 continue;
3867 }
3868 UniformedLinearThis = E;
3869 if (E->isValueDependent() || E->isTypeDependent() ||
3870 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3871 continue;
3872 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3873 E->getType());
3874 continue;
3875 }
3876 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3877 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3878 }
3879 Expr *Step = nullptr;
3880 Expr *NewStep = nullptr;
3881 SmallVector<Expr *, 4> NewSteps;
Alexey Bataeve3727102018-04-18 15:57:46 +00003882 for (Expr *E : Steps) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003883 // Skip the same step expression, it was checked already.
3884 if (Step == E || !E) {
3885 NewSteps.push_back(E ? NewStep : nullptr);
3886 continue;
3887 }
3888 Step = E;
Alexey Bataeve3727102018-04-18 15:57:46 +00003889 if (const auto *DRE = dyn_cast<DeclRefExpr>(Step))
3890 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3891 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevecba70f2016-04-12 11:02:11 +00003892 if (UniformedArgs.count(CanonPVD) == 0) {
3893 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
3894 << Step->getSourceRange();
3895 } else if (E->isValueDependent() || E->isTypeDependent() ||
3896 E->isInstantiationDependent() ||
3897 E->containsUnexpandedParameterPack() ||
Alexey Bataeve3727102018-04-18 15:57:46 +00003898 CanonPVD->getType()->hasIntegerRepresentation()) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003899 NewSteps.push_back(Step);
Alexey Bataeve3727102018-04-18 15:57:46 +00003900 } else {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003901 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
3902 << Step->getSourceRange();
3903 }
3904 continue;
3905 }
3906 NewStep = Step;
3907 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3908 !Step->isInstantiationDependent() &&
3909 !Step->containsUnexpandedParameterPack()) {
3910 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
3911 .get();
3912 if (NewStep)
3913 NewStep = VerifyIntegerConstantExpression(NewStep).get();
3914 }
3915 NewSteps.push_back(NewStep);
3916 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003917 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3918 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
Alexey Bataevd93d3762016-04-12 09:35:56 +00003919 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
Alexey Bataevecba70f2016-04-12 11:02:11 +00003920 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
3921 const_cast<Expr **>(Linears.data()), Linears.size(),
3922 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
3923 NewSteps.data(), NewSteps.size(), SR);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003924 ADecl->addAttr(NewAttr);
3925 return ConvertDeclToDeclGroup(ADecl);
3926}
3927
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003928StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3929 Stmt *AStmt,
3930 SourceLocation StartLoc,
3931 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003932 if (!AStmt)
3933 return StmtError();
3934
Alexey Bataeve3727102018-04-18 15:57:46 +00003935 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev9959db52014-05-06 10:08:46 +00003936 // 1.2.2 OpenMP Language Terminology
3937 // Structured block - An executable statement with a single entry at the
3938 // top and a single exit at the bottom.
3939 // The point of exit cannot be a branch out of the structured block.
3940 // longjmp() and throw() must not violate the entry/exit criteria.
3941 CS->getCapturedDecl()->setNothrow();
3942
Reid Kleckner87a31802018-03-12 21:43:02 +00003943 setFunctionHasBranchProtectedScope();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003944
Alexey Bataev25e5b442015-09-15 12:52:43 +00003945 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3946 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003947}
3948
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003949namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003950/// Helper class for checking canonical form of the OpenMP loops and
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003951/// extracting iteration space of each loop in the loop nest, that will be used
3952/// for IR generation.
3953class OpenMPIterationSpaceChecker {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003954 /// Reference to Sema.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003955 Sema &SemaRef;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003956 /// A location for diagnostics (when there is no some better location).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003957 SourceLocation DefaultLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003958 /// A location for diagnostics (when increment is not compatible).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003959 SourceLocation ConditionLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003960 /// A source location for referring to loop init later.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003961 SourceRange InitSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003962 /// A source location for referring to condition later.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003963 SourceRange ConditionSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003964 /// A source location for referring to increment later.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003965 SourceRange IncrementSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003966 /// Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003967 ValueDecl *LCDecl = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003968 /// Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003969 Expr *LCRef = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003970 /// Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003971 Expr *LB = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003972 /// Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003973 Expr *UB = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003974 /// Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003975 Expr *Step = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003976 /// This flag is true when condition is one of:
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003977 /// Var < UB
3978 /// Var <= UB
3979 /// UB > Var
3980 /// UB >= Var
Kelvin Liefbe4af2018-11-21 19:10:48 +00003981 /// This will have no value when the condition is !=
3982 llvm::Optional<bool> TestIsLessOp;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003983 /// This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003984 bool TestIsStrictOp = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003985 /// This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003986 bool SubtractStep = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003987
3988public:
3989 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003990 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003991 /// Check init-expr for canonical loop form and save loop counter
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003992 /// variable - #Var and its initialization value - #LB.
Alexey Bataeve3727102018-04-18 15:57:46 +00003993 bool checkAndSetInit(Stmt *S, bool EmitDiags = true);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003994 /// Check test-expr for canonical form, save upper-bound (#UB), flags
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003995 /// for less/greater and for strict/non-strict comparison.
Alexey Bataeve3727102018-04-18 15:57:46 +00003996 bool checkAndSetCond(Expr *S);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003997 /// Check incr-expr for canonical loop form and return true if it
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003998 /// does not conform, otherwise save loop step (#Step).
Alexey Bataeve3727102018-04-18 15:57:46 +00003999 bool checkAndSetInc(Expr *S);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004000 /// Return the loop counter variable.
Alexey Bataeve3727102018-04-18 15:57:46 +00004001 ValueDecl *getLoopDecl() const { return LCDecl; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004002 /// Return the reference expression to loop counter variable.
Alexey Bataeve3727102018-04-18 15:57:46 +00004003 Expr *getLoopDeclRefExpr() const { return LCRef; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004004 /// Source range of the loop init.
Alexey Bataeve3727102018-04-18 15:57:46 +00004005 SourceRange getInitSrcRange() const { return InitSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004006 /// Source range of the loop condition.
Alexey Bataeve3727102018-04-18 15:57:46 +00004007 SourceRange getConditionSrcRange() const { return ConditionSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004008 /// Source range of the loop increment.
Alexey Bataeve3727102018-04-18 15:57:46 +00004009 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004010 /// True if the step should be subtracted.
Alexey Bataeve3727102018-04-18 15:57:46 +00004011 bool shouldSubtractStep() const { return SubtractStep; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004012 /// Build the expression to calculate the number of iterations.
Alexey Bataeve3727102018-04-18 15:57:46 +00004013 Expr *buildNumIterations(
4014 Scope *S, const bool LimitedType,
4015 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004016 /// Build the precondition expression for the loops.
Alexey Bataeve3727102018-04-18 15:57:46 +00004017 Expr *
4018 buildPreCond(Scope *S, Expr *Cond,
4019 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004020 /// Build reference expression to the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004021 DeclRefExpr *
4022 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
4023 DSAStackTy &DSA) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004024 /// Build reference expression to the private counter be used for
Alexey Bataeva8899172015-08-06 12:30:57 +00004025 /// codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004026 Expr *buildPrivateCounterVar() const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004027 /// Build initialization of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004028 Expr *buildCounterInit() const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004029 /// Build step of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004030 Expr *buildCounterStep() const;
Alexey Bataevf138fda2018-08-13 19:04:24 +00004031 /// Build loop data with counter value for depend clauses in ordered
4032 /// directives.
4033 Expr *
4034 buildOrderedLoopData(Scope *S, Expr *Counter,
4035 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
4036 SourceLocation Loc, Expr *Inc = nullptr,
4037 OverloadedOperatorKind OOK = OO_Amp);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004038 /// Return true if any expression is dependent.
Alexey Bataeve3727102018-04-18 15:57:46 +00004039 bool dependent() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004040
4041private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004042 /// Check the right-hand side of an assignment in the increment
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004043 /// expression.
Alexey Bataeve3727102018-04-18 15:57:46 +00004044 bool checkAndSetIncRHS(Expr *RHS);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004045 /// Helper to set loop counter variable and its initializer.
Alexey Bataeve3727102018-04-18 15:57:46 +00004046 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004047 /// Helper to set upper bound.
Kelvin Liefbe4af2018-11-21 19:10:48 +00004048 bool setUB(Expr *NewUB, llvm::Optional<bool> LessOp, bool StrictOp,
4049 SourceRange SR, SourceLocation SL);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004050 /// Helper to set loop increment.
Alexey Bataeve3727102018-04-18 15:57:46 +00004051 bool setStep(Expr *NewStep, bool Subtract);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004052};
4053
Alexey Bataeve3727102018-04-18 15:57:46 +00004054bool OpenMPIterationSpaceChecker::dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004055 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004056 assert(!LB && !UB && !Step);
4057 return false;
4058 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004059 return LCDecl->getType()->isDependentType() ||
4060 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
4061 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004062}
4063
Alexey Bataeve3727102018-04-18 15:57:46 +00004064bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl,
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004065 Expr *NewLCRefExpr,
4066 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004067 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004068 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00004069 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004070 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004071 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004072 LCDecl = getCanonicalDecl(NewLCDecl);
4073 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004074 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
4075 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00004076 if ((Ctor->isCopyOrMoveConstructor() ||
4077 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4078 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004079 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004080 LB = NewLB;
4081 return false;
4082}
4083
Kelvin Liefbe4af2018-11-21 19:10:48 +00004084bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB, llvm::Optional<bool> LessOp,
4085 bool StrictOp, SourceRange SR,
4086 SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004087 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004088 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
4089 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004090 if (!NewUB)
4091 return true;
4092 UB = NewUB;
Kelvin Liefbe4af2018-11-21 19:10:48 +00004093 if (LessOp)
4094 TestIsLessOp = LessOp;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004095 TestIsStrictOp = StrictOp;
4096 ConditionSrcRange = SR;
4097 ConditionLoc = SL;
4098 return false;
4099}
4100
Alexey Bataeve3727102018-04-18 15:57:46 +00004101bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004102 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004103 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004104 if (!NewStep)
4105 return true;
4106 if (!NewStep->isValueDependent()) {
4107 // Check that the step is integer expression.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004108 SourceLocation StepLoc = NewStep->getBeginLoc();
Alexey Bataev5372fb82017-08-31 23:06:52 +00004109 ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion(
4110 StepLoc, getExprAsWritten(NewStep));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004111 if (Val.isInvalid())
4112 return true;
4113 NewStep = Val.get();
4114
4115 // OpenMP [2.6, Canonical Loop Form, Restrictions]
4116 // If test-expr is of form var relational-op b and relational-op is < or
4117 // <= then incr-expr must cause var to increase on each iteration of the
4118 // loop. If test-expr is of form var relational-op b and relational-op is
4119 // > or >= then incr-expr must cause var to decrease on each iteration of
4120 // the loop.
4121 // If test-expr is of form b relational-op var and relational-op is < or
4122 // <= then incr-expr must cause var to decrease on each iteration of the
4123 // loop. If test-expr is of form b relational-op var and relational-op is
4124 // > or >= then incr-expr must cause var to increase on each iteration of
4125 // the loop.
4126 llvm::APSInt Result;
4127 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
4128 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
4129 bool IsConstNeg =
4130 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004131 bool IsConstPos =
4132 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004133 bool IsConstZero = IsConstant && !Result.getBoolValue();
Kelvin Liefbe4af2018-11-21 19:10:48 +00004134
4135 // != with increment is treated as <; != with decrement is treated as >
4136 if (!TestIsLessOp.hasValue())
4137 TestIsLessOp = IsConstPos || (IsUnsigned && !Subtract);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004138 if (UB && (IsConstZero ||
Kelvin Liefbe4af2018-11-21 19:10:48 +00004139 (TestIsLessOp.getValue() ?
4140 (IsConstNeg || (IsUnsigned && Subtract)) :
4141 (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004142 SemaRef.Diag(NewStep->getExprLoc(),
4143 diag::err_omp_loop_incr_not_compatible)
Kelvin Liefbe4af2018-11-21 19:10:48 +00004144 << LCDecl << TestIsLessOp.getValue() << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004145 SemaRef.Diag(ConditionLoc,
4146 diag::note_omp_loop_cond_requres_compatible_incr)
Kelvin Liefbe4af2018-11-21 19:10:48 +00004147 << TestIsLessOp.getValue() << ConditionSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004148 return true;
4149 }
Kelvin Liefbe4af2018-11-21 19:10:48 +00004150 if (TestIsLessOp.getValue() == Subtract) {
David Majnemer9d168222016-08-05 17:44:54 +00004151 NewStep =
4152 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep)
4153 .get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004154 Subtract = !Subtract;
4155 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004156 }
4157
4158 Step = NewStep;
4159 SubtractStep = Subtract;
4160 return false;
4161}
4162
Alexey Bataeve3727102018-04-18 15:57:46 +00004163bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004164 // Check init-expr for canonical loop form and save loop counter
4165 // variable - #Var and its initialization value - #LB.
4166 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
4167 // var = lb
4168 // integer-type var = lb
4169 // random-access-iterator-type var = lb
4170 // pointer-type var = lb
4171 //
4172 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00004173 if (EmitDiags) {
4174 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
4175 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004176 return true;
4177 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004178 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4179 if (!ExprTemp->cleanupsHaveSideEffects())
4180 S = ExprTemp->getSubExpr();
4181
Alexander Musmana5f070a2014-10-01 06:03:56 +00004182 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004183 if (Expr *E = dyn_cast<Expr>(S))
4184 S = E->IgnoreParens();
David Majnemer9d168222016-08-05 17:44:54 +00004185 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004186 if (BO->getOpcode() == BO_Assign) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004187 Expr *LHS = BO->getLHS()->IgnoreParens();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004188 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
4189 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4190 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataeve3727102018-04-18 15:57:46 +00004191 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4192 return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004193 }
4194 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4195 if (ME->isArrow() &&
4196 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
Alexey Bataeve3727102018-04-18 15:57:46 +00004197 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004198 }
4199 }
David Majnemer9d168222016-08-05 17:44:54 +00004200 } else if (auto *DS = dyn_cast<DeclStmt>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004201 if (DS->isSingleDecl()) {
David Majnemer9d168222016-08-05 17:44:54 +00004202 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00004203 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004204 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00004205 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004206 SemaRef.Diag(S->getBeginLoc(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004207 diag::ext_omp_loop_not_canonical_init)
4208 << S->getSourceRange();
Alexey Bataevf138fda2018-08-13 19:04:24 +00004209 return setLCDeclAndLB(
4210 Var,
4211 buildDeclRefExpr(SemaRef, Var,
4212 Var->getType().getNonReferenceType(),
4213 DS->getBeginLoc()),
4214 Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004215 }
4216 }
4217 }
David Majnemer9d168222016-08-05 17:44:54 +00004218 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004219 if (CE->getOperator() == OO_Equal) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004220 Expr *LHS = CE->getArg(0);
David Majnemer9d168222016-08-05 17:44:54 +00004221 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004222 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4223 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataeve3727102018-04-18 15:57:46 +00004224 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4225 return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004226 }
4227 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4228 if (ME->isArrow() &&
4229 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
Alexey Bataeve3727102018-04-18 15:57:46 +00004230 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004231 }
4232 }
4233 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004234
Alexey Bataeve3727102018-04-18 15:57:46 +00004235 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004236 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00004237 if (EmitDiags) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004238 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init)
Alexey Bataev9c821032015-04-30 04:23:23 +00004239 << S->getSourceRange();
4240 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004241 return true;
4242}
4243
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004244/// Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004245/// variable (which may be the loop variable) if possible.
Alexey Bataeve3727102018-04-18 15:57:46 +00004246static const ValueDecl *getInitLCDecl(const Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004247 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00004248 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004249 E = getExprAsWritten(E);
Alexey Bataeve3727102018-04-18 15:57:46 +00004250 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004251 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00004252 if ((Ctor->isCopyOrMoveConstructor() ||
4253 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4254 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004255 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00004256 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
4257 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004258 return getCanonicalDecl(VD);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004259 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004260 if (const auto *ME = dyn_cast_or_null<MemberExpr>(E))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004261 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4262 return getCanonicalDecl(ME->getMemberDecl());
4263 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004264}
4265
Alexey Bataeve3727102018-04-18 15:57:46 +00004266bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004267 // Check test-expr for canonical form, save upper-bound UB, flags for
4268 // less/greater and for strict/non-strict comparison.
4269 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4270 // var relational-op b
4271 // b relational-op var
4272 //
4273 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004274 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004275 return true;
4276 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004277 S = getExprAsWritten(S);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004278 SourceLocation CondLoc = S->getBeginLoc();
David Majnemer9d168222016-08-05 17:44:54 +00004279 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004280 if (BO->isRelationalOp()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004281 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4282 return setUB(BO->getRHS(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004283 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
4284 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4285 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +00004286 if (getInitLCDecl(BO->getRHS()) == LCDecl)
4287 return setUB(BO->getLHS(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004288 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
4289 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4290 BO->getSourceRange(), BO->getOperatorLoc());
Kelvin Liefbe4af2018-11-21 19:10:48 +00004291 } else if (BO->getOpcode() == BO_NE)
4292 return setUB(getInitLCDecl(BO->getLHS()) == LCDecl ?
4293 BO->getRHS() : BO->getLHS(),
4294 /*LessOp=*/llvm::None,
4295 /*StrictOp=*/true,
4296 BO->getSourceRange(), BO->getOperatorLoc());
David Majnemer9d168222016-08-05 17:44:54 +00004297 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004298 if (CE->getNumArgs() == 2) {
4299 auto Op = CE->getOperator();
4300 switch (Op) {
4301 case OO_Greater:
4302 case OO_GreaterEqual:
4303 case OO_Less:
4304 case OO_LessEqual:
Alexey Bataeve3727102018-04-18 15:57:46 +00004305 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4306 return setUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004307 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4308 CE->getOperatorLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +00004309 if (getInitLCDecl(CE->getArg(1)) == LCDecl)
4310 return setUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004311 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4312 CE->getOperatorLoc());
4313 break;
Kelvin Liefbe4af2018-11-21 19:10:48 +00004314 case OO_ExclaimEqual:
4315 return setUB(getInitLCDecl(CE->getArg(0)) == LCDecl ?
4316 CE->getArg(1) : CE->getArg(0),
4317 /*LessOp=*/llvm::None,
4318 /*StrictOp=*/true,
4319 CE->getSourceRange(),
4320 CE->getOperatorLoc());
4321 break;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004322 default:
4323 break;
4324 }
4325 }
4326 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004327 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004328 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004329 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004330 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004331 return true;
4332}
4333
Alexey Bataeve3727102018-04-18 15:57:46 +00004334bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004335 // RHS of canonical loop form increment can be:
4336 // var + incr
4337 // incr + var
4338 // var - incr
4339 //
4340 RHS = RHS->IgnoreParenImpCasts();
David Majnemer9d168222016-08-05 17:44:54 +00004341 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004342 if (BO->isAdditiveOp()) {
4343 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataeve3727102018-04-18 15:57:46 +00004344 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4345 return setStep(BO->getRHS(), !IsAdd);
4346 if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl)
4347 return setStep(BO->getLHS(), /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004348 }
David Majnemer9d168222016-08-05 17:44:54 +00004349 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004350 bool IsAdd = CE->getOperator() == OO_Plus;
4351 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004352 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4353 return setStep(CE->getArg(1), !IsAdd);
4354 if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl)
4355 return setStep(CE->getArg(0), /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004356 }
4357 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004358 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004359 return false;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004360 SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004361 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004362 return true;
4363}
4364
Alexey Bataeve3727102018-04-18 15:57:46 +00004365bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004366 // Check incr-expr for canonical loop form and return true if it
4367 // does not conform.
4368 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4369 // ++var
4370 // var++
4371 // --var
4372 // var--
4373 // var += incr
4374 // var -= incr
4375 // var = var + incr
4376 // var = incr + var
4377 // var = var - incr
4378 //
4379 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004380 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004381 return true;
4382 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004383 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4384 if (!ExprTemp->cleanupsHaveSideEffects())
4385 S = ExprTemp->getSubExpr();
4386
Alexander Musmana5f070a2014-10-01 06:03:56 +00004387 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004388 S = S->IgnoreParens();
David Majnemer9d168222016-08-05 17:44:54 +00004389 if (auto *UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004390 if (UO->isIncrementDecrementOp() &&
Alexey Bataeve3727102018-04-18 15:57:46 +00004391 getInitLCDecl(UO->getSubExpr()) == LCDecl)
4392 return setStep(SemaRef
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004393 .ActOnIntegerConstant(UO->getBeginLoc(),
David Majnemer9d168222016-08-05 17:44:54 +00004394 (UO->isDecrementOp() ? -1 : 1))
4395 .get(),
Alexey Bataeve3727102018-04-18 15:57:46 +00004396 /*Subtract=*/false);
David Majnemer9d168222016-08-05 17:44:54 +00004397 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004398 switch (BO->getOpcode()) {
4399 case BO_AddAssign:
4400 case BO_SubAssign:
Alexey Bataeve3727102018-04-18 15:57:46 +00004401 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4402 return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004403 break;
4404 case BO_Assign:
Alexey Bataeve3727102018-04-18 15:57:46 +00004405 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4406 return checkAndSetIncRHS(BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004407 break;
4408 default:
4409 break;
4410 }
David Majnemer9d168222016-08-05 17:44:54 +00004411 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004412 switch (CE->getOperator()) {
4413 case OO_PlusPlus:
4414 case OO_MinusMinus:
Alexey Bataeve3727102018-04-18 15:57:46 +00004415 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4416 return setStep(SemaRef
David Majnemer9d168222016-08-05 17:44:54 +00004417 .ActOnIntegerConstant(
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004418 CE->getBeginLoc(),
David Majnemer9d168222016-08-05 17:44:54 +00004419 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
4420 .get(),
Alexey Bataeve3727102018-04-18 15:57:46 +00004421 /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004422 break;
4423 case OO_PlusEqual:
4424 case OO_MinusEqual:
Alexey Bataeve3727102018-04-18 15:57:46 +00004425 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4426 return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004427 break;
4428 case OO_Equal:
Alexey Bataeve3727102018-04-18 15:57:46 +00004429 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4430 return checkAndSetIncRHS(CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004431 break;
4432 default:
4433 break;
4434 }
4435 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004436 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004437 return false;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004438 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004439 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004440 return true;
4441}
Alexander Musmana5f070a2014-10-01 06:03:56 +00004442
Alexey Bataev5a3af132016-03-29 08:58:54 +00004443static ExprResult
4444tryBuildCapture(Sema &SemaRef, Expr *Capture,
Alexey Bataeve3727102018-04-18 15:57:46 +00004445 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00004446 if (SemaRef.CurContext->isDependentContext())
4447 return ExprResult(Capture);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004448 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
4449 return SemaRef.PerformImplicitConversion(
4450 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
4451 /*AllowExplicit=*/true);
4452 auto I = Captures.find(Capture);
4453 if (I != Captures.end())
4454 return buildCapture(SemaRef, Capture, I->second);
4455 DeclRefExpr *Ref = nullptr;
4456 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
4457 Captures[Capture] = Ref;
4458 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004459}
4460
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004461/// Build the expression to calculate the number of iterations.
Alexey Bataeve3727102018-04-18 15:57:46 +00004462Expr *OpenMPIterationSpaceChecker::buildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004463 Scope *S, const bool LimitedType,
Alexey Bataeve3727102018-04-18 15:57:46 +00004464 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004465 ExprResult Diff;
Alexey Bataeve3727102018-04-18 15:57:46 +00004466 QualType VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004467 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004468 SemaRef.getLangOpts().CPlusPlus) {
4469 // Upper - Lower
Kelvin Liefbe4af2018-11-21 19:10:48 +00004470 Expr *UBExpr = TestIsLessOp.getValue() ? UB : LB;
4471 Expr *LBExpr = TestIsLessOp.getValue() ? LB : UB;
Alexey Bataev5a3af132016-03-29 08:58:54 +00004472 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
4473 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004474 if (!Upper || !Lower)
4475 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004476
4477 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4478
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004479 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004480 // BuildBinOp already emitted error, this one is to point user to upper
4481 // and lower bound, and to tell what is passed to 'operator-'.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004482 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004483 << Upper->getSourceRange() << Lower->getSourceRange();
4484 return nullptr;
4485 }
4486 }
4487
4488 if (!Diff.isUsable())
4489 return nullptr;
4490
4491 // Upper - Lower [- 1]
4492 if (TestIsStrictOp)
4493 Diff = SemaRef.BuildBinOp(
4494 S, DefaultLoc, BO_Sub, Diff.get(),
4495 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4496 if (!Diff.isUsable())
4497 return nullptr;
4498
4499 // Upper - Lower [- 1] + Step
Alexey Bataeve3727102018-04-18 15:57:46 +00004500 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004501 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004502 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004503 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004504 if (!Diff.isUsable())
4505 return nullptr;
4506
4507 // Parentheses (for dumping/debugging purposes only).
4508 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4509 if (!Diff.isUsable())
4510 return nullptr;
4511
4512 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004513 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004514 if (!Diff.isUsable())
4515 return nullptr;
4516
Alexander Musman174b3ca2014-10-06 11:16:29 +00004517 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004518 QualType Type = Diff.get()->getType();
Alexey Bataeve3727102018-04-18 15:57:46 +00004519 ASTContext &C = SemaRef.Context;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004520 bool UseVarType = VarType->hasIntegerRepresentation() &&
4521 C.getTypeSize(Type) > C.getTypeSize(VarType);
4522 if (!Type->isIntegerType() || UseVarType) {
4523 unsigned NewSize =
4524 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4525 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4526 : Type->hasSignedIntegerRepresentation();
4527 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00004528 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4529 Diff = SemaRef.PerformImplicitConversion(
4530 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4531 if (!Diff.isUsable())
4532 return nullptr;
4533 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004534 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004535 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00004536 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4537 if (NewSize != C.getTypeSize(Type)) {
4538 if (NewSize < C.getTypeSize(Type)) {
4539 assert(NewSize == 64 && "incorrect loop var size");
4540 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4541 << InitSrcRange << ConditionSrcRange;
4542 }
4543 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004544 NewSize, Type->hasSignedIntegerRepresentation() ||
4545 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00004546 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4547 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4548 Sema::AA_Converting, true);
4549 if (!Diff.isUsable())
4550 return nullptr;
4551 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004552 }
4553 }
4554
Alexander Musmana5f070a2014-10-01 06:03:56 +00004555 return Diff.get();
4556}
4557
Alexey Bataeve3727102018-04-18 15:57:46 +00004558Expr *OpenMPIterationSpaceChecker::buildPreCond(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004559 Scope *S, Expr *Cond,
Alexey Bataeve3727102018-04-18 15:57:46 +00004560 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004561 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4562 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4563 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004564
Alexey Bataeve3727102018-04-18 15:57:46 +00004565 ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures);
4566 ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004567 if (!NewLB.isUsable() || !NewUB.isUsable())
4568 return nullptr;
4569
Alexey Bataeve3727102018-04-18 15:57:46 +00004570 ExprResult CondExpr =
4571 SemaRef.BuildBinOp(S, DefaultLoc,
Kelvin Liefbe4af2018-11-21 19:10:48 +00004572 TestIsLessOp.getValue() ?
4573 (TestIsStrictOp ? BO_LT : BO_LE) :
4574 (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataeve3727102018-04-18 15:57:46 +00004575 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004576 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004577 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4578 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00004579 CondExpr = SemaRef.PerformImplicitConversion(
4580 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4581 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004582 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00004583 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4584 // Otherwise use original loop conditon and evaluate it in runtime.
4585 return CondExpr.isUsable() ? CondExpr.get() : Cond;
4586}
4587
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004588/// Build reference expression to the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004589DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar(
Alexey Bataevf138fda2018-08-13 19:04:24 +00004590 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
4591 DSAStackTy &DSA) const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004592 auto *VD = dyn_cast<VarDecl>(LCDecl);
4593 if (!VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004594 VD = SemaRef.isOpenMPCapturedDecl(LCDecl);
4595 DeclRefExpr *Ref = buildDeclRefExpr(
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004596 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
Alexey Bataeve3727102018-04-18 15:57:46 +00004597 const DSAStackTy::DSAVarData Data =
4598 DSA.getTopDSA(LCDecl, /*FromParent=*/false);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004599 // If the loop control decl is explicitly marked as private, do not mark it
4600 // as captured again.
4601 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4602 Captures.insert(std::make_pair(LCRef, Ref));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004603 return Ref;
4604 }
4605 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
Alexey Bataeva8899172015-08-06 12:30:57 +00004606 DefaultLoc);
4607}
4608
Alexey Bataeve3727102018-04-18 15:57:46 +00004609Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004610 if (LCDecl && !LCDecl->isInvalidDecl()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004611 QualType Type = LCDecl->getType().getNonReferenceType();
4612 VarDecl *PrivateVar = buildVarDecl(
Alexey Bataev63cc8e92018-03-20 14:45:59 +00004613 SemaRef, DefaultLoc, Type, LCDecl->getName(),
4614 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr,
4615 isa<VarDecl>(LCDecl)
4616 ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc)
4617 : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00004618 if (PrivateVar->isInvalidDecl())
4619 return nullptr;
4620 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4621 }
4622 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004623}
4624
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004625/// Build initialization of the counter to be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004626Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004627
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004628/// Build step of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004629Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004630
Alexey Bataevf138fda2018-08-13 19:04:24 +00004631Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData(
4632 Scope *S, Expr *Counter,
4633 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc,
4634 Expr *Inc, OverloadedOperatorKind OOK) {
4635 Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get();
4636 if (!Cnt)
4637 return nullptr;
4638 if (Inc) {
4639 assert((OOK == OO_Plus || OOK == OO_Minus) &&
4640 "Expected only + or - operations for depend clauses.");
4641 BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub;
4642 Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get();
4643 if (!Cnt)
4644 return nullptr;
4645 }
4646 ExprResult Diff;
4647 QualType VarType = LCDecl->getType().getNonReferenceType();
4648 if (VarType->isIntegerType() || VarType->isPointerType() ||
4649 SemaRef.getLangOpts().CPlusPlus) {
4650 // Upper - Lower
4651 Expr *Upper =
Kelvin Liefbe4af2018-11-21 19:10:48 +00004652 TestIsLessOp.getValue() ? Cnt : tryBuildCapture(SemaRef, UB, Captures).get();
Alexey Bataevf138fda2018-08-13 19:04:24 +00004653 Expr *Lower =
Kelvin Liefbe4af2018-11-21 19:10:48 +00004654 TestIsLessOp.getValue() ? tryBuildCapture(SemaRef, LB, Captures).get() : Cnt;
Alexey Bataevf138fda2018-08-13 19:04:24 +00004655 if (!Upper || !Lower)
4656 return nullptr;
4657
4658 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4659
4660 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
4661 // BuildBinOp already emitted error, this one is to point user to upper
4662 // and lower bound, and to tell what is passed to 'operator-'.
4663 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
4664 << Upper->getSourceRange() << Lower->getSourceRange();
4665 return nullptr;
4666 }
4667 }
4668
4669 if (!Diff.isUsable())
4670 return nullptr;
4671
4672 // Parentheses (for dumping/debugging purposes only).
4673 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4674 if (!Diff.isUsable())
4675 return nullptr;
4676
4677 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
4678 if (!NewStep.isUsable())
4679 return nullptr;
4680 // (Upper - Lower) / Step
4681 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
4682 if (!Diff.isUsable())
4683 return nullptr;
4684
4685 return Diff.get();
4686}
4687
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004688/// Iteration space of a single for loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004689struct LoopIterationSpace final {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004690 /// Condition of the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004691 Expr *PreCond = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004692 /// This expression calculates the number of iterations in the loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004693 /// It is always possible to calculate it before starting the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004694 Expr *NumIterations = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004695 /// The loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004696 Expr *CounterVar = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004697 /// Private loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004698 Expr *PrivateCounterVar = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004699 /// This is initializer for the initial value of #CounterVar.
Alexey Bataev8b427062016-05-25 12:36:08 +00004700 Expr *CounterInit = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004701 /// This is step for the #CounterVar used to generate its update:
Alexander Musmana5f070a2014-10-01 06:03:56 +00004702 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
Alexey Bataev8b427062016-05-25 12:36:08 +00004703 Expr *CounterStep = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004704 /// Should step be subtracted?
Alexey Bataev8b427062016-05-25 12:36:08 +00004705 bool Subtract = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004706 /// Source range of the loop init.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004707 SourceRange InitSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004708 /// Source range of the loop condition.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004709 SourceRange CondSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004710 /// Source range of the loop increment.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004711 SourceRange IncSrcRange;
4712};
4713
Alexey Bataev23b69422014-06-18 07:08:49 +00004714} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004715
Alexey Bataev9c821032015-04-30 04:23:23 +00004716void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4717 assert(getLangOpts().OpenMP && "OpenMP is not active.");
4718 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004719 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4720 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00004721 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevce901812018-12-19 18:16:37 +00004722 DSAStack->loopStart();
Alexey Bataev9c821032015-04-30 04:23:23 +00004723 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataeve3727102018-04-18 15:57:46 +00004724 if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) {
4725 if (ValueDecl *D = ISC.getLoopDecl()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004726 auto *VD = dyn_cast<VarDecl>(D);
4727 if (!VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004728 if (VarDecl *Private = isOpenMPCapturedDecl(D)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004729 VD = Private;
Alexey Bataeve3727102018-04-18 15:57:46 +00004730 } else {
4731 DeclRefExpr *Ref = buildCapture(*this, D, ISC.getLoopDeclRefExpr(),
4732 /*WithInit=*/false);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004733 VD = cast<VarDecl>(Ref->getDecl());
4734 }
4735 }
4736 DSAStack->addLoopControlVariable(D, VD);
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00004737 const Decl *LD = DSAStack->getPossiblyLoopCunter();
4738 if (LD != D->getCanonicalDecl()) {
4739 DSAStack->resetPossibleLoopCounter();
4740 if (auto *Var = dyn_cast_or_null<VarDecl>(LD))
4741 MarkDeclarationsReferencedInExpr(
4742 buildDeclRefExpr(*this, const_cast<VarDecl *>(Var),
4743 Var->getType().getNonLValueExprType(Context),
4744 ForLoc, /*RefersToCapture=*/true));
4745 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004746 }
4747 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004748 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00004749 }
4750}
4751
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004752/// Called on a for stmt to check and extract its iteration space
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004753/// for further processing (such as collapsing).
Alexey Bataeve3727102018-04-18 15:57:46 +00004754static bool checkOpenMPIterationSpace(
Alexey Bataev4acb8592014-07-07 13:01:15 +00004755 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4756 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataevf138fda2018-08-13 19:04:24 +00004757 unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr,
4758 Expr *OrderedLoopCountExpr,
Alexey Bataeve3727102018-04-18 15:57:46 +00004759 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004760 LoopIterationSpace &ResultIterSpace,
Alexey Bataeve3727102018-04-18 15:57:46 +00004761 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004762 // OpenMP [2.6, Canonical Loop Form]
4763 // for (init-expr; test-expr; incr-expr) structured-block
David Majnemer9d168222016-08-05 17:44:54 +00004764 auto *For = dyn_cast_or_null<ForStmt>(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004765 if (!For) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004766 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004767 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
Alexey Bataevf138fda2018-08-13 19:04:24 +00004768 << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount
Alexey Bataev10e775f2015-07-30 11:36:16 +00004769 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
Alexey Bataevf138fda2018-08-13 19:04:24 +00004770 if (TotalNestedLoopCount > 1) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00004771 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4772 SemaRef.Diag(DSA.getConstructLoc(),
4773 diag::note_omp_collapse_ordered_expr)
4774 << 2 << CollapseLoopCountExpr->getSourceRange()
4775 << OrderedLoopCountExpr->getSourceRange();
4776 else if (CollapseLoopCountExpr)
4777 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4778 diag::note_omp_collapse_ordered_expr)
4779 << 0 << CollapseLoopCountExpr->getSourceRange();
4780 else
4781 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4782 diag::note_omp_collapse_ordered_expr)
4783 << 1 << OrderedLoopCountExpr->getSourceRange();
4784 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004785 return true;
4786 }
4787 assert(For->getBody());
4788
4789 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4790
4791 // Check init.
Alexey Bataeve3727102018-04-18 15:57:46 +00004792 Stmt *Init = For->getInit();
4793 if (ISC.checkAndSetInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004794 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004795
4796 bool HasErrors = false;
4797
4798 // Check loop variable's type.
Alexey Bataeve3727102018-04-18 15:57:46 +00004799 if (ValueDecl *LCDecl = ISC.getLoopDecl()) {
4800 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004801
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004802 // OpenMP [2.6, Canonical Loop Form]
4803 // Var is one of the following:
4804 // A variable of signed or unsigned integer type.
4805 // For C++, a variable of a random access iterator type.
4806 // For C, a variable of a pointer type.
Alexey Bataeve3727102018-04-18 15:57:46 +00004807 QualType VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004808 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4809 !VarType->isPointerType() &&
4810 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004811 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004812 << SemaRef.getLangOpts().CPlusPlus;
4813 HasErrors = true;
4814 }
4815
4816 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4817 // a Construct
4818 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4819 // parallel for construct is (are) private.
4820 // The loop iteration variable in the associated for-loop of a simd
4821 // construct with just one associated for-loop is linear with a
4822 // constant-linear-step that is the increment of the associated for-loop.
4823 // Exclude loop var from the list of variables with implicitly defined data
4824 // sharing attributes.
4825 VarsWithImplicitDSA.erase(LCDecl);
4826
4827 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4828 // in a Construct, C/C++].
4829 // The loop iteration variable in the associated for-loop of a simd
4830 // construct with just one associated for-loop may be listed in a linear
4831 // clause with a constant-linear-step that is the increment of the
4832 // associated for-loop.
4833 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4834 // parallel for construct may be listed in a private or lastprivate clause.
4835 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4836 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4837 // declared in the loop and it is predetermined as a private.
Alexey Bataeve3727102018-04-18 15:57:46 +00004838 OpenMPClauseKind PredeterminedCKind =
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004839 isOpenMPSimdDirective(DKind)
4840 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4841 : OMPC_private;
4842 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4843 DVar.CKind != PredeterminedCKind) ||
4844 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4845 isOpenMPDistributeDirective(DKind)) &&
4846 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4847 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4848 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004849 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004850 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4851 << getOpenMPClauseName(PredeterminedCKind);
4852 if (DVar.RefExpr == nullptr)
4853 DVar.CKind = PredeterminedCKind;
Alexey Bataeve3727102018-04-18 15:57:46 +00004854 reportOriginalDsa(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004855 HasErrors = true;
4856 } else if (LoopDeclRefExpr != nullptr) {
4857 // Make the loop iteration variable private (for worksharing constructs),
4858 // linear (for simd directives with the only one associated loop) or
4859 // lastprivate (for simd directives with several collapsed or ordered
4860 // loops).
4861 if (DVar.CKind == OMPC_unknown)
Alexey Bataev7ace49d2016-05-17 08:55:33 +00004862 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate,
4863 [](OpenMPDirectiveKind) -> bool { return true; },
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004864 /*FromParent=*/false);
4865 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4866 }
4867
4868 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4869
4870 // Check test-expr.
Alexey Bataeve3727102018-04-18 15:57:46 +00004871 HasErrors |= ISC.checkAndSetCond(For->getCond());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004872
4873 // Check incr-expr.
Alexey Bataeve3727102018-04-18 15:57:46 +00004874 HasErrors |= ISC.checkAndSetInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004875 }
4876
Alexey Bataeve3727102018-04-18 15:57:46 +00004877 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004878 return HasErrors;
4879
Alexander Musmana5f070a2014-10-01 06:03:56 +00004880 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004881 ResultIterSpace.PreCond =
Alexey Bataeve3727102018-04-18 15:57:46 +00004882 ISC.buildPreCond(DSA.getCurScope(), For->getCond(), Captures);
4883 ResultIterSpace.NumIterations = ISC.buildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004884 DSA.getCurScope(),
4885 (isOpenMPWorksharingDirective(DKind) ||
4886 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4887 Captures);
Alexey Bataeve3727102018-04-18 15:57:46 +00004888 ResultIterSpace.CounterVar = ISC.buildCounterVar(Captures, DSA);
4889 ResultIterSpace.PrivateCounterVar = ISC.buildPrivateCounterVar();
4890 ResultIterSpace.CounterInit = ISC.buildCounterInit();
4891 ResultIterSpace.CounterStep = ISC.buildCounterStep();
4892 ResultIterSpace.InitSrcRange = ISC.getInitSrcRange();
4893 ResultIterSpace.CondSrcRange = ISC.getConditionSrcRange();
4894 ResultIterSpace.IncSrcRange = ISC.getIncrementSrcRange();
4895 ResultIterSpace.Subtract = ISC.shouldSubtractStep();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004896
Alexey Bataev62dbb972015-04-22 11:59:37 +00004897 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4898 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004899 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004900 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004901 ResultIterSpace.CounterInit == nullptr ||
4902 ResultIterSpace.CounterStep == nullptr);
Alexey Bataevf138fda2018-08-13 19:04:24 +00004903 if (!HasErrors && DSA.isOrderedRegion()) {
4904 if (DSA.getOrderedRegionParam().second->getNumForLoops()) {
4905 if (CurrentNestedLoopCount <
4906 DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) {
4907 DSA.getOrderedRegionParam().second->setLoopNumIterations(
4908 CurrentNestedLoopCount, ResultIterSpace.NumIterations);
4909 DSA.getOrderedRegionParam().second->setLoopCounter(
4910 CurrentNestedLoopCount, ResultIterSpace.CounterVar);
4911 }
4912 }
4913 for (auto &Pair : DSA.getDoacrossDependClauses()) {
4914 if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) {
4915 // Erroneous case - clause has some problems.
4916 continue;
4917 }
4918 if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink &&
4919 Pair.second.size() <= CurrentNestedLoopCount) {
4920 // Erroneous case - clause has some problems.
4921 Pair.first->setLoopData(CurrentNestedLoopCount, nullptr);
4922 continue;
4923 }
4924 Expr *CntValue;
4925 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
4926 CntValue = ISC.buildOrderedLoopData(
4927 DSA.getCurScope(), ResultIterSpace.CounterVar, Captures,
4928 Pair.first->getDependencyLoc());
4929 else
4930 CntValue = ISC.buildOrderedLoopData(
4931 DSA.getCurScope(), ResultIterSpace.CounterVar, Captures,
4932 Pair.first->getDependencyLoc(),
4933 Pair.second[CurrentNestedLoopCount].first,
4934 Pair.second[CurrentNestedLoopCount].second);
4935 Pair.first->setLoopData(CurrentNestedLoopCount, CntValue);
4936 }
4937 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004938
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004939 return HasErrors;
4940}
4941
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004942/// Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004943static ExprResult
Alexey Bataeve3727102018-04-18 15:57:46 +00004944buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004945 ExprResult Start,
Alexey Bataeve3727102018-04-18 15:57:46 +00004946 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004947 // Build 'VarRef = Start.
Alexey Bataeve3727102018-04-18 15:57:46 +00004948 ExprResult NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004949 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004950 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004951 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00004952 VarRef.get()->getType())) {
4953 NewStart = SemaRef.PerformImplicitConversion(
4954 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4955 /*AllowExplicit=*/true);
4956 if (!NewStart.isUsable())
4957 return ExprError();
4958 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004959
Alexey Bataeve3727102018-04-18 15:57:46 +00004960 ExprResult Init =
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004961 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4962 return Init;
4963}
4964
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004965/// Build 'VarRef = Start + Iter * Step'.
Alexey Bataeve3727102018-04-18 15:57:46 +00004966static ExprResult buildCounterUpdate(
4967 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4968 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract,
4969 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004970 // Add parentheses (for debugging purposes only).
4971 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4972 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4973 !Step.isUsable())
4974 return ExprError();
4975
Alexey Bataev5a3af132016-03-29 08:58:54 +00004976 ExprResult NewStep = Step;
4977 if (Captures)
4978 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004979 if (NewStep.isInvalid())
4980 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004981 ExprResult Update =
4982 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004983 if (!Update.isUsable())
4984 return ExprError();
4985
Alexey Bataevc0214e02016-02-16 12:13:49 +00004986 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4987 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004988 ExprResult NewStart = Start;
4989 if (Captures)
4990 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004991 if (NewStart.isInvalid())
4992 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004993
Alexey Bataevc0214e02016-02-16 12:13:49 +00004994 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4995 ExprResult SavedUpdate = Update;
4996 ExprResult UpdateVal;
4997 if (VarRef.get()->getType()->isOverloadableType() ||
4998 NewStart.get()->getType()->isOverloadableType() ||
4999 Update.get()->getType()->isOverloadableType()) {
5000 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
5001 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
5002 Update =
5003 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
5004 if (Update.isUsable()) {
5005 UpdateVal =
5006 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
5007 VarRef.get(), SavedUpdate.get());
5008 if (UpdateVal.isUsable()) {
5009 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
5010 UpdateVal.get());
5011 }
5012 }
5013 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
5014 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005015
Alexey Bataevc0214e02016-02-16 12:13:49 +00005016 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
5017 if (!Update.isUsable() || !UpdateVal.isUsable()) {
5018 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
5019 NewStart.get(), SavedUpdate.get());
5020 if (!Update.isUsable())
5021 return ExprError();
5022
Alexey Bataev11481f52016-02-17 10:29:05 +00005023 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
5024 VarRef.get()->getType())) {
5025 Update = SemaRef.PerformImplicitConversion(
5026 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
5027 if (!Update.isUsable())
5028 return ExprError();
5029 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00005030
5031 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
5032 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005033 return Update;
5034}
5035
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005036/// Convert integer expression \a E to make it have at least \a Bits
Alexander Musmana5f070a2014-10-01 06:03:56 +00005037/// bits.
Alexey Bataeve3727102018-04-18 15:57:46 +00005038static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00005039 if (E == nullptr)
5040 return ExprError();
Alexey Bataeve3727102018-04-18 15:57:46 +00005041 ASTContext &C = SemaRef.Context;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005042 QualType OldType = E->getType();
5043 unsigned HasBits = C.getTypeSize(OldType);
5044 if (HasBits >= Bits)
5045 return ExprResult(E);
5046 // OK to convert to signed, because new type has more bits than old.
5047 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
5048 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
5049 true);
5050}
5051
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005052/// Check if the given expression \a E is a constant integer that fits
Alexander Musmana5f070a2014-10-01 06:03:56 +00005053/// into \a Bits bits.
Alexey Bataeve3727102018-04-18 15:57:46 +00005054static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00005055 if (E == nullptr)
5056 return false;
5057 llvm::APSInt Result;
5058 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
5059 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
5060 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005061}
5062
Alexey Bataev5a3af132016-03-29 08:58:54 +00005063/// Build preinits statement for the given declarations.
5064static Stmt *buildPreInits(ASTContext &Context,
Alexey Bataevc5514062017-10-25 15:44:52 +00005065 MutableArrayRef<Decl *> PreInits) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005066 if (!PreInits.empty()) {
5067 return new (Context) DeclStmt(
5068 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
5069 SourceLocation(), SourceLocation());
5070 }
5071 return nullptr;
5072}
5073
5074/// Build preinits statement for the given declarations.
Alexey Bataevc5514062017-10-25 15:44:52 +00005075static Stmt *
5076buildPreInits(ASTContext &Context,
Alexey Bataeve3727102018-04-18 15:57:46 +00005077 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005078 if (!Captures.empty()) {
5079 SmallVector<Decl *, 16> PreInits;
Alexey Bataeve3727102018-04-18 15:57:46 +00005080 for (const auto &Pair : Captures)
Alexey Bataev5a3af132016-03-29 08:58:54 +00005081 PreInits.push_back(Pair.second->getDecl());
5082 return buildPreInits(Context, PreInits);
5083 }
5084 return nullptr;
5085}
5086
5087/// Build postupdate expression for the given list of postupdates expressions.
5088static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
5089 Expr *PostUpdate = nullptr;
5090 if (!PostUpdates.empty()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00005091 for (Expr *E : PostUpdates) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005092 Expr *ConvE = S.BuildCStyleCastExpr(
5093 E->getExprLoc(),
5094 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
5095 E->getExprLoc(), E)
5096 .get();
5097 PostUpdate = PostUpdate
5098 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
5099 PostUpdate, ConvE)
5100 .get()
5101 : ConvE;
5102 }
5103 }
5104 return PostUpdate;
5105}
5106
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005107/// Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00005108/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
5109/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005110static unsigned
Alexey Bataeve3727102018-04-18 15:57:46 +00005111checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
Alexey Bataev10e775f2015-07-30 11:36:16 +00005112 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
5113 DSAStackTy &DSA,
Alexey Bataeve3727102018-04-18 15:57:46 +00005114 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00005115 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005116 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005117 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005118 // Found 'collapse' clause - calculate collapse number.
Fangrui Song407659a2018-11-30 23:41:18 +00005119 Expr::EvalResult Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005120 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Fangrui Song407659a2018-11-30 23:41:18 +00005121 NestedLoopCount = Result.Val.getInt().getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005122 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00005123 unsigned OrderedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005124 if (OrderedLoopCountExpr) {
5125 // Found 'ordered' clause - calculate collapse number.
Fangrui Song407659a2018-11-30 23:41:18 +00005126 Expr::EvalResult EVResult;
5127 if (OrderedLoopCountExpr->EvaluateAsInt(EVResult, SemaRef.getASTContext())) {
5128 llvm::APSInt Result = EVResult.Val.getInt();
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005129 if (Result.getLimitedValue() < NestedLoopCount) {
5130 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
5131 diag::err_omp_wrong_ordered_loop_count)
5132 << OrderedLoopCountExpr->getSourceRange();
5133 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
5134 diag::note_collapse_loop_count)
5135 << CollapseLoopCountExpr->getSourceRange();
5136 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00005137 OrderedLoopCount = Result.getLimitedValue();
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005138 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005139 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005140 // This is helper routine for loop directives (e.g., 'for', 'simd',
5141 // 'for simd', etc.).
Alexey Bataeve3727102018-04-18 15:57:46 +00005142 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005143 SmallVector<LoopIterationSpace, 4> IterSpaces;
Alexey Bataevf138fda2018-08-13 19:04:24 +00005144 IterSpaces.resize(std::max(OrderedLoopCount, NestedLoopCount));
Alexander Musmana5f070a2014-10-01 06:03:56 +00005145 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005146 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataevf138fda2018-08-13 19:04:24 +00005147 if (checkOpenMPIterationSpace(
5148 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
5149 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
5150 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt],
5151 Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00005152 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005153 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005154 // OpenMP [2.8.1, simd construct, Restrictions]
5155 // All loops associated with the construct must be perfectly nested; that
5156 // is, there must be no intervening code nor any OpenMP directive between
5157 // any two loops.
5158 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005159 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00005160 for (unsigned Cnt = NestedLoopCount; Cnt < OrderedLoopCount; ++Cnt) {
5161 if (checkOpenMPIterationSpace(
5162 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
5163 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
5164 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt],
5165 Captures))
5166 return 0;
5167 if (Cnt > 0 && IterSpaces[Cnt].CounterVar) {
5168 // Handle initialization of captured loop iterator variables.
5169 auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar);
5170 if (isa<OMPCapturedExprDecl>(DRE->getDecl())) {
5171 Captures[DRE] = DRE;
5172 }
5173 }
5174 // Move on to the next nested for loop, or to the loop body.
5175 // OpenMP [2.8.1, simd construct, Restrictions]
5176 // All loops associated with the construct must be perfectly nested; that
5177 // is, there must be no intervening code nor any OpenMP directive between
5178 // any two loops.
5179 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
5180 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005181
Alexander Musmana5f070a2014-10-01 06:03:56 +00005182 Built.clear(/* size */ NestedLoopCount);
5183
5184 if (SemaRef.CurContext->isDependentContext())
5185 return NestedLoopCount;
5186
5187 // An example of what is generated for the following code:
5188 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00005189 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00005190 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00005191 // for (k = 0; k < NK; ++k)
5192 // for (j = J0; j < NJ; j+=2) {
5193 // <loop body>
5194 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005195 //
5196 // We generate the code below.
5197 // Note: the loop body may be outlined in CodeGen.
5198 // Note: some counters may be C++ classes, operator- is used to find number of
5199 // iterations and operator+= to calculate counter value.
5200 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
5201 // or i64 is currently supported).
5202 //
5203 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
5204 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
5205 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
5206 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
5207 // // similar updates for vars in clauses (e.g. 'linear')
5208 // <loop body (using local i and j)>
5209 // }
5210 // i = NI; // assign final values of counters
5211 // j = NJ;
5212 //
5213
5214 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
5215 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00005216 // Precondition tests if there is at least one iteration (all conditions are
5217 // true).
5218 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexey Bataeve3727102018-04-18 15:57:46 +00005219 Expr *N0 = IterSpaces[0].NumIterations;
5220 ExprResult LastIteration32 =
5221 widenIterationCount(/*Bits=*/32,
5222 SemaRef
5223 .PerformImplicitConversion(
5224 N0->IgnoreImpCasts(), N0->getType(),
5225 Sema::AA_Converting, /*AllowExplicit=*/true)
5226 .get(),
5227 SemaRef);
5228 ExprResult LastIteration64 = widenIterationCount(
5229 /*Bits=*/64,
5230 SemaRef
5231 .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(),
5232 Sema::AA_Converting,
5233 /*AllowExplicit=*/true)
5234 .get(),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005235 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005236
5237 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
5238 return NestedLoopCount;
5239
Alexey Bataeve3727102018-04-18 15:57:46 +00005240 ASTContext &C = SemaRef.Context;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005241 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
5242
5243 Scope *CurScope = DSA.getCurScope();
5244 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00005245 if (PreCond.isUsable()) {
Alexey Bataeva7206b92016-12-20 16:51:02 +00005246 PreCond =
5247 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd,
5248 PreCond.get(), IterSpaces[Cnt].PreCond);
Alexey Bataev62dbb972015-04-22 11:59:37 +00005249 }
Alexey Bataeve3727102018-04-18 15:57:46 +00005250 Expr *N = IterSpaces[Cnt].NumIterations;
Alexey Bataeva7206b92016-12-20 16:51:02 +00005251 SourceLocation Loc = N->getExprLoc();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005252 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
5253 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005254 LastIteration32 = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005255 CurScope, Loc, BO_Mul, LastIteration32.get(),
David Majnemer9d168222016-08-05 17:44:54 +00005256 SemaRef
5257 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5258 Sema::AA_Converting,
5259 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005260 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005261 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005262 LastIteration64 = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005263 CurScope, Loc, BO_Mul, LastIteration64.get(),
David Majnemer9d168222016-08-05 17:44:54 +00005264 SemaRef
5265 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5266 Sema::AA_Converting,
5267 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005268 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005269 }
5270
5271 // Choose either the 32-bit or 64-bit version.
5272 ExprResult LastIteration = LastIteration64;
5273 if (LastIteration32.isUsable() &&
5274 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
5275 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
Alexey Bataeve3727102018-04-18 15:57:46 +00005276 fitsInto(
5277 /*Bits=*/32,
Alexander Musmana5f070a2014-10-01 06:03:56 +00005278 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
5279 LastIteration64.get(), SemaRef)))
5280 LastIteration = LastIteration32;
Alexey Bataev7292c292016-04-25 12:22:29 +00005281 QualType VType = LastIteration.get()->getType();
5282 QualType RealVType = VType;
5283 QualType StrideVType = VType;
5284 if (isOpenMPTaskLoopDirective(DKind)) {
5285 VType =
5286 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
5287 StrideVType =
5288 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
5289 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005290
5291 if (!LastIteration.isUsable())
5292 return 0;
5293
5294 // Save the number of iterations.
5295 ExprResult NumIterations = LastIteration;
5296 {
5297 LastIteration = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005298 CurScope, LastIteration.get()->getExprLoc(), BO_Sub,
5299 LastIteration.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00005300 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5301 if (!LastIteration.isUsable())
5302 return 0;
5303 }
5304
5305 // Calculate the last iteration number beforehand instead of doing this on
5306 // each iteration. Do not do this if the number of iterations may be kfold-ed.
5307 llvm::APSInt Result;
5308 bool IsConstant =
5309 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
5310 ExprResult CalcLastIteration;
5311 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005312 ExprResult SaveRef =
5313 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005314 LastIteration = SaveRef;
5315
5316 // Prepare SaveRef + 1.
5317 NumIterations = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005318 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00005319 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5320 if (!NumIterations.isUsable())
5321 return 0;
5322 }
5323
5324 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
5325
David Majnemer9d168222016-08-05 17:44:54 +00005326 // Build variables passed into runtime, necessary for worksharing directives.
Carlo Bertolliffafe102017-04-20 00:39:39 +00005327 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005328 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5329 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005330 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005331 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
5332 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005333 SemaRef.AddInitializerToDecl(LBDecl,
5334 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5335 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005336
5337 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005338 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
5339 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005340 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
Richard Smith3beb7c62017-01-12 02:27:38 +00005341 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005342
5343 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
5344 // This will be used to implement clause 'lastprivate'.
5345 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005346 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
5347 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005348 SemaRef.AddInitializerToDecl(ILDecl,
5349 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5350 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005351
5352 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev7292c292016-04-25 12:22:29 +00005353 VarDecl *STDecl =
5354 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
5355 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005356 SemaRef.AddInitializerToDecl(STDecl,
5357 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
5358 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005359
5360 // Build expression: UB = min(UB, LastIteration)
David Majnemer9d168222016-08-05 17:44:54 +00005361 // It is necessary for CodeGen of directives with static scheduling.
Alexander Musmanc6388682014-12-15 07:07:06 +00005362 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
5363 UB.get(), LastIteration.get());
5364 ExprResult CondOp = SemaRef.ActOnConditionalOp(
Alexey Bataev86ec3fe2018-07-25 14:40:26 +00005365 LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(),
5366 LastIteration.get(), UB.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00005367 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
5368 CondOp.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005369 EUB = SemaRef.ActOnFinishFullExpr(EUB.get(), /*DiscardedValue*/ false);
Carlo Bertolli9925f152016-06-27 14:55:37 +00005370
5371 // If we have a combined directive that combines 'distribute', 'for' or
5372 // 'simd' we need to be able to access the bounds of the schedule of the
5373 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
5374 // by scheduling 'distribute' have to be passed to the schedule of 'for'.
5375 if (isOpenMPLoopBoundSharingDirective(DKind)) {
Carlo Bertolliffafe102017-04-20 00:39:39 +00005376 // Lower bound variable, initialized with zero.
5377 VarDecl *CombLBDecl =
5378 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb");
5379 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc);
5380 SemaRef.AddInitializerToDecl(
5381 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5382 /*DirectInit*/ false);
5383
5384 // Upper bound variable, initialized with last iteration number.
5385 VarDecl *CombUBDecl =
5386 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub");
5387 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc);
5388 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(),
5389 /*DirectInit*/ false);
5390
5391 ExprResult CombIsUBGreater = SemaRef.BuildBinOp(
5392 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get());
5393 ExprResult CombCondOp =
5394 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(),
5395 LastIteration.get(), CombUB.get());
5396 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(),
5397 CombCondOp.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005398 CombEUB =
5399 SemaRef.ActOnFinishFullExpr(CombEUB.get(), /*DiscardedValue*/ false);
Carlo Bertolliffafe102017-04-20 00:39:39 +00005400
Alexey Bataeve3727102018-04-18 15:57:46 +00005401 const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005402 // We expect to have at least 2 more parameters than the 'parallel'
5403 // directive does - the lower and upper bounds of the previous schedule.
5404 assert(CD->getNumParams() >= 4 &&
5405 "Unexpected number of parameters in loop combined directive");
5406
5407 // Set the proper type for the bounds given what we learned from the
5408 // enclosed loops.
Alexey Bataeve3727102018-04-18 15:57:46 +00005409 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
5410 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
Carlo Bertolli9925f152016-06-27 14:55:37 +00005411
5412 // Previous lower and upper bounds are obtained from the region
5413 // parameters.
5414 PrevLB =
5415 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
5416 PrevUB =
5417 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
5418 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005419 }
5420
5421 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005422 ExprResult IV;
Carlo Bertolliffafe102017-04-20 00:39:39 +00005423 ExprResult Init, CombInit;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005424 {
Alexey Bataev7292c292016-04-25 12:22:29 +00005425 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
5426 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
David Majnemer9d168222016-08-05 17:44:54 +00005427 Expr *RHS =
5428 (isOpenMPWorksharingDirective(DKind) ||
5429 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
5430 ? LB.get()
5431 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005432 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005433 Init = SemaRef.ActOnFinishFullExpr(Init.get(), /*DiscardedValue*/ false);
Carlo Bertolliffafe102017-04-20 00:39:39 +00005434
5435 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5436 Expr *CombRHS =
5437 (isOpenMPWorksharingDirective(DKind) ||
5438 isOpenMPTaskLoopDirective(DKind) ||
5439 isOpenMPDistributeDirective(DKind))
5440 ? CombLB.get()
5441 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
5442 CombInit =
5443 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS);
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005444 CombInit =
5445 SemaRef.ActOnFinishFullExpr(CombInit.get(), /*DiscardedValue*/ false);
Carlo Bertolliffafe102017-04-20 00:39:39 +00005446 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005447 }
5448
Alexander Musmanc6388682014-12-15 07:07:06 +00005449 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005450 SourceLocation CondLoc = AStmt->getBeginLoc();
Alexander Musmanc6388682014-12-15 07:07:06 +00005451 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005452 (isOpenMPWorksharingDirective(DKind) ||
5453 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00005454 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
5455 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
5456 NumIterations.get());
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005457 ExprResult CombDistCond;
5458 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5459 CombDistCond =
Gheorghe-Teodor Bercea9d6341f2018-10-29 19:44:25 +00005460 SemaRef.BuildBinOp(
5461 CurScope, CondLoc, BO_LT, IV.get(), NumIterations.get());
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005462 }
5463
Carlo Bertolliffafe102017-04-20 00:39:39 +00005464 ExprResult CombCond;
5465 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5466 CombCond =
5467 SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), CombUB.get());
5468 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005469 // Loop increment (IV = IV + 1)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005470 SourceLocation IncLoc = AStmt->getBeginLoc();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005471 ExprResult Inc =
5472 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
5473 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
5474 if (!Inc.isUsable())
5475 return 0;
5476 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005477 Inc = SemaRef.ActOnFinishFullExpr(Inc.get(), /*DiscardedValue*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005478 if (!Inc.isUsable())
5479 return 0;
5480
5481 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
5482 // Used for directives with static scheduling.
Carlo Bertolliffafe102017-04-20 00:39:39 +00005483 // In combined construct, add combined version that use CombLB and CombUB
5484 // base variables for the update
5485 ExprResult NextLB, NextUB, CombNextLB, CombNextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005486 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5487 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005488 // LB + ST
5489 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
5490 if (!NextLB.isUsable())
5491 return 0;
5492 // LB = LB + ST
5493 NextLB =
5494 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005495 NextLB =
5496 SemaRef.ActOnFinishFullExpr(NextLB.get(), /*DiscardedValue*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005497 if (!NextLB.isUsable())
5498 return 0;
5499 // UB + ST
5500 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
5501 if (!NextUB.isUsable())
5502 return 0;
5503 // UB = UB + ST
5504 NextUB =
5505 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005506 NextUB =
5507 SemaRef.ActOnFinishFullExpr(NextUB.get(), /*DiscardedValue*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005508 if (!NextUB.isUsable())
5509 return 0;
Carlo Bertolliffafe102017-04-20 00:39:39 +00005510 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5511 CombNextLB =
5512 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get());
5513 if (!NextLB.isUsable())
5514 return 0;
5515 // LB = LB + ST
5516 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(),
5517 CombNextLB.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005518 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get(),
5519 /*DiscardedValue*/ false);
Carlo Bertolliffafe102017-04-20 00:39:39 +00005520 if (!CombNextLB.isUsable())
5521 return 0;
5522 // UB + ST
5523 CombNextUB =
5524 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get());
5525 if (!CombNextUB.isUsable())
5526 return 0;
5527 // UB = UB + ST
5528 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(),
5529 CombNextUB.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005530 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get(),
5531 /*DiscardedValue*/ false);
Carlo Bertolliffafe102017-04-20 00:39:39 +00005532 if (!CombNextUB.isUsable())
5533 return 0;
5534 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005535 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005536
Carlo Bertolliffafe102017-04-20 00:39:39 +00005537 // Create increment expression for distribute loop when combined in a same
Carlo Bertolli8429d812017-02-17 21:29:13 +00005538 // directive with for as IV = IV + ST; ensure upper bound expression based
5539 // on PrevUB instead of NumIterations - used to implement 'for' when found
5540 // in combination with 'distribute', like in 'distribute parallel for'
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005541 SourceLocation DistIncLoc = AStmt->getBeginLoc();
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005542 ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond;
Carlo Bertolli8429d812017-02-17 21:29:13 +00005543 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5544 DistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get());
5545 assert(DistCond.isUsable() && "distribute cond expr was not built");
5546
5547 DistInc =
5548 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get());
5549 assert(DistInc.isUsable() && "distribute inc expr was not built");
5550 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(),
5551 DistInc.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005552 DistInc =
5553 SemaRef.ActOnFinishFullExpr(DistInc.get(), /*DiscardedValue*/ false);
Carlo Bertolli8429d812017-02-17 21:29:13 +00005554 assert(DistInc.isUsable() && "distribute inc expr was not built");
5555
5556 // Build expression: UB = min(UB, prevUB) for #for in composite or combined
5557 // construct
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005558 SourceLocation DistEUBLoc = AStmt->getBeginLoc();
Carlo Bertolli8429d812017-02-17 21:29:13 +00005559 ExprResult IsUBGreater =
5560 SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get());
5561 ExprResult CondOp = SemaRef.ActOnConditionalOp(
5562 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get());
5563 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(),
5564 CondOp.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005565 PrevEUB =
5566 SemaRef.ActOnFinishFullExpr(PrevEUB.get(), /*DiscardedValue*/ false);
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005567
5568 // Build IV <= PrevUB to be used in parallel for is in combination with
5569 // a distribute directive with schedule(static, 1)
5570 ParForInDistCond =
5571 SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), PrevUB.get());
Carlo Bertolli8429d812017-02-17 21:29:13 +00005572 }
5573
Alexander Musmana5f070a2014-10-01 06:03:56 +00005574 // Build updates and final values of the loop counters.
5575 bool HasErrors = false;
5576 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005577 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005578 Built.Updates.resize(NestedLoopCount);
5579 Built.Finals.resize(NestedLoopCount);
5580 {
5581 ExprResult Div;
5582 // Go from inner nested loop to outer.
5583 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5584 LoopIterationSpace &IS = IterSpaces[Cnt];
5585 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
5586 // Build: Iter = (IV / Div) % IS.NumIters
5587 // where Div is product of previous iterations' IS.NumIters.
5588 ExprResult Iter;
5589 if (Div.isUsable()) {
5590 Iter =
5591 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
5592 } else {
5593 Iter = IV;
5594 assert((Cnt == (int)NestedLoopCount - 1) &&
5595 "unusable div expected on first iteration only");
5596 }
5597
5598 if (Cnt != 0 && Iter.isUsable())
5599 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
5600 IS.NumIterations);
5601 if (!Iter.isUsable()) {
5602 HasErrors = true;
5603 break;
5604 }
5605
Alexey Bataev39f915b82015-05-08 10:41:21 +00005606 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005607 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00005608 DeclRefExpr *CounterVar = buildDeclRefExpr(
5609 SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
5610 /*RefersToCapture=*/true);
5611 ExprResult Init = buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005612 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005613 if (!Init.isUsable()) {
5614 HasErrors = true;
5615 break;
5616 }
Alexey Bataeve3727102018-04-18 15:57:46 +00005617 ExprResult Update = buildCounterUpdate(
Alexey Bataev5a3af132016-03-29 08:58:54 +00005618 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
5619 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005620 if (!Update.isUsable()) {
5621 HasErrors = true;
5622 break;
5623 }
5624
5625 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
Alexey Bataeve3727102018-04-18 15:57:46 +00005626 ExprResult Final = buildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00005627 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005628 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005629 if (!Final.isUsable()) {
5630 HasErrors = true;
5631 break;
5632 }
5633
5634 // Build Div for the next iteration: Div <- Div * IS.NumIters
5635 if (Cnt != 0) {
5636 if (Div.isUnset())
5637 Div = IS.NumIterations;
5638 else
5639 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
5640 IS.NumIterations);
5641
5642 // Add parentheses (for debugging purposes only).
5643 if (Div.isUsable())
Alexey Bataev8b427062016-05-25 12:36:08 +00005644 Div = tryBuildCapture(SemaRef, Div.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005645 if (!Div.isUsable()) {
5646 HasErrors = true;
5647 break;
5648 }
5649 }
5650 if (!Update.isUsable() || !Final.isUsable()) {
5651 HasErrors = true;
5652 break;
5653 }
5654 // Save results
5655 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00005656 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005657 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005658 Built.Updates[Cnt] = Update.get();
5659 Built.Finals[Cnt] = Final.get();
5660 }
5661 }
5662
5663 if (HasErrors)
5664 return 0;
5665
5666 // Save results
5667 Built.IterationVarRef = IV.get();
5668 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00005669 Built.NumIterations = NumIterations.get();
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005670 Built.CalcLastIteration = SemaRef
5671 .ActOnFinishFullExpr(CalcLastIteration.get(),
5672 /*DiscardedValue*/ false)
5673 .get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005674 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00005675 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005676 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005677 Built.Init = Init.get();
5678 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005679 Built.LB = LB.get();
5680 Built.UB = UB.get();
5681 Built.IL = IL.get();
5682 Built.ST = ST.get();
5683 Built.EUB = EUB.get();
5684 Built.NLB = NextLB.get();
5685 Built.NUB = NextUB.get();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005686 Built.PrevLB = PrevLB.get();
5687 Built.PrevUB = PrevUB.get();
Carlo Bertolli8429d812017-02-17 21:29:13 +00005688 Built.DistInc = DistInc.get();
5689 Built.PrevEUB = PrevEUB.get();
Carlo Bertolliffafe102017-04-20 00:39:39 +00005690 Built.DistCombinedFields.LB = CombLB.get();
5691 Built.DistCombinedFields.UB = CombUB.get();
5692 Built.DistCombinedFields.EUB = CombEUB.get();
5693 Built.DistCombinedFields.Init = CombInit.get();
5694 Built.DistCombinedFields.Cond = CombCond.get();
5695 Built.DistCombinedFields.NLB = CombNextLB.get();
5696 Built.DistCombinedFields.NUB = CombNextUB.get();
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005697 Built.DistCombinedFields.DistCond = CombDistCond.get();
5698 Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005699
Alexey Bataevabfc0692014-06-25 06:52:00 +00005700 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005701}
5702
Alexey Bataev10e775f2015-07-30 11:36:16 +00005703static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005704 auto CollapseClauses =
5705 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
5706 if (CollapseClauses.begin() != CollapseClauses.end())
5707 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005708 return nullptr;
5709}
5710
Alexey Bataev10e775f2015-07-30 11:36:16 +00005711static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005712 auto OrderedClauses =
5713 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
5714 if (OrderedClauses.begin() != OrderedClauses.end())
5715 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005716 return nullptr;
5717}
5718
Kelvin Lic5609492016-07-15 04:39:07 +00005719static bool checkSimdlenSafelenSpecified(Sema &S,
5720 const ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00005721 const OMPSafelenClause *Safelen = nullptr;
5722 const OMPSimdlenClause *Simdlen = nullptr;
Kelvin Lic5609492016-07-15 04:39:07 +00005723
Alexey Bataeve3727102018-04-18 15:57:46 +00005724 for (const OMPClause *Clause : Clauses) {
Kelvin Lic5609492016-07-15 04:39:07 +00005725 if (Clause->getClauseKind() == OMPC_safelen)
5726 Safelen = cast<OMPSafelenClause>(Clause);
5727 else if (Clause->getClauseKind() == OMPC_simdlen)
5728 Simdlen = cast<OMPSimdlenClause>(Clause);
5729 if (Safelen && Simdlen)
5730 break;
5731 }
5732
5733 if (Simdlen && Safelen) {
Alexey Bataeve3727102018-04-18 15:57:46 +00005734 const Expr *SimdlenLength = Simdlen->getSimdlen();
5735 const Expr *SafelenLength = Safelen->getSafelen();
Kelvin Lic5609492016-07-15 04:39:07 +00005736 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
5737 SimdlenLength->isInstantiationDependent() ||
5738 SimdlenLength->containsUnexpandedParameterPack())
5739 return false;
5740 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
5741 SafelenLength->isInstantiationDependent() ||
5742 SafelenLength->containsUnexpandedParameterPack())
5743 return false;
Fangrui Song407659a2018-11-30 23:41:18 +00005744 Expr::EvalResult SimdlenResult, SafelenResult;
5745 SimdlenLength->EvaluateAsInt(SimdlenResult, S.Context);
5746 SafelenLength->EvaluateAsInt(SafelenResult, S.Context);
5747 llvm::APSInt SimdlenRes = SimdlenResult.Val.getInt();
5748 llvm::APSInt SafelenRes = SafelenResult.Val.getInt();
Kelvin Lic5609492016-07-15 04:39:07 +00005749 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
5750 // If both simdlen and safelen clauses are specified, the value of the
5751 // simdlen parameter must be less than or equal to the value of the safelen
5752 // parameter.
5753 if (SimdlenRes > SafelenRes) {
5754 S.Diag(SimdlenLength->getExprLoc(),
5755 diag::err_omp_wrong_simdlen_safelen_values)
5756 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
5757 return true;
5758 }
Alexey Bataev66b15b52015-08-21 11:14:16 +00005759 }
5760 return false;
5761}
5762
Alexey Bataeve3727102018-04-18 15:57:46 +00005763StmtResult
5764Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
5765 SourceLocation StartLoc, SourceLocation EndLoc,
5766 VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005767 if (!AStmt)
5768 return StmtError();
5769
5770 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005771 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005772 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5773 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00005774 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataev10e775f2015-07-30 11:36:16 +00005775 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5776 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005777 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005778 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005779
Alexander Musmana5f070a2014-10-01 06:03:56 +00005780 assert((CurContext->isDependentContext() || B.builtAll()) &&
5781 "omp simd loop exprs were not built");
5782
Alexander Musman3276a272015-03-21 10:12:56 +00005783 if (!CurContext->isDependentContext()) {
5784 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005785 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005786 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexander Musman3276a272015-03-21 10:12:56 +00005787 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005788 B.NumIterations, *this, CurScope,
5789 DSAStack))
Alexander Musman3276a272015-03-21 10:12:56 +00005790 return StmtError();
5791 }
5792 }
5793
Kelvin Lic5609492016-07-15 04:39:07 +00005794 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005795 return StmtError();
5796
Reid Kleckner87a31802018-03-12 21:43:02 +00005797 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005798 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5799 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005800}
5801
Alexey Bataeve3727102018-04-18 15:57:46 +00005802StmtResult
5803Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
5804 SourceLocation StartLoc, SourceLocation EndLoc,
5805 VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005806 if (!AStmt)
5807 return StmtError();
5808
5809 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005810 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005811 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5812 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00005813 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataev10e775f2015-07-30 11:36:16 +00005814 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5815 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005816 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005817 return StmtError();
5818
Alexander Musmana5f070a2014-10-01 06:03:56 +00005819 assert((CurContext->isDependentContext() || B.builtAll()) &&
5820 "omp for loop exprs were not built");
5821
Alexey Bataev54acd402015-08-04 11:18:19 +00005822 if (!CurContext->isDependentContext()) {
5823 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005824 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005825 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00005826 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005827 B.NumIterations, *this, CurScope,
5828 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005829 return StmtError();
5830 }
5831 }
5832
Reid Kleckner87a31802018-03-12 21:43:02 +00005833 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005834 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005835 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005836}
5837
Alexander Musmanf82886e2014-09-18 05:12:34 +00005838StmtResult Sema::ActOnOpenMPForSimdDirective(
5839 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00005840 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005841 if (!AStmt)
5842 return StmtError();
5843
5844 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005845 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005846 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5847 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00005848 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00005849 checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00005850 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5851 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005852 if (NestedLoopCount == 0)
5853 return StmtError();
5854
Alexander Musmanc6388682014-12-15 07:07:06 +00005855 assert((CurContext->isDependentContext() || B.builtAll()) &&
5856 "omp for simd loop exprs were not built");
5857
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005858 if (!CurContext->isDependentContext()) {
5859 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005860 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005861 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005862 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005863 B.NumIterations, *this, CurScope,
5864 DSAStack))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005865 return StmtError();
5866 }
5867 }
5868
Kelvin Lic5609492016-07-15 04:39:07 +00005869 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005870 return StmtError();
5871
Reid Kleckner87a31802018-03-12 21:43:02 +00005872 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005873 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5874 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005875}
5876
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005877StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5878 Stmt *AStmt,
5879 SourceLocation StartLoc,
5880 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005881 if (!AStmt)
5882 return StmtError();
5883
5884 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005885 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00005886 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005887 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00005888 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005889 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005890 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005891 return StmtError();
5892 // All associated statements must be '#pragma omp section' except for
5893 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005894 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005895 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5896 if (SectionStmt)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005897 Diag(SectionStmt->getBeginLoc(),
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005898 diag::err_omp_sections_substmt_not_section);
5899 return StmtError();
5900 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005901 cast<OMPSectionDirective>(SectionStmt)
5902 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005903 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005904 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005905 Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005906 return StmtError();
5907 }
5908
Reid Kleckner87a31802018-03-12 21:43:02 +00005909 setFunctionHasBranchProtectedScope();
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005910
Alexey Bataev25e5b442015-09-15 12:52:43 +00005911 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5912 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005913}
5914
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005915StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5916 SourceLocation StartLoc,
5917 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005918 if (!AStmt)
5919 return StmtError();
5920
5921 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005922
Reid Kleckner87a31802018-03-12 21:43:02 +00005923 setFunctionHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00005924 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005925
Alexey Bataev25e5b442015-09-15 12:52:43 +00005926 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5927 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005928}
5929
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005930StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5931 Stmt *AStmt,
5932 SourceLocation StartLoc,
5933 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005934 if (!AStmt)
5935 return StmtError();
5936
5937 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00005938
Reid Kleckner87a31802018-03-12 21:43:02 +00005939 setFunctionHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00005940
Alexey Bataev3255bf32015-01-19 05:20:46 +00005941 // OpenMP [2.7.3, single Construct, Restrictions]
5942 // The copyprivate clause must not be used with the nowait clause.
Alexey Bataeve3727102018-04-18 15:57:46 +00005943 const OMPClause *Nowait = nullptr;
5944 const OMPClause *Copyprivate = nullptr;
5945 for (const OMPClause *Clause : Clauses) {
Alexey Bataev3255bf32015-01-19 05:20:46 +00005946 if (Clause->getClauseKind() == OMPC_nowait)
5947 Nowait = Clause;
5948 else if (Clause->getClauseKind() == OMPC_copyprivate)
5949 Copyprivate = Clause;
5950 if (Copyprivate && Nowait) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005951 Diag(Copyprivate->getBeginLoc(),
Alexey Bataev3255bf32015-01-19 05:20:46 +00005952 diag::err_omp_single_copyprivate_with_nowait);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005953 Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here);
Alexey Bataev3255bf32015-01-19 05:20:46 +00005954 return StmtError();
5955 }
5956 }
5957
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005958 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5959}
5960
Alexander Musman80c22892014-07-17 08:54:58 +00005961StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5962 SourceLocation StartLoc,
5963 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005964 if (!AStmt)
5965 return StmtError();
5966
5967 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00005968
Reid Kleckner87a31802018-03-12 21:43:02 +00005969 setFunctionHasBranchProtectedScope();
Alexander Musman80c22892014-07-17 08:54:58 +00005970
5971 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5972}
5973
Alexey Bataev28c75412015-12-15 08:19:24 +00005974StmtResult Sema::ActOnOpenMPCriticalDirective(
5975 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5976 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005977 if (!AStmt)
5978 return StmtError();
5979
5980 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005981
Alexey Bataev28c75412015-12-15 08:19:24 +00005982 bool ErrorFound = false;
5983 llvm::APSInt Hint;
5984 SourceLocation HintLoc;
5985 bool DependentHint = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00005986 for (const OMPClause *C : Clauses) {
Alexey Bataev28c75412015-12-15 08:19:24 +00005987 if (C->getClauseKind() == OMPC_hint) {
5988 if (!DirName.getName()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005989 Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name);
Alexey Bataev28c75412015-12-15 08:19:24 +00005990 ErrorFound = true;
5991 }
5992 Expr *E = cast<OMPHintClause>(C)->getHint();
5993 if (E->isTypeDependent() || E->isValueDependent() ||
Alexey Bataeve3727102018-04-18 15:57:46 +00005994 E->isInstantiationDependent()) {
Alexey Bataev28c75412015-12-15 08:19:24 +00005995 DependentHint = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00005996 } else {
Alexey Bataev28c75412015-12-15 08:19:24 +00005997 Hint = E->EvaluateKnownConstInt(Context);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005998 HintLoc = C->getBeginLoc();
Alexey Bataev28c75412015-12-15 08:19:24 +00005999 }
6000 }
6001 }
6002 if (ErrorFound)
6003 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00006004 const auto Pair = DSAStack->getCriticalWithHint(DirName);
Alexey Bataev28c75412015-12-15 08:19:24 +00006005 if (Pair.first && DirName.getName() && !DependentHint) {
6006 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
6007 Diag(StartLoc, diag::err_omp_critical_with_hint);
Alexey Bataeve3727102018-04-18 15:57:46 +00006008 if (HintLoc.isValid())
Alexey Bataev28c75412015-12-15 08:19:24 +00006009 Diag(HintLoc, diag::note_omp_critical_hint_here)
6010 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +00006011 else
Alexey Bataev28c75412015-12-15 08:19:24 +00006012 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
Alexey Bataeve3727102018-04-18 15:57:46 +00006013 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006014 Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here)
Alexey Bataev28c75412015-12-15 08:19:24 +00006015 << 1
6016 << C->getHint()->EvaluateKnownConstInt(Context).toString(
6017 /*Radix=*/10, /*Signed=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +00006018 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006019 Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1;
Alexey Bataeve3727102018-04-18 15:57:46 +00006020 }
Alexey Bataev28c75412015-12-15 08:19:24 +00006021 }
6022 }
6023
Reid Kleckner87a31802018-03-12 21:43:02 +00006024 setFunctionHasBranchProtectedScope();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00006025
Alexey Bataev28c75412015-12-15 08:19:24 +00006026 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
6027 Clauses, AStmt);
6028 if (!Pair.first && DirName.getName() && !DependentHint)
6029 DSAStack->addCriticalWithHint(Dir, Hint);
6030 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00006031}
6032
Alexey Bataev4acb8592014-07-07 13:01:15 +00006033StmtResult Sema::ActOnOpenMPParallelForDirective(
6034 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00006035 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006036 if (!AStmt)
6037 return StmtError();
6038
Alexey Bataeve3727102018-04-18 15:57:46 +00006039 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev4acb8592014-07-07 13:01:15 +00006040 // 1.2.2 OpenMP Language Terminology
6041 // Structured block - An executable statement with a single entry at the
6042 // top and a single exit at the bottom.
6043 // The point of exit cannot be a branch out of the structured block.
6044 // longjmp() and throw() must not violate the entry/exit criteria.
6045 CS->getCapturedDecl()->setNothrow();
6046
Alexander Musmanc6388682014-12-15 07:07:06 +00006047 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006048 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6049 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00006050 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00006051 checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00006052 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6053 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00006054 if (NestedLoopCount == 0)
6055 return StmtError();
6056
Alexander Musmana5f070a2014-10-01 06:03:56 +00006057 assert((CurContext->isDependentContext() || B.builtAll()) &&
6058 "omp parallel for loop exprs were not built");
6059
Alexey Bataev54acd402015-08-04 11:18:19 +00006060 if (!CurContext->isDependentContext()) {
6061 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00006062 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00006063 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00006064 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006065 B.NumIterations, *this, CurScope,
6066 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00006067 return StmtError();
6068 }
6069 }
6070
Reid Kleckner87a31802018-03-12 21:43:02 +00006071 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00006072 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00006073 NestedLoopCount, Clauses, AStmt, B,
6074 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00006075}
6076
Alexander Musmane4e893b2014-09-23 09:33:00 +00006077StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
6078 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00006079 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006080 if (!AStmt)
6081 return StmtError();
6082
Alexey Bataeve3727102018-04-18 15:57:46 +00006083 auto *CS = cast<CapturedStmt>(AStmt);
Alexander Musmane4e893b2014-09-23 09:33:00 +00006084 // 1.2.2 OpenMP Language Terminology
6085 // Structured block - An executable statement with a single entry at the
6086 // top and a single exit at the bottom.
6087 // The point of exit cannot be a branch out of the structured block.
6088 // longjmp() and throw() must not violate the entry/exit criteria.
6089 CS->getCapturedDecl()->setNothrow();
6090
Alexander Musmanc6388682014-12-15 07:07:06 +00006091 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006092 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6093 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00006094 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00006095 checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00006096 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6097 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00006098 if (NestedLoopCount == 0)
6099 return StmtError();
6100
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00006101 if (!CurContext->isDependentContext()) {
6102 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00006103 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00006104 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00006105 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006106 B.NumIterations, *this, CurScope,
6107 DSAStack))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00006108 return StmtError();
6109 }
6110 }
6111
Kelvin Lic5609492016-07-15 04:39:07 +00006112 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00006113 return StmtError();
6114
Reid Kleckner87a31802018-03-12 21:43:02 +00006115 setFunctionHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00006116 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00006117 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00006118}
6119
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006120StmtResult
6121Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
6122 Stmt *AStmt, SourceLocation StartLoc,
6123 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006124 if (!AStmt)
6125 return StmtError();
6126
6127 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006128 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00006129 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006130 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00006131 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006132 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00006133 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006134 return StmtError();
6135 // All associated statements must be '#pragma omp section' except for
6136 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00006137 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006138 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
6139 if (SectionStmt)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006140 Diag(SectionStmt->getBeginLoc(),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006141 diag::err_omp_parallel_sections_substmt_not_section);
6142 return StmtError();
6143 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006144 cast<OMPSectionDirective>(SectionStmt)
6145 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006146 }
6147 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006148 Diag(AStmt->getBeginLoc(),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006149 diag::err_omp_parallel_sections_not_compound_stmt);
6150 return StmtError();
6151 }
6152
Reid Kleckner87a31802018-03-12 21:43:02 +00006153 setFunctionHasBranchProtectedScope();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006154
Alexey Bataev25e5b442015-09-15 12:52:43 +00006155 return OMPParallelSectionsDirective::Create(
6156 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006157}
6158
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006159StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
6160 Stmt *AStmt, SourceLocation StartLoc,
6161 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006162 if (!AStmt)
6163 return StmtError();
6164
David Majnemer9d168222016-08-05 17:44:54 +00006165 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006166 // 1.2.2 OpenMP Language Terminology
6167 // Structured block - An executable statement with a single entry at the
6168 // top and a single exit at the bottom.
6169 // The point of exit cannot be a branch out of the structured block.
6170 // longjmp() and throw() must not violate the entry/exit criteria.
6171 CS->getCapturedDecl()->setNothrow();
6172
Reid Kleckner87a31802018-03-12 21:43:02 +00006173 setFunctionHasBranchProtectedScope();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006174
Alexey Bataev25e5b442015-09-15 12:52:43 +00006175 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
6176 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006177}
6178
Alexey Bataev68446b72014-07-18 07:47:19 +00006179StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
6180 SourceLocation EndLoc) {
6181 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
6182}
6183
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00006184StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
6185 SourceLocation EndLoc) {
6186 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
6187}
6188
Alexey Bataev2df347a2014-07-18 10:17:07 +00006189StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
6190 SourceLocation EndLoc) {
6191 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
6192}
6193
Alexey Bataev169d96a2017-07-18 20:17:46 +00006194StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses,
6195 Stmt *AStmt,
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006196 SourceLocation StartLoc,
6197 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006198 if (!AStmt)
6199 return StmtError();
6200
6201 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006202
Reid Kleckner87a31802018-03-12 21:43:02 +00006203 setFunctionHasBranchProtectedScope();
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006204
Alexey Bataev169d96a2017-07-18 20:17:46 +00006205 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses,
Alexey Bataev3b1b8952017-07-25 15:53:26 +00006206 AStmt,
6207 DSAStack->getTaskgroupReductionRef());
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006208}
6209
Alexey Bataev6125da92014-07-21 11:26:11 +00006210StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
6211 SourceLocation StartLoc,
6212 SourceLocation EndLoc) {
6213 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
6214 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
6215}
6216
Alexey Bataev346265e2015-09-25 10:37:12 +00006217StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
6218 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00006219 SourceLocation StartLoc,
6220 SourceLocation EndLoc) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006221 const OMPClause *DependFound = nullptr;
6222 const OMPClause *DependSourceClause = nullptr;
6223 const OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00006224 bool ErrorFound = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00006225 const OMPThreadsClause *TC = nullptr;
6226 const OMPSIMDClause *SC = nullptr;
6227 for (const OMPClause *C : Clauses) {
Alexey Bataeveb482352015-12-18 05:05:56 +00006228 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
6229 DependFound = C;
6230 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
6231 if (DependSourceClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006232 Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
Alexey Bataeveb482352015-12-18 05:05:56 +00006233 << getOpenMPDirectiveName(OMPD_ordered)
6234 << getOpenMPClauseName(OMPC_depend) << 2;
6235 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006236 } else {
Alexey Bataeveb482352015-12-18 05:05:56 +00006237 DependSourceClause = C;
Alexey Bataeve3727102018-04-18 15:57:46 +00006238 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006239 if (DependSinkClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006240 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006241 << 0;
6242 ErrorFound = true;
6243 }
6244 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
6245 if (DependSourceClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006246 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006247 << 1;
6248 ErrorFound = true;
6249 }
6250 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00006251 }
Alexey Bataeve3727102018-04-18 15:57:46 +00006252 } else if (C->getClauseKind() == OMPC_threads) {
Alexey Bataev346265e2015-09-25 10:37:12 +00006253 TC = cast<OMPThreadsClause>(C);
Alexey Bataeve3727102018-04-18 15:57:46 +00006254 } else if (C->getClauseKind() == OMPC_simd) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006255 SC = cast<OMPSIMDClause>(C);
Alexey Bataeve3727102018-04-18 15:57:46 +00006256 }
Alexey Bataev346265e2015-09-25 10:37:12 +00006257 }
Alexey Bataeveb482352015-12-18 05:05:56 +00006258 if (!ErrorFound && !SC &&
6259 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006260 // OpenMP [2.8.1,simd Construct, Restrictions]
6261 // An ordered construct with the simd clause is the only OpenMP construct
6262 // that can appear in the simd region.
6263 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00006264 ErrorFound = true;
6265 } else if (DependFound && (TC || SC)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006266 Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd)
Alexey Bataeveb482352015-12-18 05:05:56 +00006267 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
6268 ErrorFound = true;
Alexey Bataevf138fda2018-08-13 19:04:24 +00006269 } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006270 Diag(DependFound->getBeginLoc(),
Alexey Bataeveb482352015-12-18 05:05:56 +00006271 diag::err_omp_ordered_directive_without_param);
6272 ErrorFound = true;
6273 } else if (TC || Clauses.empty()) {
Alexey Bataevf138fda2018-08-13 19:04:24 +00006274 if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006275 SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc;
Alexey Bataeveb482352015-12-18 05:05:56 +00006276 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
6277 << (TC != nullptr);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006278 Diag(Param->getBeginLoc(), diag::note_omp_ordered_param);
Alexey Bataeveb482352015-12-18 05:05:56 +00006279 ErrorFound = true;
6280 }
6281 }
6282 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006283 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00006284
6285 if (AStmt) {
6286 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6287
Reid Kleckner87a31802018-03-12 21:43:02 +00006288 setFunctionHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006289 }
Alexey Bataev346265e2015-09-25 10:37:12 +00006290
6291 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00006292}
6293
Alexey Bataev1d160b12015-03-13 12:27:31 +00006294namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006295/// Helper class for checking expression in 'omp atomic [update]'
Alexey Bataev1d160b12015-03-13 12:27:31 +00006296/// construct.
6297class OpenMPAtomicUpdateChecker {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006298 /// Error results for atomic update expressions.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006299 enum ExprAnalysisErrorCode {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006300 /// A statement is not an expression statement.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006301 NotAnExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006302 /// Expression is not builtin binary or unary operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006303 NotABinaryOrUnaryExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006304 /// Unary operation is not post-/pre- increment/decrement operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006305 NotAnUnaryIncDecExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006306 /// An expression is not of scalar type.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006307 NotAScalarType,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006308 /// A binary operation is not an assignment operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006309 NotAnAssignmentOp,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006310 /// RHS part of the binary operation is not a binary expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006311 NotABinaryExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006312 /// RHS part is not additive/multiplicative/shift/biwise binary
Alexey Bataev1d160b12015-03-13 12:27:31 +00006313 /// expression.
6314 NotABinaryOperator,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006315 /// RHS binary operation does not have reference to the updated LHS
Alexey Bataev1d160b12015-03-13 12:27:31 +00006316 /// part.
6317 NotAnUpdateExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006318 /// No errors is found.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006319 NoError
6320 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006321 /// Reference to Sema.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006322 Sema &SemaRef;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006323 /// A location for note diagnostics (when error is found).
Alexey Bataev1d160b12015-03-13 12:27:31 +00006324 SourceLocation NoteLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006325 /// 'x' lvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006326 Expr *X;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006327 /// 'expr' rvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006328 Expr *E;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006329 /// Helper expression of the form
Alexey Bataevb4505a72015-03-30 05:20:59 +00006330 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6331 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6332 Expr *UpdateExpr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006333 /// Is 'x' a LHS in a RHS part of full update expression. It is
Alexey Bataevb4505a72015-03-30 05:20:59 +00006334 /// important for non-associative operations.
6335 bool IsXLHSInRHSPart;
6336 BinaryOperatorKind Op;
6337 SourceLocation OpLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006338 /// true if the source expression is a postfix unary operation, false
Alexey Bataevb78ca832015-04-01 03:33:17 +00006339 /// if it is a prefix unary operation.
6340 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006341
6342public:
6343 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00006344 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00006345 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006346 /// Check specified statement that it is suitable for 'atomic update'
Alexey Bataev1d160b12015-03-13 12:27:31 +00006347 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00006348 /// expression. If DiagId and NoteId == 0, then only check is performed
6349 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006350 /// \param DiagId Diagnostic which should be emitted if error is found.
6351 /// \param NoteId Diagnostic note for the main error message.
6352 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00006353 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006354 /// Return the 'x' lvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006355 Expr *getX() const { return X; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006356 /// Return the 'expr' rvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006357 Expr *getExpr() const { return E; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006358 /// Return the update expression used in calculation of the updated
Alexey Bataevb4505a72015-03-30 05:20:59 +00006359 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6360 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6361 Expr *getUpdateExpr() const { return UpdateExpr; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006362 /// Return true if 'x' is LHS in RHS part of full update expression,
Alexey Bataevb4505a72015-03-30 05:20:59 +00006363 /// false otherwise.
6364 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
6365
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006366 /// true if the source expression is a postfix unary operation, false
Alexey Bataevb78ca832015-04-01 03:33:17 +00006367 /// if it is a prefix unary operation.
6368 bool isPostfixUpdate() const { return IsPostfixUpdate; }
6369
Alexey Bataev1d160b12015-03-13 12:27:31 +00006370private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00006371 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
6372 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00006373};
6374} // namespace
6375
6376bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
6377 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
6378 ExprAnalysisErrorCode ErrorFound = NoError;
6379 SourceLocation ErrorLoc, NoteLoc;
6380 SourceRange ErrorRange, NoteRange;
6381 // Allowed constructs are:
6382 // x = x binop expr;
6383 // x = expr binop x;
6384 if (AtomicBinOp->getOpcode() == BO_Assign) {
6385 X = AtomicBinOp->getLHS();
Alexey Bataeve3727102018-04-18 15:57:46 +00006386 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006387 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
6388 if (AtomicInnerBinOp->isMultiplicativeOp() ||
6389 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
6390 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006391 Op = AtomicInnerBinOp->getOpcode();
6392 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataeve3727102018-04-18 15:57:46 +00006393 Expr *LHS = AtomicInnerBinOp->getLHS();
6394 Expr *RHS = AtomicInnerBinOp->getRHS();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006395 llvm::FoldingSetNodeID XId, LHSId, RHSId;
6396 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
6397 /*Canonical=*/true);
6398 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
6399 /*Canonical=*/true);
6400 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
6401 /*Canonical=*/true);
6402 if (XId == LHSId) {
6403 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006404 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006405 } else if (XId == RHSId) {
6406 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006407 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006408 } else {
6409 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6410 ErrorRange = AtomicInnerBinOp->getSourceRange();
6411 NoteLoc = X->getExprLoc();
6412 NoteRange = X->getSourceRange();
6413 ErrorFound = NotAnUpdateExpression;
6414 }
6415 } else {
6416 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6417 ErrorRange = AtomicInnerBinOp->getSourceRange();
6418 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
6419 NoteRange = SourceRange(NoteLoc, NoteLoc);
6420 ErrorFound = NotABinaryOperator;
6421 }
6422 } else {
6423 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
6424 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
6425 ErrorFound = NotABinaryExpression;
6426 }
6427 } else {
6428 ErrorLoc = AtomicBinOp->getExprLoc();
6429 ErrorRange = AtomicBinOp->getSourceRange();
6430 NoteLoc = AtomicBinOp->getOperatorLoc();
6431 NoteRange = SourceRange(NoteLoc, NoteLoc);
6432 ErrorFound = NotAnAssignmentOp;
6433 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006434 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006435 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6436 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6437 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006438 }
6439 if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006440 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006441 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006442}
6443
6444bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
6445 unsigned NoteId) {
6446 ExprAnalysisErrorCode ErrorFound = NoError;
6447 SourceLocation ErrorLoc, NoteLoc;
6448 SourceRange ErrorRange, NoteRange;
6449 // Allowed constructs are:
6450 // x++;
6451 // x--;
6452 // ++x;
6453 // --x;
6454 // x binop= expr;
6455 // x = x binop expr;
6456 // x = expr binop x;
6457 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
6458 AtomicBody = AtomicBody->IgnoreParenImpCasts();
6459 if (AtomicBody->getType()->isScalarType() ||
6460 AtomicBody->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006461 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006462 AtomicBody->IgnoreParenImpCasts())) {
6463 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00006464 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006465 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00006466 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006467 E = AtomicCompAssignOp->getRHS();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006468 X = AtomicCompAssignOp->getLHS()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006469 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006470 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
6471 AtomicBody->IgnoreParenImpCasts())) {
6472 // Check for Binary Operation
David Majnemer9d168222016-08-05 17:44:54 +00006473 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
Alexey Bataevb4505a72015-03-30 05:20:59 +00006474 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006475 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
David Majnemer9d168222016-08-05 17:44:54 +00006476 AtomicBody->IgnoreParenImpCasts())) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006477 // Check for Unary Operation
6478 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006479 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006480 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
6481 OpLoc = AtomicUnaryOp->getOperatorLoc();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006482 X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006483 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
6484 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006485 } else {
6486 ErrorFound = NotAnUnaryIncDecExpression;
6487 ErrorLoc = AtomicUnaryOp->getExprLoc();
6488 ErrorRange = AtomicUnaryOp->getSourceRange();
6489 NoteLoc = AtomicUnaryOp->getOperatorLoc();
6490 NoteRange = SourceRange(NoteLoc, NoteLoc);
6491 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006492 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006493 ErrorFound = NotABinaryOrUnaryExpression;
6494 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
6495 NoteRange = ErrorRange = AtomicBody->getSourceRange();
6496 }
6497 } else {
6498 ErrorFound = NotAScalarType;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006499 NoteLoc = ErrorLoc = AtomicBody->getBeginLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006500 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6501 }
6502 } else {
6503 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006504 NoteLoc = ErrorLoc = S->getBeginLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006505 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6506 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006507 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006508 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6509 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6510 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006511 }
6512 if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006513 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006514 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006515 // Build an update expression of form 'OpaqueValueExpr(x) binop
6516 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
6517 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
6518 auto *OVEX = new (SemaRef.getASTContext())
6519 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
6520 auto *OVEExpr = new (SemaRef.getASTContext())
6521 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
Alexey Bataeve3727102018-04-18 15:57:46 +00006522 ExprResult Update =
Alexey Bataevb4505a72015-03-30 05:20:59 +00006523 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
6524 IsXLHSInRHSPart ? OVEExpr : OVEX);
6525 if (Update.isInvalid())
6526 return true;
6527 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
6528 Sema::AA_Casting);
6529 if (Update.isInvalid())
6530 return true;
6531 UpdateExpr = Update.get();
6532 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00006533 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006534}
6535
Alexey Bataev0162e452014-07-22 10:10:35 +00006536StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
6537 Stmt *AStmt,
6538 SourceLocation StartLoc,
6539 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006540 if (!AStmt)
6541 return StmtError();
6542
David Majnemer9d168222016-08-05 17:44:54 +00006543 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00006544 // 1.2.2 OpenMP Language Terminology
6545 // Structured block - An executable statement with a single entry at the
6546 // top and a single exit at the bottom.
6547 // The point of exit cannot be a branch out of the structured block.
6548 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00006549 OpenMPClauseKind AtomicKind = OMPC_unknown;
6550 SourceLocation AtomicKindLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00006551 for (const OMPClause *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00006552 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00006553 C->getClauseKind() == OMPC_update ||
6554 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00006555 if (AtomicKind != OMPC_unknown) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006556 Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00006557 << SourceRange(C->getBeginLoc(), C->getEndLoc());
Alexey Bataevdea47612014-07-23 07:46:59 +00006558 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
6559 << getOpenMPClauseName(AtomicKind);
6560 } else {
6561 AtomicKind = C->getClauseKind();
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006562 AtomicKindLoc = C->getBeginLoc();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006563 }
6564 }
6565 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006566
Alexey Bataeve3727102018-04-18 15:57:46 +00006567 Stmt *Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00006568 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
6569 Body = EWC->getSubExpr();
6570
Alexey Bataev62cec442014-11-18 10:14:22 +00006571 Expr *X = nullptr;
6572 Expr *V = nullptr;
6573 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006574 Expr *UE = nullptr;
6575 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006576 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00006577 // OpenMP [2.12.6, atomic Construct]
6578 // In the next expressions:
6579 // * x and v (as applicable) are both l-value expressions with scalar type.
6580 // * During the execution of an atomic region, multiple syntactic
6581 // occurrences of x must designate the same storage location.
6582 // * Neither of v and expr (as applicable) may access the storage location
6583 // designated by x.
6584 // * Neither of x and expr (as applicable) may access the storage location
6585 // designated by v.
6586 // * expr is an expression with scalar type.
6587 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
6588 // * binop, binop=, ++, and -- are not overloaded operators.
6589 // * The expression x binop expr must be numerically equivalent to x binop
6590 // (expr). This requirement is satisfied if the operators in expr have
6591 // precedence greater than binop, or by using parentheses around expr or
6592 // subexpressions of expr.
6593 // * The expression expr binop x must be numerically equivalent to (expr)
6594 // binop x. This requirement is satisfied if the operators in expr have
6595 // precedence equal to or greater than binop, or by using parentheses around
6596 // expr or subexpressions of expr.
6597 // * For forms that allow multiple occurrences of x, the number of times
6598 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00006599 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006600 enum {
6601 NotAnExpression,
6602 NotAnAssignmentOp,
6603 NotAScalarType,
6604 NotAnLValue,
6605 NoError
6606 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00006607 SourceLocation ErrorLoc, NoteLoc;
6608 SourceRange ErrorRange, NoteRange;
6609 // If clause is read:
6610 // v = x;
Alexey Bataeve3727102018-04-18 15:57:46 +00006611 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
6612 const auto *AtomicBinOp =
Alexey Bataev62cec442014-11-18 10:14:22 +00006613 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6614 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6615 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6616 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
6617 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6618 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
6619 if (!X->isLValue() || !V->isLValue()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006620 const Expr *NotLValueExpr = X->isLValue() ? V : X;
Alexey Bataev62cec442014-11-18 10:14:22 +00006621 ErrorFound = NotAnLValue;
6622 ErrorLoc = AtomicBinOp->getExprLoc();
6623 ErrorRange = AtomicBinOp->getSourceRange();
6624 NoteLoc = NotLValueExpr->getExprLoc();
6625 NoteRange = NotLValueExpr->getSourceRange();
6626 }
6627 } else if (!X->isInstantiationDependent() ||
6628 !V->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006629 const Expr *NotScalarExpr =
Alexey Bataev62cec442014-11-18 10:14:22 +00006630 (X->isInstantiationDependent() || X->getType()->isScalarType())
6631 ? V
6632 : X;
6633 ErrorFound = NotAScalarType;
6634 ErrorLoc = AtomicBinOp->getExprLoc();
6635 ErrorRange = AtomicBinOp->getSourceRange();
6636 NoteLoc = NotScalarExpr->getExprLoc();
6637 NoteRange = NotScalarExpr->getSourceRange();
6638 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006639 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00006640 ErrorFound = NotAnAssignmentOp;
6641 ErrorLoc = AtomicBody->getExprLoc();
6642 ErrorRange = AtomicBody->getSourceRange();
6643 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6644 : AtomicBody->getExprLoc();
6645 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6646 : AtomicBody->getSourceRange();
6647 }
6648 } else {
6649 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006650 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataev62cec442014-11-18 10:14:22 +00006651 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006652 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006653 if (ErrorFound != NoError) {
6654 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
6655 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006656 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6657 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00006658 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00006659 }
6660 if (CurContext->isDependentContext())
Alexey Bataev62cec442014-11-18 10:14:22 +00006661 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00006662 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006663 enum {
6664 NotAnExpression,
6665 NotAnAssignmentOp,
6666 NotAScalarType,
6667 NotAnLValue,
6668 NoError
6669 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006670 SourceLocation ErrorLoc, NoteLoc;
6671 SourceRange ErrorRange, NoteRange;
6672 // If clause is write:
6673 // x = expr;
Alexey Bataeve3727102018-04-18 15:57:46 +00006674 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
6675 const auto *AtomicBinOp =
Alexey Bataevf33eba62014-11-28 07:21:40 +00006676 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6677 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00006678 X = AtomicBinOp->getLHS();
6679 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006680 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6681 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
6682 if (!X->isLValue()) {
6683 ErrorFound = NotAnLValue;
6684 ErrorLoc = AtomicBinOp->getExprLoc();
6685 ErrorRange = AtomicBinOp->getSourceRange();
6686 NoteLoc = X->getExprLoc();
6687 NoteRange = X->getSourceRange();
6688 }
6689 } else if (!X->isInstantiationDependent() ||
6690 !E->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006691 const Expr *NotScalarExpr =
Alexey Bataevf33eba62014-11-28 07:21:40 +00006692 (X->isInstantiationDependent() || X->getType()->isScalarType())
6693 ? E
6694 : X;
6695 ErrorFound = NotAScalarType;
6696 ErrorLoc = AtomicBinOp->getExprLoc();
6697 ErrorRange = AtomicBinOp->getSourceRange();
6698 NoteLoc = NotScalarExpr->getExprLoc();
6699 NoteRange = NotScalarExpr->getSourceRange();
6700 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006701 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00006702 ErrorFound = NotAnAssignmentOp;
6703 ErrorLoc = AtomicBody->getExprLoc();
6704 ErrorRange = AtomicBody->getSourceRange();
6705 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6706 : AtomicBody->getExprLoc();
6707 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6708 : AtomicBody->getSourceRange();
6709 }
6710 } else {
6711 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006712 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006713 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006714 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00006715 if (ErrorFound != NoError) {
6716 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
6717 << ErrorRange;
6718 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6719 << NoteRange;
6720 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00006721 }
6722 if (CurContext->isDependentContext())
Alexey Bataevf33eba62014-11-28 07:21:40 +00006723 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006724 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006725 // If clause is update:
6726 // x++;
6727 // x--;
6728 // ++x;
6729 // --x;
6730 // x binop= expr;
6731 // x = x binop expr;
6732 // x = expr binop x;
6733 OpenMPAtomicUpdateChecker Checker(*this);
6734 if (Checker.checkStatement(
6735 Body, (AtomicKind == OMPC_update)
6736 ? diag::err_omp_atomic_update_not_expression_statement
6737 : diag::err_omp_atomic_not_expression_statement,
6738 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00006739 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006740 if (!CurContext->isDependentContext()) {
6741 E = Checker.getExpr();
6742 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006743 UE = Checker.getUpdateExpr();
6744 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00006745 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006746 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006747 enum {
6748 NotAnAssignmentOp,
6749 NotACompoundStatement,
6750 NotTwoSubstatements,
6751 NotASpecificExpression,
6752 NoError
6753 } ErrorFound = NoError;
6754 SourceLocation ErrorLoc, NoteLoc;
6755 SourceRange ErrorRange, NoteRange;
Alexey Bataeve3727102018-04-18 15:57:46 +00006756 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006757 // If clause is a capture:
6758 // v = x++;
6759 // v = x--;
6760 // v = ++x;
6761 // v = --x;
6762 // v = x binop= expr;
6763 // v = x = x binop expr;
6764 // v = x = expr binop x;
Alexey Bataeve3727102018-04-18 15:57:46 +00006765 const auto *AtomicBinOp =
Alexey Bataevb78ca832015-04-01 03:33:17 +00006766 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6767 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6768 V = AtomicBinOp->getLHS();
6769 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6770 OpenMPAtomicUpdateChecker Checker(*this);
6771 if (Checker.checkStatement(
6772 Body, diag::err_omp_atomic_capture_not_expression_statement,
6773 diag::note_omp_atomic_update))
6774 return StmtError();
6775 E = Checker.getExpr();
6776 X = Checker.getX();
6777 UE = Checker.getUpdateExpr();
6778 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6779 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00006780 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006781 ErrorLoc = AtomicBody->getExprLoc();
6782 ErrorRange = AtomicBody->getSourceRange();
6783 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6784 : AtomicBody->getExprLoc();
6785 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6786 : AtomicBody->getSourceRange();
6787 ErrorFound = NotAnAssignmentOp;
6788 }
6789 if (ErrorFound != NoError) {
6790 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6791 << ErrorRange;
6792 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6793 return StmtError();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006794 }
Alexey Bataeve3727102018-04-18 15:57:46 +00006795 if (CurContext->isDependentContext())
6796 UE = V = E = X = nullptr;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006797 } else {
6798 // If clause is a capture:
6799 // { v = x; x = expr; }
6800 // { v = x; x++; }
6801 // { v = x; x--; }
6802 // { v = x; ++x; }
6803 // { v = x; --x; }
6804 // { v = x; x binop= expr; }
6805 // { v = x; x = x binop expr; }
6806 // { v = x; x = expr binop x; }
6807 // { x++; v = x; }
6808 // { x--; v = x; }
6809 // { ++x; v = x; }
6810 // { --x; v = x; }
6811 // { x binop= expr; v = x; }
6812 // { x = x binop expr; v = x; }
6813 // { x = expr binop x; v = x; }
6814 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6815 // Check that this is { expr1; expr2; }
6816 if (CS->size() == 2) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006817 Stmt *First = CS->body_front();
6818 Stmt *Second = CS->body_back();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006819 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6820 First = EWC->getSubExpr()->IgnoreParenImpCasts();
6821 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6822 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6823 // Need to find what subexpression is 'v' and what is 'x'.
6824 OpenMPAtomicUpdateChecker Checker(*this);
6825 bool IsUpdateExprFound = !Checker.checkStatement(Second);
6826 BinaryOperator *BinOp = nullptr;
6827 if (IsUpdateExprFound) {
6828 BinOp = dyn_cast<BinaryOperator>(First);
6829 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6830 }
6831 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6832 // { v = x; x++; }
6833 // { v = x; x--; }
6834 // { v = x; ++x; }
6835 // { v = x; --x; }
6836 // { v = x; x binop= expr; }
6837 // { v = x; x = x binop expr; }
6838 // { v = x; x = expr binop x; }
6839 // Check that the first expression has form v = x.
Alexey Bataeve3727102018-04-18 15:57:46 +00006840 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006841 llvm::FoldingSetNodeID XId, PossibleXId;
6842 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6843 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6844 IsUpdateExprFound = XId == PossibleXId;
6845 if (IsUpdateExprFound) {
6846 V = BinOp->getLHS();
6847 X = Checker.getX();
6848 E = Checker.getExpr();
6849 UE = Checker.getUpdateExpr();
6850 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006851 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006852 }
6853 }
6854 if (!IsUpdateExprFound) {
6855 IsUpdateExprFound = !Checker.checkStatement(First);
6856 BinOp = nullptr;
6857 if (IsUpdateExprFound) {
6858 BinOp = dyn_cast<BinaryOperator>(Second);
6859 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6860 }
6861 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6862 // { x++; v = x; }
6863 // { x--; v = x; }
6864 // { ++x; v = x; }
6865 // { --x; v = x; }
6866 // { x binop= expr; v = x; }
6867 // { x = x binop expr; v = x; }
6868 // { x = expr binop x; v = x; }
6869 // Check that the second expression has form v = x.
Alexey Bataeve3727102018-04-18 15:57:46 +00006870 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006871 llvm::FoldingSetNodeID XId, PossibleXId;
6872 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6873 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6874 IsUpdateExprFound = XId == PossibleXId;
6875 if (IsUpdateExprFound) {
6876 V = BinOp->getLHS();
6877 X = Checker.getX();
6878 E = Checker.getExpr();
6879 UE = Checker.getUpdateExpr();
6880 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006881 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006882 }
6883 }
6884 }
6885 if (!IsUpdateExprFound) {
6886 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00006887 auto *FirstExpr = dyn_cast<Expr>(First);
6888 auto *SecondExpr = dyn_cast<Expr>(Second);
6889 if (!FirstExpr || !SecondExpr ||
6890 !(FirstExpr->isInstantiationDependent() ||
6891 SecondExpr->isInstantiationDependent())) {
6892 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6893 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006894 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00006895 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006896 : First->getBeginLoc();
Alexey Bataev5a195472015-09-04 12:55:50 +00006897 NoteRange = ErrorRange = FirstBinOp
6898 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00006899 : SourceRange(ErrorLoc, ErrorLoc);
6900 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006901 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6902 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6903 ErrorFound = NotAnAssignmentOp;
6904 NoteLoc = ErrorLoc = SecondBinOp
6905 ? SecondBinOp->getOperatorLoc()
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006906 : Second->getBeginLoc();
Alexey Bataev5a195472015-09-04 12:55:50 +00006907 NoteRange = ErrorRange =
6908 SecondBinOp ? SecondBinOp->getSourceRange()
6909 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00006910 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +00006911 Expr *PossibleXRHSInFirst =
Alexey Bataev5a195472015-09-04 12:55:50 +00006912 FirstBinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00006913 Expr *PossibleXLHSInSecond =
Alexey Bataev5a195472015-09-04 12:55:50 +00006914 SecondBinOp->getLHS()->IgnoreParenImpCasts();
6915 llvm::FoldingSetNodeID X1Id, X2Id;
6916 PossibleXRHSInFirst->Profile(X1Id, Context,
6917 /*Canonical=*/true);
6918 PossibleXLHSInSecond->Profile(X2Id, Context,
6919 /*Canonical=*/true);
6920 IsUpdateExprFound = X1Id == X2Id;
6921 if (IsUpdateExprFound) {
6922 V = FirstBinOp->getLHS();
6923 X = SecondBinOp->getLHS();
6924 E = SecondBinOp->getRHS();
6925 UE = nullptr;
6926 IsXLHSInRHSPart = false;
6927 IsPostfixUpdate = true;
6928 } else {
6929 ErrorFound = NotASpecificExpression;
6930 ErrorLoc = FirstBinOp->getExprLoc();
6931 ErrorRange = FirstBinOp->getSourceRange();
6932 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6933 NoteRange = SecondBinOp->getRHS()->getSourceRange();
6934 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006935 }
6936 }
6937 }
6938 }
6939 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006940 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006941 NoteRange = ErrorRange =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006942 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
Alexey Bataevb78ca832015-04-01 03:33:17 +00006943 ErrorFound = NotTwoSubstatements;
6944 }
6945 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006946 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006947 NoteRange = ErrorRange =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006948 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
Alexey Bataevb78ca832015-04-01 03:33:17 +00006949 ErrorFound = NotACompoundStatement;
6950 }
6951 if (ErrorFound != NoError) {
6952 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6953 << ErrorRange;
6954 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6955 return StmtError();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006956 }
Alexey Bataeve3727102018-04-18 15:57:46 +00006957 if (CurContext->isDependentContext())
6958 UE = V = E = X = nullptr;
Alexey Bataev459dec02014-07-24 06:46:57 +00006959 }
Alexey Bataevdea47612014-07-23 07:46:59 +00006960 }
Alexey Bataev0162e452014-07-22 10:10:35 +00006961
Reid Kleckner87a31802018-03-12 21:43:02 +00006962 setFunctionHasBranchProtectedScope();
Alexey Bataev0162e452014-07-22 10:10:35 +00006963
Alexey Bataev62cec442014-11-18 10:14:22 +00006964 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00006965 X, V, E, UE, IsXLHSInRHSPart,
6966 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00006967}
6968
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006969StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6970 Stmt *AStmt,
6971 SourceLocation StartLoc,
6972 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006973 if (!AStmt)
6974 return StmtError();
6975
Alexey Bataeve3727102018-04-18 15:57:46 +00006976 auto *CS = cast<CapturedStmt>(AStmt);
Samuel Antao4af1b7b2015-12-02 17:44:43 +00006977 // 1.2.2 OpenMP Language Terminology
6978 // Structured block - An executable statement with a single entry at the
6979 // top and a single exit at the bottom.
6980 // The point of exit cannot be a branch out of the structured block.
6981 // longjmp() and throw() must not violate the entry/exit criteria.
6982 CS->getCapturedDecl()->setNothrow();
Alexey Bataev8451efa2018-01-15 19:06:12 +00006983 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target);
6984 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6985 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6986 // 1.2.2 OpenMP Language Terminology
6987 // Structured block - An executable statement with a single entry at the
6988 // top and a single exit at the bottom.
6989 // The point of exit cannot be a branch out of the structured block.
6990 // longjmp() and throw() must not violate the entry/exit criteria.
6991 CS->getCapturedDecl()->setNothrow();
6992 }
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006993
Alexey Bataev13314bf2014-10-09 04:18:56 +00006994 // OpenMP [2.16, Nesting of Regions]
6995 // If specified, a teams construct must be contained within a target
6996 // construct. That target construct must contain no statements or directives
6997 // outside of the teams construct.
6998 if (DSAStack->hasInnerTeamsRegion()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006999 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true);
Alexey Bataev13314bf2014-10-09 04:18:56 +00007000 bool OMPTeamsFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00007001 if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00007002 auto I = CS->body_begin();
7003 while (I != CS->body_end()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007004 const auto *OED = dyn_cast<OMPExecutableDirective>(*I);
Alexey Bataev13314bf2014-10-09 04:18:56 +00007005 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
7006 OMPTeamsFound = false;
7007 break;
7008 }
7009 ++I;
7010 }
7011 assert(I != CS->body_end() && "Not found statement");
7012 S = *I;
Kelvin Li3834dce2016-06-27 19:15:43 +00007013 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +00007014 const auto *OED = dyn_cast<OMPExecutableDirective>(S);
Kelvin Li3834dce2016-06-27 19:15:43 +00007015 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
Alexey Bataev13314bf2014-10-09 04:18:56 +00007016 }
7017 if (!OMPTeamsFound) {
7018 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
7019 Diag(DSAStack->getInnerTeamsRegionLoc(),
7020 diag::note_omp_nested_teams_construct_here);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007021 Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here)
Alexey Bataev13314bf2014-10-09 04:18:56 +00007022 << isa<OMPExecutableDirective>(S);
7023 return StmtError();
7024 }
7025 }
7026
Reid Kleckner87a31802018-03-12 21:43:02 +00007027 setFunctionHasBranchProtectedScope();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00007028
7029 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
7030}
7031
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00007032StmtResult
7033Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
7034 Stmt *AStmt, SourceLocation StartLoc,
7035 SourceLocation EndLoc) {
7036 if (!AStmt)
7037 return StmtError();
7038
Alexey Bataeve3727102018-04-18 15:57:46 +00007039 auto *CS = cast<CapturedStmt>(AStmt);
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00007040 // 1.2.2 OpenMP Language Terminology
7041 // Structured block - An executable statement with a single entry at the
7042 // top and a single exit at the bottom.
7043 // The point of exit cannot be a branch out of the structured block.
7044 // longjmp() and throw() must not violate the entry/exit criteria.
7045 CS->getCapturedDecl()->setNothrow();
Alexey Bataev8451efa2018-01-15 19:06:12 +00007046 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel);
7047 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7048 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7049 // 1.2.2 OpenMP Language Terminology
7050 // Structured block - An executable statement with a single entry at the
7051 // top and a single exit at the bottom.
7052 // The point of exit cannot be a branch out of the structured block.
7053 // longjmp() and throw() must not violate the entry/exit criteria.
7054 CS->getCapturedDecl()->setNothrow();
7055 }
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00007056
Reid Kleckner87a31802018-03-12 21:43:02 +00007057 setFunctionHasBranchProtectedScope();
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00007058
7059 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
7060 AStmt);
7061}
7062
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007063StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
7064 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007065 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007066 if (!AStmt)
7067 return StmtError();
7068
Alexey Bataeve3727102018-04-18 15:57:46 +00007069 auto *CS = cast<CapturedStmt>(AStmt);
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007070 // 1.2.2 OpenMP Language Terminology
7071 // Structured block - An executable statement with a single entry at the
7072 // top and a single exit at the bottom.
7073 // The point of exit cannot be a branch out of the structured block.
7074 // longjmp() and throw() must not violate the entry/exit criteria.
7075 CS->getCapturedDecl()->setNothrow();
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00007076 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
7077 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7078 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7079 // 1.2.2 OpenMP Language Terminology
7080 // Structured block - An executable statement with a single entry at the
7081 // top and a single exit at the bottom.
7082 // The point of exit cannot be a branch out of the structured block.
7083 // longjmp() and throw() must not violate the entry/exit criteria.
7084 CS->getCapturedDecl()->setNothrow();
7085 }
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007086
7087 OMPLoopDirective::HelperExprs B;
7088 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7089 // define the nested loops number.
7090 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007091 checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00007092 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007093 VarsWithImplicitDSA, B);
7094 if (NestedLoopCount == 0)
7095 return StmtError();
7096
7097 assert((CurContext->isDependentContext() || B.builtAll()) &&
7098 "omp target parallel for loop exprs were not built");
7099
7100 if (!CurContext->isDependentContext()) {
7101 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007102 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007103 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007104 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007105 B.NumIterations, *this, CurScope,
7106 DSAStack))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007107 return StmtError();
7108 }
7109 }
7110
Reid Kleckner87a31802018-03-12 21:43:02 +00007111 setFunctionHasBranchProtectedScope();
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007112 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
7113 NestedLoopCount, Clauses, AStmt,
7114 B, DSAStack->isCancelRegion());
7115}
7116
Alexey Bataev95b64a92017-05-30 16:00:04 +00007117/// Check for existence of a map clause in the list of clauses.
7118static bool hasClauses(ArrayRef<OMPClause *> Clauses,
7119 const OpenMPClauseKind K) {
7120 return llvm::any_of(
7121 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; });
7122}
Samuel Antaodf67fc42016-01-19 19:15:56 +00007123
Alexey Bataev95b64a92017-05-30 16:00:04 +00007124template <typename... Params>
7125static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K,
7126 const Params... ClauseTypes) {
7127 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...);
Samuel Antaodf67fc42016-01-19 19:15:56 +00007128}
7129
Michael Wong65f367f2015-07-21 13:44:28 +00007130StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
7131 Stmt *AStmt,
7132 SourceLocation StartLoc,
7133 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007134 if (!AStmt)
7135 return StmtError();
7136
7137 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7138
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00007139 // OpenMP [2.10.1, Restrictions, p. 97]
7140 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00007141 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr)) {
7142 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
7143 << "'map' or 'use_device_ptr'"
David Majnemer9d168222016-08-05 17:44:54 +00007144 << getOpenMPDirectiveName(OMPD_target_data);
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00007145 return StmtError();
7146 }
7147
Reid Kleckner87a31802018-03-12 21:43:02 +00007148 setFunctionHasBranchProtectedScope();
Michael Wong65f367f2015-07-21 13:44:28 +00007149
7150 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
7151 AStmt);
7152}
7153
Samuel Antaodf67fc42016-01-19 19:15:56 +00007154StmtResult
7155Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
7156 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00007157 SourceLocation EndLoc, Stmt *AStmt) {
7158 if (!AStmt)
7159 return StmtError();
7160
Alexey Bataeve3727102018-04-18 15:57:46 +00007161 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00007162 // 1.2.2 OpenMP Language Terminology
7163 // Structured block - An executable statement with a single entry at the
7164 // top and a single exit at the bottom.
7165 // The point of exit cannot be a branch out of the structured block.
7166 // longjmp() and throw() must not violate the entry/exit criteria.
7167 CS->getCapturedDecl()->setNothrow();
7168 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data);
7169 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7170 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7171 // 1.2.2 OpenMP Language Terminology
7172 // Structured block - An executable statement with a single entry at the
7173 // top and a single exit at the bottom.
7174 // The point of exit cannot be a branch out of the structured block.
7175 // longjmp() and throw() must not violate the entry/exit criteria.
7176 CS->getCapturedDecl()->setNothrow();
7177 }
7178
Samuel Antaodf67fc42016-01-19 19:15:56 +00007179 // OpenMP [2.10.2, Restrictions, p. 99]
7180 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00007181 if (!hasClauses(Clauses, OMPC_map)) {
7182 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
7183 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data);
Samuel Antaodf67fc42016-01-19 19:15:56 +00007184 return StmtError();
7185 }
7186
Alexey Bataev7828b252017-11-21 17:08:48 +00007187 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
7188 AStmt);
Samuel Antaodf67fc42016-01-19 19:15:56 +00007189}
7190
Samuel Antao72590762016-01-19 20:04:50 +00007191StmtResult
7192Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
7193 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00007194 SourceLocation EndLoc, Stmt *AStmt) {
7195 if (!AStmt)
7196 return StmtError();
7197
Alexey Bataeve3727102018-04-18 15:57:46 +00007198 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00007199 // 1.2.2 OpenMP Language Terminology
7200 // Structured block - An executable statement with a single entry at the
7201 // top and a single exit at the bottom.
7202 // The point of exit cannot be a branch out of the structured block.
7203 // longjmp() and throw() must not violate the entry/exit criteria.
7204 CS->getCapturedDecl()->setNothrow();
7205 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data);
7206 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7207 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7208 // 1.2.2 OpenMP Language Terminology
7209 // Structured block - An executable statement with a single entry at the
7210 // top and a single exit at the bottom.
7211 // The point of exit cannot be a branch out of the structured block.
7212 // longjmp() and throw() must not violate the entry/exit criteria.
7213 CS->getCapturedDecl()->setNothrow();
7214 }
7215
Samuel Antao72590762016-01-19 20:04:50 +00007216 // OpenMP [2.10.3, Restrictions, p. 102]
7217 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00007218 if (!hasClauses(Clauses, OMPC_map)) {
7219 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
7220 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data);
Samuel Antao72590762016-01-19 20:04:50 +00007221 return StmtError();
7222 }
7223
Alexey Bataev7828b252017-11-21 17:08:48 +00007224 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
7225 AStmt);
Samuel Antao72590762016-01-19 20:04:50 +00007226}
7227
Samuel Antao686c70c2016-05-26 17:30:50 +00007228StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
7229 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00007230 SourceLocation EndLoc,
7231 Stmt *AStmt) {
7232 if (!AStmt)
7233 return StmtError();
7234
Alexey Bataeve3727102018-04-18 15:57:46 +00007235 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00007236 // 1.2.2 OpenMP Language Terminology
7237 // Structured block - An executable statement with a single entry at the
7238 // top and a single exit at the bottom.
7239 // The point of exit cannot be a branch out of the structured block.
7240 // longjmp() and throw() must not violate the entry/exit criteria.
7241 CS->getCapturedDecl()->setNothrow();
7242 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update);
7243 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7244 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7245 // 1.2.2 OpenMP Language Terminology
7246 // Structured block - An executable statement with a single entry at the
7247 // top and a single exit at the bottom.
7248 // The point of exit cannot be a branch out of the structured block.
7249 // longjmp() and throw() must not violate the entry/exit criteria.
7250 CS->getCapturedDecl()->setNothrow();
7251 }
7252
Alexey Bataev95b64a92017-05-30 16:00:04 +00007253 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) {
Samuel Antao686c70c2016-05-26 17:30:50 +00007254 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
7255 return StmtError();
7256 }
Alexey Bataev7828b252017-11-21 17:08:48 +00007257 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses,
7258 AStmt);
Samuel Antao686c70c2016-05-26 17:30:50 +00007259}
7260
Alexey Bataev13314bf2014-10-09 04:18:56 +00007261StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
7262 Stmt *AStmt, SourceLocation StartLoc,
7263 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007264 if (!AStmt)
7265 return StmtError();
7266
Alexey Bataeve3727102018-04-18 15:57:46 +00007267 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev13314bf2014-10-09 04:18:56 +00007268 // 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
Reid Kleckner87a31802018-03-12 21:43:02 +00007275 setFunctionHasBranchProtectedScope();
Alexey Bataev13314bf2014-10-09 04:18:56 +00007276
Alexey Bataevceabd412017-11-30 18:01:54 +00007277 DSAStack->setParentTeamsRegionLoc(StartLoc);
7278
Alexey Bataev13314bf2014-10-09 04:18:56 +00007279 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
7280}
7281
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007282StmtResult
7283Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
7284 SourceLocation EndLoc,
7285 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007286 if (DSAStack->isParentNowaitRegion()) {
7287 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
7288 return StmtError();
7289 }
7290 if (DSAStack->isParentOrderedRegion()) {
7291 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
7292 return StmtError();
7293 }
7294 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
7295 CancelRegion);
7296}
7297
Alexey Bataev87933c72015-09-18 08:07:34 +00007298StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
7299 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00007300 SourceLocation EndLoc,
7301 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev80909872015-07-02 11:25:17 +00007302 if (DSAStack->isParentNowaitRegion()) {
7303 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
7304 return StmtError();
7305 }
7306 if (DSAStack->isParentOrderedRegion()) {
7307 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
7308 return StmtError();
7309 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00007310 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00007311 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
7312 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00007313}
7314
Alexey Bataev382967a2015-12-08 12:06:20 +00007315static bool checkGrainsizeNumTasksClauses(Sema &S,
7316 ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007317 const OMPClause *PrevClause = nullptr;
Alexey Bataev382967a2015-12-08 12:06:20 +00007318 bool ErrorFound = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00007319 for (const OMPClause *C : Clauses) {
Alexey Bataev382967a2015-12-08 12:06:20 +00007320 if (C->getClauseKind() == OMPC_grainsize ||
7321 C->getClauseKind() == OMPC_num_tasks) {
7322 if (!PrevClause)
7323 PrevClause = C;
7324 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007325 S.Diag(C->getBeginLoc(),
Alexey Bataev382967a2015-12-08 12:06:20 +00007326 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
7327 << getOpenMPClauseName(C->getClauseKind())
7328 << getOpenMPClauseName(PrevClause->getClauseKind());
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007329 S.Diag(PrevClause->getBeginLoc(),
Alexey Bataev382967a2015-12-08 12:06:20 +00007330 diag::note_omp_previous_grainsize_num_tasks)
7331 << getOpenMPClauseName(PrevClause->getClauseKind());
7332 ErrorFound = true;
7333 }
7334 }
7335 }
7336 return ErrorFound;
7337}
7338
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007339static bool checkReductionClauseWithNogroup(Sema &S,
7340 ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007341 const OMPClause *ReductionClause = nullptr;
7342 const OMPClause *NogroupClause = nullptr;
7343 for (const OMPClause *C : Clauses) {
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007344 if (C->getClauseKind() == OMPC_reduction) {
7345 ReductionClause = C;
7346 if (NogroupClause)
7347 break;
7348 continue;
7349 }
7350 if (C->getClauseKind() == OMPC_nogroup) {
7351 NogroupClause = C;
7352 if (ReductionClause)
7353 break;
7354 continue;
7355 }
7356 }
7357 if (ReductionClause && NogroupClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007358 S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup)
7359 << SourceRange(NogroupClause->getBeginLoc(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007360 NogroupClause->getEndLoc());
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007361 return true;
7362 }
7363 return false;
7364}
7365
Alexey Bataev49f6e782015-12-01 04:18:41 +00007366StmtResult Sema::ActOnOpenMPTaskLoopDirective(
7367 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007368 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00007369 if (!AStmt)
7370 return StmtError();
7371
7372 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7373 OMPLoopDirective::HelperExprs B;
7374 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7375 // define the nested loops number.
7376 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007377 checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007378 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00007379 VarsWithImplicitDSA, B);
7380 if (NestedLoopCount == 0)
7381 return StmtError();
7382
7383 assert((CurContext->isDependentContext() || B.builtAll()) &&
7384 "omp for loop exprs were not built");
7385
Alexey Bataev382967a2015-12-08 12:06:20 +00007386 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7387 // The grainsize clause and num_tasks clause are mutually exclusive and may
7388 // not appear on the same taskloop directive.
7389 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7390 return StmtError();
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007391 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7392 // If a reduction clause is present on the taskloop directive, the nogroup
7393 // clause must not be specified.
7394 if (checkReductionClauseWithNogroup(*this, Clauses))
7395 return StmtError();
Alexey Bataev382967a2015-12-08 12:06:20 +00007396
Reid Kleckner87a31802018-03-12 21:43:02 +00007397 setFunctionHasBranchProtectedScope();
Alexey Bataev49f6e782015-12-01 04:18:41 +00007398 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
7399 NestedLoopCount, Clauses, AStmt, B);
7400}
7401
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007402StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
7403 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007404 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007405 if (!AStmt)
7406 return StmtError();
7407
7408 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7409 OMPLoopDirective::HelperExprs B;
7410 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7411 // define the nested loops number.
7412 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007413 checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007414 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
7415 VarsWithImplicitDSA, B);
7416 if (NestedLoopCount == 0)
7417 return StmtError();
7418
7419 assert((CurContext->isDependentContext() || B.builtAll()) &&
7420 "omp for loop exprs were not built");
7421
Alexey Bataev5a3af132016-03-29 08:58:54 +00007422 if (!CurContext->isDependentContext()) {
7423 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007424 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007425 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007426 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007427 B.NumIterations, *this, CurScope,
7428 DSAStack))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007429 return StmtError();
7430 }
7431 }
7432
Alexey Bataev382967a2015-12-08 12:06:20 +00007433 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7434 // The grainsize clause and num_tasks clause are mutually exclusive and may
7435 // not appear on the same taskloop directive.
7436 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7437 return StmtError();
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007438 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7439 // If a reduction clause is present on the taskloop directive, the nogroup
7440 // clause must not be specified.
7441 if (checkReductionClauseWithNogroup(*this, Clauses))
7442 return StmtError();
Alexey Bataev438388c2017-11-22 18:34:02 +00007443 if (checkSimdlenSafelenSpecified(*this, Clauses))
7444 return StmtError();
Alexey Bataev382967a2015-12-08 12:06:20 +00007445
Reid Kleckner87a31802018-03-12 21:43:02 +00007446 setFunctionHasBranchProtectedScope();
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007447 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
7448 NestedLoopCount, Clauses, AStmt, B);
7449}
7450
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007451StmtResult Sema::ActOnOpenMPDistributeDirective(
7452 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007453 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007454 if (!AStmt)
7455 return StmtError();
7456
7457 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7458 OMPLoopDirective::HelperExprs B;
7459 // In presence of clause 'collapse' with number of loops, it will
7460 // define the nested loops number.
7461 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007462 checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007463 nullptr /*ordered not a clause on distribute*/, AStmt,
7464 *this, *DSAStack, VarsWithImplicitDSA, B);
7465 if (NestedLoopCount == 0)
7466 return StmtError();
7467
7468 assert((CurContext->isDependentContext() || B.builtAll()) &&
7469 "omp for loop exprs were not built");
7470
Reid Kleckner87a31802018-03-12 21:43:02 +00007471 setFunctionHasBranchProtectedScope();
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007472 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
7473 NestedLoopCount, Clauses, AStmt, B);
7474}
7475
Carlo Bertolli9925f152016-06-27 14:55:37 +00007476StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
7477 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007478 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Carlo Bertolli9925f152016-06-27 14:55:37 +00007479 if (!AStmt)
7480 return StmtError();
7481
Alexey Bataeve3727102018-04-18 15:57:46 +00007482 auto *CS = cast<CapturedStmt>(AStmt);
Carlo Bertolli9925f152016-06-27 14:55:37 +00007483 // 1.2.2 OpenMP Language Terminology
7484 // Structured block - An executable statement with a single entry at the
7485 // top and a single exit at the bottom.
7486 // The point of exit cannot be a branch out of the structured block.
7487 // longjmp() and throw() must not violate the entry/exit criteria.
7488 CS->getCapturedDecl()->setNothrow();
Alexey Bataev7f96c372017-11-22 17:19:31 +00007489 for (int ThisCaptureLevel =
7490 getOpenMPCaptureLevels(OMPD_distribute_parallel_for);
7491 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7492 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7493 // 1.2.2 OpenMP Language Terminology
7494 // Structured block - An executable statement with a single entry at the
7495 // top and a single exit at the bottom.
7496 // The point of exit cannot be a branch out of the structured block.
7497 // longjmp() and throw() must not violate the entry/exit criteria.
7498 CS->getCapturedDecl()->setNothrow();
7499 }
Carlo Bertolli9925f152016-06-27 14:55:37 +00007500
7501 OMPLoopDirective::HelperExprs B;
7502 // In presence of clause 'collapse' with number of loops, it will
7503 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007504 unsigned NestedLoopCount = checkOpenMPLoop(
Carlo Bertolli9925f152016-06-27 14:55:37 +00007505 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataev7f96c372017-11-22 17:19:31 +00007506 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Carlo Bertolli9925f152016-06-27 14:55:37 +00007507 VarsWithImplicitDSA, B);
7508 if (NestedLoopCount == 0)
7509 return StmtError();
7510
7511 assert((CurContext->isDependentContext() || B.builtAll()) &&
7512 "omp for loop exprs were not built");
7513
Reid Kleckner87a31802018-03-12 21:43:02 +00007514 setFunctionHasBranchProtectedScope();
Carlo Bertolli9925f152016-06-27 14:55:37 +00007515 return OMPDistributeParallelForDirective::Create(
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00007516 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7517 DSAStack->isCancelRegion());
Carlo Bertolli9925f152016-06-27 14:55:37 +00007518}
7519
Kelvin Li4a39add2016-07-05 05:00:15 +00007520StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
7521 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007522 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li4a39add2016-07-05 05:00:15 +00007523 if (!AStmt)
7524 return StmtError();
7525
Alexey Bataeve3727102018-04-18 15:57:46 +00007526 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li4a39add2016-07-05 05:00:15 +00007527 // 1.2.2 OpenMP Language Terminology
7528 // Structured block - An executable statement with a single entry at the
7529 // top and a single exit at the bottom.
7530 // The point of exit cannot be a branch out of the structured block.
7531 // longjmp() and throw() must not violate the entry/exit criteria.
7532 CS->getCapturedDecl()->setNothrow();
Alexey Bataev974acd62017-11-27 19:38:52 +00007533 for (int ThisCaptureLevel =
7534 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd);
7535 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7536 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7537 // 1.2.2 OpenMP Language Terminology
7538 // Structured block - An executable statement with a single entry at the
7539 // top and a single exit at the bottom.
7540 // The point of exit cannot be a branch out of the structured block.
7541 // longjmp() and throw() must not violate the entry/exit criteria.
7542 CS->getCapturedDecl()->setNothrow();
7543 }
Kelvin Li4a39add2016-07-05 05:00:15 +00007544
7545 OMPLoopDirective::HelperExprs B;
7546 // In presence of clause 'collapse' with number of loops, it will
7547 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007548 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li4a39add2016-07-05 05:00:15 +00007549 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev974acd62017-11-27 19:38:52 +00007550 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li4a39add2016-07-05 05:00:15 +00007551 VarsWithImplicitDSA, B);
7552 if (NestedLoopCount == 0)
7553 return StmtError();
7554
7555 assert((CurContext->isDependentContext() || B.builtAll()) &&
7556 "omp for loop exprs were not built");
7557
Alexey Bataev438388c2017-11-22 18:34:02 +00007558 if (!CurContext->isDependentContext()) {
7559 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007560 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00007561 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7562 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7563 B.NumIterations, *this, CurScope,
7564 DSAStack))
7565 return StmtError();
7566 }
7567 }
7568
Kelvin Lic5609492016-07-15 04:39:07 +00007569 if (checkSimdlenSafelenSpecified(*this, Clauses))
7570 return StmtError();
7571
Reid Kleckner87a31802018-03-12 21:43:02 +00007572 setFunctionHasBranchProtectedScope();
Kelvin Li4a39add2016-07-05 05:00:15 +00007573 return OMPDistributeParallelForSimdDirective::Create(
7574 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7575}
7576
Kelvin Li787f3fc2016-07-06 04:45:38 +00007577StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
7578 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007579 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li787f3fc2016-07-06 04:45:38 +00007580 if (!AStmt)
7581 return StmtError();
7582
Alexey Bataeve3727102018-04-18 15:57:46 +00007583 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li787f3fc2016-07-06 04:45:38 +00007584 // 1.2.2 OpenMP Language Terminology
7585 // Structured block - An executable statement with a single entry at the
7586 // top and a single exit at the bottom.
7587 // The point of exit cannot be a branch out of the structured block.
7588 // longjmp() and throw() must not violate the entry/exit criteria.
7589 CS->getCapturedDecl()->setNothrow();
Alexey Bataev617db5f2017-12-04 15:38:33 +00007590 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd);
7591 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7592 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7593 // 1.2.2 OpenMP Language Terminology
7594 // Structured block - An executable statement with a single entry at the
7595 // top and a single exit at the bottom.
7596 // The point of exit cannot be a branch out of the structured block.
7597 // longjmp() and throw() must not violate the entry/exit criteria.
7598 CS->getCapturedDecl()->setNothrow();
7599 }
Kelvin Li787f3fc2016-07-06 04:45:38 +00007600
7601 OMPLoopDirective::HelperExprs B;
7602 // In presence of clause 'collapse' with number of loops, it will
7603 // define the nested loops number.
7604 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007605 checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev617db5f2017-12-04 15:38:33 +00007606 nullptr /*ordered not a clause on distribute*/, CS, *this,
7607 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li787f3fc2016-07-06 04:45:38 +00007608 if (NestedLoopCount == 0)
7609 return StmtError();
7610
7611 assert((CurContext->isDependentContext() || B.builtAll()) &&
7612 "omp for loop exprs were not built");
7613
Alexey Bataev438388c2017-11-22 18:34:02 +00007614 if (!CurContext->isDependentContext()) {
7615 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007616 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00007617 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7618 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7619 B.NumIterations, *this, CurScope,
7620 DSAStack))
7621 return StmtError();
7622 }
7623 }
7624
Kelvin Lic5609492016-07-15 04:39:07 +00007625 if (checkSimdlenSafelenSpecified(*this, Clauses))
7626 return StmtError();
7627
Reid Kleckner87a31802018-03-12 21:43:02 +00007628 setFunctionHasBranchProtectedScope();
Kelvin Li787f3fc2016-07-06 04:45:38 +00007629 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
7630 NestedLoopCount, Clauses, AStmt, B);
7631}
7632
Kelvin Lia579b912016-07-14 02:54:56 +00007633StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
7634 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007635 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Lia579b912016-07-14 02:54:56 +00007636 if (!AStmt)
7637 return StmtError();
7638
Alexey Bataeve3727102018-04-18 15:57:46 +00007639 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Lia579b912016-07-14 02:54:56 +00007640 // 1.2.2 OpenMP Language Terminology
7641 // Structured block - An executable statement with a single entry at the
7642 // top and a single exit at the bottom.
7643 // The point of exit cannot be a branch out of the structured block.
7644 // longjmp() and throw() must not violate the entry/exit criteria.
7645 CS->getCapturedDecl()->setNothrow();
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007646 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
7647 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7648 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7649 // 1.2.2 OpenMP Language Terminology
7650 // Structured block - An executable statement with a single entry at the
7651 // top and a single exit at the bottom.
7652 // The point of exit cannot be a branch out of the structured block.
7653 // longjmp() and throw() must not violate the entry/exit criteria.
7654 CS->getCapturedDecl()->setNothrow();
7655 }
Kelvin Lia579b912016-07-14 02:54:56 +00007656
7657 OMPLoopDirective::HelperExprs B;
7658 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7659 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007660 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Lia579b912016-07-14 02:54:56 +00007661 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007662 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Kelvin Lia579b912016-07-14 02:54:56 +00007663 VarsWithImplicitDSA, B);
7664 if (NestedLoopCount == 0)
7665 return StmtError();
7666
7667 assert((CurContext->isDependentContext() || B.builtAll()) &&
7668 "omp target parallel for simd loop exprs were not built");
7669
7670 if (!CurContext->isDependentContext()) {
7671 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007672 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007673 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Lia579b912016-07-14 02:54:56 +00007674 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7675 B.NumIterations, *this, CurScope,
7676 DSAStack))
7677 return StmtError();
7678 }
7679 }
Kelvin Lic5609492016-07-15 04:39:07 +00007680 if (checkSimdlenSafelenSpecified(*this, Clauses))
Kelvin Lia579b912016-07-14 02:54:56 +00007681 return StmtError();
7682
Reid Kleckner87a31802018-03-12 21:43:02 +00007683 setFunctionHasBranchProtectedScope();
Kelvin Lia579b912016-07-14 02:54:56 +00007684 return OMPTargetParallelForSimdDirective::Create(
7685 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7686}
7687
Kelvin Li986330c2016-07-20 22:57:10 +00007688StmtResult Sema::ActOnOpenMPTargetSimdDirective(
7689 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007690 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li986330c2016-07-20 22:57:10 +00007691 if (!AStmt)
7692 return StmtError();
7693
Alexey Bataeve3727102018-04-18 15:57:46 +00007694 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li986330c2016-07-20 22:57:10 +00007695 // 1.2.2 OpenMP Language Terminology
7696 // Structured block - An executable statement with a single entry at the
7697 // top and a single exit at the bottom.
7698 // The point of exit cannot be a branch out of the structured block.
7699 // longjmp() and throw() must not violate the entry/exit criteria.
7700 CS->getCapturedDecl()->setNothrow();
Alexey Bataevf8365372017-11-17 17:57:25 +00007701 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd);
7702 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7703 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7704 // 1.2.2 OpenMP Language Terminology
7705 // Structured block - An executable statement with a single entry at the
7706 // top and a single exit at the bottom.
7707 // The point of exit cannot be a branch out of the structured block.
7708 // longjmp() and throw() must not violate the entry/exit criteria.
7709 CS->getCapturedDecl()->setNothrow();
7710 }
7711
Kelvin Li986330c2016-07-20 22:57:10 +00007712 OMPLoopDirective::HelperExprs B;
7713 // In presence of clause 'collapse' with number of loops, it will define the
7714 // nested loops number.
David Majnemer9d168222016-08-05 17:44:54 +00007715 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007716 checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
Alexey Bataevf8365372017-11-17 17:57:25 +00007717 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Kelvin Li986330c2016-07-20 22:57:10 +00007718 VarsWithImplicitDSA, B);
7719 if (NestedLoopCount == 0)
7720 return StmtError();
7721
7722 assert((CurContext->isDependentContext() || B.builtAll()) &&
7723 "omp target simd loop exprs were not built");
7724
7725 if (!CurContext->isDependentContext()) {
7726 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007727 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007728 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Li986330c2016-07-20 22:57:10 +00007729 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7730 B.NumIterations, *this, CurScope,
7731 DSAStack))
7732 return StmtError();
7733 }
7734 }
7735
7736 if (checkSimdlenSafelenSpecified(*this, Clauses))
7737 return StmtError();
7738
Reid Kleckner87a31802018-03-12 21:43:02 +00007739 setFunctionHasBranchProtectedScope();
Kelvin Li986330c2016-07-20 22:57:10 +00007740 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
7741 NestedLoopCount, Clauses, AStmt, B);
7742}
7743
Kelvin Li02532872016-08-05 14:37:37 +00007744StmtResult Sema::ActOnOpenMPTeamsDistributeDirective(
7745 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007746 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li02532872016-08-05 14:37:37 +00007747 if (!AStmt)
7748 return StmtError();
7749
Alexey Bataeve3727102018-04-18 15:57:46 +00007750 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li02532872016-08-05 14:37:37 +00007751 // 1.2.2 OpenMP Language Terminology
7752 // Structured block - An executable statement with a single entry at the
7753 // top and a single exit at the bottom.
7754 // The point of exit cannot be a branch out of the structured block.
7755 // longjmp() and throw() must not violate the entry/exit criteria.
7756 CS->getCapturedDecl()->setNothrow();
Alexey Bataev95c6dd42017-11-29 15:14:16 +00007757 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute);
7758 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7759 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7760 // 1.2.2 OpenMP Language Terminology
7761 // Structured block - An executable statement with a single entry at the
7762 // top and a single exit at the bottom.
7763 // The point of exit cannot be a branch out of the structured block.
7764 // longjmp() and throw() must not violate the entry/exit criteria.
7765 CS->getCapturedDecl()->setNothrow();
7766 }
Kelvin Li02532872016-08-05 14:37:37 +00007767
7768 OMPLoopDirective::HelperExprs B;
7769 // In presence of clause 'collapse' with number of loops, it will
7770 // define the nested loops number.
7771 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007772 checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses),
Alexey Bataev95c6dd42017-11-29 15:14:16 +00007773 nullptr /*ordered not a clause on distribute*/, CS, *this,
7774 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li02532872016-08-05 14:37:37 +00007775 if (NestedLoopCount == 0)
7776 return StmtError();
7777
7778 assert((CurContext->isDependentContext() || B.builtAll()) &&
7779 "omp teams distribute loop exprs were not built");
7780
Reid Kleckner87a31802018-03-12 21:43:02 +00007781 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007782
7783 DSAStack->setParentTeamsRegionLoc(StartLoc);
7784
David Majnemer9d168222016-08-05 17:44:54 +00007785 return OMPTeamsDistributeDirective::Create(
7786 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Kelvin Li02532872016-08-05 14:37:37 +00007787}
7788
Kelvin Li4e325f72016-10-25 12:50:55 +00007789StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective(
7790 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007791 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li4e325f72016-10-25 12:50:55 +00007792 if (!AStmt)
7793 return StmtError();
7794
Alexey Bataeve3727102018-04-18 15:57:46 +00007795 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li4e325f72016-10-25 12:50:55 +00007796 // 1.2.2 OpenMP Language Terminology
7797 // Structured block - An executable statement with a single entry at the
7798 // top and a single exit at the bottom.
7799 // The point of exit cannot be a branch out of the structured block.
7800 // longjmp() and throw() must not violate the entry/exit criteria.
7801 CS->getCapturedDecl()->setNothrow();
Alexey Bataev999277a2017-12-06 14:31:09 +00007802 for (int ThisCaptureLevel =
7803 getOpenMPCaptureLevels(OMPD_teams_distribute_simd);
7804 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7805 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7806 // 1.2.2 OpenMP Language Terminology
7807 // Structured block - An executable statement with a single entry at the
7808 // top and a single exit at the bottom.
7809 // The point of exit cannot be a branch out of the structured block.
7810 // longjmp() and throw() must not violate the entry/exit criteria.
7811 CS->getCapturedDecl()->setNothrow();
7812 }
7813
Kelvin Li4e325f72016-10-25 12:50:55 +00007814
7815 OMPLoopDirective::HelperExprs B;
7816 // In presence of clause 'collapse' with number of loops, it will
7817 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007818 unsigned NestedLoopCount = checkOpenMPLoop(
Samuel Antao4c8035b2016-12-12 18:00:20 +00007819 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev999277a2017-12-06 14:31:09 +00007820 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Samuel Antao4c8035b2016-12-12 18:00:20 +00007821 VarsWithImplicitDSA, B);
Kelvin Li4e325f72016-10-25 12:50:55 +00007822
7823 if (NestedLoopCount == 0)
7824 return StmtError();
7825
7826 assert((CurContext->isDependentContext() || B.builtAll()) &&
7827 "omp teams distribute simd loop exprs were not built");
7828
7829 if (!CurContext->isDependentContext()) {
7830 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007831 for (OMPClause *C : Clauses) {
Kelvin Li4e325f72016-10-25 12:50:55 +00007832 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7833 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7834 B.NumIterations, *this, CurScope,
7835 DSAStack))
7836 return StmtError();
7837 }
7838 }
7839
7840 if (checkSimdlenSafelenSpecified(*this, Clauses))
7841 return StmtError();
7842
Reid Kleckner87a31802018-03-12 21:43:02 +00007843 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007844
7845 DSAStack->setParentTeamsRegionLoc(StartLoc);
7846
Kelvin Li4e325f72016-10-25 12:50:55 +00007847 return OMPTeamsDistributeSimdDirective::Create(
7848 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7849}
7850
Kelvin Li579e41c2016-11-30 23:51:03 +00007851StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective(
7852 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007853 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li579e41c2016-11-30 23:51:03 +00007854 if (!AStmt)
7855 return StmtError();
7856
Alexey Bataeve3727102018-04-18 15:57:46 +00007857 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li579e41c2016-11-30 23:51:03 +00007858 // 1.2.2 OpenMP Language Terminology
7859 // Structured block - An executable statement with a single entry at the
7860 // top and a single exit at the bottom.
7861 // The point of exit cannot be a branch out of the structured block.
7862 // longjmp() and throw() must not violate the entry/exit criteria.
7863 CS->getCapturedDecl()->setNothrow();
7864
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00007865 for (int ThisCaptureLevel =
7866 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd);
7867 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7868 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7869 // 1.2.2 OpenMP Language Terminology
7870 // Structured block - An executable statement with a single entry at the
7871 // top and a single exit at the bottom.
7872 // The point of exit cannot be a branch out of the structured block.
7873 // longjmp() and throw() must not violate the entry/exit criteria.
7874 CS->getCapturedDecl()->setNothrow();
7875 }
7876
Kelvin Li579e41c2016-11-30 23:51:03 +00007877 OMPLoopDirective::HelperExprs B;
7878 // In presence of clause 'collapse' with number of loops, it will
7879 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007880 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li579e41c2016-11-30 23:51:03 +00007881 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00007882 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li579e41c2016-11-30 23:51:03 +00007883 VarsWithImplicitDSA, B);
7884
7885 if (NestedLoopCount == 0)
7886 return StmtError();
7887
7888 assert((CurContext->isDependentContext() || B.builtAll()) &&
7889 "omp for loop exprs were not built");
7890
7891 if (!CurContext->isDependentContext()) {
7892 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007893 for (OMPClause *C : Clauses) {
Kelvin Li579e41c2016-11-30 23:51:03 +00007894 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7895 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7896 B.NumIterations, *this, CurScope,
7897 DSAStack))
7898 return StmtError();
7899 }
7900 }
7901
7902 if (checkSimdlenSafelenSpecified(*this, Clauses))
7903 return StmtError();
7904
Reid Kleckner87a31802018-03-12 21:43:02 +00007905 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007906
7907 DSAStack->setParentTeamsRegionLoc(StartLoc);
7908
Kelvin Li579e41c2016-11-30 23:51:03 +00007909 return OMPTeamsDistributeParallelForSimdDirective::Create(
7910 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7911}
7912
Kelvin Li7ade93f2016-12-09 03:24:30 +00007913StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective(
7914 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007915 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li7ade93f2016-12-09 03:24:30 +00007916 if (!AStmt)
7917 return StmtError();
7918
Alexey Bataeve3727102018-04-18 15:57:46 +00007919 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li7ade93f2016-12-09 03:24:30 +00007920 // 1.2.2 OpenMP Language Terminology
7921 // Structured block - An executable statement with a single entry at the
7922 // top and a single exit at the bottom.
7923 // The point of exit cannot be a branch out of the structured block.
7924 // longjmp() and throw() must not violate the entry/exit criteria.
7925 CS->getCapturedDecl()->setNothrow();
7926
Carlo Bertolli62fae152017-11-20 20:46:39 +00007927 for (int ThisCaptureLevel =
7928 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for);
7929 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7930 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7931 // 1.2.2 OpenMP Language Terminology
7932 // Structured block - An executable statement with a single entry at the
7933 // top and a single exit at the bottom.
7934 // The point of exit cannot be a branch out of the structured block.
7935 // longjmp() and throw() must not violate the entry/exit criteria.
7936 CS->getCapturedDecl()->setNothrow();
7937 }
7938
Kelvin Li7ade93f2016-12-09 03:24:30 +00007939 OMPLoopDirective::HelperExprs B;
7940 // In presence of clause 'collapse' with number of loops, it will
7941 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007942 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li7ade93f2016-12-09 03:24:30 +00007943 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
Carlo Bertolli62fae152017-11-20 20:46:39 +00007944 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li7ade93f2016-12-09 03:24:30 +00007945 VarsWithImplicitDSA, B);
7946
7947 if (NestedLoopCount == 0)
7948 return StmtError();
7949
7950 assert((CurContext->isDependentContext() || B.builtAll()) &&
7951 "omp for loop exprs were not built");
7952
Reid Kleckner87a31802018-03-12 21:43:02 +00007953 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007954
7955 DSAStack->setParentTeamsRegionLoc(StartLoc);
7956
Kelvin Li7ade93f2016-12-09 03:24:30 +00007957 return OMPTeamsDistributeParallelForDirective::Create(
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00007958 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7959 DSAStack->isCancelRegion());
Kelvin Li7ade93f2016-12-09 03:24:30 +00007960}
7961
Kelvin Libf594a52016-12-17 05:48:59 +00007962StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses,
7963 Stmt *AStmt,
7964 SourceLocation StartLoc,
7965 SourceLocation EndLoc) {
7966 if (!AStmt)
7967 return StmtError();
7968
Alexey Bataeve3727102018-04-18 15:57:46 +00007969 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Libf594a52016-12-17 05:48:59 +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();
7976
Alexey Bataevf9fc42e2017-11-22 14:25:55 +00007977 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams);
7978 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7979 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7980 // 1.2.2 OpenMP Language Terminology
7981 // Structured block - An executable statement with a single entry at the
7982 // top and a single exit at the bottom.
7983 // The point of exit cannot be a branch out of the structured block.
7984 // longjmp() and throw() must not violate the entry/exit criteria.
7985 CS->getCapturedDecl()->setNothrow();
7986 }
Reid Kleckner87a31802018-03-12 21:43:02 +00007987 setFunctionHasBranchProtectedScope();
Kelvin Libf594a52016-12-17 05:48:59 +00007988
7989 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses,
7990 AStmt);
7991}
7992
Kelvin Li83c451e2016-12-25 04:52:54 +00007993StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective(
7994 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007995 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li83c451e2016-12-25 04:52:54 +00007996 if (!AStmt)
7997 return StmtError();
7998
Alexey Bataeve3727102018-04-18 15:57:46 +00007999 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li83c451e2016-12-25 04:52:54 +00008000 // 1.2.2 OpenMP Language Terminology
8001 // Structured block - An executable statement with a single entry at the
8002 // top and a single exit at the bottom.
8003 // The point of exit cannot be a branch out of the structured block.
8004 // longjmp() and throw() must not violate the entry/exit criteria.
8005 CS->getCapturedDecl()->setNothrow();
Alexey Bataevdfa430f2017-12-08 15:03:50 +00008006 for (int ThisCaptureLevel =
8007 getOpenMPCaptureLevels(OMPD_target_teams_distribute);
8008 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8009 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8010 // 1.2.2 OpenMP Language Terminology
8011 // Structured block - An executable statement with a single entry at the
8012 // top and a single exit at the bottom.
8013 // The point of exit cannot be a branch out of the structured block.
8014 // longjmp() and throw() must not violate the entry/exit criteria.
8015 CS->getCapturedDecl()->setNothrow();
8016 }
Kelvin Li83c451e2016-12-25 04:52:54 +00008017
8018 OMPLoopDirective::HelperExprs B;
8019 // In presence of clause 'collapse' with number of loops, it will
8020 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00008021 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataevdfa430f2017-12-08 15:03:50 +00008022 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses),
8023 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li83c451e2016-12-25 04:52:54 +00008024 VarsWithImplicitDSA, B);
8025 if (NestedLoopCount == 0)
8026 return StmtError();
8027
8028 assert((CurContext->isDependentContext() || B.builtAll()) &&
8029 "omp target teams distribute loop exprs were not built");
8030
Reid Kleckner87a31802018-03-12 21:43:02 +00008031 setFunctionHasBranchProtectedScope();
Kelvin Li83c451e2016-12-25 04:52:54 +00008032 return OMPTargetTeamsDistributeDirective::Create(
8033 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
8034}
8035
Kelvin Li80e8f562016-12-29 22:16:30 +00008036StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective(
8037 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00008038 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li80e8f562016-12-29 22:16:30 +00008039 if (!AStmt)
8040 return StmtError();
8041
Alexey Bataeve3727102018-04-18 15:57:46 +00008042 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li80e8f562016-12-29 22:16:30 +00008043 // 1.2.2 OpenMP Language Terminology
8044 // Structured block - An executable statement with a single entry at the
8045 // top and a single exit at the bottom.
8046 // The point of exit cannot be a branch out of the structured block.
8047 // longjmp() and throw() must not violate the entry/exit criteria.
8048 CS->getCapturedDecl()->setNothrow();
Carlo Bertolli52978c32018-01-03 21:12:44 +00008049 for (int ThisCaptureLevel =
8050 getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for);
8051 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8052 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8053 // 1.2.2 OpenMP Language Terminology
8054 // Structured block - An executable statement with a single entry at the
8055 // top and a single exit at the bottom.
8056 // The point of exit cannot be a branch out of the structured block.
8057 // longjmp() and throw() must not violate the entry/exit criteria.
8058 CS->getCapturedDecl()->setNothrow();
8059 }
8060
Kelvin Li80e8f562016-12-29 22:16:30 +00008061 OMPLoopDirective::HelperExprs B;
8062 // In presence of clause 'collapse' with number of loops, it will
8063 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00008064 unsigned NestedLoopCount = checkOpenMPLoop(
Carlo Bertolli52978c32018-01-03 21:12:44 +00008065 OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
8066 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li80e8f562016-12-29 22:16:30 +00008067 VarsWithImplicitDSA, B);
8068 if (NestedLoopCount == 0)
8069 return StmtError();
8070
8071 assert((CurContext->isDependentContext() || B.builtAll()) &&
8072 "omp target teams distribute parallel for loop exprs were not built");
8073
Alexey Bataev647dd842018-01-15 20:59:40 +00008074 if (!CurContext->isDependentContext()) {
8075 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00008076 for (OMPClause *C : Clauses) {
Alexey Bataev647dd842018-01-15 20:59:40 +00008077 if (auto *LC = dyn_cast<OMPLinearClause>(C))
8078 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8079 B.NumIterations, *this, CurScope,
8080 DSAStack))
8081 return StmtError();
8082 }
8083 }
8084
Reid Kleckner87a31802018-03-12 21:43:02 +00008085 setFunctionHasBranchProtectedScope();
Kelvin Li80e8f562016-12-29 22:16:30 +00008086 return OMPTargetTeamsDistributeParallelForDirective::Create(
Alexey Bataev16e79882017-11-22 21:12:03 +00008087 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
8088 DSAStack->isCancelRegion());
Kelvin Li80e8f562016-12-29 22:16:30 +00008089}
8090
Kelvin Li1851df52017-01-03 05:23:48 +00008091StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
8092 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00008093 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li1851df52017-01-03 05:23:48 +00008094 if (!AStmt)
8095 return StmtError();
8096
Alexey Bataeve3727102018-04-18 15:57:46 +00008097 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li1851df52017-01-03 05:23:48 +00008098 // 1.2.2 OpenMP Language Terminology
8099 // Structured block - An executable statement with a single entry at the
8100 // top and a single exit at the bottom.
8101 // The point of exit cannot be a branch out of the structured block.
8102 // longjmp() and throw() must not violate the entry/exit criteria.
8103 CS->getCapturedDecl()->setNothrow();
Alexey Bataev647dd842018-01-15 20:59:40 +00008104 for (int ThisCaptureLevel = getOpenMPCaptureLevels(
8105 OMPD_target_teams_distribute_parallel_for_simd);
8106 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8107 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8108 // 1.2.2 OpenMP Language Terminology
8109 // Structured block - An executable statement with a single entry at the
8110 // top and a single exit at the bottom.
8111 // The point of exit cannot be a branch out of the structured block.
8112 // longjmp() and throw() must not violate the entry/exit criteria.
8113 CS->getCapturedDecl()->setNothrow();
8114 }
Kelvin Li1851df52017-01-03 05:23:48 +00008115
8116 OMPLoopDirective::HelperExprs B;
8117 // In presence of clause 'collapse' with number of loops, it will
8118 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00008119 unsigned NestedLoopCount =
8120 checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd,
Alexey Bataev647dd842018-01-15 20:59:40 +00008121 getCollapseNumberExpr(Clauses),
8122 nullptr /*ordered not a clause on distribute*/, CS, *this,
8123 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li1851df52017-01-03 05:23:48 +00008124 if (NestedLoopCount == 0)
8125 return StmtError();
8126
8127 assert((CurContext->isDependentContext() || B.builtAll()) &&
8128 "omp target teams distribute parallel for simd loop exprs were not "
8129 "built");
8130
8131 if (!CurContext->isDependentContext()) {
8132 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00008133 for (OMPClause *C : Clauses) {
Kelvin Li1851df52017-01-03 05:23:48 +00008134 if (auto *LC = dyn_cast<OMPLinearClause>(C))
8135 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8136 B.NumIterations, *this, CurScope,
8137 DSAStack))
8138 return StmtError();
8139 }
8140 }
8141
Alexey Bataev438388c2017-11-22 18:34:02 +00008142 if (checkSimdlenSafelenSpecified(*this, Clauses))
8143 return StmtError();
8144
Reid Kleckner87a31802018-03-12 21:43:02 +00008145 setFunctionHasBranchProtectedScope();
Kelvin Li1851df52017-01-03 05:23:48 +00008146 return OMPTargetTeamsDistributeParallelForSimdDirective::Create(
8147 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
8148}
8149
Kelvin Lida681182017-01-10 18:08:18 +00008150StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective(
8151 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00008152 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Lida681182017-01-10 18:08:18 +00008153 if (!AStmt)
8154 return StmtError();
8155
8156 auto *CS = cast<CapturedStmt>(AStmt);
8157 // 1.2.2 OpenMP Language Terminology
8158 // Structured block - An executable statement with a single entry at the
8159 // top and a single exit at the bottom.
8160 // The point of exit cannot be a branch out of the structured block.
8161 // longjmp() and throw() must not violate the entry/exit criteria.
8162 CS->getCapturedDecl()->setNothrow();
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00008163 for (int ThisCaptureLevel =
8164 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd);
8165 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8166 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8167 // 1.2.2 OpenMP Language Terminology
8168 // Structured block - An executable statement with a single entry at the
8169 // top and a single exit at the bottom.
8170 // The point of exit cannot be a branch out of the structured block.
8171 // longjmp() and throw() must not violate the entry/exit criteria.
8172 CS->getCapturedDecl()->setNothrow();
8173 }
Kelvin Lida681182017-01-10 18:08:18 +00008174
8175 OMPLoopDirective::HelperExprs B;
8176 // In presence of clause 'collapse' with number of loops, it will
8177 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00008178 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Lida681182017-01-10 18:08:18 +00008179 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00008180 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Lida681182017-01-10 18:08:18 +00008181 VarsWithImplicitDSA, B);
8182 if (NestedLoopCount == 0)
8183 return StmtError();
8184
8185 assert((CurContext->isDependentContext() || B.builtAll()) &&
8186 "omp target teams distribute simd loop exprs were not built");
8187
Alexey Bataev438388c2017-11-22 18:34:02 +00008188 if (!CurContext->isDependentContext()) {
8189 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00008190 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00008191 if (auto *LC = dyn_cast<OMPLinearClause>(C))
8192 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8193 B.NumIterations, *this, CurScope,
8194 DSAStack))
8195 return StmtError();
8196 }
8197 }
8198
8199 if (checkSimdlenSafelenSpecified(*this, Clauses))
8200 return StmtError();
8201
Reid Kleckner87a31802018-03-12 21:43:02 +00008202 setFunctionHasBranchProtectedScope();
Kelvin Lida681182017-01-10 18:08:18 +00008203 return OMPTargetTeamsDistributeSimdDirective::Create(
8204 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
8205}
8206
Alexey Bataeved09d242014-05-28 05:53:51 +00008207OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008208 SourceLocation StartLoc,
8209 SourceLocation LParenLoc,
8210 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008211 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008212 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00008213 case OMPC_final:
8214 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
8215 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00008216 case OMPC_num_threads:
8217 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
8218 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00008219 case OMPC_safelen:
8220 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
8221 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00008222 case OMPC_simdlen:
8223 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
8224 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00008225 case OMPC_collapse:
8226 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
8227 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00008228 case OMPC_ordered:
8229 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
8230 break;
Michael Wonge710d542015-08-07 16:16:36 +00008231 case OMPC_device:
8232 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
8233 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00008234 case OMPC_num_teams:
8235 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
8236 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008237 case OMPC_thread_limit:
8238 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
8239 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00008240 case OMPC_priority:
8241 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
8242 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008243 case OMPC_grainsize:
8244 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
8245 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00008246 case OMPC_num_tasks:
8247 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
8248 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00008249 case OMPC_hint:
8250 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
8251 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008252 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008253 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008254 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008255 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008256 case OMPC_private:
8257 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00008258 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008259 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00008260 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008261 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008262 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00008263 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008264 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008265 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008266 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00008267 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008268 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008269 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008270 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00008271 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008272 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008273 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008274 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008275 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008276 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008277 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00008278 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008279 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008280 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00008281 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008282 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008283 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008284 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008285 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00008286 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00008287 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00008288 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00008289 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00008290 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00008291 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00008292 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00008293 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00008294 case OMPC_atomic_default_mem_order:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008295 llvm_unreachable("Clause is not allowed.");
8296 }
8297 return Res;
8298}
8299
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008300// An OpenMP directive such as 'target parallel' has two captured regions:
8301// for the 'target' and 'parallel' respectively. This function returns
8302// the region in which to capture expressions associated with a clause.
8303// A return value of OMPD_unknown signifies that the expression should not
8304// be captured.
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008305static OpenMPDirectiveKind getOpenMPCaptureRegionForClause(
8306 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
8307 OpenMPDirectiveKind NameModifier = OMPD_unknown) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008308 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008309 switch (CKind) {
8310 case OMPC_if:
8311 switch (DKind) {
8312 case OMPD_target_parallel:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008313 case OMPD_target_parallel_for:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00008314 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008315 // If this clause applies to the nested 'parallel' region, capture within
8316 // the 'target' region, otherwise do not capture.
8317 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
8318 CaptureRegion = OMPD_target;
8319 break;
Carlo Bertolli52978c32018-01-03 21:12:44 +00008320 case OMPD_target_teams_distribute_parallel_for:
8321 case OMPD_target_teams_distribute_parallel_for_simd:
8322 // If this clause applies to the nested 'parallel' region, capture within
8323 // the 'teams' region, otherwise do not capture.
8324 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
8325 CaptureRegion = OMPD_teams;
8326 break;
Carlo Bertolli62fae152017-11-20 20:46:39 +00008327 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008328 case OMPD_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008329 CaptureRegion = OMPD_teams;
8330 break;
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008331 case OMPD_target_update:
Alexey Bataevfab20e42017-12-27 18:49:38 +00008332 case OMPD_target_enter_data:
8333 case OMPD_target_exit_data:
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008334 CaptureRegion = OMPD_task;
8335 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008336 case OMPD_cancel:
8337 case OMPD_parallel:
8338 case OMPD_parallel_sections:
8339 case OMPD_parallel_for:
8340 case OMPD_parallel_for_simd:
8341 case OMPD_target:
8342 case OMPD_target_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008343 case OMPD_target_teams:
8344 case OMPD_target_teams_distribute:
8345 case OMPD_target_teams_distribute_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008346 case OMPD_distribute_parallel_for:
8347 case OMPD_distribute_parallel_for_simd:
8348 case OMPD_task:
8349 case OMPD_taskloop:
8350 case OMPD_taskloop_simd:
8351 case OMPD_target_data:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008352 // Do not capture if-clause expressions.
8353 break;
8354 case OMPD_threadprivate:
8355 case OMPD_taskyield:
8356 case OMPD_barrier:
8357 case OMPD_taskwait:
8358 case OMPD_cancellation_point:
8359 case OMPD_flush:
8360 case OMPD_declare_reduction:
8361 case OMPD_declare_simd:
8362 case OMPD_declare_target:
8363 case OMPD_end_declare_target:
8364 case OMPD_teams:
8365 case OMPD_simd:
8366 case OMPD_for:
8367 case OMPD_for_simd:
8368 case OMPD_sections:
8369 case OMPD_section:
8370 case OMPD_single:
8371 case OMPD_master:
8372 case OMPD_critical:
8373 case OMPD_taskgroup:
8374 case OMPD_distribute:
8375 case OMPD_ordered:
8376 case OMPD_atomic:
8377 case OMPD_distribute_simd:
8378 case OMPD_teams_distribute:
8379 case OMPD_teams_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008380 case OMPD_requires:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008381 llvm_unreachable("Unexpected OpenMP directive with if-clause");
8382 case OMPD_unknown:
8383 llvm_unreachable("Unknown OpenMP directive");
8384 }
8385 break;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008386 case OMPC_num_threads:
8387 switch (DKind) {
8388 case OMPD_target_parallel:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008389 case OMPD_target_parallel_for:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00008390 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008391 CaptureRegion = OMPD_target;
8392 break;
Carlo Bertolli62fae152017-11-20 20:46:39 +00008393 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008394 case OMPD_teams_distribute_parallel_for_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008395 case OMPD_target_teams_distribute_parallel_for:
8396 case OMPD_target_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008397 CaptureRegion = OMPD_teams;
8398 break;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008399 case OMPD_parallel:
8400 case OMPD_parallel_sections:
8401 case OMPD_parallel_for:
8402 case OMPD_parallel_for_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008403 case OMPD_distribute_parallel_for:
8404 case OMPD_distribute_parallel_for_simd:
8405 // Do not capture num_threads-clause expressions.
8406 break;
8407 case OMPD_target_data:
8408 case OMPD_target_enter_data:
8409 case OMPD_target_exit_data:
8410 case OMPD_target_update:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008411 case OMPD_target:
8412 case OMPD_target_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008413 case OMPD_target_teams:
8414 case OMPD_target_teams_distribute:
8415 case OMPD_target_teams_distribute_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008416 case OMPD_cancel:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008417 case OMPD_task:
8418 case OMPD_taskloop:
8419 case OMPD_taskloop_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008420 case OMPD_threadprivate:
8421 case OMPD_taskyield:
8422 case OMPD_barrier:
8423 case OMPD_taskwait:
8424 case OMPD_cancellation_point:
8425 case OMPD_flush:
8426 case OMPD_declare_reduction:
8427 case OMPD_declare_simd:
8428 case OMPD_declare_target:
8429 case OMPD_end_declare_target:
8430 case OMPD_teams:
8431 case OMPD_simd:
8432 case OMPD_for:
8433 case OMPD_for_simd:
8434 case OMPD_sections:
8435 case OMPD_section:
8436 case OMPD_single:
8437 case OMPD_master:
8438 case OMPD_critical:
8439 case OMPD_taskgroup:
8440 case OMPD_distribute:
8441 case OMPD_ordered:
8442 case OMPD_atomic:
8443 case OMPD_distribute_simd:
8444 case OMPD_teams_distribute:
8445 case OMPD_teams_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008446 case OMPD_requires:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008447 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause");
8448 case OMPD_unknown:
8449 llvm_unreachable("Unknown OpenMP directive");
8450 }
8451 break;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008452 case OMPC_num_teams:
8453 switch (DKind) {
8454 case OMPD_target_teams:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008455 case OMPD_target_teams_distribute:
8456 case OMPD_target_teams_distribute_simd:
8457 case OMPD_target_teams_distribute_parallel_for:
8458 case OMPD_target_teams_distribute_parallel_for_simd:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008459 CaptureRegion = OMPD_target;
8460 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008461 case OMPD_teams_distribute_parallel_for:
8462 case OMPD_teams_distribute_parallel_for_simd:
8463 case OMPD_teams:
8464 case OMPD_teams_distribute:
8465 case OMPD_teams_distribute_simd:
8466 // Do not capture num_teams-clause expressions.
8467 break;
8468 case OMPD_distribute_parallel_for:
8469 case OMPD_distribute_parallel_for_simd:
8470 case OMPD_task:
8471 case OMPD_taskloop:
8472 case OMPD_taskloop_simd:
8473 case OMPD_target_data:
8474 case OMPD_target_enter_data:
8475 case OMPD_target_exit_data:
8476 case OMPD_target_update:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008477 case OMPD_cancel:
8478 case OMPD_parallel:
8479 case OMPD_parallel_sections:
8480 case OMPD_parallel_for:
8481 case OMPD_parallel_for_simd:
8482 case OMPD_target:
8483 case OMPD_target_simd:
8484 case OMPD_target_parallel:
8485 case OMPD_target_parallel_for:
8486 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008487 case OMPD_threadprivate:
8488 case OMPD_taskyield:
8489 case OMPD_barrier:
8490 case OMPD_taskwait:
8491 case OMPD_cancellation_point:
8492 case OMPD_flush:
8493 case OMPD_declare_reduction:
8494 case OMPD_declare_simd:
8495 case OMPD_declare_target:
8496 case OMPD_end_declare_target:
8497 case OMPD_simd:
8498 case OMPD_for:
8499 case OMPD_for_simd:
8500 case OMPD_sections:
8501 case OMPD_section:
8502 case OMPD_single:
8503 case OMPD_master:
8504 case OMPD_critical:
8505 case OMPD_taskgroup:
8506 case OMPD_distribute:
8507 case OMPD_ordered:
8508 case OMPD_atomic:
8509 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008510 case OMPD_requires:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008511 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
8512 case OMPD_unknown:
8513 llvm_unreachable("Unknown OpenMP directive");
8514 }
8515 break;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008516 case OMPC_thread_limit:
8517 switch (DKind) {
8518 case OMPD_target_teams:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008519 case OMPD_target_teams_distribute:
8520 case OMPD_target_teams_distribute_simd:
8521 case OMPD_target_teams_distribute_parallel_for:
8522 case OMPD_target_teams_distribute_parallel_for_simd:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008523 CaptureRegion = OMPD_target;
8524 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008525 case OMPD_teams_distribute_parallel_for:
8526 case OMPD_teams_distribute_parallel_for_simd:
8527 case OMPD_teams:
8528 case OMPD_teams_distribute:
8529 case OMPD_teams_distribute_simd:
8530 // Do not capture thread_limit-clause expressions.
8531 break;
8532 case OMPD_distribute_parallel_for:
8533 case OMPD_distribute_parallel_for_simd:
8534 case OMPD_task:
8535 case OMPD_taskloop:
8536 case OMPD_taskloop_simd:
8537 case OMPD_target_data:
8538 case OMPD_target_enter_data:
8539 case OMPD_target_exit_data:
8540 case OMPD_target_update:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008541 case OMPD_cancel:
8542 case OMPD_parallel:
8543 case OMPD_parallel_sections:
8544 case OMPD_parallel_for:
8545 case OMPD_parallel_for_simd:
8546 case OMPD_target:
8547 case OMPD_target_simd:
8548 case OMPD_target_parallel:
8549 case OMPD_target_parallel_for:
8550 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008551 case OMPD_threadprivate:
8552 case OMPD_taskyield:
8553 case OMPD_barrier:
8554 case OMPD_taskwait:
8555 case OMPD_cancellation_point:
8556 case OMPD_flush:
8557 case OMPD_declare_reduction:
8558 case OMPD_declare_simd:
8559 case OMPD_declare_target:
8560 case OMPD_end_declare_target:
8561 case OMPD_simd:
8562 case OMPD_for:
8563 case OMPD_for_simd:
8564 case OMPD_sections:
8565 case OMPD_section:
8566 case OMPD_single:
8567 case OMPD_master:
8568 case OMPD_critical:
8569 case OMPD_taskgroup:
8570 case OMPD_distribute:
8571 case OMPD_ordered:
8572 case OMPD_atomic:
8573 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008574 case OMPD_requires:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008575 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause");
8576 case OMPD_unknown:
8577 llvm_unreachable("Unknown OpenMP directive");
8578 }
8579 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008580 case OMPC_schedule:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008581 switch (DKind) {
Alexey Bataev2ba67042017-11-28 21:11:44 +00008582 case OMPD_parallel_for:
8583 case OMPD_parallel_for_simd:
Alexey Bataev7f96c372017-11-22 17:19:31 +00008584 case OMPD_distribute_parallel_for:
Alexey Bataev974acd62017-11-27 19:38:52 +00008585 case OMPD_distribute_parallel_for_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008586 case OMPD_teams_distribute_parallel_for:
8587 case OMPD_teams_distribute_parallel_for_simd:
8588 case OMPD_target_parallel_for:
8589 case OMPD_target_parallel_for_simd:
8590 case OMPD_target_teams_distribute_parallel_for:
8591 case OMPD_target_teams_distribute_parallel_for_simd:
Alexey Bataev7f96c372017-11-22 17:19:31 +00008592 CaptureRegion = OMPD_parallel;
8593 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008594 case OMPD_for:
8595 case OMPD_for_simd:
8596 // Do not capture schedule-clause expressions.
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008597 break;
8598 case OMPD_task:
8599 case OMPD_taskloop:
8600 case OMPD_taskloop_simd:
8601 case OMPD_target_data:
8602 case OMPD_target_enter_data:
8603 case OMPD_target_exit_data:
8604 case OMPD_target_update:
8605 case OMPD_teams:
8606 case OMPD_teams_distribute:
8607 case OMPD_teams_distribute_simd:
8608 case OMPD_target_teams_distribute:
8609 case OMPD_target_teams_distribute_simd:
8610 case OMPD_target:
8611 case OMPD_target_simd:
8612 case OMPD_target_parallel:
8613 case OMPD_cancel:
8614 case OMPD_parallel:
8615 case OMPD_parallel_sections:
8616 case OMPD_threadprivate:
8617 case OMPD_taskyield:
8618 case OMPD_barrier:
8619 case OMPD_taskwait:
8620 case OMPD_cancellation_point:
8621 case OMPD_flush:
8622 case OMPD_declare_reduction:
8623 case OMPD_declare_simd:
8624 case OMPD_declare_target:
8625 case OMPD_end_declare_target:
8626 case OMPD_simd:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008627 case OMPD_sections:
8628 case OMPD_section:
8629 case OMPD_single:
8630 case OMPD_master:
8631 case OMPD_critical:
8632 case OMPD_taskgroup:
8633 case OMPD_distribute:
8634 case OMPD_ordered:
8635 case OMPD_atomic:
8636 case OMPD_distribute_simd:
8637 case OMPD_target_teams:
Kelvin Li1408f912018-09-26 04:28:39 +00008638 case OMPD_requires:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008639 llvm_unreachable("Unexpected OpenMP directive with schedule clause");
8640 case OMPD_unknown:
8641 llvm_unreachable("Unknown OpenMP directive");
8642 }
8643 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008644 case OMPC_dist_schedule:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008645 switch (DKind) {
8646 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008647 case OMPD_teams_distribute_parallel_for_simd:
8648 case OMPD_teams_distribute:
8649 case OMPD_teams_distribute_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008650 case OMPD_target_teams_distribute_parallel_for:
8651 case OMPD_target_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008652 case OMPD_target_teams_distribute:
8653 case OMPD_target_teams_distribute_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008654 CaptureRegion = OMPD_teams;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008655 break;
8656 case OMPD_distribute_parallel_for:
8657 case OMPD_distribute_parallel_for_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008658 case OMPD_distribute:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008659 case OMPD_distribute_simd:
8660 // Do not capture thread_limit-clause expressions.
8661 break;
8662 case OMPD_parallel_for:
8663 case OMPD_parallel_for_simd:
8664 case OMPD_target_parallel_for_simd:
8665 case OMPD_target_parallel_for:
8666 case OMPD_task:
8667 case OMPD_taskloop:
8668 case OMPD_taskloop_simd:
8669 case OMPD_target_data:
8670 case OMPD_target_enter_data:
8671 case OMPD_target_exit_data:
8672 case OMPD_target_update:
8673 case OMPD_teams:
8674 case OMPD_target:
8675 case OMPD_target_simd:
8676 case OMPD_target_parallel:
8677 case OMPD_cancel:
8678 case OMPD_parallel:
8679 case OMPD_parallel_sections:
8680 case OMPD_threadprivate:
8681 case OMPD_taskyield:
8682 case OMPD_barrier:
8683 case OMPD_taskwait:
8684 case OMPD_cancellation_point:
8685 case OMPD_flush:
8686 case OMPD_declare_reduction:
8687 case OMPD_declare_simd:
8688 case OMPD_declare_target:
8689 case OMPD_end_declare_target:
8690 case OMPD_simd:
8691 case OMPD_for:
8692 case OMPD_for_simd:
8693 case OMPD_sections:
8694 case OMPD_section:
8695 case OMPD_single:
8696 case OMPD_master:
8697 case OMPD_critical:
8698 case OMPD_taskgroup:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008699 case OMPD_ordered:
8700 case OMPD_atomic:
8701 case OMPD_target_teams:
Kelvin Li1408f912018-09-26 04:28:39 +00008702 case OMPD_requires:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008703 llvm_unreachable("Unexpected OpenMP directive with schedule clause");
8704 case OMPD_unknown:
8705 llvm_unreachable("Unknown OpenMP directive");
8706 }
8707 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008708 case OMPC_device:
8709 switch (DKind) {
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008710 case OMPD_target_update:
Alexey Bataevfab20e42017-12-27 18:49:38 +00008711 case OMPD_target_enter_data:
8712 case OMPD_target_exit_data:
Alexey Bataev8451efa2018-01-15 19:06:12 +00008713 case OMPD_target:
Alexey Bataevf41c88f2018-01-16 15:05:16 +00008714 case OMPD_target_simd:
Alexey Bataev0c869ef2018-01-16 15:57:07 +00008715 case OMPD_target_teams:
Alexey Bataev54d5c7d2018-01-16 16:27:49 +00008716 case OMPD_target_parallel:
Alexey Bataev79df7562018-01-16 16:46:46 +00008717 case OMPD_target_teams_distribute:
Alexey Bataev8d16a432018-01-16 17:22:50 +00008718 case OMPD_target_teams_distribute_simd:
Alexey Bataev8ed895512018-01-16 17:41:04 +00008719 case OMPD_target_parallel_for:
Alexey Bataevd60d1ba2018-01-16 17:55:15 +00008720 case OMPD_target_parallel_for_simd:
Alexey Bataev9f9fb0b2018-01-16 19:02:33 +00008721 case OMPD_target_teams_distribute_parallel_for:
Alexey Bataev9350fc32018-01-16 19:18:24 +00008722 case OMPD_target_teams_distribute_parallel_for_simd:
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008723 CaptureRegion = OMPD_task;
8724 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008725 case OMPD_target_data:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008726 // Do not capture device-clause expressions.
8727 break;
8728 case OMPD_teams_distribute_parallel_for:
8729 case OMPD_teams_distribute_parallel_for_simd:
8730 case OMPD_teams:
8731 case OMPD_teams_distribute:
8732 case OMPD_teams_distribute_simd:
8733 case OMPD_distribute_parallel_for:
8734 case OMPD_distribute_parallel_for_simd:
8735 case OMPD_task:
8736 case OMPD_taskloop:
8737 case OMPD_taskloop_simd:
8738 case OMPD_cancel:
8739 case OMPD_parallel:
8740 case OMPD_parallel_sections:
8741 case OMPD_parallel_for:
8742 case OMPD_parallel_for_simd:
8743 case OMPD_threadprivate:
8744 case OMPD_taskyield:
8745 case OMPD_barrier:
8746 case OMPD_taskwait:
8747 case OMPD_cancellation_point:
8748 case OMPD_flush:
8749 case OMPD_declare_reduction:
8750 case OMPD_declare_simd:
8751 case OMPD_declare_target:
8752 case OMPD_end_declare_target:
8753 case OMPD_simd:
8754 case OMPD_for:
8755 case OMPD_for_simd:
8756 case OMPD_sections:
8757 case OMPD_section:
8758 case OMPD_single:
8759 case OMPD_master:
8760 case OMPD_critical:
8761 case OMPD_taskgroup:
8762 case OMPD_distribute:
8763 case OMPD_ordered:
8764 case OMPD_atomic:
8765 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008766 case OMPD_requires:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008767 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
8768 case OMPD_unknown:
8769 llvm_unreachable("Unknown OpenMP directive");
8770 }
8771 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008772 case OMPC_firstprivate:
8773 case OMPC_lastprivate:
8774 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008775 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008776 case OMPC_in_reduction:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008777 case OMPC_linear:
8778 case OMPC_default:
8779 case OMPC_proc_bind:
8780 case OMPC_final:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008781 case OMPC_safelen:
8782 case OMPC_simdlen:
8783 case OMPC_collapse:
8784 case OMPC_private:
8785 case OMPC_shared:
8786 case OMPC_aligned:
8787 case OMPC_copyin:
8788 case OMPC_copyprivate:
8789 case OMPC_ordered:
8790 case OMPC_nowait:
8791 case OMPC_untied:
8792 case OMPC_mergeable:
8793 case OMPC_threadprivate:
8794 case OMPC_flush:
8795 case OMPC_read:
8796 case OMPC_write:
8797 case OMPC_update:
8798 case OMPC_capture:
8799 case OMPC_seq_cst:
8800 case OMPC_depend:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008801 case OMPC_threads:
8802 case OMPC_simd:
8803 case OMPC_map:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008804 case OMPC_priority:
8805 case OMPC_grainsize:
8806 case OMPC_nogroup:
8807 case OMPC_num_tasks:
8808 case OMPC_hint:
8809 case OMPC_defaultmap:
8810 case OMPC_unknown:
8811 case OMPC_uniform:
8812 case OMPC_to:
8813 case OMPC_from:
8814 case OMPC_use_device_ptr:
8815 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00008816 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00008817 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00008818 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00008819 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00008820 case OMPC_atomic_default_mem_order:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008821 llvm_unreachable("Unexpected OpenMP clause.");
8822 }
8823 return CaptureRegion;
8824}
8825
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008826OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
8827 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008828 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008829 SourceLocation NameModifierLoc,
8830 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008831 SourceLocation EndLoc) {
8832 Expr *ValExpr = Condition;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008833 Stmt *HelperValStmt = nullptr;
8834 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008835 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
8836 !Condition->isInstantiationDependent() &&
8837 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00008838 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008839 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008840 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008841
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008842 ValExpr = Val.get();
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008843
8844 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
8845 CaptureRegion =
8846 getOpenMPCaptureRegionForClause(DKind, OMPC_if, NameModifier);
Alexey Bataev2ba67042017-11-28 21:11:44 +00008847 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008848 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00008849 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008850 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8851 HelperValStmt = buildPreInits(Context, Captures);
8852 }
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008853 }
8854
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008855 return new (Context)
8856 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
8857 LParenLoc, NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008858}
8859
Alexey Bataev3778b602014-07-17 07:32:53 +00008860OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
8861 SourceLocation StartLoc,
8862 SourceLocation LParenLoc,
8863 SourceLocation EndLoc) {
8864 Expr *ValExpr = Condition;
8865 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
8866 !Condition->isInstantiationDependent() &&
8867 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00008868 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataev3778b602014-07-17 07:32:53 +00008869 if (Val.isInvalid())
8870 return nullptr;
8871
Richard Smith03a4aa32016-06-23 19:02:52 +00008872 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataev3778b602014-07-17 07:32:53 +00008873 }
8874
8875 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8876}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00008877ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
8878 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00008879 if (!Op)
8880 return ExprError();
8881
8882 class IntConvertDiagnoser : public ICEConvertDiagnoser {
8883 public:
8884 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00008885 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00008886 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
8887 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008888 return S.Diag(Loc, diag::err_omp_not_integral) << T;
8889 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008890 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
8891 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008892 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
8893 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008894 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
8895 QualType T,
8896 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008897 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
8898 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008899 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
8900 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008901 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00008902 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00008903 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008904 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
8905 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008906 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
8907 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008908 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
8909 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008910 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00008911 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00008912 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008913 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
8914 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008915 llvm_unreachable("conversion functions are permitted");
8916 }
8917 } ConvertDiagnoser;
8918 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
8919}
8920
Alexey Bataeve3727102018-04-18 15:57:46 +00008921static bool isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00008922 OpenMPClauseKind CKind,
8923 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008924 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
8925 !ValExpr->isInstantiationDependent()) {
8926 SourceLocation Loc = ValExpr->getExprLoc();
8927 ExprResult Value =
8928 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
8929 if (Value.isInvalid())
8930 return false;
8931
8932 ValExpr = Value.get();
8933 // The expression must evaluate to a non-negative integer value.
8934 llvm::APSInt Result;
8935 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00008936 Result.isSigned() &&
8937 !((!StrictlyPositive && Result.isNonNegative()) ||
8938 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008939 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00008940 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
8941 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008942 return false;
8943 }
8944 }
8945 return true;
8946}
8947
Alexey Bataev568a8332014-03-06 06:15:19 +00008948OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
8949 SourceLocation StartLoc,
8950 SourceLocation LParenLoc,
8951 SourceLocation EndLoc) {
8952 Expr *ValExpr = NumThreads;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008953 Stmt *HelperValStmt = nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00008954
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008955 // OpenMP [2.5, Restrictions]
8956 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +00008957 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
Alexey Bataeva0569352015-12-01 10:17:31 +00008958 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008959 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00008960
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008961 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +00008962 OpenMPDirectiveKind CaptureRegion =
8963 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads);
8964 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008965 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00008966 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008967 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8968 HelperValStmt = buildPreInits(Context, Captures);
8969 }
8970
8971 return new (Context) OMPNumThreadsClause(
8972 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00008973}
8974
Alexey Bataev62c87d22014-03-21 04:51:18 +00008975ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008976 OpenMPClauseKind CKind,
8977 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00008978 if (!E)
8979 return ExprError();
8980 if (E->isValueDependent() || E->isTypeDependent() ||
8981 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00008982 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00008983 llvm::APSInt Result;
8984 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
8985 if (ICE.isInvalid())
8986 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008987 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
8988 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00008989 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008990 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
8991 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00008992 return ExprError();
8993 }
Alexander Musman09184fe2014-09-30 05:29:28 +00008994 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
8995 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
8996 << E->getSourceRange();
8997 return ExprError();
8998 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008999 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
9000 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00009001 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009002 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00009003 return ICE;
9004}
9005
9006OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
9007 SourceLocation LParenLoc,
9008 SourceLocation EndLoc) {
9009 // OpenMP [2.8.1, simd construct, Description]
9010 // The parameter of the safelen clause must be a constant
9011 // positive integer expression.
9012 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
9013 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009014 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00009015 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00009016 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00009017}
9018
Alexey Bataev66b15b52015-08-21 11:14:16 +00009019OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
9020 SourceLocation LParenLoc,
9021 SourceLocation EndLoc) {
9022 // OpenMP [2.8.1, simd construct, Description]
9023 // The parameter of the simdlen clause must be a constant
9024 // positive integer expression.
9025 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
9026 if (Simdlen.isInvalid())
9027 return nullptr;
9028 return new (Context)
9029 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
9030}
9031
Alexander Musman64d33f12014-06-04 07:53:32 +00009032OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
9033 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00009034 SourceLocation LParenLoc,
9035 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00009036 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00009037 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00009038 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00009039 // The parameter of the collapse clause must be a constant
9040 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00009041 ExprResult NumForLoopsResult =
9042 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
9043 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00009044 return nullptr;
9045 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00009046 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00009047}
9048
Alexey Bataev10e775f2015-07-30 11:36:16 +00009049OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
9050 SourceLocation EndLoc,
9051 SourceLocation LParenLoc,
9052 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00009053 // OpenMP [2.7.1, loop construct, Description]
9054 // OpenMP [2.8.1, simd construct, Description]
9055 // OpenMP [2.9.6, distribute construct, Description]
9056 // The parameter of the ordered clause must be a constant
9057 // positive integer expression if any.
9058 if (NumForLoops && LParenLoc.isValid()) {
9059 ExprResult NumForLoopsResult =
9060 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
9061 if (NumForLoopsResult.isInvalid())
9062 return nullptr;
9063 NumForLoops = NumForLoopsResult.get();
Alexey Bataeve3727102018-04-18 15:57:46 +00009064 } else {
Alexey Bataev346265e2015-09-25 10:37:12 +00009065 NumForLoops = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +00009066 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00009067 auto *Clause = OMPOrderedClause::Create(
9068 Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0,
9069 StartLoc, LParenLoc, EndLoc);
9070 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause);
9071 return Clause;
Alexey Bataev10e775f2015-07-30 11:36:16 +00009072}
9073
Alexey Bataeved09d242014-05-28 05:53:51 +00009074OMPClause *Sema::ActOnOpenMPSimpleClause(
9075 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
9076 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009077 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009078 switch (Kind) {
9079 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00009080 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00009081 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
9082 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009083 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009084 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00009085 Res = ActOnOpenMPProcBindClause(
9086 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
9087 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009088 break;
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009089 case OMPC_atomic_default_mem_order:
9090 Res = ActOnOpenMPAtomicDefaultMemOrderClause(
9091 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument),
9092 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
9093 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009094 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00009095 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00009096 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00009097 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009098 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00009099 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009100 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009101 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009102 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00009103 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00009104 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00009105 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00009106 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00009107 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00009108 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009109 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009110 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009111 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009112 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00009113 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009114 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009115 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009116 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00009117 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009118 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00009119 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00009120 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00009121 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009122 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009123 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00009124 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00009125 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009126 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00009127 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009128 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009129 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009130 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009131 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00009132 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00009133 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009134 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00009135 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009136 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009137 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009138 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00009139 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00009140 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00009141 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00009142 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00009143 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00009144 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009145 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009146 case OMPC_dynamic_allocators:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009147 llvm_unreachable("Clause is not allowed.");
9148 }
9149 return Res;
9150}
9151
Alexey Bataev6402bca2015-12-28 07:25:51 +00009152static std::string
9153getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
9154 ArrayRef<unsigned> Exclude = llvm::None) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009155 SmallString<256> Buffer;
9156 llvm::raw_svector_ostream Out(Buffer);
Alexey Bataev6402bca2015-12-28 07:25:51 +00009157 unsigned Bound = Last >= 2 ? Last - 2 : 0;
9158 unsigned Skipped = Exclude.size();
9159 auto S = Exclude.begin(), E = Exclude.end();
Alexey Bataeve3727102018-04-18 15:57:46 +00009160 for (unsigned I = First; I < Last; ++I) {
9161 if (std::find(S, E, I) != E) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00009162 --Skipped;
9163 continue;
9164 }
Alexey Bataeve3727102018-04-18 15:57:46 +00009165 Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'";
9166 if (I == Bound - Skipped)
9167 Out << " or ";
9168 else if (I != Bound + 1 - Skipped)
9169 Out << ", ";
Alexey Bataev6402bca2015-12-28 07:25:51 +00009170 }
Alexey Bataeve3727102018-04-18 15:57:46 +00009171 return Out.str();
Alexey Bataev6402bca2015-12-28 07:25:51 +00009172}
9173
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009174OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
9175 SourceLocation KindKwLoc,
9176 SourceLocation StartLoc,
9177 SourceLocation LParenLoc,
9178 SourceLocation EndLoc) {
9179 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00009180 static_assert(OMPC_DEFAULT_unknown > 0,
9181 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009182 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009183 << getListOfPossibleValues(OMPC_default, /*First=*/0,
9184 /*Last=*/OMPC_DEFAULT_unknown)
9185 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009186 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009187 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00009188 switch (Kind) {
9189 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009190 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009191 break;
9192 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009193 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009194 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009195 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009196 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00009197 break;
9198 }
Alexey Bataeved09d242014-05-28 05:53:51 +00009199 return new (Context)
9200 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009201}
9202
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009203OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
9204 SourceLocation KindKwLoc,
9205 SourceLocation StartLoc,
9206 SourceLocation LParenLoc,
9207 SourceLocation EndLoc) {
9208 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009209 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009210 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
9211 /*Last=*/OMPC_PROC_BIND_unknown)
9212 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009213 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009214 }
Alexey Bataeved09d242014-05-28 05:53:51 +00009215 return new (Context)
9216 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009217}
9218
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009219OMPClause *Sema::ActOnOpenMPAtomicDefaultMemOrderClause(
9220 OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc,
9221 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
9222 if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) {
9223 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
9224 << getListOfPossibleValues(
9225 OMPC_atomic_default_mem_order, /*First=*/0,
9226 /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown)
9227 << getOpenMPClauseName(OMPC_atomic_default_mem_order);
9228 return nullptr;
9229 }
9230 return new (Context) OMPAtomicDefaultMemOrderClause(Kind, KindKwLoc, StartLoc,
9231 LParenLoc, EndLoc);
9232}
9233
Alexey Bataev56dafe82014-06-20 07:16:17 +00009234OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00009235 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00009236 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00009237 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00009238 SourceLocation EndLoc) {
9239 OMPClause *Res = nullptr;
9240 switch (Kind) {
9241 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00009242 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
9243 assert(Argument.size() == NumberOfElements &&
9244 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009245 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00009246 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
9247 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
9248 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
9249 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
9250 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009251 break;
9252 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00009253 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
9254 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
9255 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
9256 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009257 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00009258 case OMPC_dist_schedule:
9259 Res = ActOnOpenMPDistScheduleClause(
9260 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
9261 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
9262 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009263 case OMPC_defaultmap:
9264 enum { Modifier, DefaultmapKind };
9265 Res = ActOnOpenMPDefaultmapClause(
9266 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
9267 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
David Majnemer9d168222016-08-05 17:44:54 +00009268 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind],
9269 EndLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009270 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00009271 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009272 case OMPC_num_threads:
9273 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009274 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009275 case OMPC_collapse:
9276 case OMPC_default:
9277 case OMPC_proc_bind:
9278 case OMPC_private:
9279 case OMPC_firstprivate:
9280 case OMPC_lastprivate:
9281 case OMPC_shared:
9282 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00009283 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00009284 case OMPC_in_reduction:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009285 case OMPC_linear:
9286 case OMPC_aligned:
9287 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009288 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009289 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00009290 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009291 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009292 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009293 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00009294 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009295 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00009296 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00009297 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00009298 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009299 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009300 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00009301 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00009302 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009303 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00009304 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009305 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009306 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009307 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009308 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00009309 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00009310 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009311 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009312 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009313 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00009314 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00009315 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00009316 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00009317 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00009318 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00009319 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009320 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009321 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009322 case OMPC_atomic_default_mem_order:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009323 llvm_unreachable("Clause is not allowed.");
9324 }
9325 return Res;
9326}
9327
Alexey Bataev6402bca2015-12-28 07:25:51 +00009328static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
9329 OpenMPScheduleClauseModifier M2,
9330 SourceLocation M1Loc, SourceLocation M2Loc) {
9331 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
9332 SmallVector<unsigned, 2> Excluded;
9333 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
9334 Excluded.push_back(M2);
9335 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
9336 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
9337 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
9338 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
9339 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
9340 << getListOfPossibleValues(OMPC_schedule,
9341 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
9342 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
9343 Excluded)
9344 << getOpenMPClauseName(OMPC_schedule);
9345 return true;
9346 }
9347 return false;
9348}
9349
Alexey Bataev56dafe82014-06-20 07:16:17 +00009350OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00009351 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00009352 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00009353 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
9354 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
9355 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
9356 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
9357 return nullptr;
9358 // OpenMP, 2.7.1, Loop Construct, Restrictions
9359 // Either the monotonic modifier or the nonmonotonic modifier can be specified
9360 // but not both.
9361 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
9362 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
9363 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
9364 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
9365 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
9366 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
9367 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
9368 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
9369 return nullptr;
9370 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00009371 if (Kind == OMPC_SCHEDULE_unknown) {
9372 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00009373 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
9374 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
9375 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
9376 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
9377 Exclude);
9378 } else {
9379 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
9380 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009381 }
9382 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
9383 << Values << getOpenMPClauseName(OMPC_schedule);
9384 return nullptr;
9385 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00009386 // OpenMP, 2.7.1, Loop Construct, Restrictions
9387 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
9388 // schedule(guided).
9389 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
9390 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
9391 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
9392 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
9393 diag::err_omp_schedule_nonmonotonic_static);
9394 return nullptr;
9395 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00009396 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00009397 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00009398 if (ChunkSize) {
9399 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
9400 !ChunkSize->isInstantiationDependent() &&
9401 !ChunkSize->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00009402 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
Alexey Bataev56dafe82014-06-20 07:16:17 +00009403 ExprResult Val =
9404 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
9405 if (Val.isInvalid())
9406 return nullptr;
9407
9408 ValExpr = Val.get();
9409
9410 // OpenMP [2.7.1, Restrictions]
9411 // chunk_size must be a loop invariant integer expression with a positive
9412 // value.
9413 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00009414 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
9415 if (Result.isSigned() && !Result.isStrictlyPositive()) {
9416 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00009417 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00009418 return nullptr;
9419 }
Alexey Bataev2ba67042017-11-28 21:11:44 +00009420 } else if (getOpenMPCaptureRegionForClause(
9421 DSAStack->getCurrentDirective(), OMPC_schedule) !=
9422 OMPD_unknown &&
Alexey Bataevb46cdea2016-06-15 11:20:48 +00009423 !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00009424 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00009425 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev5a3af132016-03-29 08:58:54 +00009426 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
9427 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009428 }
9429 }
9430 }
9431
Alexey Bataev6402bca2015-12-28 07:25:51 +00009432 return new (Context)
9433 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00009434 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009435}
9436
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009437OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
9438 SourceLocation StartLoc,
9439 SourceLocation EndLoc) {
9440 OMPClause *Res = nullptr;
9441 switch (Kind) {
9442 case OMPC_ordered:
9443 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
9444 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00009445 case OMPC_nowait:
9446 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
9447 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009448 case OMPC_untied:
9449 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
9450 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009451 case OMPC_mergeable:
9452 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
9453 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009454 case OMPC_read:
9455 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
9456 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00009457 case OMPC_write:
9458 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
9459 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00009460 case OMPC_update:
9461 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
9462 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00009463 case OMPC_capture:
9464 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
9465 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009466 case OMPC_seq_cst:
9467 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
9468 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00009469 case OMPC_threads:
9470 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
9471 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009472 case OMPC_simd:
9473 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
9474 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00009475 case OMPC_nogroup:
9476 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
9477 break;
Kelvin Li1408f912018-09-26 04:28:39 +00009478 case OMPC_unified_address:
9479 Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc);
9480 break;
Patrick Lyster4a370b92018-10-01 13:47:43 +00009481 case OMPC_unified_shared_memory:
9482 Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
9483 break;
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009484 case OMPC_reverse_offload:
9485 Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc);
9486 break;
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009487 case OMPC_dynamic_allocators:
9488 Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc);
9489 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009490 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00009491 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009492 case OMPC_num_threads:
9493 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009494 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009495 case OMPC_collapse:
9496 case OMPC_schedule:
9497 case OMPC_private:
9498 case OMPC_firstprivate:
9499 case OMPC_lastprivate:
9500 case OMPC_shared:
9501 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00009502 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00009503 case OMPC_in_reduction:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009504 case OMPC_linear:
9505 case OMPC_aligned:
9506 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009507 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009508 case OMPC_default:
9509 case OMPC_proc_bind:
9510 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00009511 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009512 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00009513 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00009514 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009515 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009516 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009517 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009518 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00009519 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009520 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00009521 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009522 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009523 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009524 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00009525 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00009526 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00009527 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00009528 case OMPC_is_device_ptr:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009529 case OMPC_atomic_default_mem_order:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009530 llvm_unreachable("Clause is not allowed.");
9531 }
9532 return Res;
9533}
9534
Alexey Bataev236070f2014-06-20 11:19:47 +00009535OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
9536 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00009537 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00009538 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
9539}
9540
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009541OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
9542 SourceLocation EndLoc) {
9543 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
9544}
9545
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009546OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
9547 SourceLocation EndLoc) {
9548 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
9549}
9550
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009551OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
9552 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009553 return new (Context) OMPReadClause(StartLoc, EndLoc);
9554}
9555
Alexey Bataevdea47612014-07-23 07:46:59 +00009556OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
9557 SourceLocation EndLoc) {
9558 return new (Context) OMPWriteClause(StartLoc, EndLoc);
9559}
9560
Alexey Bataev67a4f222014-07-23 10:25:33 +00009561OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
9562 SourceLocation EndLoc) {
9563 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
9564}
9565
Alexey Bataev459dec02014-07-24 06:46:57 +00009566OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
9567 SourceLocation EndLoc) {
9568 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
9569}
9570
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009571OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
9572 SourceLocation EndLoc) {
9573 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
9574}
9575
Alexey Bataev346265e2015-09-25 10:37:12 +00009576OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
9577 SourceLocation EndLoc) {
9578 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
9579}
9580
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009581OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
9582 SourceLocation EndLoc) {
9583 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
9584}
9585
Alexey Bataevb825de12015-12-07 10:51:44 +00009586OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
9587 SourceLocation EndLoc) {
9588 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
9589}
9590
Kelvin Li1408f912018-09-26 04:28:39 +00009591OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc,
9592 SourceLocation EndLoc) {
9593 return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc);
9594}
9595
Patrick Lyster4a370b92018-10-01 13:47:43 +00009596OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc,
9597 SourceLocation EndLoc) {
9598 return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
9599}
9600
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009601OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc,
9602 SourceLocation EndLoc) {
9603 return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc);
9604}
9605
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009606OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc,
9607 SourceLocation EndLoc) {
9608 return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc);
9609}
9610
Alexey Bataevc5e02582014-06-16 07:08:35 +00009611OMPClause *Sema::ActOnOpenMPVarListClause(
9612 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
9613 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
9614 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009615 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Kelvin Lief579432018-12-18 22:18:41 +00009616 OpenMPLinearClauseKind LinKind,
9617 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers,
9618 ArrayRef<SourceLocation> MapTypeModifiersLoc,
Samuel Antao23abd722016-01-19 20:40:49 +00009619 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
9620 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009621 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009622 switch (Kind) {
9623 case OMPC_private:
9624 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9625 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009626 case OMPC_firstprivate:
9627 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9628 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00009629 case OMPC_lastprivate:
9630 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9631 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00009632 case OMPC_shared:
9633 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
9634 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009635 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00009636 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9637 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00009638 break;
Alexey Bataev169d96a2017-07-18 20:17:46 +00009639 case OMPC_task_reduction:
9640 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9641 EndLoc, ReductionIdScopeSpec,
9642 ReductionId);
9643 break;
Alexey Bataevfa312f32017-07-21 18:48:21 +00009644 case OMPC_in_reduction:
9645 Res =
9646 ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9647 EndLoc, ReductionIdScopeSpec, ReductionId);
9648 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00009649 case OMPC_linear:
9650 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00009651 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00009652 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009653 case OMPC_aligned:
9654 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
9655 ColonLoc, EndLoc);
9656 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009657 case OMPC_copyin:
9658 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
9659 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009660 case OMPC_copyprivate:
9661 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9662 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00009663 case OMPC_flush:
9664 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
9665 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009666 case OMPC_depend:
David Majnemer9d168222016-08-05 17:44:54 +00009667 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
Kelvin Li0bff7af2015-11-23 05:32:03 +00009668 StartLoc, LParenLoc, EndLoc);
9669 break;
9670 case OMPC_map:
Kelvin Lief579432018-12-18 22:18:41 +00009671 Res = ActOnOpenMPMapClause(MapTypeModifiers, MapTypeModifiersLoc, MapType,
9672 IsMapTypeImplicit, DepLinMapLoc, ColonLoc,
9673 VarList, StartLoc, LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009674 break;
Samuel Antao661c0902016-05-26 17:39:58 +00009675 case OMPC_to:
9676 Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc);
9677 break;
Samuel Antaoec172c62016-05-26 17:49:04 +00009678 case OMPC_from:
9679 Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc);
9680 break;
Carlo Bertolli2404b172016-07-13 15:37:16 +00009681 case OMPC_use_device_ptr:
9682 Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
9683 break;
Carlo Bertolli70594e92016-07-13 17:16:49 +00009684 case OMPC_is_device_ptr:
9685 Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
9686 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009687 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00009688 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00009689 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00009690 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009691 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00009692 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009693 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009694 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009695 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009696 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00009697 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009698 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009699 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009700 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009701 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00009702 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00009703 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00009704 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009705 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00009706 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00009707 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009708 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009709 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009710 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009711 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009712 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00009713 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00009714 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009715 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00009716 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009717 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009718 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009719 case OMPC_uniform:
Kelvin Li1408f912018-09-26 04:28:39 +00009720 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00009721 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009722 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009723 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009724 case OMPC_atomic_default_mem_order:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009725 llvm_unreachable("Clause is not allowed.");
9726 }
9727 return Res;
9728}
9729
Alexey Bataev90c228f2016-02-08 09:29:13 +00009730ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00009731 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00009732 ExprResult Res = BuildDeclRefExpr(
9733 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
9734 if (!Res.isUsable())
9735 return ExprError();
9736 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
9737 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
9738 if (!Res.isUsable())
9739 return ExprError();
9740 }
9741 if (VK != VK_LValue && Res.get()->isGLValue()) {
9742 Res = DefaultLvalueConversion(Res.get());
9743 if (!Res.isUsable())
9744 return ExprError();
9745 }
9746 return Res;
9747}
9748
Alexey Bataev60da77e2016-02-29 05:54:20 +00009749static std::pair<ValueDecl *, bool>
9750getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
9751 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00009752 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
9753 RefExpr->containsUnexpandedParameterPack())
9754 return std::make_pair(nullptr, true);
9755
Alexey Bataevd985eda2016-02-10 11:29:16 +00009756 // OpenMP [3.1, C/C++]
9757 // A list item is a variable name.
9758 // OpenMP [2.9.3.3, Restrictions, p.1]
9759 // A variable that is part of another variable (as an array or
9760 // structure element) cannot appear in a private clause.
9761 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009762 enum {
9763 NoArrayExpr = -1,
9764 ArraySubscript = 0,
9765 OMPArraySection = 1
9766 } IsArrayExpr = NoArrayExpr;
9767 if (AllowArraySection) {
9768 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009769 Expr *Base = ASE->getBase()->IgnoreParenImpCasts();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009770 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
9771 Base = TempASE->getBase()->IgnoreParenImpCasts();
9772 RefExpr = Base;
9773 IsArrayExpr = ArraySubscript;
9774 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009775 Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009776 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
9777 Base = TempOASE->getBase()->IgnoreParenImpCasts();
9778 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
9779 Base = TempASE->getBase()->IgnoreParenImpCasts();
9780 RefExpr = Base;
9781 IsArrayExpr = OMPArraySection;
9782 }
9783 }
9784 ELoc = RefExpr->getExprLoc();
9785 ERange = RefExpr->getSourceRange();
9786 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00009787 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
9788 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
9789 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
9790 (S.getCurrentThisType().isNull() || !ME ||
9791 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
9792 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009793 if (IsArrayExpr != NoArrayExpr) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009794 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
9795 << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +00009796 } else {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009797 S.Diag(ELoc,
9798 AllowArraySection
9799 ? diag::err_omp_expected_var_name_member_expr_or_array_item
9800 : diag::err_omp_expected_var_name_member_expr)
9801 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
9802 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009803 return std::make_pair(nullptr, false);
9804 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +00009805 return std::make_pair(
9806 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009807}
9808
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009809OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
9810 SourceLocation StartLoc,
9811 SourceLocation LParenLoc,
9812 SourceLocation EndLoc) {
9813 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00009814 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeve3727102018-04-18 15:57:46 +00009815 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +00009816 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009817 SourceLocation ELoc;
9818 SourceRange ERange;
9819 Expr *SimpleRefExpr = RefExpr;
9820 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009821 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009822 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009823 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009824 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009825 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009826 ValueDecl *D = Res.first;
9827 if (!D)
9828 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009829
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009830 QualType Type = D->getType();
9831 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009832
9833 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9834 // A variable that appears in a private clause must not have an incomplete
9835 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009836 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009837 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009838 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009839
Joel E. Dennye6234d1422019-01-04 22:11:31 +00009840 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
9841 // A variable that is privatized must not have a const-qualified type
9842 // unless it is of class type with a mutable member. This restriction does
9843 // not apply to the firstprivate clause.
9844 //
9845 // OpenMP 3.1 [2.9.3.3, private clause, Restrictions]
9846 // A variable that appears in a private clause must not have a
9847 // const-qualified type unless it is of class type with a mutable member.
Joel E. Dennyd2649292019-01-04 22:11:56 +00009848 if (rejectConstNotMutableType(*this, D, Type, OMPC_private, ELoc))
Joel E. Dennye6234d1422019-01-04 22:11:31 +00009849 continue;
9850
Alexey Bataev758e55e2013-09-06 18:03:48 +00009851 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9852 // in a Construct]
9853 // Variables with the predetermined data-sharing attributes may not be
9854 // listed in data-sharing attributes clauses, except for the cases
9855 // listed below. For these exceptions only, listing a predetermined
9856 // variable in a data-sharing attribute clause is allowed and overrides
9857 // the variable's predetermined data-sharing attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +00009858 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009859 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00009860 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9861 << getOpenMPClauseName(OMPC_private);
Alexey Bataeve3727102018-04-18 15:57:46 +00009862 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009863 continue;
9864 }
9865
Alexey Bataeve3727102018-04-18 15:57:46 +00009866 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009867 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00009868 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Kelvin Libf594a52016-12-17 05:48:59 +00009869 isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009870 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
9871 << getOpenMPClauseName(OMPC_private) << Type
Kelvin Libf594a52016-12-17 05:48:59 +00009872 << getOpenMPDirectiveName(CurrDir);
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009873 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009874 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009875 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009876 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009877 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009878 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009879 continue;
9880 }
9881
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009882 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
9883 // A list item cannot appear in both a map clause and a data-sharing
9884 // attribute clause on the same construct
Alexey Bataeve3727102018-04-18 15:57:46 +00009885 if (isOpenMPTargetExecutionDirective(CurrDir)) {
Samuel Antao6890b092016-07-28 14:25:09 +00009886 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +00009887 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +00009888 VD, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +00009889 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
9890 OpenMPClauseKind WhereFoundClauseKind) -> bool {
9891 ConflictKind = WhereFoundClauseKind;
9892 return true;
9893 })) {
9894 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009895 << getOpenMPClauseName(OMPC_private)
Samuel Antao6890b092016-07-28 14:25:09 +00009896 << getOpenMPClauseName(ConflictKind)
Kelvin Libf594a52016-12-17 05:48:59 +00009897 << getOpenMPDirectiveName(CurrDir);
Alexey Bataeve3727102018-04-18 15:57:46 +00009898 reportOriginalDsa(*this, DSAStack, D, DVar);
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009899 continue;
9900 }
9901 }
9902
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009903 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
9904 // A variable of class type (or array thereof) that appears in a private
9905 // clause requires an accessible, unambiguous default constructor for the
9906 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00009907 // Generate helper private variable and initialize it with the default
9908 // value. The address of the original variable is replaced by the address of
9909 // the new private variable in CodeGen. This new variable is not added to
9910 // IdResolver, so the code in the OpenMP region uses original variable for
9911 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009912 Type = Type.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +00009913 VarDecl *VDPrivate =
Alexey Bataev63cc8e92018-03-20 14:45:59 +00009914 buildVarDecl(*this, ELoc, Type, D->getName(),
9915 D->hasAttrs() ? &D->getAttrs() : nullptr,
9916 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Richard Smith3beb7c62017-01-12 02:27:38 +00009917 ActOnUninitializedDecl(VDPrivate);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009918 if (VDPrivate->isInvalidDecl())
9919 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00009920 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009921 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009922
Alexey Bataev90c228f2016-02-08 09:29:13 +00009923 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009924 if (!VD && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00009925 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00009926 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009927 Vars.push_back((VD || CurContext->isDependentContext())
9928 ? RefExpr->IgnoreParens()
9929 : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009930 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009931 }
9932
Alexey Bataeved09d242014-05-28 05:53:51 +00009933 if (Vars.empty())
9934 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009935
Alexey Bataev03b340a2014-10-21 03:16:40 +00009936 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
9937 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009938}
9939
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009940namespace {
9941class DiagsUninitializedSeveretyRAII {
9942private:
9943 DiagnosticsEngine &Diags;
9944 SourceLocation SavedLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00009945 bool IsIgnored = false;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009946
9947public:
9948 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
9949 bool IsIgnored)
9950 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
9951 if (!IsIgnored) {
9952 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
9953 /*Map*/ diag::Severity::Ignored, Loc);
9954 }
9955 }
9956 ~DiagsUninitializedSeveretyRAII() {
9957 if (!IsIgnored)
9958 Diags.popMappings(SavedLoc);
9959 }
9960};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00009961}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009962
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009963OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
9964 SourceLocation StartLoc,
9965 SourceLocation LParenLoc,
9966 SourceLocation EndLoc) {
9967 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009968 SmallVector<Expr *, 8> PrivateCopies;
9969 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00009970 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009971 bool IsImplicitClause =
9972 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
Alexey Bataeve3727102018-04-18 15:57:46 +00009973 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009974
Alexey Bataeve3727102018-04-18 15:57:46 +00009975 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +00009976 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009977 SourceLocation ELoc;
9978 SourceRange ERange;
9979 Expr *SimpleRefExpr = RefExpr;
9980 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009981 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009982 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009983 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009984 PrivateCopies.push_back(nullptr);
9985 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009986 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009987 ValueDecl *D = Res.first;
9988 if (!D)
9989 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009990
Alexey Bataev60da77e2016-02-29 05:54:20 +00009991 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00009992 QualType Type = D->getType();
9993 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009994
9995 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9996 // A variable that appears in a private clause must not have an incomplete
9997 // type or a reference type.
9998 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00009999 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010000 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010001 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010002
10003 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
10004 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +000010005 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010006 // class type.
Alexey Bataeve3727102018-04-18 15:57:46 +000010007 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010008
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010009 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +000010010 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010011 if (!IsImplicitClause) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010012 DSAStackTy::DSAVarData DVar =
10013 DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +000010014 TopDVar = DVar;
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010015 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010016 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010017 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
10018 // A list item that specifies a given variable may not appear in more
10019 // than one clause on the same directive, except that a variable may be
10020 // specified in both firstprivate and lastprivate clauses.
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010021 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
10022 // A list item may appear in a firstprivate or lastprivate clause but not
10023 // both.
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010024 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevb358f992017-12-01 17:40:15 +000010025 (isOpenMPDistributeDirective(CurrDir) ||
10026 DVar.CKind != OMPC_lastprivate) &&
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010027 DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010028 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +000010029 << getOpenMPClauseName(DVar.CKind)
10030 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000010031 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010032 continue;
10033 }
10034
10035 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
10036 // in a Construct]
10037 // Variables with the predetermined data-sharing attributes may not be
10038 // listed in data-sharing attributes clauses, except for the cases
10039 // listed below. For these exceptions only, listing a predetermined
10040 // variable in a data-sharing attribute clause is allowed and overrides
10041 // the variable's predetermined data-sharing attributes.
10042 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
10043 // in a Construct, C/C++, p.2]
10044 // Variables with const-qualified type having no mutable member may be
10045 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +000010046 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010047 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
10048 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +000010049 << getOpenMPClauseName(DVar.CKind)
10050 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000010051 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010052 continue;
10053 }
10054
10055 // OpenMP [2.9.3.4, Restrictions, p.2]
10056 // A list item that is private within a parallel region must not appear
10057 // in a firstprivate clause on a worksharing construct if any of the
10058 // worksharing regions arising from the worksharing construct ever bind
10059 // to any of the parallel regions arising from the parallel construct.
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010060 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
10061 // A list item that is private within a teams region must not appear in a
10062 // firstprivate clause on a distribute construct if any of the distribute
10063 // regions arising from the distribute construct ever bind to any of the
10064 // teams regions arising from the teams construct.
10065 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
10066 // A list item that appears in a reduction clause of a teams construct
10067 // must not appear in a firstprivate clause on a distribute construct if
10068 // any of the distribute regions arising from the distribute construct
10069 // ever bind to any of the teams regions arising from the teams construct.
10070 if ((isOpenMPWorksharingDirective(CurrDir) ||
10071 isOpenMPDistributeDirective(CurrDir)) &&
Kelvin Li579e41c2016-11-30 23:51:03 +000010072 !isOpenMPParallelDirective(CurrDir) &&
10073 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +000010074 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010075 if (DVar.CKind != OMPC_shared &&
10076 (isOpenMPParallelDirective(DVar.DKind) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010077 isOpenMPTeamsDirective(DVar.DKind) ||
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010078 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +000010079 Diag(ELoc, diag::err_omp_required_access)
10080 << getOpenMPClauseName(OMPC_firstprivate)
10081 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +000010082 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +000010083 continue;
10084 }
10085 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010086 // OpenMP [2.9.3.4, Restrictions, p.3]
10087 // A list item that appears in a reduction clause of a parallel construct
10088 // must not appear in a firstprivate clause on a worksharing or task
10089 // construct if any of the worksharing or task regions arising from the
10090 // worksharing or task construct ever bind to any of the parallel regions
10091 // arising from the parallel construct.
10092 // OpenMP [2.9.3.4, Restrictions, p.4]
10093 // A list item that appears in a reduction clause in worksharing
10094 // construct must not appear in a firstprivate clause in a task construct
10095 // encountered during execution of any of the worksharing regions arising
10096 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +000010097 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010098 DVar = DSAStack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +000010099 D, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
10100 [](OpenMPDirectiveKind K) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010101 return isOpenMPParallelDirective(K) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010102 isOpenMPWorksharingDirective(K) ||
10103 isOpenMPTeamsDirective(K);
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010104 },
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010105 /*FromParent=*/true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010106 if (DVar.CKind == OMPC_reduction &&
10107 (isOpenMPParallelDirective(DVar.DKind) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010108 isOpenMPWorksharingDirective(DVar.DKind) ||
10109 isOpenMPTeamsDirective(DVar.DKind))) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010110 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
10111 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataeve3727102018-04-18 15:57:46 +000010112 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010113 continue;
10114 }
10115 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +000010116
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010117 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
10118 // A list item cannot appear in both a map clause and a data-sharing
10119 // attribute clause on the same construct
Alexey Bataevb358f992017-12-01 17:40:15 +000010120 if (isOpenMPTargetExecutionDirective(CurrDir)) {
Samuel Antao6890b092016-07-28 14:25:09 +000010121 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +000010122 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +000010123 VD, /*CurrentRegionOnly=*/true,
Alexey Bataeve3727102018-04-18 15:57:46 +000010124 [&ConflictKind](
10125 OMPClauseMappableExprCommon::MappableExprComponentListRef,
10126 OpenMPClauseKind WhereFoundClauseKind) {
Samuel Antao6890b092016-07-28 14:25:09 +000010127 ConflictKind = WhereFoundClauseKind;
10128 return true;
10129 })) {
10130 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010131 << getOpenMPClauseName(OMPC_firstprivate)
Samuel Antao6890b092016-07-28 14:25:09 +000010132 << getOpenMPClauseName(ConflictKind)
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010133 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000010134 reportOriginalDsa(*this, DSAStack, D, DVar);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010135 continue;
10136 }
10137 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010138 }
10139
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010140 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000010141 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +000010142 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010143 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
10144 << getOpenMPClauseName(OMPC_firstprivate) << Type
10145 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
10146 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +000010147 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010148 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +000010149 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010150 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +000010151 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010152 continue;
10153 }
10154
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010155 Type = Type.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010156 VarDecl *VDPrivate =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000010157 buildVarDecl(*this, ELoc, Type, D->getName(),
10158 D->hasAttrs() ? &D->getAttrs() : nullptr,
10159 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010160 // Generate helper private variable and initialize it with the value of the
10161 // original variable. The address of the original variable is replaced by
10162 // the address of the new private variable in the CodeGen. This new variable
10163 // is not added to IdResolver, so the code in the OpenMP region uses
10164 // original variable for proper diagnostics and variable capturing.
10165 Expr *VDInitRefExpr = nullptr;
10166 // For arrays generate initializer for single element and replace it by the
10167 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010168 if (Type->isArrayType()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010169 VarDecl *VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +000010170 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010171 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataeve3727102018-04-18 15:57:46 +000010172 Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010173 ElemType = ElemType.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010174 VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
10175 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +000010176 InitializedEntity Entity =
10177 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010178 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
10179
10180 InitializationSequence InitSeq(*this, Entity, Kind, Init);
10181 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
10182 if (Result.isInvalid())
10183 VDPrivate->setInvalidDecl();
10184 else
10185 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010186 // Remove temp variable declaration.
10187 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010188 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +000010189 VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
10190 ".firstprivate.temp");
Alexey Bataevd985eda2016-02-10 11:29:16 +000010191 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
10192 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +000010193 AddInitializerToDecl(VDPrivate,
10194 DefaultLvalueConversion(VDInitRefExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000010195 /*DirectInit=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010196 }
10197 if (VDPrivate->isInvalidDecl()) {
10198 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +000010199 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010200 diag::note_omp_task_predetermined_firstprivate_here);
10201 }
10202 continue;
10203 }
10204 CurContext->addDecl(VDPrivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000010205 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +000010206 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
10207 RefExpr->getExprLoc());
10208 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010209 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010210 if (TopDVar.CKind == OMPC_lastprivate) {
Alexey Bataev005248a2016-02-25 05:25:57 +000010211 Ref = TopDVar.PrivateCopy;
Alexey Bataeve3727102018-04-18 15:57:46 +000010212 } else {
Alexey Bataev61205072016-03-02 04:57:40 +000010213 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataeve3727102018-04-18 15:57:46 +000010214 if (!isOpenMPCapturedDecl(D))
Alexey Bataev005248a2016-02-25 05:25:57 +000010215 ExprCaptures.push_back(Ref->getDecl());
10216 }
Alexey Bataev417089f2016-02-17 13:19:37 +000010217 }
Alexey Bataevd985eda2016-02-10 11:29:16 +000010218 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010219 Vars.push_back((VD || CurContext->isDependentContext())
10220 ? RefExpr->IgnoreParens()
10221 : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010222 PrivateCopies.push_back(VDPrivateRefExpr);
10223 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010224 }
10225
Alexey Bataeved09d242014-05-28 05:53:51 +000010226 if (Vars.empty())
10227 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010228
10229 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +000010230 Vars, PrivateCopies, Inits,
10231 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010232}
10233
Alexander Musman1bb328c2014-06-04 13:06:39 +000010234OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
10235 SourceLocation StartLoc,
10236 SourceLocation LParenLoc,
10237 SourceLocation EndLoc) {
10238 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +000010239 SmallVector<Expr *, 8> SrcExprs;
10240 SmallVector<Expr *, 8> DstExprs;
10241 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +000010242 SmallVector<Decl *, 4> ExprCaptures;
10243 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeve3727102018-04-18 15:57:46 +000010244 for (Expr *RefExpr : VarList) {
Alexander Musman1bb328c2014-06-04 13:06:39 +000010245 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +000010246 SourceLocation ELoc;
10247 SourceRange ERange;
10248 Expr *SimpleRefExpr = RefExpr;
10249 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +000010250 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +000010251 // It will be analyzed later.
10252 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +000010253 SrcExprs.push_back(nullptr);
10254 DstExprs.push_back(nullptr);
10255 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010256 }
Alexey Bataev74caaf22016-02-20 04:09:36 +000010257 ValueDecl *D = Res.first;
10258 if (!D)
10259 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +000010260
Alexey Bataev74caaf22016-02-20 04:09:36 +000010261 QualType Type = D->getType();
10262 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010263
10264 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
10265 // A variable that appears in a lastprivate clause must not have an
10266 // incomplete type or a reference type.
10267 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +000010268 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +000010269 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010270 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +000010271
Joel E. Dennye6234d1422019-01-04 22:11:31 +000010272 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
10273 // A variable that is privatized must not have a const-qualified type
10274 // unless it is of class type with a mutable member. This restriction does
10275 // not apply to the firstprivate clause.
10276 //
10277 // OpenMP 3.1 [2.9.3.5, lastprivate clause, Restrictions]
10278 // A variable that appears in a lastprivate clause must not have a
10279 // const-qualified type unless it is of class type with a mutable member.
Joel E. Dennyd2649292019-01-04 22:11:56 +000010280 if (rejectConstNotMutableType(*this, D, Type, OMPC_lastprivate, ELoc))
Joel E. Dennye6234d1422019-01-04 22:11:31 +000010281 continue;
10282
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010283 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexander Musman1bb328c2014-06-04 13:06:39 +000010284 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
10285 // in a Construct]
10286 // Variables with the predetermined data-sharing attributes may not be
10287 // listed in data-sharing attributes clauses, except for the cases
10288 // listed below.
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010289 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
10290 // A list item may appear in a firstprivate or lastprivate clause but not
10291 // both.
Alexey Bataeve3727102018-04-18 15:57:46 +000010292 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010293 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
Alexey Bataevb358f992017-12-01 17:40:15 +000010294 (isOpenMPDistributeDirective(CurrDir) ||
10295 DVar.CKind != OMPC_firstprivate) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +000010296 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
10297 Diag(ELoc, diag::err_omp_wrong_dsa)
10298 << getOpenMPClauseName(DVar.CKind)
10299 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000010300 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010301 continue;
10302 }
10303
Alexey Bataevf29276e2014-06-18 04:14:57 +000010304 // OpenMP [2.14.3.5, Restrictions, p.2]
10305 // A list item that is private within a parallel region, or that appears in
10306 // the reduction clause of a parallel construct, must not appear in a
10307 // lastprivate clause on a worksharing construct if any of the corresponding
10308 // worksharing regions ever binds to any of the corresponding parallel
10309 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +000010310 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +000010311 if (isOpenMPWorksharingDirective(CurrDir) &&
Kelvin Li579e41c2016-11-30 23:51:03 +000010312 !isOpenMPParallelDirective(CurrDir) &&
10313 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +000010314 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +000010315 if (DVar.CKind != OMPC_shared) {
10316 Diag(ELoc, diag::err_omp_required_access)
10317 << getOpenMPClauseName(OMPC_lastprivate)
10318 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +000010319 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +000010320 continue;
10321 }
10322 }
Alexey Bataev74caaf22016-02-20 04:09:36 +000010323
Alexander Musman1bb328c2014-06-04 13:06:39 +000010324 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +000010325 // A variable of class type (or array thereof) that appears in a
10326 // lastprivate clause requires an accessible, unambiguous default
10327 // constructor for the class type, unless the list item is also specified
10328 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +000010329 // A variable of class type (or array thereof) that appears in a
10330 // lastprivate clause requires an accessible, unambiguous copy assignment
10331 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +000010332 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010333 VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(),
10334 Type.getUnqualifiedType(), ".lastprivate.src",
10335 D->hasAttrs() ? &D->getAttrs() : nullptr);
10336 DeclRefExpr *PseudoSrcExpr =
Alexey Bataev74caaf22016-02-20 04:09:36 +000010337 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataeve3727102018-04-18 15:57:46 +000010338 VarDecl *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +000010339 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +000010340 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000010341 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +000010342 // For arrays generate assignment operation for single element and replace
10343 // it by the original array element in CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +000010344 ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
10345 PseudoDstExpr, PseudoSrcExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +000010346 if (AssignmentOp.isInvalid())
10347 continue;
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000010348 AssignmentOp =
10349 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataev38e89532015-04-16 04:54:05 +000010350 if (AssignmentOp.isInvalid())
10351 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +000010352
Alexey Bataev74caaf22016-02-20 04:09:36 +000010353 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010354 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010355 if (TopDVar.CKind == OMPC_firstprivate) {
Alexey Bataev005248a2016-02-25 05:25:57 +000010356 Ref = TopDVar.PrivateCopy;
Alexey Bataeve3727102018-04-18 15:57:46 +000010357 } else {
Alexey Bataev61205072016-03-02 04:57:40 +000010358 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000010359 if (!isOpenMPCapturedDecl(D))
Alexey Bataev005248a2016-02-25 05:25:57 +000010360 ExprCaptures.push_back(Ref->getDecl());
10361 }
10362 if (TopDVar.CKind == OMPC_firstprivate ||
Alexey Bataeve3727102018-04-18 15:57:46 +000010363 (!isOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010364 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +000010365 ExprResult RefRes = DefaultLvalueConversion(Ref);
10366 if (!RefRes.isUsable())
10367 continue;
10368 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +000010369 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
10370 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +000010371 if (!PostUpdateRes.isUsable())
10372 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +000010373 ExprPostUpdates.push_back(
10374 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +000010375 }
10376 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010377 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010378 Vars.push_back((VD || CurContext->isDependentContext())
10379 ? RefExpr->IgnoreParens()
10380 : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +000010381 SrcExprs.push_back(PseudoSrcExpr);
10382 DstExprs.push_back(PseudoDstExpr);
10383 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +000010384 }
10385
10386 if (Vars.empty())
10387 return nullptr;
10388
10389 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +000010390 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +000010391 buildPreInits(Context, ExprCaptures),
10392 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +000010393}
10394
Alexey Bataev758e55e2013-09-06 18:03:48 +000010395OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
10396 SourceLocation StartLoc,
10397 SourceLocation LParenLoc,
10398 SourceLocation EndLoc) {
10399 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +000010400 for (Expr *RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010401 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +000010402 SourceLocation ELoc;
10403 SourceRange ERange;
10404 Expr *SimpleRefExpr = RefExpr;
10405 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010406 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +000010407 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000010408 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010409 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010410 ValueDecl *D = Res.first;
10411 if (!D)
10412 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +000010413
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010414 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010415 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
10416 // in a Construct]
10417 // Variables with the predetermined data-sharing attributes may not be
10418 // listed in data-sharing attributes clauses, except for the cases
10419 // listed below. For these exceptions only, listing a predetermined
10420 // variable in a data-sharing attribute clause is allowed and overrides
10421 // the variable's predetermined data-sharing attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +000010422 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataeved09d242014-05-28 05:53:51 +000010423 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
10424 DVar.RefExpr) {
10425 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
10426 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +000010427 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010428 continue;
10429 }
10430
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010431 DeclRefExpr *Ref = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000010432 if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +000010433 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010434 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010435 Vars.push_back((VD || !Ref || CurContext->isDependentContext())
10436 ? RefExpr->IgnoreParens()
10437 : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010438 }
10439
Alexey Bataeved09d242014-05-28 05:53:51 +000010440 if (Vars.empty())
10441 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +000010442
10443 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
10444}
10445
Alexey Bataevc5e02582014-06-16 07:08:35 +000010446namespace {
10447class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
10448 DSAStackTy *Stack;
10449
10450public:
10451 bool VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010452 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
10453 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataevc5e02582014-06-16 07:08:35 +000010454 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
10455 return false;
10456 if (DVar.CKind != OMPC_unknown)
10457 return true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010458 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +000010459 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) { return true; },
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010460 /*FromParent=*/true);
Alexey Bataeve3727102018-04-18 15:57:46 +000010461 return DVarPrivate.CKind != OMPC_unknown;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010462 }
10463 return false;
10464 }
10465 bool VisitStmt(Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010466 for (Stmt *Child : S->children()) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010467 if (Child && Visit(Child))
10468 return true;
10469 }
10470 return false;
10471 }
Alexey Bataev23b69422014-06-18 07:08:49 +000010472 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +000010473};
Alexey Bataev23b69422014-06-18 07:08:49 +000010474} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +000010475
Alexey Bataev60da77e2016-02-29 05:54:20 +000010476namespace {
10477// Transform MemberExpression for specified FieldDecl of current class to
10478// DeclRefExpr to specified OMPCapturedExprDecl.
10479class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
10480 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
Alexey Bataeve3727102018-04-18 15:57:46 +000010481 ValueDecl *Field = nullptr;
10482 DeclRefExpr *CapturedExpr = nullptr;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010483
10484public:
10485 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
10486 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
10487
10488 ExprResult TransformMemberExpr(MemberExpr *E) {
10489 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
10490 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +000010491 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +000010492 return CapturedExpr;
10493 }
10494 return BaseTransform::TransformMemberExpr(E);
10495 }
10496 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
10497};
10498} // namespace
10499
Alexey Bataev97d18bf2018-04-11 19:21:00 +000010500template <typename T, typename U>
10501static T filterLookupForUDR(SmallVectorImpl<U> &Lookups,
10502 const llvm::function_ref<T(ValueDecl *)> Gen) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010503 for (U &Set : Lookups) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010504 for (auto *D : Set) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010505 if (T Res = Gen(cast<ValueDecl>(D)))
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010506 return Res;
10507 }
10508 }
10509 return T();
10510}
10511
Alexey Bataev43b90b72018-09-12 16:31:59 +000010512static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) {
10513 assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case");
10514
10515 for (auto RD : D->redecls()) {
10516 // Don't bother with extra checks if we already know this one isn't visible.
10517 if (RD == D)
10518 continue;
10519
10520 auto ND = cast<NamedDecl>(RD);
10521 if (LookupResult::isVisible(SemaRef, ND))
10522 return ND;
10523 }
10524
10525 return nullptr;
10526}
10527
10528static void
10529argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &ReductionId,
10530 SourceLocation Loc, QualType Ty,
10531 SmallVectorImpl<UnresolvedSet<8>> &Lookups) {
10532 // Find all of the associated namespaces and classes based on the
10533 // arguments we have.
10534 Sema::AssociatedNamespaceSet AssociatedNamespaces;
10535 Sema::AssociatedClassSet AssociatedClasses;
10536 OpaqueValueExpr OVE(Loc, Ty, VK_LValue);
10537 SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces,
10538 AssociatedClasses);
10539
10540 // C++ [basic.lookup.argdep]p3:
10541 // Let X be the lookup set produced by unqualified lookup (3.4.1)
10542 // and let Y be the lookup set produced by argument dependent
10543 // lookup (defined as follows). If X contains [...] then Y is
10544 // empty. Otherwise Y is the set of declarations found in the
10545 // namespaces associated with the argument types as described
10546 // below. The set of declarations found by the lookup of the name
10547 // is the union of X and Y.
10548 //
10549 // Here, we compute Y and add its members to the overloaded
10550 // candidate set.
10551 for (auto *NS : AssociatedNamespaces) {
10552 // When considering an associated namespace, the lookup is the
10553 // same as the lookup performed when the associated namespace is
10554 // used as a qualifier (3.4.3.2) except that:
10555 //
10556 // -- Any using-directives in the associated namespace are
10557 // ignored.
10558 //
10559 // -- Any namespace-scope friend functions declared in
10560 // associated classes are visible within their respective
10561 // namespaces even if they are not visible during an ordinary
10562 // lookup (11.4).
10563 DeclContext::lookup_result R = NS->lookup(ReductionId.getName());
10564 for (auto *D : R) {
10565 auto *Underlying = D;
10566 if (auto *USD = dyn_cast<UsingShadowDecl>(D))
10567 Underlying = USD->getTargetDecl();
10568
10569 if (!isa<OMPDeclareReductionDecl>(Underlying))
10570 continue;
10571
10572 if (!SemaRef.isVisible(D)) {
10573 D = findAcceptableDecl(SemaRef, D);
10574 if (!D)
10575 continue;
10576 if (auto *USD = dyn_cast<UsingShadowDecl>(D))
10577 Underlying = USD->getTargetDecl();
10578 }
10579 Lookups.emplace_back();
10580 Lookups.back().addDecl(Underlying);
10581 }
10582 }
10583}
10584
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010585static ExprResult
10586buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
10587 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
10588 const DeclarationNameInfo &ReductionId, QualType Ty,
10589 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
10590 if (ReductionIdScopeSpec.isInvalid())
10591 return ExprError();
10592 SmallVector<UnresolvedSet<8>, 4> Lookups;
10593 if (S) {
10594 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
10595 Lookup.suppressDiagnostics();
10596 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010597 NamedDecl *D = Lookup.getRepresentativeDecl();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010598 do {
10599 S = S->getParent();
10600 } while (S && !S->isDeclScope(D));
10601 if (S)
10602 S = S->getParent();
Alexey Bataev43b90b72018-09-12 16:31:59 +000010603 Lookups.emplace_back();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010604 Lookups.back().append(Lookup.begin(), Lookup.end());
10605 Lookup.clear();
10606 }
10607 } else if (auto *ULE =
10608 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
10609 Lookups.push_back(UnresolvedSet<8>());
10610 Decl *PrevD = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000010611 for (NamedDecl *D : ULE->decls()) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010612 if (D == PrevD)
10613 Lookups.push_back(UnresolvedSet<8>());
10614 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
10615 Lookups.back().addDecl(DRD);
10616 PrevD = D;
10617 }
10618 }
Alexey Bataevfdc20352017-08-25 15:43:55 +000010619 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() ||
10620 Ty->isInstantiationDependentType() ||
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010621 Ty->containsUnexpandedParameterPack() ||
Alexey Bataeve3727102018-04-18 15:57:46 +000010622 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010623 return !D->isInvalidDecl() &&
10624 (D->getType()->isDependentType() ||
10625 D->getType()->isInstantiationDependentType() ||
10626 D->getType()->containsUnexpandedParameterPack());
10627 })) {
10628 UnresolvedSet<8> ResSet;
Alexey Bataeve3727102018-04-18 15:57:46 +000010629 for (const UnresolvedSet<8> &Set : Lookups) {
Alexey Bataev43b90b72018-09-12 16:31:59 +000010630 if (Set.empty())
10631 continue;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010632 ResSet.append(Set.begin(), Set.end());
10633 // The last item marks the end of all declarations at the specified scope.
10634 ResSet.addDecl(Set[Set.size() - 1]);
10635 }
10636 return UnresolvedLookupExpr::Create(
10637 SemaRef.Context, /*NamingClass=*/nullptr,
10638 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
10639 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
10640 }
Alexey Bataev43b90b72018-09-12 16:31:59 +000010641 // Lookup inside the classes.
10642 // C++ [over.match.oper]p3:
10643 // For a unary operator @ with an operand of a type whose
10644 // cv-unqualified version is T1, and for a binary operator @ with
10645 // a left operand of a type whose cv-unqualified version is T1 and
10646 // a right operand of a type whose cv-unqualified version is T2,
10647 // three sets of candidate functions, designated member
10648 // candidates, non-member candidates and built-in candidates, are
10649 // constructed as follows:
10650 // -- If T1 is a complete class type or a class currently being
10651 // defined, the set of member candidates is the result of the
10652 // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise,
10653 // the set of member candidates is empty.
10654 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
10655 Lookup.suppressDiagnostics();
10656 if (const auto *TyRec = Ty->getAs<RecordType>()) {
10657 // Complete the type if it can be completed.
10658 // If the type is neither complete nor being defined, bail out now.
10659 if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() ||
10660 TyRec->getDecl()->getDefinition()) {
10661 Lookup.clear();
10662 SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl());
10663 if (Lookup.empty()) {
10664 Lookups.emplace_back();
10665 Lookups.back().append(Lookup.begin(), Lookup.end());
10666 }
10667 }
10668 }
10669 // Perform ADL.
10670 argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010671 if (auto *VD = filterLookupForUDR<ValueDecl *>(
10672 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
10673 if (!D->isInvalidDecl() &&
10674 SemaRef.Context.hasSameType(D->getType(), Ty))
10675 return D;
10676 return nullptr;
10677 }))
10678 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
10679 if (auto *VD = filterLookupForUDR<ValueDecl *>(
10680 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
10681 if (!D->isInvalidDecl() &&
10682 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
10683 !Ty.isMoreQualifiedThan(D->getType()))
10684 return D;
10685 return nullptr;
10686 })) {
10687 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
10688 /*DetectVirtual=*/false);
10689 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
10690 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
10691 VD->getType().getUnqualifiedType()))) {
10692 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
10693 /*DiagID=*/0) !=
10694 Sema::AR_inaccessible) {
10695 SemaRef.BuildBasePathArray(Paths, BasePath);
10696 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
10697 }
10698 }
10699 }
10700 }
10701 if (ReductionIdScopeSpec.isSet()) {
10702 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
10703 return ExprError();
10704 }
10705 return ExprEmpty();
10706}
10707
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010708namespace {
10709/// Data for the reduction-based clauses.
10710struct ReductionData {
10711 /// List of original reduction items.
10712 SmallVector<Expr *, 8> Vars;
10713 /// List of private copies of the reduction items.
10714 SmallVector<Expr *, 8> Privates;
10715 /// LHS expressions for the reduction_op expressions.
10716 SmallVector<Expr *, 8> LHSs;
10717 /// RHS expressions for the reduction_op expressions.
10718 SmallVector<Expr *, 8> RHSs;
10719 /// Reduction operation expression.
10720 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev88202be2017-07-27 13:20:36 +000010721 /// Taskgroup descriptors for the corresponding reduction items in
10722 /// in_reduction clauses.
10723 SmallVector<Expr *, 8> TaskgroupDescriptors;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010724 /// List of captures for clause.
10725 SmallVector<Decl *, 4> ExprCaptures;
10726 /// List of postupdate expressions.
10727 SmallVector<Expr *, 4> ExprPostUpdates;
10728 ReductionData() = delete;
10729 /// Reserves required memory for the reduction data.
10730 ReductionData(unsigned Size) {
10731 Vars.reserve(Size);
10732 Privates.reserve(Size);
10733 LHSs.reserve(Size);
10734 RHSs.reserve(Size);
10735 ReductionOps.reserve(Size);
Alexey Bataev88202be2017-07-27 13:20:36 +000010736 TaskgroupDescriptors.reserve(Size);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010737 ExprCaptures.reserve(Size);
10738 ExprPostUpdates.reserve(Size);
10739 }
10740 /// Stores reduction item and reduction operation only (required for dependent
10741 /// reduction item).
10742 void push(Expr *Item, Expr *ReductionOp) {
10743 Vars.emplace_back(Item);
10744 Privates.emplace_back(nullptr);
10745 LHSs.emplace_back(nullptr);
10746 RHSs.emplace_back(nullptr);
10747 ReductionOps.emplace_back(ReductionOp);
Alexey Bataev88202be2017-07-27 13:20:36 +000010748 TaskgroupDescriptors.emplace_back(nullptr);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010749 }
10750 /// Stores reduction data.
Alexey Bataev88202be2017-07-27 13:20:36 +000010751 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp,
10752 Expr *TaskgroupDescriptor) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010753 Vars.emplace_back(Item);
10754 Privates.emplace_back(Private);
10755 LHSs.emplace_back(LHS);
10756 RHSs.emplace_back(RHS);
10757 ReductionOps.emplace_back(ReductionOp);
Alexey Bataev88202be2017-07-27 13:20:36 +000010758 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010759 }
10760};
10761} // namespace
10762
Alexey Bataeve3727102018-04-18 15:57:46 +000010763static bool checkOMPArraySectionConstantForReduction(
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010764 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement,
10765 SmallVectorImpl<llvm::APSInt> &ArraySizes) {
10766 const Expr *Length = OASE->getLength();
10767 if (Length == nullptr) {
10768 // For array sections of the form [1:] or [:], we would need to analyze
10769 // the lower bound...
10770 if (OASE->getColonLoc().isValid())
10771 return false;
10772
10773 // This is an array subscript which has implicit length 1!
10774 SingleElement = true;
10775 ArraySizes.push_back(llvm::APSInt::get(1));
10776 } else {
Fangrui Song407659a2018-11-30 23:41:18 +000010777 Expr::EvalResult Result;
10778 if (!Length->EvaluateAsInt(Result, Context))
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010779 return false;
10780
Fangrui Song407659a2018-11-30 23:41:18 +000010781 llvm::APSInt ConstantLengthValue = Result.Val.getInt();
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010782 SingleElement = (ConstantLengthValue.getSExtValue() == 1);
10783 ArraySizes.push_back(ConstantLengthValue);
10784 }
10785
10786 // Get the base of this array section and walk up from there.
10787 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
10788
10789 // We require length = 1 for all array sections except the right-most to
10790 // guarantee that the memory region is contiguous and has no holes in it.
10791 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) {
10792 Length = TempOASE->getLength();
10793 if (Length == nullptr) {
10794 // For array sections of the form [1:] or [:], we would need to analyze
10795 // the lower bound...
10796 if (OASE->getColonLoc().isValid())
10797 return false;
10798
10799 // This is an array subscript which has implicit length 1!
10800 ArraySizes.push_back(llvm::APSInt::get(1));
10801 } else {
Fangrui Song407659a2018-11-30 23:41:18 +000010802 Expr::EvalResult Result;
10803 if (!Length->EvaluateAsInt(Result, Context))
10804 return false;
10805
10806 llvm::APSInt ConstantLengthValue = Result.Val.getInt();
10807 if (ConstantLengthValue.getSExtValue() != 1)
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010808 return false;
10809
10810 ArraySizes.push_back(ConstantLengthValue);
10811 }
10812 Base = TempOASE->getBase()->IgnoreParenImpCasts();
10813 }
10814
10815 // If we have a single element, we don't need to add the implicit lengths.
10816 if (!SingleElement) {
10817 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) {
10818 // Has implicit length 1!
10819 ArraySizes.push_back(llvm::APSInt::get(1));
10820 Base = TempASE->getBase()->IgnoreParenImpCasts();
10821 }
10822 }
10823
10824 // This array section can be privatized as a single value or as a constant
10825 // sized array.
10826 return true;
10827}
10828
Alexey Bataeve3727102018-04-18 15:57:46 +000010829static bool actOnOMPReductionKindClause(
Alexey Bataev169d96a2017-07-18 20:17:46 +000010830 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind,
10831 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
10832 SourceLocation ColonLoc, SourceLocation EndLoc,
10833 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010834 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010835 DeclarationName DN = ReductionId.getName();
10836 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator();
Alexey Bataevc5e02582014-06-16 07:08:35 +000010837 BinaryOperatorKind BOK = BO_Comma;
10838
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010839 ASTContext &Context = S.Context;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010840 // OpenMP [2.14.3.6, reduction clause]
10841 // C
10842 // reduction-identifier is either an identifier or one of the following
10843 // operators: +, -, *, &, |, ^, && and ||
10844 // C++
10845 // reduction-identifier is either an id-expression or one of the following
10846 // operators: +, -, *, &, |, ^, && and ||
Alexey Bataevc5e02582014-06-16 07:08:35 +000010847 switch (OOK) {
10848 case OO_Plus:
10849 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010850 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010851 break;
10852 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010853 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010854 break;
10855 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010856 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010857 break;
10858 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010859 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010860 break;
10861 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010862 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010863 break;
10864 case OO_AmpAmp:
10865 BOK = BO_LAnd;
10866 break;
10867 case OO_PipePipe:
10868 BOK = BO_LOr;
10869 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010870 case OO_New:
10871 case OO_Delete:
10872 case OO_Array_New:
10873 case OO_Array_Delete:
10874 case OO_Slash:
10875 case OO_Percent:
10876 case OO_Tilde:
10877 case OO_Exclaim:
10878 case OO_Equal:
10879 case OO_Less:
10880 case OO_Greater:
10881 case OO_LessEqual:
10882 case OO_GreaterEqual:
10883 case OO_PlusEqual:
10884 case OO_MinusEqual:
10885 case OO_StarEqual:
10886 case OO_SlashEqual:
10887 case OO_PercentEqual:
10888 case OO_CaretEqual:
10889 case OO_AmpEqual:
10890 case OO_PipeEqual:
10891 case OO_LessLess:
10892 case OO_GreaterGreater:
10893 case OO_LessLessEqual:
10894 case OO_GreaterGreaterEqual:
10895 case OO_EqualEqual:
10896 case OO_ExclaimEqual:
Richard Smithd30b23d2017-12-01 02:13:10 +000010897 case OO_Spaceship:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010898 case OO_PlusPlus:
10899 case OO_MinusMinus:
10900 case OO_Comma:
10901 case OO_ArrowStar:
10902 case OO_Arrow:
10903 case OO_Call:
10904 case OO_Subscript:
10905 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +000010906 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010907 case NUM_OVERLOADED_OPERATORS:
10908 llvm_unreachable("Unexpected reduction identifier");
10909 case OO_None:
Alexey Bataeve3727102018-04-18 15:57:46 +000010910 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010911 if (II->isStr("max"))
10912 BOK = BO_GT;
10913 else if (II->isStr("min"))
10914 BOK = BO_LT;
10915 }
10916 break;
10917 }
10918 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010919 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +000010920 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataev4d4624c2017-07-20 16:47:47 +000010921 else
10922 ReductionIdRange.setBegin(ReductionId.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +000010923 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +000010924
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010925 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
10926 bool FirstIter = true;
Alexey Bataeve3727102018-04-18 15:57:46 +000010927 for (Expr *RefExpr : VarList) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010928 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +000010929 // OpenMP [2.1, C/C++]
10930 // A list item is a variable or array section, subject to the restrictions
10931 // specified in Section 2.4 on page 42 and in each of the sections
10932 // describing clauses and directives for which a list appears.
10933 // OpenMP [2.14.3.3, Restrictions, p.1]
10934 // A variable that is part of another variable (as an array or
10935 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010936 if (!FirstIter && IR != ER)
10937 ++IR;
10938 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010939 SourceLocation ELoc;
10940 SourceRange ERange;
10941 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010942 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
Alexey Bataev60da77e2016-02-29 05:54:20 +000010943 /*AllowArraySection=*/true);
10944 if (Res.second) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010945 // Try to find 'declare reduction' corresponding construct before using
10946 // builtin/overloaded operators.
10947 QualType Type = Context.DependentTy;
10948 CXXCastPath BasePath;
10949 ExprResult DeclareReductionRef = buildDeclareReductionRef(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010950 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010951 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010952 Expr *ReductionOp = nullptr;
10953 if (S.CurContext->isDependentContext() &&
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010954 (DeclareReductionRef.isUnset() ||
10955 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010956 ReductionOp = DeclareReductionRef.get();
10957 // It will be analyzed later.
10958 RD.push(RefExpr, ReductionOp);
Alexey Bataevc5e02582014-06-16 07:08:35 +000010959 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000010960 ValueDecl *D = Res.first;
10961 if (!D)
10962 continue;
10963
Alexey Bataev88202be2017-07-27 13:20:36 +000010964 Expr *TaskgroupDescriptor = nullptr;
Alexey Bataeva1764212015-09-30 09:22:36 +000010965 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010966 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
10967 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
Alexey Bataeve3727102018-04-18 15:57:46 +000010968 if (ASE) {
Alexey Bataev31300ed2016-02-04 11:27:03 +000010969 Type = ASE->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010970 } else if (OASE) {
10971 QualType BaseType =
10972 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
10973 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
Alexey Bataeva1764212015-09-30 09:22:36 +000010974 Type = ATy->getElementType();
10975 else
10976 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +000010977 Type = Type.getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010978 } else {
Alexey Bataev60da77e2016-02-29 05:54:20 +000010979 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
Alexey Bataeve3727102018-04-18 15:57:46 +000010980 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000010981 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +000010982
Alexey Bataevc5e02582014-06-16 07:08:35 +000010983 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
10984 // A variable that appears in a private clause must not have an incomplete
10985 // type or a reference type.
Joel E. Denny3cabf732018-06-28 19:54:49 +000010986 if (S.RequireCompleteType(ELoc, D->getType(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010987 diag::err_omp_reduction_incomplete_type))
Alexey Bataevc5e02582014-06-16 07:08:35 +000010988 continue;
10989 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +000010990 // A list item that appears in a reduction clause must not be
10991 // const-qualified.
Joel E. Dennyd2649292019-01-04 22:11:56 +000010992 if (rejectConstNotMutableType(S, D, Type, ClauseKind, ELoc,
10993 /*AcceptIfMutable*/ false, ASE || OASE))
Alexey Bataevc5e02582014-06-16 07:08:35 +000010994 continue;
Alexey Bataevbc529672018-09-28 19:33:14 +000010995
10996 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective();
Alexey Bataevc5e02582014-06-16 07:08:35 +000010997 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
10998 // If a list-item is a reference type then it must bind to the same object
10999 // for all threads of the team.
Alexey Bataevbc529672018-09-28 19:33:14 +000011000 if (!ASE && !OASE) {
11001 if (VD) {
11002 VarDecl *VDDef = VD->getDefinition();
11003 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) {
11004 DSARefChecker Check(Stack);
11005 if (Check.Visit(VDDef->getInit())) {
11006 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg)
11007 << getOpenMPClauseName(ClauseKind) << ERange;
11008 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
11009 continue;
11010 }
Alexey Bataeva1764212015-09-30 09:22:36 +000011011 }
Alexey Bataevc5e02582014-06-16 07:08:35 +000011012 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011013
Alexey Bataevbc529672018-09-28 19:33:14 +000011014 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
11015 // in a Construct]
11016 // Variables with the predetermined data-sharing attributes may not be
11017 // listed in data-sharing attributes clauses, except for the cases
11018 // listed below. For these exceptions only, listing a predetermined
11019 // variable in a data-sharing attribute clause is allowed and overrides
11020 // the variable's predetermined data-sharing attributes.
11021 // OpenMP [2.14.3.6, Restrictions, p.3]
11022 // Any number of reduction clauses can be specified on the directive,
11023 // but a list item can appear only once in the reduction clauses for that
11024 // directive.
11025 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false);
11026 if (DVar.CKind == OMPC_reduction) {
11027 S.Diag(ELoc, diag::err_omp_once_referenced)
11028 << getOpenMPClauseName(ClauseKind);
11029 if (DVar.RefExpr)
11030 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
11031 continue;
11032 }
11033 if (DVar.CKind != OMPC_unknown) {
11034 S.Diag(ELoc, diag::err_omp_wrong_dsa)
11035 << getOpenMPClauseName(DVar.CKind)
11036 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataeve3727102018-04-18 15:57:46 +000011037 reportOriginalDsa(S, Stack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011038 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +000011039 }
Alexey Bataevbc529672018-09-28 19:33:14 +000011040
11041 // OpenMP [2.14.3.6, Restrictions, p.1]
11042 // A list item that appears in a reduction clause of a worksharing
11043 // construct must be shared in the parallel regions to which any of the
11044 // worksharing regions arising from the worksharing construct bind.
11045 if (isOpenMPWorksharingDirective(CurrDir) &&
11046 !isOpenMPParallelDirective(CurrDir) &&
11047 !isOpenMPTeamsDirective(CurrDir)) {
11048 DVar = Stack->getImplicitDSA(D, true);
11049 if (DVar.CKind != OMPC_shared) {
11050 S.Diag(ELoc, diag::err_omp_required_access)
11051 << getOpenMPClauseName(OMPC_reduction)
11052 << getOpenMPClauseName(OMPC_shared);
11053 reportOriginalDsa(S, Stack, D, DVar);
11054 continue;
11055 }
11056 }
Alexey Bataevf29276e2014-06-18 04:14:57 +000011057 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011058
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011059 // Try to find 'declare reduction' corresponding construct before using
11060 // builtin/overloaded operators.
11061 CXXCastPath BasePath;
11062 ExprResult DeclareReductionRef = buildDeclareReductionRef(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011063 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011064 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
11065 if (DeclareReductionRef.isInvalid())
11066 continue;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011067 if (S.CurContext->isDependentContext() &&
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011068 (DeclareReductionRef.isUnset() ||
11069 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011070 RD.push(RefExpr, DeclareReductionRef.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011071 continue;
11072 }
11073 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
11074 // Not allowed reduction identifier is found.
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011075 S.Diag(ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011076 diag::err_omp_unknown_reduction_identifier)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011077 << Type << ReductionIdRange;
11078 continue;
11079 }
11080
11081 // OpenMP [2.14.3.6, reduction clause, Restrictions]
11082 // The type of a list item that appears in a reduction clause must be valid
11083 // for the reduction-identifier. For a max or min reduction in C, the type
11084 // of the list item must be an allowed arithmetic data type: char, int,
11085 // float, double, or _Bool, possibly modified with long, short, signed, or
11086 // unsigned. For a max or min reduction in C++, the type of the list item
11087 // must be an allowed arithmetic data type: char, wchar_t, int, float,
11088 // double, or bool, possibly modified with long, short, signed, or unsigned.
11089 if (DeclareReductionRef.isUnset()) {
11090 if ((BOK == BO_GT || BOK == BO_LT) &&
11091 !(Type->isScalarType() ||
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011092 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
11093 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
Alexey Bataev169d96a2017-07-18 20:17:46 +000011094 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011095 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011096 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
11097 VarDecl::DeclarationOnly;
11098 S.Diag(D->getLocation(),
11099 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011100 << D;
11101 }
11102 continue;
11103 }
11104 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011105 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) {
Alexey Bataev169d96a2017-07-18 20:17:46 +000011106 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg)
11107 << getOpenMPClauseName(ClauseKind);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011108 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011109 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
11110 VarDecl::DeclarationOnly;
11111 S.Diag(D->getLocation(),
11112 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011113 << D;
11114 }
11115 continue;
11116 }
11117 }
11118
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011119 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000011120 VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs",
11121 D->hasAttrs() ? &D->getAttrs() : nullptr);
11122 VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(),
11123 D->hasAttrs() ? &D->getAttrs() : nullptr);
11124 QualType PrivateTy = Type;
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000011125
11126 // Try if we can determine constant lengths for all array sections and avoid
11127 // the VLA.
11128 bool ConstantLengthOASE = false;
11129 if (OASE) {
11130 bool SingleElement;
11131 llvm::SmallVector<llvm::APSInt, 4> ArraySizes;
Alexey Bataeve3727102018-04-18 15:57:46 +000011132 ConstantLengthOASE = checkOMPArraySectionConstantForReduction(
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000011133 Context, OASE, SingleElement, ArraySizes);
11134
11135 // If we don't have a single element, we must emit a constant array type.
11136 if (ConstantLengthOASE && !SingleElement) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011137 for (llvm::APSInt &Size : ArraySizes)
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000011138 PrivateTy = Context.getConstantArrayType(
11139 PrivateTy, Size, ArrayType::Normal, /*IndexTypeQuals=*/0);
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000011140 }
11141 }
11142
11143 if ((OASE && !ConstantLengthOASE) ||
Jonas Hahnfeld96087f32017-11-02 13:30:42 +000011144 (!OASE && !ASE &&
Alexey Bataev60da77e2016-02-29 05:54:20 +000011145 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Jonas Hahnfeld87d44262017-11-18 21:00:46 +000011146 if (!Context.getTargetInfo().isVLASupported() &&
11147 S.shouldDiagnoseTargetSupportFromOpenMP()) {
11148 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE;
11149 S.Diag(ELoc, diag::note_vla_unsupported);
11150 continue;
11151 }
David Majnemer9d168222016-08-05 17:44:54 +000011152 // For arrays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011153 // Create pseudo array type for private copy. The size for this array will
11154 // be generated during codegen.
11155 // For array subscripts or single variables Private Ty is the same as Type
11156 // (type of the variable or single array element).
11157 PrivateTy = Context.getVariableArrayType(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011158 Type,
Alexey Bataevd070a582017-10-25 15:54:04 +000011159 new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue),
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011160 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +000011161 } else if (!ASE && !OASE &&
Alexey Bataeve3727102018-04-18 15:57:46 +000011162 Context.getAsArrayType(D->getType().getNonReferenceType())) {
Alexey Bataev60da77e2016-02-29 05:54:20 +000011163 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000011164 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011165 // Private copy.
Alexey Bataeve3727102018-04-18 15:57:46 +000011166 VarDecl *PrivateVD =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000011167 buildVarDecl(S, ELoc, PrivateTy, D->getName(),
11168 D->hasAttrs() ? &D->getAttrs() : nullptr,
11169 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011170 // Add initializer for private variable.
11171 Expr *Init = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000011172 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc);
11173 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011174 if (DeclareReductionRef.isUsable()) {
11175 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
11176 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
11177 if (DRD->getInitializer()) {
11178 Init = DRDRef;
11179 RHSVD->setInit(DRDRef);
11180 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011181 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011182 } else {
11183 switch (BOK) {
11184 case BO_Add:
11185 case BO_Xor:
11186 case BO_Or:
11187 case BO_LOr:
11188 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
11189 if (Type->isScalarType() || Type->isAnyComplexType())
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011190 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011191 break;
11192 case BO_Mul:
11193 case BO_LAnd:
11194 if (Type->isScalarType() || Type->isAnyComplexType()) {
11195 // '*' and '&&' reduction ops - initializer is '1'.
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011196 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +000011197 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011198 break;
11199 case BO_And: {
11200 // '&' reduction op - initializer is '~0'.
11201 QualType OrigType = Type;
11202 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
11203 Type = ComplexTy->getElementType();
11204 if (Type->isRealFloatingType()) {
11205 llvm::APFloat InitValue =
11206 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
11207 /*isIEEE=*/true);
11208 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
11209 Type, ELoc);
11210 } else if (Type->isScalarType()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011211 uint64_t Size = Context.getTypeSize(Type);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011212 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
11213 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
11214 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
11215 }
11216 if (Init && OrigType->isAnyComplexType()) {
11217 // Init = 0xFFFF + 0xFFFFi;
11218 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011219 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011220 }
11221 Type = OrigType;
11222 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011223 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011224 case BO_LT:
11225 case BO_GT: {
11226 // 'min' reduction op - initializer is 'Largest representable number in
11227 // the reduction list item type'.
11228 // 'max' reduction op - initializer is 'Least representable number in
11229 // the reduction list item type'.
11230 if (Type->isIntegerType() || Type->isPointerType()) {
11231 bool IsSigned = Type->hasSignedIntegerRepresentation();
Alexey Bataeve3727102018-04-18 15:57:46 +000011232 uint64_t Size = Context.getTypeSize(Type);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011233 QualType IntTy =
11234 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
11235 llvm::APInt InitValue =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011236 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
11237 : llvm::APInt::getMinValue(Size)
11238 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
11239 : llvm::APInt::getMaxValue(Size);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011240 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
11241 if (Type->isPointerType()) {
11242 // Cast to pointer type.
Alexey Bataeve3727102018-04-18 15:57:46 +000011243 ExprResult CastExpr = S.BuildCStyleCastExpr(
Alexey Bataevd070a582017-10-25 15:54:04 +000011244 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011245 if (CastExpr.isInvalid())
11246 continue;
11247 Init = CastExpr.get();
11248 }
11249 } else if (Type->isRealFloatingType()) {
11250 llvm::APFloat InitValue = llvm::APFloat::getLargest(
11251 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
11252 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
11253 Type, ELoc);
11254 }
11255 break;
11256 }
11257 case BO_PtrMemD:
11258 case BO_PtrMemI:
11259 case BO_MulAssign:
11260 case BO_Div:
11261 case BO_Rem:
11262 case BO_Sub:
11263 case BO_Shl:
11264 case BO_Shr:
11265 case BO_LE:
11266 case BO_GE:
11267 case BO_EQ:
11268 case BO_NE:
Richard Smithc70f1d62017-12-14 15:16:18 +000011269 case BO_Cmp:
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011270 case BO_AndAssign:
11271 case BO_XorAssign:
11272 case BO_OrAssign:
11273 case BO_Assign:
11274 case BO_AddAssign:
11275 case BO_SubAssign:
11276 case BO_DivAssign:
11277 case BO_RemAssign:
11278 case BO_ShlAssign:
11279 case BO_ShrAssign:
11280 case BO_Comma:
11281 llvm_unreachable("Unexpected reduction operation");
11282 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011283 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011284 if (Init && DeclareReductionRef.isUnset())
11285 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false);
11286 else if (!Init)
11287 S.ActOnUninitializedDecl(RHSVD);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011288 if (RHSVD->isInvalidDecl())
11289 continue;
11290 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011291 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible)
11292 << Type << ReductionIdRange;
11293 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
11294 VarDecl::DeclarationOnly;
11295 S.Diag(D->getLocation(),
11296 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +000011297 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011298 continue;
11299 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011300 // Store initializer for single element in private copy. Will be used during
11301 // codegen.
11302 PrivateVD->setInit(RHSVD->getInit());
11303 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataeve3727102018-04-18 15:57:46 +000011304 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011305 ExprResult ReductionOp;
11306 if (DeclareReductionRef.isUsable()) {
11307 QualType RedTy = DeclareReductionRef.get()->getType();
11308 QualType PtrRedTy = Context.getPointerType(RedTy);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011309 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
11310 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011311 if (!BasePath.empty()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011312 LHS = S.DefaultLvalueConversion(LHS.get());
11313 RHS = S.DefaultLvalueConversion(RHS.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011314 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
11315 CK_UncheckedDerivedToBase, LHS.get(),
11316 &BasePath, LHS.get()->getValueKind());
11317 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
11318 CK_UncheckedDerivedToBase, RHS.get(),
11319 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011320 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011321 FunctionProtoType::ExtProtoInfo EPI;
11322 QualType Params[] = {PtrRedTy, PtrRedTy};
11323 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
11324 auto *OVE = new (Context) OpaqueValueExpr(
11325 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011326 S.DefaultLvalueConversion(DeclareReductionRef.get()).get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011327 Expr *Args[] = {LHS.get(), RHS.get()};
Bruno Riccic5885cf2018-12-21 15:20:32 +000011328 ReductionOp =
11329 CallExpr::Create(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011330 } else {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011331 ReductionOp = S.BuildBinOp(
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011332 Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, LHSDRE, RHSDRE);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011333 if (ReductionOp.isUsable()) {
11334 if (BOK != BO_LT && BOK != BO_GT) {
11335 ReductionOp =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011336 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011337 BO_Assign, LHSDRE, ReductionOp.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011338 } else {
Alexey Bataevd070a582017-10-25 15:54:04 +000011339 auto *ConditionalOp = new (Context)
11340 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE,
11341 Type, VK_LValue, OK_Ordinary);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011342 ReductionOp =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011343 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011344 BO_Assign, LHSDRE, ConditionalOp);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011345 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +000011346 if (ReductionOp.isUsable())
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000011347 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get(),
11348 /*DiscardedValue*/ false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011349 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +000011350 if (!ReductionOp.isUsable())
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011351 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +000011352 }
11353
Alexey Bataevfa312f32017-07-21 18:48:21 +000011354 // OpenMP [2.15.4.6, Restrictions, p.2]
11355 // A list item that appears in an in_reduction clause of a task construct
11356 // must appear in a task_reduction clause of a construct associated with a
11357 // taskgroup region that includes the participating task in its taskgroup
11358 // set. The construct associated with the innermost region that meets this
11359 // condition must specify the same reduction-identifier as the in_reduction
11360 // clause.
11361 if (ClauseKind == OMPC_in_reduction) {
Alexey Bataevfa312f32017-07-21 18:48:21 +000011362 SourceRange ParentSR;
11363 BinaryOperatorKind ParentBOK;
11364 const Expr *ParentReductionOp;
Alexey Bataev88202be2017-07-27 13:20:36 +000011365 Expr *ParentBOKTD, *ParentReductionOpTD;
Alexey Bataevf189cb72017-07-24 14:52:13 +000011366 DSAStackTy::DSAVarData ParentBOKDSA =
Alexey Bataev88202be2017-07-27 13:20:36 +000011367 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK,
11368 ParentBOKTD);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011369 DSAStackTy::DSAVarData ParentReductionOpDSA =
Alexey Bataev88202be2017-07-27 13:20:36 +000011370 Stack->getTopMostTaskgroupReductionData(
11371 D, ParentSR, ParentReductionOp, ParentReductionOpTD);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011372 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown;
11373 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown;
11374 if (!IsParentBOK && !IsParentReductionOp) {
11375 S.Diag(ELoc, diag::err_omp_in_reduction_not_task_reduction);
11376 continue;
11377 }
Alexey Bataevfa312f32017-07-21 18:48:21 +000011378 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) ||
11379 (DeclareReductionRef.isUsable() && IsParentBOK) || BOK != ParentBOK ||
11380 IsParentReductionOp) {
11381 bool EmitError = true;
11382 if (IsParentReductionOp && DeclareReductionRef.isUsable()) {
11383 llvm::FoldingSetNodeID RedId, ParentRedId;
11384 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true);
11385 DeclareReductionRef.get()->Profile(RedId, Context,
11386 /*Canonical=*/true);
11387 EmitError = RedId != ParentRedId;
11388 }
11389 if (EmitError) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011390 S.Diag(ReductionId.getBeginLoc(),
Alexey Bataevfa312f32017-07-21 18:48:21 +000011391 diag::err_omp_reduction_identifier_mismatch)
11392 << ReductionIdRange << RefExpr->getSourceRange();
11393 S.Diag(ParentSR.getBegin(),
11394 diag::note_omp_previous_reduction_identifier)
Alexey Bataevf189cb72017-07-24 14:52:13 +000011395 << ParentSR
11396 << (IsParentBOK ? ParentBOKDSA.RefExpr
11397 : ParentReductionOpDSA.RefExpr)
11398 ->getSourceRange();
Alexey Bataevfa312f32017-07-21 18:48:21 +000011399 continue;
11400 }
11401 }
Alexey Bataev88202be2017-07-27 13:20:36 +000011402 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD;
11403 assert(TaskgroupDescriptor && "Taskgroup descriptor must be defined.");
Alexey Bataevfa312f32017-07-21 18:48:21 +000011404 }
11405
Alexey Bataev60da77e2016-02-29 05:54:20 +000011406 DeclRefExpr *Ref = nullptr;
11407 Expr *VarsExpr = RefExpr->IgnoreParens();
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011408 if (!VD && !S.CurContext->isDependentContext()) {
Alexey Bataev60da77e2016-02-29 05:54:20 +000011409 if (ASE || OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011410 TransformExprToCaptures RebuildToCapture(S, D);
Alexey Bataev60da77e2016-02-29 05:54:20 +000011411 VarsExpr =
11412 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
11413 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +000011414 } else {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011415 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +000011416 }
Alexey Bataeve3727102018-04-18 15:57:46 +000011417 if (!S.isOpenMPCapturedDecl(D)) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011418 RD.ExprCaptures.emplace_back(Ref->getDecl());
Alexey Bataev5a3af132016-03-29 08:58:54 +000011419 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011420 ExprResult RefRes = S.DefaultLvalueConversion(Ref);
Alexey Bataev5a3af132016-03-29 08:58:54 +000011421 if (!RefRes.isUsable())
11422 continue;
11423 ExprResult PostUpdateRes =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011424 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
11425 RefRes.get());
Alexey Bataev5a3af132016-03-29 08:58:54 +000011426 if (!PostUpdateRes.isUsable())
11427 continue;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011428 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
11429 Stack->getCurrentDirective() == OMPD_taskgroup) {
11430 S.Diag(RefExpr->getExprLoc(),
11431 diag::err_omp_reduction_non_addressable_expression)
Alexey Bataevbcd0ae02017-07-11 19:16:44 +000011432 << RefExpr->getSourceRange();
11433 continue;
11434 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011435 RD.ExprPostUpdates.emplace_back(
11436 S.IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +000011437 }
11438 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000011439 }
Alexey Bataev169d96a2017-07-18 20:17:46 +000011440 // All reduction items are still marked as reduction (to do not increase
11441 // code base size).
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011442 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011443 if (CurrDir == OMPD_taskgroup) {
11444 if (DeclareReductionRef.isUsable())
Alexey Bataev3b1b8952017-07-25 15:53:26 +000011445 Stack->addTaskgroupReductionData(D, ReductionIdRange,
11446 DeclareReductionRef.get());
Alexey Bataevf189cb72017-07-24 14:52:13 +000011447 else
Alexey Bataev3b1b8952017-07-25 15:53:26 +000011448 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011449 }
Alexey Bataev88202be2017-07-27 13:20:36 +000011450 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(),
11451 TaskgroupDescriptor);
Alexey Bataevc5e02582014-06-16 07:08:35 +000011452 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011453 return RD.Vars.empty();
11454}
Alexey Bataevc5e02582014-06-16 07:08:35 +000011455
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011456OMPClause *Sema::ActOnOpenMPReductionClause(
11457 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11458 SourceLocation ColonLoc, SourceLocation EndLoc,
11459 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11460 ArrayRef<Expr *> UnresolvedReductions) {
11461 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000011462 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList,
Alexey Bataev169d96a2017-07-18 20:17:46 +000011463 StartLoc, LParenLoc, ColonLoc, EndLoc,
11464 ReductionIdScopeSpec, ReductionId,
11465 UnresolvedReductions, RD))
Alexey Bataevc5e02582014-06-16 07:08:35 +000011466 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +000011467
Alexey Bataevc5e02582014-06-16 07:08:35 +000011468 return OMPReductionClause::Create(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011469 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11470 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
11471 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
11472 buildPreInits(Context, RD.ExprCaptures),
11473 buildPostUpdate(*this, RD.ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +000011474}
11475
Alexey Bataev169d96a2017-07-18 20:17:46 +000011476OMPClause *Sema::ActOnOpenMPTaskReductionClause(
11477 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11478 SourceLocation ColonLoc, SourceLocation EndLoc,
11479 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11480 ArrayRef<Expr *> UnresolvedReductions) {
11481 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000011482 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList,
11483 StartLoc, LParenLoc, ColonLoc, EndLoc,
11484 ReductionIdScopeSpec, ReductionId,
Alexey Bataev169d96a2017-07-18 20:17:46 +000011485 UnresolvedReductions, RD))
11486 return nullptr;
11487
11488 return OMPTaskReductionClause::Create(
11489 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11490 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
11491 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
11492 buildPreInits(Context, RD.ExprCaptures),
11493 buildPostUpdate(*this, RD.ExprPostUpdates));
11494}
11495
Alexey Bataevfa312f32017-07-21 18:48:21 +000011496OMPClause *Sema::ActOnOpenMPInReductionClause(
11497 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11498 SourceLocation ColonLoc, SourceLocation EndLoc,
11499 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11500 ArrayRef<Expr *> UnresolvedReductions) {
11501 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000011502 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList,
Alexey Bataevfa312f32017-07-21 18:48:21 +000011503 StartLoc, LParenLoc, ColonLoc, EndLoc,
11504 ReductionIdScopeSpec, ReductionId,
11505 UnresolvedReductions, RD))
11506 return nullptr;
11507
11508 return OMPInReductionClause::Create(
11509 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11510 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
Alexey Bataev88202be2017-07-27 13:20:36 +000011511 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors,
Alexey Bataevfa312f32017-07-21 18:48:21 +000011512 buildPreInits(Context, RD.ExprCaptures),
11513 buildPostUpdate(*this, RD.ExprPostUpdates));
11514}
11515
Alexey Bataevecba70f2016-04-12 11:02:11 +000011516bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
11517 SourceLocation LinLoc) {
11518 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
11519 LinKind == OMPC_LINEAR_unknown) {
11520 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
11521 return true;
11522 }
11523 return false;
11524}
11525
Alexey Bataeve3727102018-04-18 15:57:46 +000011526bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc,
Alexey Bataevecba70f2016-04-12 11:02:11 +000011527 OpenMPLinearClauseKind LinKind,
11528 QualType Type) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011529 const auto *VD = dyn_cast_or_null<VarDecl>(D);
Alexey Bataevecba70f2016-04-12 11:02:11 +000011530 // A variable must not have an incomplete type or a reference type.
11531 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
11532 return true;
11533 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
11534 !Type->isReferenceType()) {
11535 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
11536 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
11537 return true;
11538 }
11539 Type = Type.getNonReferenceType();
11540
Joel E. Dennybae586f2019-01-04 22:12:13 +000011541 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
11542 // A variable that is privatized must not have a const-qualified type
11543 // unless it is of class type with a mutable member. This restriction does
11544 // not apply to the firstprivate clause.
11545 if (rejectConstNotMutableType(*this, D, Type, OMPC_linear, ELoc))
Alexey Bataevecba70f2016-04-12 11:02:11 +000011546 return true;
Alexey Bataevecba70f2016-04-12 11:02:11 +000011547
11548 // A list item must be of integral or pointer type.
11549 Type = Type.getUnqualifiedType().getCanonicalType();
11550 const auto *Ty = Type.getTypePtrOrNull();
11551 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
11552 !Ty->isPointerType())) {
11553 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
11554 if (D) {
11555 bool IsDecl =
11556 !VD ||
11557 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
11558 Diag(D->getLocation(),
11559 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
11560 << D;
11561 }
11562 return true;
11563 }
11564 return false;
11565}
11566
Alexey Bataev182227b2015-08-20 10:54:39 +000011567OMPClause *Sema::ActOnOpenMPLinearClause(
11568 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
11569 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
11570 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +000011571 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011572 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +000011573 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +000011574 SmallVector<Decl *, 4> ExprCaptures;
11575 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +000011576 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +000011577 LinKind = OMPC_LINEAR_val;
Alexey Bataeve3727102018-04-18 15:57:46 +000011578 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000011579 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011580 SourceLocation ELoc;
11581 SourceRange ERange;
11582 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011583 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011584 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +000011585 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000011586 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011587 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +000011588 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +000011589 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011590 ValueDecl *D = Res.first;
11591 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +000011592 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +000011593
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011594 QualType Type = D->getType();
11595 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +000011596
11597 // OpenMP [2.14.3.7, linear clause]
11598 // A list-item cannot appear in more than one linear clause.
11599 // A list-item that appears in a linear clause cannot appear in any
11600 // other data-sharing attribute clause.
Alexey Bataeve3727102018-04-18 15:57:46 +000011601 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexander Musman8dba6642014-04-22 13:09:42 +000011602 if (DVar.RefExpr) {
11603 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
11604 << getOpenMPClauseName(OMPC_linear);
Alexey Bataeve3727102018-04-18 15:57:46 +000011605 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +000011606 continue;
11607 }
11608
Alexey Bataevecba70f2016-04-12 11:02:11 +000011609 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +000011610 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +000011611 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +000011612
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011613 // Build private copy of original var.
Alexey Bataeve3727102018-04-18 15:57:46 +000011614 VarDecl *Private =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000011615 buildVarDecl(*this, ELoc, Type, D->getName(),
11616 D->hasAttrs() ? &D->getAttrs() : nullptr,
11617 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011618 DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +000011619 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011620 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011621 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011622 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000011623 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev78849fb2016-03-09 09:49:00 +000011624 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011625 if (!isOpenMPCapturedDecl(D)) {
Alexey Bataev78849fb2016-03-09 09:49:00 +000011626 ExprCaptures.push_back(Ref->getDecl());
11627 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
11628 ExprResult RefRes = DefaultLvalueConversion(Ref);
11629 if (!RefRes.isUsable())
11630 continue;
11631 ExprResult PostUpdateRes =
11632 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
11633 SimpleRefExpr, RefRes.get());
11634 if (!PostUpdateRes.isUsable())
11635 continue;
11636 ExprPostUpdates.push_back(
11637 IgnoredValueConversions(PostUpdateRes.get()).get());
11638 }
11639 }
11640 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011641 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011642 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011643 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011644 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011645 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000011646 /*DirectInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011647 DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011648
11649 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000011650 Vars.push_back((VD || CurContext->isDependentContext())
11651 ? RefExpr->IgnoreParens()
11652 : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011653 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +000011654 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +000011655 }
11656
11657 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000011658 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +000011659
11660 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +000011661 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +000011662 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
11663 !Step->isInstantiationDependent() &&
11664 !Step->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011665 SourceLocation StepLoc = Step->getBeginLoc();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +000011666 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +000011667 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000011668 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +000011669 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +000011670
Alexander Musman3276a272015-03-21 10:12:56 +000011671 // Build var to save the step value.
11672 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +000011673 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +000011674 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +000011675 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +000011676 ExprResult CalcStep =
11677 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000011678 CalcStep = ActOnFinishFullExpr(CalcStep.get(), /*DiscardedValue*/ false);
Alexander Musman3276a272015-03-21 10:12:56 +000011679
Alexander Musman8dba6642014-04-22 13:09:42 +000011680 // Warn about zero linear step (it would be probably better specified as
11681 // making corresponding variables 'const').
11682 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +000011683 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
11684 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +000011685 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
11686 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +000011687 if (!IsConstant && CalcStep.isUsable()) {
11688 // Calculate the step beforehand instead of doing this on each iteration.
11689 // (This is not used if the number of iterations may be kfold-ed).
11690 CalcStepExpr = CalcStep.get();
11691 }
Alexander Musman8dba6642014-04-22 13:09:42 +000011692 }
11693
Alexey Bataev182227b2015-08-20 10:54:39 +000011694 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
11695 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +000011696 StepExpr, CalcStepExpr,
11697 buildPreInits(Context, ExprCaptures),
11698 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +000011699}
11700
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011701static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
11702 Expr *NumIterations, Sema &SemaRef,
11703 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +000011704 // Walk the vars and build update/final expressions for the CodeGen.
11705 SmallVector<Expr *, 8> Updates;
11706 SmallVector<Expr *, 8> Finals;
11707 Expr *Step = Clause.getStep();
11708 Expr *CalcStep = Clause.getCalcStep();
11709 // OpenMP [2.14.3.7, linear clause]
11710 // If linear-step is not specified it is assumed to be 1.
Alexey Bataeve3727102018-04-18 15:57:46 +000011711 if (!Step)
Alexander Musman3276a272015-03-21 10:12:56 +000011712 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000011713 else if (CalcStep)
Alexander Musman3276a272015-03-21 10:12:56 +000011714 Step = cast<BinaryOperator>(CalcStep)->getLHS();
11715 bool HasErrors = false;
11716 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011717 auto CurPrivate = Clause.privates().begin();
Alexey Bataeve3727102018-04-18 15:57:46 +000011718 OpenMPLinearClauseKind LinKind = Clause.getModifier();
11719 for (Expr *RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011720 SourceLocation ELoc;
11721 SourceRange ERange;
11722 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011723 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange);
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011724 ValueDecl *D = Res.first;
11725 if (Res.second || !D) {
11726 Updates.push_back(nullptr);
11727 Finals.push_back(nullptr);
11728 HasErrors = true;
11729 continue;
11730 }
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011731 auto &&Info = Stack->isLoopControlVariable(D);
Alexey Bataev2b86f212017-11-29 21:31:48 +000011732 // OpenMP [2.15.11, distribute simd Construct]
11733 // A list item may not appear in a linear clause, unless it is the loop
11734 // iteration variable.
11735 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) &&
11736 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) {
11737 SemaRef.Diag(ELoc,
11738 diag::err_omp_linear_distribute_var_non_loop_iteration);
11739 Updates.push_back(nullptr);
11740 Finals.push_back(nullptr);
11741 HasErrors = true;
11742 continue;
11743 }
Alexander Musman3276a272015-03-21 10:12:56 +000011744 Expr *InitExpr = *CurInit;
11745
11746 // Build privatized reference to the current linear var.
David Majnemer9d168222016-08-05 17:44:54 +000011747 auto *DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011748 Expr *CapturedRef;
11749 if (LinKind == OMPC_LINEAR_uval)
11750 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
11751 else
11752 CapturedRef =
11753 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
11754 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
11755 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +000011756
11757 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011758 ExprResult Update;
Alexey Bataeve3727102018-04-18 15:57:46 +000011759 if (!Info.first)
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011760 Update =
Alexey Bataeve3727102018-04-18 15:57:46 +000011761 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011762 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011763 else
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011764 Update = *CurPrivate;
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011765 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000011766 /*DiscardedValue*/ false);
Alexander Musman3276a272015-03-21 10:12:56 +000011767
11768 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011769 ExprResult Final;
Alexey Bataeve3727102018-04-18 15:57:46 +000011770 if (!Info.first)
11771 Final =
11772 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
11773 InitExpr, NumIterations, Step, /*Subtract=*/false);
11774 else
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011775 Final = *CurPrivate;
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011776 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000011777 /*DiscardedValue*/ false);
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011778
Alexander Musman3276a272015-03-21 10:12:56 +000011779 if (!Update.isUsable() || !Final.isUsable()) {
11780 Updates.push_back(nullptr);
11781 Finals.push_back(nullptr);
11782 HasErrors = true;
11783 } else {
11784 Updates.push_back(Update.get());
11785 Finals.push_back(Final.get());
11786 }
Richard Trieucc3949d2016-02-18 22:34:54 +000011787 ++CurInit;
11788 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +000011789 }
11790 Clause.setUpdates(Updates);
11791 Clause.setFinals(Finals);
11792 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +000011793}
11794
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011795OMPClause *Sema::ActOnOpenMPAlignedClause(
11796 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
11797 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011798 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +000011799 for (Expr *RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +000011800 assert(RefExpr && "NULL expr in OpenMP linear clause.");
11801 SourceLocation ELoc;
11802 SourceRange ERange;
11803 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011804 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev1efd1662016-03-29 10:59:56 +000011805 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011806 // It will be analyzed later.
11807 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011808 }
Alexey Bataev1efd1662016-03-29 10:59:56 +000011809 ValueDecl *D = Res.first;
11810 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011811 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011812
Alexey Bataev1efd1662016-03-29 10:59:56 +000011813 QualType QType = D->getType();
11814 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011815
11816 // OpenMP [2.8.1, simd construct, Restrictions]
11817 // The type of list items appearing in the aligned clause must be
11818 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011819 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011820 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +000011821 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011822 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +000011823 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011824 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +000011825 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011826 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +000011827 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011828 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +000011829 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011830 continue;
11831 }
11832
11833 // OpenMP [2.8.1, simd construct, Restrictions]
11834 // A list-item cannot appear in more than one aligned clause.
Alexey Bataeve3727102018-04-18 15:57:46 +000011835 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +000011836 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011837 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
11838 << getOpenMPClauseName(OMPC_aligned);
11839 continue;
11840 }
11841
Alexey Bataev1efd1662016-03-29 10:59:56 +000011842 DeclRefExpr *Ref = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000011843 if (!VD && isOpenMPCapturedDecl(D))
Alexey Bataev1efd1662016-03-29 10:59:56 +000011844 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
11845 Vars.push_back(DefaultFunctionArrayConversion(
11846 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
11847 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011848 }
11849
11850 // OpenMP [2.8.1, simd construct, Description]
11851 // The parameter of the aligned clause, alignment, must be a constant
11852 // positive integer expression.
11853 // If no optional parameter is specified, implementation-defined default
11854 // alignments for SIMD instructions on the target platforms are assumed.
11855 if (Alignment != nullptr) {
11856 ExprResult AlignResult =
11857 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
11858 if (AlignResult.isInvalid())
11859 return nullptr;
11860 Alignment = AlignResult.get();
11861 }
11862 if (Vars.empty())
11863 return nullptr;
11864
11865 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
11866 EndLoc, Vars, Alignment);
11867}
11868
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011869OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
11870 SourceLocation StartLoc,
11871 SourceLocation LParenLoc,
11872 SourceLocation EndLoc) {
11873 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011874 SmallVector<Expr *, 8> SrcExprs;
11875 SmallVector<Expr *, 8> DstExprs;
11876 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeve3727102018-04-18 15:57:46 +000011877 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000011878 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
11879 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011880 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000011881 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011882 SrcExprs.push_back(nullptr);
11883 DstExprs.push_back(nullptr);
11884 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011885 continue;
11886 }
11887
Alexey Bataeved09d242014-05-28 05:53:51 +000011888 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011889 // OpenMP [2.1, C/C++]
11890 // A list item is a variable name.
11891 // OpenMP [2.14.4.1, Restrictions, p.1]
11892 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeve3727102018-04-18 15:57:46 +000011893 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011894 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000011895 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
11896 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011897 continue;
11898 }
11899
11900 Decl *D = DE->getDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000011901 auto *VD = cast<VarDecl>(D);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011902
11903 QualType Type = VD->getType();
11904 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
11905 // It will be analyzed later.
11906 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011907 SrcExprs.push_back(nullptr);
11908 DstExprs.push_back(nullptr);
11909 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011910 continue;
11911 }
11912
11913 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
11914 // A list item that appears in a copyin clause must be threadprivate.
11915 if (!DSAStack->isThreadPrivate(VD)) {
11916 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +000011917 << getOpenMPClauseName(OMPC_copyin)
11918 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011919 continue;
11920 }
11921
11922 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
11923 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +000011924 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011925 // operator for the class type.
Alexey Bataeve3727102018-04-18 15:57:46 +000011926 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
11927 VarDecl *SrcVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011928 buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000011929 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011930 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011931 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +000011932 VarDecl *DstVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011933 buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst",
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000011934 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011935 DeclRefExpr *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011936 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011937 // For arrays generate assignment operation for single element and replace
11938 // it by the original array element in CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +000011939 ExprResult AssignmentOp =
11940 BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr,
11941 PseudoSrcExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011942 if (AssignmentOp.isInvalid())
11943 continue;
11944 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000011945 /*DiscardedValue*/ false);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011946 if (AssignmentOp.isInvalid())
11947 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011948
11949 DSAStack->addDSA(VD, DE, OMPC_copyin);
11950 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011951 SrcExprs.push_back(PseudoSrcExpr);
11952 DstExprs.push_back(PseudoDstExpr);
11953 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011954 }
11955
Alexey Bataeved09d242014-05-28 05:53:51 +000011956 if (Vars.empty())
11957 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011958
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011959 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
11960 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011961}
11962
Alexey Bataevbae9a792014-06-27 10:37:06 +000011963OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
11964 SourceLocation StartLoc,
11965 SourceLocation LParenLoc,
11966 SourceLocation EndLoc) {
11967 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +000011968 SmallVector<Expr *, 8> SrcExprs;
11969 SmallVector<Expr *, 8> DstExprs;
11970 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeve3727102018-04-18 15:57:46 +000011971 for (Expr *RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +000011972 assert(RefExpr && "NULL expr in OpenMP linear clause.");
11973 SourceLocation ELoc;
11974 SourceRange ERange;
11975 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011976 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataeve122da12016-03-17 10:50:17 +000011977 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000011978 // It will be analyzed later.
11979 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000011980 SrcExprs.push_back(nullptr);
11981 DstExprs.push_back(nullptr);
11982 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011983 }
Alexey Bataeve122da12016-03-17 10:50:17 +000011984 ValueDecl *D = Res.first;
11985 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +000011986 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000011987
Alexey Bataeve122da12016-03-17 10:50:17 +000011988 QualType Type = D->getType();
11989 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011990
11991 // OpenMP [2.14.4.2, Restrictions, p.2]
11992 // A list item that appears in a copyprivate clause may not appear in a
11993 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +000011994 if (!VD || !DSAStack->isThreadPrivate(VD)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011995 DSAStackTy::DSAVarData DVar =
11996 DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataeva63048e2015-03-23 06:18:07 +000011997 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
11998 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000011999 Diag(ELoc, diag::err_omp_wrong_dsa)
12000 << getOpenMPClauseName(DVar.CKind)
12001 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000012002 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000012003 continue;
12004 }
12005
12006 // OpenMP [2.11.4.2, Restrictions, p.1]
12007 // All list items that appear in a copyprivate clause must be either
12008 // threadprivate or private in the enclosing context.
12009 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +000012010 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +000012011 if (DVar.CKind == OMPC_shared) {
12012 Diag(ELoc, diag::err_omp_required_access)
12013 << getOpenMPClauseName(OMPC_copyprivate)
12014 << "threadprivate or private in the enclosing context";
Alexey Bataeve3727102018-04-18 15:57:46 +000012015 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000012016 continue;
12017 }
12018 }
12019 }
12020
Alexey Bataev7a3e5852015-05-19 08:19:24 +000012021 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000012022 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +000012023 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +000012024 << getOpenMPClauseName(OMPC_copyprivate) << Type
12025 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +000012026 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +000012027 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +000012028 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +000012029 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +000012030 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +000012031 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +000012032 continue;
12033 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +000012034
Alexey Bataevbae9a792014-06-27 10:37:06 +000012035 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
12036 // A variable of class type (or array thereof) that appears in a
12037 // copyin clause requires an accessible, unambiguous copy assignment
12038 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +000012039 Type = Context.getBaseElementType(Type.getNonReferenceType())
12040 .getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000012041 VarDecl *SrcVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000012042 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src",
Alexey Bataeve122da12016-03-17 10:50:17 +000012043 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000012044 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
12045 VarDecl *DstVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000012046 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst",
Alexey Bataeve122da12016-03-17 10:50:17 +000012047 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000012048 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
12049 ExprResult AssignmentOp = BuildBinOp(
12050 DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000012051 if (AssignmentOp.isInvalid())
12052 continue;
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000012053 AssignmentOp =
12054 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataeva63048e2015-03-23 06:18:07 +000012055 if (AssignmentOp.isInvalid())
12056 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000012057
12058 // No need to mark vars as copyprivate, they are already threadprivate or
12059 // implicitly private.
Alexey Bataeve3727102018-04-18 15:57:46 +000012060 assert(VD || isOpenMPCapturedDecl(D));
Alexey Bataeve122da12016-03-17 10:50:17 +000012061 Vars.push_back(
12062 VD ? RefExpr->IgnoreParens()
12063 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +000012064 SrcExprs.push_back(PseudoSrcExpr);
12065 DstExprs.push_back(PseudoDstExpr);
12066 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +000012067 }
12068
12069 if (Vars.empty())
12070 return nullptr;
12071
Alexey Bataeva63048e2015-03-23 06:18:07 +000012072 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
12073 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +000012074}
12075
Alexey Bataev6125da92014-07-21 11:26:11 +000012076OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
12077 SourceLocation StartLoc,
12078 SourceLocation LParenLoc,
12079 SourceLocation EndLoc) {
12080 if (VarList.empty())
12081 return nullptr;
12082
12083 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
12084}
Alexey Bataevdea47612014-07-23 07:46:59 +000012085
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012086OMPClause *
12087Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
12088 SourceLocation DepLoc, SourceLocation ColonLoc,
12089 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
12090 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +000012091 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012092 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +000012093 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000012094 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +000012095 return nullptr;
12096 }
12097 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012098 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
12099 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +000012100 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012101 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000012102 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
12103 /*Last=*/OMPC_DEPEND_unknown, Except)
12104 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012105 return nullptr;
12106 }
12107 SmallVector<Expr *, 8> Vars;
Alexey Bataev8b427062016-05-25 12:36:08 +000012108 DSAStackTy::OperatorOffsetTy OpsOffs;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012109 llvm::APSInt DepCounter(/*BitWidth=*/32);
12110 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
Alexey Bataevf138fda2018-08-13 19:04:24 +000012111 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) {
12112 if (const Expr *OrderedCountExpr =
12113 DSAStack->getParentOrderedRegionParam().first) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012114 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
12115 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012116 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012117 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012118 for (Expr *RefExpr : VarList) {
Alexey Bataev17daedf2018-02-15 22:42:57 +000012119 assert(RefExpr && "NULL expr in OpenMP shared clause.");
12120 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
12121 // It will be analyzed later.
12122 Vars.push_back(RefExpr);
12123 continue;
12124 }
12125
12126 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataeve3727102018-04-18 15:57:46 +000012127 Expr *SimpleExpr = RefExpr->IgnoreParenCasts();
Alexey Bataev17daedf2018-02-15 22:42:57 +000012128 if (DepKind == OMPC_DEPEND_sink) {
Alexey Bataevf138fda2018-08-13 19:04:24 +000012129 if (DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000012130 DepCounter >= TotalDepCount) {
12131 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
12132 continue;
12133 }
12134 ++DepCounter;
12135 // OpenMP [2.13.9, Summary]
12136 // depend(dependence-type : vec), where dependence-type is:
12137 // 'sink' and where vec is the iteration vector, which has the form:
12138 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
12139 // where n is the value specified by the ordered clause in the loop
12140 // directive, xi denotes the loop iteration variable of the i-th nested
12141 // loop associated with the loop directive, and di is a constant
12142 // non-negative integer.
12143 if (CurContext->isDependentContext()) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012144 // It will be analyzed later.
12145 Vars.push_back(RefExpr);
12146 continue;
12147 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000012148 SimpleExpr = SimpleExpr->IgnoreImplicit();
12149 OverloadedOperatorKind OOK = OO_None;
12150 SourceLocation OOLoc;
12151 Expr *LHS = SimpleExpr;
12152 Expr *RHS = nullptr;
12153 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
12154 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
12155 OOLoc = BO->getOperatorLoc();
12156 LHS = BO->getLHS()->IgnoreParenImpCasts();
12157 RHS = BO->getRHS()->IgnoreParenImpCasts();
12158 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
12159 OOK = OCE->getOperator();
12160 OOLoc = OCE->getOperatorLoc();
12161 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
12162 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
12163 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
12164 OOK = MCE->getMethodDecl()
12165 ->getNameInfo()
12166 .getName()
12167 .getCXXOverloadedOperator();
12168 OOLoc = MCE->getCallee()->getExprLoc();
12169 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
12170 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012171 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000012172 SourceLocation ELoc;
12173 SourceRange ERange;
Alexey Bataevbc529672018-09-28 19:33:14 +000012174 auto Res = getPrivateItem(*this, LHS, ELoc, ERange);
Alexey Bataev17daedf2018-02-15 22:42:57 +000012175 if (Res.second) {
12176 // It will be analyzed later.
12177 Vars.push_back(RefExpr);
12178 }
12179 ValueDecl *D = Res.first;
12180 if (!D)
12181 continue;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012182
Alexey Bataev17daedf2018-02-15 22:42:57 +000012183 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
12184 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
12185 continue;
12186 }
12187 if (RHS) {
12188 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
12189 RHS, OMPC_depend, /*StrictlyPositive=*/false);
12190 if (RHSRes.isInvalid())
12191 continue;
12192 }
12193 if (!CurContext->isDependentContext() &&
Alexey Bataevf138fda2018-08-13 19:04:24 +000012194 DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000012195 DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012196 const ValueDecl *VD =
Alexey Bataev17daedf2018-02-15 22:42:57 +000012197 DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue());
Alexey Bataeve3727102018-04-18 15:57:46 +000012198 if (VD)
Alexey Bataev17daedf2018-02-15 22:42:57 +000012199 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
12200 << 1 << VD;
Alexey Bataeve3727102018-04-18 15:57:46 +000012201 else
Alexey Bataev17daedf2018-02-15 22:42:57 +000012202 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0;
Alexey Bataev17daedf2018-02-15 22:42:57 +000012203 continue;
12204 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012205 OpsOffs.emplace_back(RHS, OOK);
Alexey Bataev17daedf2018-02-15 22:42:57 +000012206 } else {
12207 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
12208 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
12209 (ASE &&
12210 !ASE->getBase()->getType().getNonReferenceType()->isPointerType() &&
12211 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
12212 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
12213 << RefExpr->getSourceRange();
12214 continue;
12215 }
12216 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
12217 getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
12218 ExprResult Res =
12219 CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RefExpr->IgnoreParenImpCasts());
12220 getDiagnostics().setSuppressAllDiagnostics(Suppress);
12221 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr)) {
12222 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
12223 << RefExpr->getSourceRange();
12224 continue;
12225 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012226 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000012227 Vars.push_back(RefExpr->IgnoreParenImpCasts());
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012228 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000012229
12230 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
12231 TotalDepCount > VarList.size() &&
Alexey Bataevf138fda2018-08-13 19:04:24 +000012232 DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000012233 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) {
12234 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
12235 << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
12236 }
12237 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
12238 Vars.empty())
12239 return nullptr;
12240
Alexey Bataev8b427062016-05-25 12:36:08 +000012241 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataevf138fda2018-08-13 19:04:24 +000012242 DepKind, DepLoc, ColonLoc, Vars,
12243 TotalDepCount.getZExtValue());
Alexey Bataev17daedf2018-02-15 22:42:57 +000012244 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) &&
12245 DSAStack->isParentOrderedRegion())
Alexey Bataev8b427062016-05-25 12:36:08 +000012246 DSAStack->addDoacrossDependClause(C, OpsOffs);
12247 return C;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012248}
Michael Wonge710d542015-08-07 16:16:36 +000012249
12250OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
12251 SourceLocation LParenLoc,
12252 SourceLocation EndLoc) {
12253 Expr *ValExpr = Device;
Alexey Bataev931e19b2017-10-02 16:32:39 +000012254 Stmt *HelperValStmt = nullptr;
Michael Wonge710d542015-08-07 16:16:36 +000012255
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012256 // OpenMP [2.9.1, Restrictions]
12257 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000012258 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
Alexey Bataeva0569352015-12-01 10:17:31 +000012259 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012260 return nullptr;
12261
Alexey Bataev931e19b2017-10-02 16:32:39 +000012262 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000012263 OpenMPDirectiveKind CaptureRegion =
12264 getOpenMPCaptureRegionForClause(DKind, OMPC_device);
12265 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000012266 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000012267 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev931e19b2017-10-02 16:32:39 +000012268 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
12269 HelperValStmt = buildPreInits(Context, Captures);
12270 }
12271
Alexey Bataev8451efa2018-01-15 19:06:12 +000012272 return new (Context) OMPDeviceClause(ValExpr, HelperValStmt, CaptureRegion,
12273 StartLoc, LParenLoc, EndLoc);
Michael Wonge710d542015-08-07 16:16:36 +000012274}
Kelvin Li0bff7af2015-11-23 05:32:03 +000012275
Alexey Bataeve3727102018-04-18 15:57:46 +000012276static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
Alexey Bataev95c23e72018-02-27 21:31:11 +000012277 DSAStackTy *Stack, QualType QTy,
12278 bool FullCheck = true) {
Kelvin Li0bff7af2015-11-23 05:32:03 +000012279 NamedDecl *ND;
12280 if (QTy->isIncompleteType(&ND)) {
12281 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
12282 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +000012283 }
Alexey Bataev95c23e72018-02-27 21:31:11 +000012284 if (FullCheck && !SemaRef.CurContext->isDependentContext() &&
12285 !QTy.isTrivialType(SemaRef.Context))
12286 SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR;
Kelvin Li0bff7af2015-11-23 05:32:03 +000012287 return true;
12288}
12289
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000012290/// Return true if it can be proven that the provided array expression
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012291/// (array section or array subscript) does NOT specify the whole size of the
12292/// array whose base type is \a BaseQTy.
Alexey Bataeve3727102018-04-18 15:57:46 +000012293static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012294 const Expr *E,
12295 QualType BaseQTy) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012296 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012297
12298 // If this is an array subscript, it refers to the whole size if the size of
12299 // the dimension is constant and equals 1. Also, an array section assumes the
12300 // format of an array subscript if no colon is used.
12301 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012302 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012303 return ATy->getSize().getSExtValue() != 1;
12304 // Size can't be evaluated statically.
12305 return false;
12306 }
12307
12308 assert(OASE && "Expecting array section if not an array subscript.");
Alexey Bataeve3727102018-04-18 15:57:46 +000012309 const Expr *LowerBound = OASE->getLowerBound();
12310 const Expr *Length = OASE->getLength();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012311
12312 // If there is a lower bound that does not evaluates to zero, we are not
David Majnemer9d168222016-08-05 17:44:54 +000012313 // covering the whole dimension.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012314 if (LowerBound) {
Fangrui Song407659a2018-11-30 23:41:18 +000012315 Expr::EvalResult Result;
12316 if (!LowerBound->EvaluateAsInt(Result, SemaRef.getASTContext()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012317 return false; // Can't get the integer value as a constant.
Fangrui Song407659a2018-11-30 23:41:18 +000012318
12319 llvm::APSInt ConstLowerBound = Result.Val.getInt();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012320 if (ConstLowerBound.getSExtValue())
12321 return true;
12322 }
12323
12324 // If we don't have a length we covering the whole dimension.
12325 if (!Length)
12326 return false;
12327
12328 // If the base is a pointer, we don't have a way to get the size of the
12329 // pointee.
12330 if (BaseQTy->isPointerType())
12331 return false;
12332
12333 // We can only check if the length is the same as the size of the dimension
12334 // if we have a constant array.
Alexey Bataeve3727102018-04-18 15:57:46 +000012335 const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012336 if (!CATy)
12337 return false;
12338
Fangrui Song407659a2018-11-30 23:41:18 +000012339 Expr::EvalResult Result;
12340 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012341 return false; // Can't get the integer value as a constant.
12342
Fangrui Song407659a2018-11-30 23:41:18 +000012343 llvm::APSInt ConstLength = Result.Val.getInt();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012344 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
12345}
12346
12347// Return true if it can be proven that the provided array expression (array
12348// section or array subscript) does NOT specify a single element of the array
12349// whose base type is \a BaseQTy.
Alexey Bataeve3727102018-04-18 15:57:46 +000012350static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
David Majnemer9d168222016-08-05 17:44:54 +000012351 const Expr *E,
12352 QualType BaseQTy) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012353 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012354
12355 // An array subscript always refer to a single element. Also, an array section
12356 // assumes the format of an array subscript if no colon is used.
12357 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
12358 return false;
12359
12360 assert(OASE && "Expecting array section if not an array subscript.");
Alexey Bataeve3727102018-04-18 15:57:46 +000012361 const Expr *Length = OASE->getLength();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012362
12363 // If we don't have a length we have to check if the array has unitary size
12364 // for this dimension. Also, we should always expect a length if the base type
12365 // is pointer.
12366 if (!Length) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012367 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012368 return ATy->getSize().getSExtValue() != 1;
12369 // We cannot assume anything.
12370 return false;
12371 }
12372
12373 // Check if the length evaluates to 1.
Fangrui Song407659a2018-11-30 23:41:18 +000012374 Expr::EvalResult Result;
12375 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012376 return false; // Can't get the integer value as a constant.
12377
Fangrui Song407659a2018-11-30 23:41:18 +000012378 llvm::APSInt ConstLength = Result.Val.getInt();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012379 return ConstLength.getSExtValue() != 1;
12380}
12381
Samuel Antao661c0902016-05-26 17:39:58 +000012382// Return the expression of the base of the mappable expression or null if it
12383// cannot be determined and do all the necessary checks to see if the expression
12384// is valid as a standalone mappable expression. In the process, record all the
Samuel Antao90927002016-04-26 14:54:23 +000012385// components of the expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000012386static const Expr *checkMapClauseExpressionBase(
Samuel Antao90927002016-04-26 14:54:23 +000012387 Sema &SemaRef, Expr *E,
Samuel Antao661c0902016-05-26 17:39:58 +000012388 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012389 OpenMPClauseKind CKind, bool NoDiagnose) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012390 SourceLocation ELoc = E->getExprLoc();
12391 SourceRange ERange = E->getSourceRange();
12392
12393 // The base of elements of list in a map clause have to be either:
12394 // - a reference to variable or field.
12395 // - a member expression.
12396 // - an array expression.
12397 //
12398 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
12399 // reference to 'r'.
12400 //
12401 // If we have:
12402 //
12403 // struct SS {
12404 // Bla S;
12405 // foo() {
12406 // #pragma omp target map (S.Arr[:12]);
12407 // }
12408 // }
12409 //
12410 // We want to retrieve the member expression 'this->S';
12411
Alexey Bataeve3727102018-04-18 15:57:46 +000012412 const Expr *RelevantExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012413
Samuel Antao5de996e2016-01-22 20:21:36 +000012414 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
12415 // If a list item is an array section, it must specify contiguous storage.
12416 //
12417 // For this restriction it is sufficient that we make sure only references
12418 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012419 // exist except in the rightmost expression (unless they cover the whole
12420 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +000012421 //
12422 // r.ArrS[3:5].Arr[6:7]
12423 //
12424 // r.ArrS[3:5].x
12425 //
12426 // but these would be valid:
12427 // r.ArrS[3].Arr[6:7]
12428 //
12429 // r.ArrS[3].x
12430
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012431 bool AllowUnitySizeArraySection = true;
12432 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +000012433
Dmitry Polukhin644a9252016-03-11 07:58:34 +000012434 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012435 E = E->IgnoreParenImpCasts();
12436
12437 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
12438 if (!isa<VarDecl>(CurE->getDecl()))
Alexey Bataev27041fa2017-12-05 15:22:49 +000012439 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012440
12441 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012442
12443 // If we got a reference to a declaration, we should not expect any array
12444 // section before that.
12445 AllowUnitySizeArraySection = false;
12446 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000012447
12448 // Record the component.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012449 CurComponents.emplace_back(CurE, CurE->getDecl());
12450 } else if (auto *CurE = dyn_cast<MemberExpr>(E)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012451 Expr *BaseE = CurE->getBase()->IgnoreParenImpCasts();
Samuel Antao5de996e2016-01-22 20:21:36 +000012452
12453 if (isa<CXXThisExpr>(BaseE))
12454 // We found a base expression: this->Val.
12455 RelevantExpr = CurE;
12456 else
12457 E = BaseE;
12458
12459 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012460 if (!NoDiagnose) {
12461 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
12462 << CurE->getSourceRange();
12463 return nullptr;
12464 }
12465 if (RelevantExpr)
12466 return nullptr;
12467 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000012468 }
12469
12470 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
12471
12472 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
12473 // A bit-field cannot appear in a map clause.
12474 //
12475 if (FD->isBitField()) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012476 if (!NoDiagnose) {
12477 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
12478 << CurE->getSourceRange() << getOpenMPClauseName(CKind);
12479 return nullptr;
12480 }
12481 if (RelevantExpr)
12482 return nullptr;
12483 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000012484 }
12485
12486 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12487 // If the type of a list item is a reference to a type T then the type
12488 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012489 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000012490
12491 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
12492 // A list item cannot be a variable that is a member of a structure with
12493 // a union type.
12494 //
Alexey Bataeve3727102018-04-18 15:57:46 +000012495 if (CurType->isUnionType()) {
12496 if (!NoDiagnose) {
12497 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
12498 << CurE->getSourceRange();
12499 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012500 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012501 continue;
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012502 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012503
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012504 // If we got a member expression, we should not expect any array section
12505 // before that:
12506 //
12507 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
12508 // If a list item is an element of a structure, only the rightmost symbol
12509 // of the variable reference can be an array section.
12510 //
12511 AllowUnitySizeArraySection = false;
12512 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000012513
12514 // Record the component.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012515 CurComponents.emplace_back(CurE, FD);
12516 } else if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012517 E = CurE->getBase()->IgnoreParenImpCasts();
12518
12519 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012520 if (!NoDiagnose) {
12521 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
12522 << 0 << CurE->getSourceRange();
12523 return nullptr;
12524 }
12525 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000012526 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012527
12528 // If we got an array subscript that express the whole dimension we
12529 // can have any array expressions before. If it only expressing part of
12530 // the dimension, we can only have unitary-size array expressions.
Alexey Bataeve3727102018-04-18 15:57:46 +000012531 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012532 E->getType()))
12533 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000012534
Patrick Lystere13b1e32019-01-02 19:28:48 +000012535 if (const auto *TE = dyn_cast<CXXThisExpr>(E)) {
12536 Expr::EvalResult Result;
12537 if (CurE->getIdx()->EvaluateAsInt(Result, SemaRef.getASTContext())) {
12538 if (!Result.Val.getInt().isNullValue()) {
12539 SemaRef.Diag(CurE->getIdx()->getExprLoc(),
12540 diag::err_omp_invalid_map_this_expr);
12541 SemaRef.Diag(CurE->getIdx()->getExprLoc(),
12542 diag::note_omp_invalid_subscript_on_this_ptr_map);
12543 }
12544 }
12545 RelevantExpr = TE;
12546 }
12547
Samuel Antao90927002016-04-26 14:54:23 +000012548 // Record the component - we don't have any declaration associated.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012549 CurComponents.emplace_back(CurE, nullptr);
12550 } else if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012551 assert(!NoDiagnose && "Array sections cannot be implicitly mapped.");
Samuel Antao5de996e2016-01-22 20:21:36 +000012552 E = CurE->getBase()->IgnoreParenImpCasts();
12553
Alexey Bataev27041fa2017-12-05 15:22:49 +000012554 QualType CurType =
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012555 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
12556
Samuel Antao5de996e2016-01-22 20:21:36 +000012557 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12558 // If the type of a list item is a reference to a type T then the type
12559 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +000012560 if (CurType->isReferenceType())
12561 CurType = CurType->getPointeeType();
12562
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012563 bool IsPointer = CurType->isAnyPointerType();
12564
12565 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012566 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
12567 << 0 << CurE->getSourceRange();
Alexey Bataev27041fa2017-12-05 15:22:49 +000012568 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012569 }
12570
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012571 bool NotWhole =
Alexey Bataeve3727102018-04-18 15:57:46 +000012572 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012573 bool NotUnity =
Alexey Bataeve3727102018-04-18 15:57:46 +000012574 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012575
Samuel Antaodab51bb2016-07-18 23:22:11 +000012576 if (AllowWholeSizeArraySection) {
12577 // Any array section is currently allowed. Allowing a whole size array
12578 // section implies allowing a unity array section as well.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012579 //
12580 // If this array section refers to the whole dimension we can still
12581 // accept other array sections before this one, except if the base is a
12582 // pointer. Otherwise, only unitary sections are accepted.
12583 if (NotWhole || IsPointer)
12584 AllowWholeSizeArraySection = false;
Samuel Antaodab51bb2016-07-18 23:22:11 +000012585 } else if (AllowUnitySizeArraySection && NotUnity) {
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012586 // A unity or whole array section is not allowed and that is not
12587 // compatible with the properties of the current array section.
12588 SemaRef.Diag(
12589 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
12590 << CurE->getSourceRange();
Alexey Bataev27041fa2017-12-05 15:22:49 +000012591 return nullptr;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012592 }
Samuel Antao90927002016-04-26 14:54:23 +000012593
Patrick Lystere13b1e32019-01-02 19:28:48 +000012594 if (const auto *TE = dyn_cast<CXXThisExpr>(E)) {
12595 Expr::EvalResult ResultR;
12596 Expr::EvalResult ResultL;
12597 if (CurE->getLength()->EvaluateAsInt(ResultR,
12598 SemaRef.getASTContext())) {
12599 if (!ResultR.Val.getInt().isOneValue()) {
12600 SemaRef.Diag(CurE->getLength()->getExprLoc(),
12601 diag::err_omp_invalid_map_this_expr);
12602 SemaRef.Diag(CurE->getLength()->getExprLoc(),
12603 diag::note_omp_invalid_length_on_this_ptr_mapping);
12604 }
12605 }
12606 if (CurE->getLowerBound() && CurE->getLowerBound()->EvaluateAsInt(
12607 ResultL, SemaRef.getASTContext())) {
12608 if (!ResultL.Val.getInt().isNullValue()) {
12609 SemaRef.Diag(CurE->getLowerBound()->getExprLoc(),
12610 diag::err_omp_invalid_map_this_expr);
12611 SemaRef.Diag(CurE->getLowerBound()->getExprLoc(),
12612 diag::note_omp_invalid_lower_bound_on_this_ptr_mapping);
12613 }
12614 }
12615 RelevantExpr = TE;
12616 }
12617
Samuel Antao90927002016-04-26 14:54:23 +000012618 // Record the component - we don't have any declaration associated.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012619 CurComponents.emplace_back(CurE, nullptr);
12620 } else {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012621 if (!NoDiagnose) {
12622 // If nothing else worked, this is not a valid map clause expression.
12623 SemaRef.Diag(
12624 ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
12625 << ERange;
12626 }
Alexey Bataev27041fa2017-12-05 15:22:49 +000012627 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012628 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012629 }
12630
12631 return RelevantExpr;
12632}
12633
12634// Return true if expression E associated with value VD has conflicts with other
12635// map information.
Alexey Bataeve3727102018-04-18 15:57:46 +000012636static bool checkMapConflicts(
12637 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E,
Samuel Antao90927002016-04-26 14:54:23 +000012638 bool CurrentRegionOnly,
Samuel Antao661c0902016-05-26 17:39:58 +000012639 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
12640 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012641 assert(VD && E);
Samuel Antao5de996e2016-01-22 20:21:36 +000012642 SourceLocation ELoc = E->getExprLoc();
12643 SourceRange ERange = E->getSourceRange();
12644
12645 // In order to easily check the conflicts we need to match each component of
12646 // the expression under test with the components of the expressions that are
12647 // already in the stack.
12648
Samuel Antao5de996e2016-01-22 20:21:36 +000012649 assert(!CurComponents.empty() && "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000012650 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000012651 "Map clause expression with unexpected base!");
12652
12653 // Variables to help detecting enclosing problems in data environment nests.
12654 bool IsEnclosedByDataEnvironmentExpr = false;
Samuel Antao90927002016-04-26 14:54:23 +000012655 const Expr *EnclosingExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012656
Samuel Antao90927002016-04-26 14:54:23 +000012657 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
12658 VD, CurrentRegionOnly,
Alexey Bataeve3727102018-04-18 15:57:46 +000012659 [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc,
12660 ERange, CKind, &EnclosingExpr,
12661 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef
12662 StackComponents,
12663 OpenMPClauseKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012664 assert(!StackComponents.empty() &&
12665 "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000012666 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000012667 "Map clause expression with unexpected base!");
Fangrui Song16fe49a2018-04-18 19:32:01 +000012668 (void)VD;
Samuel Antao5de996e2016-01-22 20:21:36 +000012669
Samuel Antao90927002016-04-26 14:54:23 +000012670 // The whole expression in the stack.
Alexey Bataeve3727102018-04-18 15:57:46 +000012671 const Expr *RE = StackComponents.front().getAssociatedExpression();
Samuel Antao90927002016-04-26 14:54:23 +000012672
Samuel Antao5de996e2016-01-22 20:21:36 +000012673 // Expressions must start from the same base. Here we detect at which
12674 // point both expressions diverge from each other and see if we can
12675 // detect if the memory referred to both expressions is contiguous and
12676 // do not overlap.
12677 auto CI = CurComponents.rbegin();
12678 auto CE = CurComponents.rend();
12679 auto SI = StackComponents.rbegin();
12680 auto SE = StackComponents.rend();
12681 for (; CI != CE && SI != SE; ++CI, ++SI) {
12682
12683 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
12684 // At most one list item can be an array item derived from a given
12685 // variable in map clauses of the same construct.
Samuel Antao90927002016-04-26 14:54:23 +000012686 if (CurrentRegionOnly &&
12687 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
12688 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
12689 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
12690 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
12691 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
Samuel Antao5de996e2016-01-22 20:21:36 +000012692 diag::err_omp_multiple_array_items_in_map_clause)
Samuel Antao90927002016-04-26 14:54:23 +000012693 << CI->getAssociatedExpression()->getSourceRange();
12694 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
12695 diag::note_used_here)
12696 << SI->getAssociatedExpression()->getSourceRange();
Samuel Antao5de996e2016-01-22 20:21:36 +000012697 return true;
12698 }
12699
12700 // Do both expressions have the same kind?
Samuel Antao90927002016-04-26 14:54:23 +000012701 if (CI->getAssociatedExpression()->getStmtClass() !=
12702 SI->getAssociatedExpression()->getStmtClass())
Samuel Antao5de996e2016-01-22 20:21:36 +000012703 break;
12704
12705 // Are we dealing with different variables/fields?
Samuel Antao90927002016-04-26 14:54:23 +000012706 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
Samuel Antao5de996e2016-01-22 20:21:36 +000012707 break;
12708 }
Kelvin Li9f645ae2016-07-18 22:49:16 +000012709 // Check if the extra components of the expressions in the enclosing
12710 // data environment are redundant for the current base declaration.
12711 // If they are, the maps completely overlap, which is legal.
12712 for (; SI != SE; ++SI) {
12713 QualType Type;
Alexey Bataeve3727102018-04-18 15:57:46 +000012714 if (const auto *ASE =
David Majnemer9d168222016-08-05 17:44:54 +000012715 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
Kelvin Li9f645ae2016-07-18 22:49:16 +000012716 Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
Alexey Bataeve3727102018-04-18 15:57:46 +000012717 } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(
David Majnemer9d168222016-08-05 17:44:54 +000012718 SI->getAssociatedExpression())) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012719 const Expr *E = OASE->getBase()->IgnoreParenImpCasts();
Kelvin Li9f645ae2016-07-18 22:49:16 +000012720 Type =
12721 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
12722 }
12723 if (Type.isNull() || Type->isAnyPointerType() ||
Alexey Bataeve3727102018-04-18 15:57:46 +000012724 checkArrayExpressionDoesNotReferToWholeSize(
Kelvin Li9f645ae2016-07-18 22:49:16 +000012725 SemaRef, SI->getAssociatedExpression(), Type))
12726 break;
12727 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012728
12729 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
12730 // List items of map clauses in the same construct must not share
12731 // original storage.
12732 //
12733 // If the expressions are exactly the same or one is a subset of the
12734 // other, it means they are sharing storage.
12735 if (CI == CE && SI == SE) {
12736 if (CurrentRegionOnly) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012737 if (CKind == OMPC_map) {
Samuel Antao661c0902016-05-26 17:39:58 +000012738 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +000012739 } else {
Samuel Antaoec172c62016-05-26 17:49:04 +000012740 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000012741 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
12742 << ERange;
12743 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012744 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12745 << RE->getSourceRange();
12746 return true;
Samuel Antao5de996e2016-01-22 20:21:36 +000012747 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012748 // If we find the same expression in the enclosing data environment,
12749 // that is legal.
12750 IsEnclosedByDataEnvironmentExpr = true;
12751 return false;
Samuel Antao5de996e2016-01-22 20:21:36 +000012752 }
12753
Samuel Antao90927002016-04-26 14:54:23 +000012754 QualType DerivedType =
12755 std::prev(CI)->getAssociatedDeclaration()->getType();
12756 SourceLocation DerivedLoc =
12757 std::prev(CI)->getAssociatedExpression()->getExprLoc();
Samuel Antao5de996e2016-01-22 20:21:36 +000012758
12759 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12760 // If the type of a list item is a reference to a type T then the type
12761 // will be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000012762 DerivedType = DerivedType.getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000012763
12764 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
12765 // A variable for which the type is pointer and an array section
12766 // derived from that variable must not appear as list items of map
12767 // clauses of the same construct.
12768 //
12769 // Also, cover one of the cases in:
12770 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
12771 // If any part of the original storage of a list item has corresponding
12772 // storage in the device data environment, all of the original storage
12773 // must have corresponding storage in the device data environment.
12774 //
12775 if (DerivedType->isAnyPointerType()) {
12776 if (CI == CE || SI == SE) {
12777 SemaRef.Diag(
12778 DerivedLoc,
12779 diag::err_omp_pointer_mapped_along_with_derived_section)
12780 << DerivedLoc;
Alexey Bataev2819260b2018-02-27 17:42:00 +000012781 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12782 << RE->getSourceRange();
12783 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +000012784 }
12785 if (CI->getAssociatedExpression()->getStmtClass() !=
Alexey Bataev2819260b2018-02-27 17:42:00 +000012786 SI->getAssociatedExpression()->getStmtClass() ||
12787 CI->getAssociatedDeclaration()->getCanonicalDecl() ==
12788 SI->getAssociatedDeclaration()->getCanonicalDecl()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012789 assert(CI != CE && SI != SE);
Alexey Bataev2819260b2018-02-27 17:42:00 +000012790 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced)
Samuel Antao5de996e2016-01-22 20:21:36 +000012791 << DerivedLoc;
Alexey Bataev2819260b2018-02-27 17:42:00 +000012792 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12793 << RE->getSourceRange();
12794 return true;
Samuel Antao5de996e2016-01-22 20:21:36 +000012795 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012796 }
12797
12798 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
12799 // List items of map clauses in the same construct must not share
12800 // original storage.
12801 //
12802 // An expression is a subset of the other.
12803 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012804 if (CKind == OMPC_map) {
Alexey Bataeve82445f2018-09-20 13:54:02 +000012805 if (CI != CE || SI != SE) {
12806 // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is
12807 // a pointer.
12808 auto Begin =
12809 CI != CE ? CurComponents.begin() : StackComponents.begin();
12810 auto End = CI != CE ? CurComponents.end() : StackComponents.end();
12811 auto It = Begin;
12812 while (It != End && !It->getAssociatedDeclaration())
12813 std::advance(It, 1);
12814 assert(It != End &&
12815 "Expected at least one component with the declaration.");
12816 if (It != Begin && It->getAssociatedDeclaration()
12817 ->getType()
12818 .getCanonicalType()
12819 ->isAnyPointerType()) {
12820 IsEnclosedByDataEnvironmentExpr = false;
12821 EnclosingExpr = nullptr;
12822 return false;
12823 }
12824 }
Samuel Antao661c0902016-05-26 17:39:58 +000012825 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +000012826 } else {
Samuel Antaoec172c62016-05-26 17:49:04 +000012827 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000012828 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
12829 << ERange;
12830 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012831 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12832 << RE->getSourceRange();
12833 return true;
12834 }
12835
12836 // The current expression uses the same base as other expression in the
Samuel Antao90927002016-04-26 14:54:23 +000012837 // data environment but does not contain it completely.
Samuel Antao5de996e2016-01-22 20:21:36 +000012838 if (!CurrentRegionOnly && SI != SE)
12839 EnclosingExpr = RE;
12840
12841 // The current expression is a subset of the expression in the data
12842 // environment.
12843 IsEnclosedByDataEnvironmentExpr |=
12844 (!CurrentRegionOnly && CI != CE && SI == SE);
12845
12846 return false;
12847 });
12848
12849 if (CurrentRegionOnly)
12850 return FoundError;
12851
12852 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
12853 // If any part of the original storage of a list item has corresponding
12854 // storage in the device data environment, all of the original storage must
12855 // have corresponding storage in the device data environment.
12856 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
12857 // If a list item is an element of a structure, and a different element of
12858 // the structure has a corresponding list item in the device data environment
12859 // prior to a task encountering the construct associated with the map clause,
Samuel Antao90927002016-04-26 14:54:23 +000012860 // then the list item must also have a corresponding list item in the device
Samuel Antao5de996e2016-01-22 20:21:36 +000012861 // data environment prior to the task encountering the construct.
12862 //
12863 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
12864 SemaRef.Diag(ELoc,
12865 diag::err_omp_original_storage_is_shared_and_does_not_contain)
12866 << ERange;
12867 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
12868 << EnclosingExpr->getSourceRange();
12869 return true;
12870 }
12871
12872 return FoundError;
12873}
12874
Samuel Antao661c0902016-05-26 17:39:58 +000012875namespace {
12876// Utility struct that gathers all the related lists associated with a mappable
12877// expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000012878struct MappableVarListInfo {
Samuel Antao661c0902016-05-26 17:39:58 +000012879 // The list of expressions.
12880 ArrayRef<Expr *> VarList;
12881 // The list of processed expressions.
12882 SmallVector<Expr *, 16> ProcessedVarList;
12883 // The mappble components for each expression.
12884 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
12885 // The base declaration of the variable.
12886 SmallVector<ValueDecl *, 16> VarBaseDeclarations;
12887
12888 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
12889 // We have a list of components and base declarations for each entry in the
12890 // variable list.
12891 VarComponents.reserve(VarList.size());
12892 VarBaseDeclarations.reserve(VarList.size());
12893 }
12894};
12895}
12896
12897// Check the validity of the provided variable list for the provided clause kind
12898// \a CKind. In the check process the valid expressions, and mappable expression
12899// components and variables are extracted and used to fill \a Vars,
12900// \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and
12901// \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'.
12902static void
12903checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS,
12904 OpenMPClauseKind CKind, MappableVarListInfo &MVLI,
12905 SourceLocation StartLoc,
12906 OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
12907 bool IsMapTypeImplicit = false) {
Samuel Antaoec172c62016-05-26 17:49:04 +000012908 // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
12909 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
Samuel Antao661c0902016-05-26 17:39:58 +000012910 "Unexpected clause kind with mappable expressions!");
Kelvin Li0bff7af2015-11-23 05:32:03 +000012911
Samuel Antao90927002016-04-26 14:54:23 +000012912 // Keep track of the mappable components and base declarations in this clause.
12913 // Each entry in the list is going to have a list of components associated. We
12914 // record each set of the components so that we can build the clause later on.
12915 // In the end we should have the same amount of declarations and component
12916 // lists.
Samuel Antao90927002016-04-26 14:54:23 +000012917
Alexey Bataeve3727102018-04-18 15:57:46 +000012918 for (Expr *RE : MVLI.VarList) {
Samuel Antaoec172c62016-05-26 17:49:04 +000012919 assert(RE && "Null expr in omp to/from/map clause");
Kelvin Li0bff7af2015-11-23 05:32:03 +000012920 SourceLocation ELoc = RE->getExprLoc();
12921
Alexey Bataeve3727102018-04-18 15:57:46 +000012922 const Expr *VE = RE->IgnoreParenLValueCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000012923
12924 if (VE->isValueDependent() || VE->isTypeDependent() ||
12925 VE->isInstantiationDependent() ||
12926 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012927 // We can only analyze this information once the missing information is
12928 // resolved.
Samuel Antao661c0902016-05-26 17:39:58 +000012929 MVLI.ProcessedVarList.push_back(RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012930 continue;
12931 }
12932
Alexey Bataeve3727102018-04-18 15:57:46 +000012933 Expr *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000012934
Samuel Antao5de996e2016-01-22 20:21:36 +000012935 if (!RE->IgnoreParenImpCasts()->isLValue()) {
Samuel Antao661c0902016-05-26 17:39:58 +000012936 SemaRef.Diag(ELoc,
12937 diag::err_omp_expected_named_var_member_or_array_expression)
Samuel Antao5de996e2016-01-22 20:21:36 +000012938 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000012939 continue;
12940 }
12941
Samuel Antao90927002016-04-26 14:54:23 +000012942 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
12943 ValueDecl *CurDeclaration = nullptr;
12944
12945 // Obtain the array or member expression bases if required. Also, fill the
12946 // components array with all the components identified in the process.
Alexey Bataeve3727102018-04-18 15:57:46 +000012947 const Expr *BE = checkMapClauseExpressionBase(
12948 SemaRef, SimpleExpr, CurComponents, CKind, /*NoDiagnose=*/false);
Samuel Antao5de996e2016-01-22 20:21:36 +000012949 if (!BE)
12950 continue;
12951
Samuel Antao90927002016-04-26 14:54:23 +000012952 assert(!CurComponents.empty() &&
12953 "Invalid mappable expression information.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000012954
Patrick Lystere13b1e32019-01-02 19:28:48 +000012955 if (const auto *TE = dyn_cast<CXXThisExpr>(BE)) {
12956 // Add store "this" pointer to class in DSAStackTy for future checking
12957 DSAS->addMappedClassesQualTypes(TE->getType());
12958 // Skip restriction checking for variable or field declarations
12959 MVLI.ProcessedVarList.push_back(RE);
12960 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
12961 MVLI.VarComponents.back().append(CurComponents.begin(),
12962 CurComponents.end());
12963 MVLI.VarBaseDeclarations.push_back(nullptr);
12964 continue;
12965 }
12966
Samuel Antao90927002016-04-26 14:54:23 +000012967 // For the following checks, we rely on the base declaration which is
12968 // expected to be associated with the last component. The declaration is
12969 // expected to be a variable or a field (if 'this' is being mapped).
12970 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
12971 assert(CurDeclaration && "Null decl on map clause.");
12972 assert(
12973 CurDeclaration->isCanonicalDecl() &&
12974 "Expecting components to have associated only canonical declarations.");
12975
12976 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
Alexey Bataeve3727102018-04-18 15:57:46 +000012977 const auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
Samuel Antao5de996e2016-01-22 20:21:36 +000012978
12979 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000012980 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000012981
12982 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
Samuel Antao661c0902016-05-26 17:39:58 +000012983 // threadprivate variables cannot appear in a map clause.
12984 // OpenMP 4.5 [2.10.5, target update Construct]
12985 // threadprivate variables cannot appear in a from clause.
12986 if (VD && DSAS->isThreadPrivate(VD)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012987 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
Samuel Antao661c0902016-05-26 17:39:58 +000012988 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
12989 << getOpenMPClauseName(CKind);
Alexey Bataeve3727102018-04-18 15:57:46 +000012990 reportOriginalDsa(SemaRef, DSAS, VD, DVar);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012991 continue;
12992 }
12993
Samuel Antao5de996e2016-01-22 20:21:36 +000012994 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
12995 // A list item cannot appear in both a map clause and a data-sharing
12996 // attribute clause on the same construct.
Kelvin Li0bff7af2015-11-23 05:32:03 +000012997
Samuel Antao5de996e2016-01-22 20:21:36 +000012998 // Check conflicts with other map clause expressions. We check the conflicts
12999 // with the current construct separately from the enclosing data
Samuel Antao661c0902016-05-26 17:39:58 +000013000 // environment, because the restrictions are different. We only have to
13001 // check conflicts across regions for the map clauses.
Alexey Bataeve3727102018-04-18 15:57:46 +000013002 if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
Samuel Antao661c0902016-05-26 17:39:58 +000013003 /*CurrentRegionOnly=*/true, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000013004 break;
Samuel Antao661c0902016-05-26 17:39:58 +000013005 if (CKind == OMPC_map &&
Alexey Bataeve3727102018-04-18 15:57:46 +000013006 checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
Samuel Antao661c0902016-05-26 17:39:58 +000013007 /*CurrentRegionOnly=*/false, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000013008 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000013009
Samuel Antao661c0902016-05-26 17:39:58 +000013010 // OpenMP 4.5 [2.10.5, target update Construct]
Samuel Antao5de996e2016-01-22 20:21:36 +000013011 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
13012 // If the type of a list item is a reference to a type T then the type will
13013 // be considered to be T for all purposes of this clause.
Alexey Bataev354df2e2018-05-02 18:44:10 +000013014 auto I = llvm::find_if(
13015 CurComponents,
13016 [](const OMPClauseMappableExprCommon::MappableComponent &MC) {
13017 return MC.getAssociatedDeclaration();
13018 });
13019 assert(I != CurComponents.end() && "Null decl on map clause.");
13020 QualType Type =
13021 I->getAssociatedDeclaration()->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000013022
Samuel Antao661c0902016-05-26 17:39:58 +000013023 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
13024 // A list item in a to or from clause must have a mappable type.
Samuel Antao5de996e2016-01-22 20:21:36 +000013025 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000013026 // A list item must have a mappable type.
Alexey Bataeve3727102018-04-18 15:57:46 +000013027 if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
Samuel Antao661c0902016-05-26 17:39:58 +000013028 DSAS, Type))
Kelvin Li0bff7af2015-11-23 05:32:03 +000013029 continue;
13030
Samuel Antao661c0902016-05-26 17:39:58 +000013031 if (CKind == OMPC_map) {
13032 // target enter data
13033 // OpenMP [2.10.2, Restrictions, p. 99]
13034 // A map-type must be specified in all map clauses and must be either
13035 // to or alloc.
13036 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
13037 if (DKind == OMPD_target_enter_data &&
13038 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
13039 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
13040 << (IsMapTypeImplicit ? 1 : 0)
13041 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
13042 << getOpenMPDirectiveName(DKind);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000013043 continue;
13044 }
Samuel Antao661c0902016-05-26 17:39:58 +000013045
13046 // target exit_data
13047 // OpenMP [2.10.3, Restrictions, p. 102]
13048 // A map-type must be specified in all map clauses and must be either
13049 // from, release, or delete.
13050 if (DKind == OMPD_target_exit_data &&
13051 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
13052 MapType == OMPC_MAP_delete)) {
13053 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
13054 << (IsMapTypeImplicit ? 1 : 0)
13055 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
13056 << getOpenMPDirectiveName(DKind);
13057 continue;
13058 }
13059
13060 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
13061 // A list item cannot appear in both a map clause and a data-sharing
13062 // attribute clause on the same construct
Alexey Bataeve3727102018-04-18 15:57:46 +000013063 if (VD && isOpenMPTargetExecutionDirective(DKind)) {
13064 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
Samuel Antao661c0902016-05-26 17:39:58 +000013065 if (isOpenMPPrivate(DVar.CKind)) {
Samuel Antao6890b092016-07-28 14:25:09 +000013066 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Samuel Antao661c0902016-05-26 17:39:58 +000013067 << getOpenMPClauseName(DVar.CKind)
Samuel Antao6890b092016-07-28 14:25:09 +000013068 << getOpenMPClauseName(OMPC_map)
Samuel Antao661c0902016-05-26 17:39:58 +000013069 << getOpenMPDirectiveName(DSAS->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000013070 reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar);
Samuel Antao661c0902016-05-26 17:39:58 +000013071 continue;
13072 }
13073 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +000013074 }
13075
Samuel Antao90927002016-04-26 14:54:23 +000013076 // Save the current expression.
Samuel Antao661c0902016-05-26 17:39:58 +000013077 MVLI.ProcessedVarList.push_back(RE);
Samuel Antao90927002016-04-26 14:54:23 +000013078
13079 // Store the components in the stack so that they can be used to check
13080 // against other clauses later on.
Samuel Antao6890b092016-07-28 14:25:09 +000013081 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents,
13082 /*WhereFoundClauseKind=*/OMPC_map);
Samuel Antao90927002016-04-26 14:54:23 +000013083
13084 // Save the components and declaration to create the clause. For purposes of
13085 // the clause creation, any component list that has has base 'this' uses
Samuel Antao686c70c2016-05-26 17:30:50 +000013086 // null as base declaration.
Samuel Antao661c0902016-05-26 17:39:58 +000013087 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13088 MVLI.VarComponents.back().append(CurComponents.begin(),
13089 CurComponents.end());
13090 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
13091 : CurDeclaration);
Kelvin Li0bff7af2015-11-23 05:32:03 +000013092 }
Samuel Antao661c0902016-05-26 17:39:58 +000013093}
13094
13095OMPClause *
Kelvin Lief579432018-12-18 22:18:41 +000013096Sema::ActOnOpenMPMapClause(ArrayRef<OpenMPMapModifierKind> MapTypeModifiers,
13097 ArrayRef<SourceLocation> MapTypeModifiersLoc,
Samuel Antao661c0902016-05-26 17:39:58 +000013098 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
13099 SourceLocation MapLoc, SourceLocation ColonLoc,
13100 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
13101 SourceLocation LParenLoc, SourceLocation EndLoc) {
13102 MappableVarListInfo MVLI(VarList);
13103 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc,
13104 MapType, IsMapTypeImplicit);
Kelvin Li0bff7af2015-11-23 05:32:03 +000013105
Kelvin Lief579432018-12-18 22:18:41 +000013106 OpenMPMapModifierKind Modifiers[] = { OMPC_MAP_MODIFIER_unknown,
13107 OMPC_MAP_MODIFIER_unknown };
13108 SourceLocation ModifiersLoc[OMPMapClause::NumberOfModifiers];
13109
13110 // Process map-type-modifiers, flag errors for duplicate modifiers.
13111 unsigned Count = 0;
13112 for (unsigned I = 0, E = MapTypeModifiers.size(); I < E; ++I) {
13113 if (MapTypeModifiers[I] != OMPC_MAP_MODIFIER_unknown &&
13114 llvm::find(Modifiers, MapTypeModifiers[I]) != std::end(Modifiers)) {
13115 Diag(MapTypeModifiersLoc[I], diag::err_omp_duplicate_map_type_modifier);
13116 continue;
13117 }
13118 assert(Count < OMPMapClause::NumberOfModifiers &&
13119 "Modifiers exceed the allowed number of map type modifiers");
13120 Modifiers[Count] = MapTypeModifiers[I];
13121 ModifiersLoc[Count] = MapTypeModifiersLoc[I];
13122 ++Count;
13123 }
13124
Samuel Antao5de996e2016-01-22 20:21:36 +000013125 // We need to produce a map clause even if we don't have variables so that
13126 // other diagnostics related with non-existing map clauses are accurate.
Samuel Antao661c0902016-05-26 17:39:58 +000013127 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13128 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
Kelvin Lief579432018-12-18 22:18:41 +000013129 MVLI.VarComponents, Modifiers, ModifiersLoc,
13130 MapType, IsMapTypeImplicit, MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000013131}
Kelvin Li099bb8c2015-11-24 20:50:12 +000013132
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013133QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
13134 TypeResult ParsedType) {
13135 assert(ParsedType.isUsable());
13136
13137 QualType ReductionType = GetTypeFromParser(ParsedType.get());
13138 if (ReductionType.isNull())
13139 return QualType();
13140
13141 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
13142 // A type name in a declare reduction directive cannot be a function type, an
13143 // array type, a reference type, or a type qualified with const, volatile or
13144 // restrict.
13145 if (ReductionType.hasQualifiers()) {
13146 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
13147 return QualType();
13148 }
13149
13150 if (ReductionType->isFunctionType()) {
13151 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
13152 return QualType();
13153 }
13154 if (ReductionType->isReferenceType()) {
13155 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
13156 return QualType();
13157 }
13158 if (ReductionType->isArrayType()) {
13159 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
13160 return QualType();
13161 }
13162 return ReductionType;
13163}
13164
13165Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
13166 Scope *S, DeclContext *DC, DeclarationName Name,
13167 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
13168 AccessSpecifier AS, Decl *PrevDeclInScope) {
13169 SmallVector<Decl *, 8> Decls;
13170 Decls.reserve(ReductionTypes.size());
13171
13172 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
Richard Smithbecb92d2017-10-10 22:33:17 +000013173 forRedeclarationInCurContext());
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013174 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
13175 // A reduction-identifier may not be re-declared in the current scope for the
13176 // same type or for a type that is compatible according to the base language
13177 // rules.
13178 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
13179 OMPDeclareReductionDecl *PrevDRD = nullptr;
13180 bool InCompoundScope = true;
13181 if (S != nullptr) {
13182 // Find previous declaration with the same name not referenced in other
13183 // declarations.
13184 FunctionScopeInfo *ParentFn = getEnclosingFunction();
13185 InCompoundScope =
13186 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
13187 LookupName(Lookup, S);
13188 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
13189 /*AllowInlineNamespace=*/false);
13190 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
Alexey Bataeve3727102018-04-18 15:57:46 +000013191 LookupResult::Filter Filter = Lookup.makeFilter();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013192 while (Filter.hasNext()) {
13193 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
13194 if (InCompoundScope) {
13195 auto I = UsedAsPrevious.find(PrevDecl);
13196 if (I == UsedAsPrevious.end())
13197 UsedAsPrevious[PrevDecl] = false;
Alexey Bataeve3727102018-04-18 15:57:46 +000013198 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope())
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013199 UsedAsPrevious[D] = true;
13200 }
13201 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
13202 PrevDecl->getLocation();
13203 }
13204 Filter.done();
13205 if (InCompoundScope) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013206 for (const auto &PrevData : UsedAsPrevious) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013207 if (!PrevData.second) {
13208 PrevDRD = PrevData.first;
13209 break;
13210 }
13211 }
13212 }
13213 } else if (PrevDeclInScope != nullptr) {
13214 auto *PrevDRDInScope = PrevDRD =
13215 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
13216 do {
13217 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
13218 PrevDRDInScope->getLocation();
13219 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
13220 } while (PrevDRDInScope != nullptr);
13221 }
Alexey Bataeve3727102018-04-18 15:57:46 +000013222 for (const auto &TyData : ReductionTypes) {
13223 const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013224 bool Invalid = false;
13225 if (I != PreviousRedeclTypes.end()) {
13226 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
13227 << TyData.first;
13228 Diag(I->second, diag::note_previous_definition);
13229 Invalid = true;
13230 }
13231 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
13232 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
13233 Name, TyData.first, PrevDRD);
13234 DC->addDecl(DRD);
13235 DRD->setAccess(AS);
13236 Decls.push_back(DRD);
13237 if (Invalid)
13238 DRD->setInvalidDecl();
13239 else
13240 PrevDRD = DRD;
13241 }
13242
13243 return DeclGroupPtrTy::make(
13244 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
13245}
13246
13247void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
13248 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13249
13250 // Enter new function scope.
13251 PushFunctionScope();
Reid Kleckner87a31802018-03-12 21:43:02 +000013252 setFunctionHasBranchProtectedScope();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013253 getCurFunction()->setHasOMPDeclareReductionCombiner();
13254
13255 if (S != nullptr)
13256 PushDeclContext(S, DRD);
13257 else
13258 CurContext = DRD;
13259
Faisal Valid143a0c2017-04-01 21:30:49 +000013260 PushExpressionEvaluationContext(
13261 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013262
13263 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013264 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
13265 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
13266 // uses semantics of argument handles by value, but it should be passed by
13267 // reference. C lang does not support references, so pass all parameters as
13268 // pointers.
13269 // Create 'T omp_in;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000013270 VarDecl *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013271 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013272 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
13273 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
13274 // uses semantics of argument handles by value, but it should be passed by
13275 // reference. C lang does not support references, so pass all parameters as
13276 // pointers.
13277 // Create 'T omp_out;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000013278 VarDecl *OmpOutParm =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013279 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
13280 if (S != nullptr) {
13281 PushOnScopeChains(OmpInParm, S);
13282 PushOnScopeChains(OmpOutParm, S);
13283 } else {
13284 DRD->addDecl(OmpInParm);
13285 DRD->addDecl(OmpOutParm);
13286 }
Alexey Bataeve6aa4692018-09-13 16:54:05 +000013287 Expr *InE =
13288 ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation());
13289 Expr *OutE =
13290 ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation());
13291 DRD->setCombinerData(InE, OutE);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013292}
13293
13294void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
13295 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13296 DiscardCleanupsInEvaluationContext();
13297 PopExpressionEvaluationContext();
13298
13299 PopDeclContext();
13300 PopFunctionScopeInfo();
13301
13302 if (Combiner != nullptr)
13303 DRD->setCombiner(Combiner);
13304 else
13305 DRD->setInvalidDecl();
13306}
13307
Alexey Bataev070f43a2017-09-06 14:49:58 +000013308VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013309 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13310
13311 // Enter new function scope.
13312 PushFunctionScope();
Reid Kleckner87a31802018-03-12 21:43:02 +000013313 setFunctionHasBranchProtectedScope();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013314
13315 if (S != nullptr)
13316 PushDeclContext(S, DRD);
13317 else
13318 CurContext = DRD;
13319
Faisal Valid143a0c2017-04-01 21:30:49 +000013320 PushExpressionEvaluationContext(
13321 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013322
13323 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013324 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
13325 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
13326 // uses semantics of argument handles by value, but it should be passed by
13327 // reference. C lang does not support references, so pass all parameters as
13328 // pointers.
13329 // Create 'T omp_priv;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000013330 VarDecl *OmpPrivParm =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013331 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013332 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
13333 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
13334 // uses semantics of argument handles by value, but it should be passed by
13335 // reference. C lang does not support references, so pass all parameters as
13336 // pointers.
13337 // Create 'T omp_orig;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000013338 VarDecl *OmpOrigParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013339 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013340 if (S != nullptr) {
13341 PushOnScopeChains(OmpPrivParm, S);
13342 PushOnScopeChains(OmpOrigParm, S);
13343 } else {
13344 DRD->addDecl(OmpPrivParm);
13345 DRD->addDecl(OmpOrigParm);
13346 }
Alexey Bataeve6aa4692018-09-13 16:54:05 +000013347 Expr *OrigE =
13348 ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation());
13349 Expr *PrivE =
13350 ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation());
13351 DRD->setInitializerData(OrigE, PrivE);
Alexey Bataev070f43a2017-09-06 14:49:58 +000013352 return OmpPrivParm;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013353}
13354
Alexey Bataev070f43a2017-09-06 14:49:58 +000013355void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer,
13356 VarDecl *OmpPrivParm) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013357 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13358 DiscardCleanupsInEvaluationContext();
13359 PopExpressionEvaluationContext();
13360
13361 PopDeclContext();
13362 PopFunctionScopeInfo();
13363
Alexey Bataev070f43a2017-09-06 14:49:58 +000013364 if (Initializer != nullptr) {
13365 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit);
13366 } else if (OmpPrivParm->hasInit()) {
13367 DRD->setInitializer(OmpPrivParm->getInit(),
13368 OmpPrivParm->isDirectInit()
13369 ? OMPDeclareReductionDecl::DirectInit
13370 : OMPDeclareReductionDecl::CopyInit);
13371 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013372 DRD->setInvalidDecl();
Alexey Bataev070f43a2017-09-06 14:49:58 +000013373 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013374}
13375
13376Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
13377 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013378 for (Decl *D : DeclReductions.get()) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013379 if (IsValid) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013380 if (S)
13381 PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S,
13382 /*AddToContext=*/false);
13383 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013384 D->setInvalidDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000013385 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013386 }
13387 return DeclReductions;
13388}
13389
David Majnemer9d168222016-08-05 17:44:54 +000013390OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
Kelvin Li099bb8c2015-11-24 20:50:12 +000013391 SourceLocation StartLoc,
13392 SourceLocation LParenLoc,
13393 SourceLocation EndLoc) {
13394 Expr *ValExpr = NumTeams;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000013395 Stmt *HelperValStmt = nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000013396
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013397 // OpenMP [teams Constrcut, Restrictions]
13398 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000013399 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
Alexey Bataeva0569352015-12-01 10:17:31 +000013400 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013401 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000013402
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000013403 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000013404 OpenMPDirectiveKind CaptureRegion =
13405 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams);
13406 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000013407 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000013408 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000013409 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
13410 HelperValStmt = buildPreInits(Context, Captures);
13411 }
13412
13413 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion,
13414 StartLoc, LParenLoc, EndLoc);
Kelvin Li099bb8c2015-11-24 20:50:12 +000013415}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013416
13417OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
13418 SourceLocation StartLoc,
13419 SourceLocation LParenLoc,
13420 SourceLocation EndLoc) {
13421 Expr *ValExpr = ThreadLimit;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000013422 Stmt *HelperValStmt = nullptr;
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013423
13424 // OpenMP [teams Constrcut, Restrictions]
13425 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000013426 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
Alexey Bataeva0569352015-12-01 10:17:31 +000013427 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013428 return nullptr;
13429
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000013430 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000013431 OpenMPDirectiveKind CaptureRegion =
13432 getOpenMPCaptureRegionForClause(DKind, OMPC_thread_limit);
13433 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000013434 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000013435 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000013436 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
13437 HelperValStmt = buildPreInits(Context, Captures);
13438 }
13439
13440 return new (Context) OMPThreadLimitClause(
13441 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013442}
Alexey Bataeva0569352015-12-01 10:17:31 +000013443
13444OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
13445 SourceLocation StartLoc,
13446 SourceLocation LParenLoc,
13447 SourceLocation EndLoc) {
13448 Expr *ValExpr = Priority;
13449
13450 // OpenMP [2.9.1, task Constrcut]
13451 // The priority-value is a non-negative numerical scalar expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000013452 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
Alexey Bataeva0569352015-12-01 10:17:31 +000013453 /*StrictlyPositive=*/false))
13454 return nullptr;
13455
13456 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
13457}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000013458
13459OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
13460 SourceLocation StartLoc,
13461 SourceLocation LParenLoc,
13462 SourceLocation EndLoc) {
13463 Expr *ValExpr = Grainsize;
13464
13465 // OpenMP [2.9.2, taskloop Constrcut]
13466 // The parameter of the grainsize clause must be a positive integer
13467 // expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000013468 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000013469 /*StrictlyPositive=*/true))
13470 return nullptr;
13471
13472 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
13473}
Alexey Bataev382967a2015-12-08 12:06:20 +000013474
13475OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
13476 SourceLocation StartLoc,
13477 SourceLocation LParenLoc,
13478 SourceLocation EndLoc) {
13479 Expr *ValExpr = NumTasks;
13480
13481 // OpenMP [2.9.2, taskloop Constrcut]
13482 // The parameter of the num_tasks clause must be a positive integer
13483 // expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000013484 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
Alexey Bataev382967a2015-12-08 12:06:20 +000013485 /*StrictlyPositive=*/true))
13486 return nullptr;
13487
13488 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
13489}
13490
Alexey Bataev28c75412015-12-15 08:19:24 +000013491OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
13492 SourceLocation LParenLoc,
13493 SourceLocation EndLoc) {
13494 // OpenMP [2.13.2, critical construct, Description]
13495 // ... where hint-expression is an integer constant expression that evaluates
13496 // to a valid lock hint.
13497 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
13498 if (HintExpr.isInvalid())
13499 return nullptr;
13500 return new (Context)
13501 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
13502}
13503
Carlo Bertollib4adf552016-01-15 18:50:31 +000013504OMPClause *Sema::ActOnOpenMPDistScheduleClause(
13505 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
13506 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
13507 SourceLocation EndLoc) {
13508 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
13509 std::string Values;
13510 Values += "'";
13511 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
13512 Values += "'";
13513 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
13514 << Values << getOpenMPClauseName(OMPC_dist_schedule);
13515 return nullptr;
13516 }
13517 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000013518 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000013519 if (ChunkSize) {
13520 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
13521 !ChunkSize->isInstantiationDependent() &&
13522 !ChunkSize->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000013523 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
Carlo Bertollib4adf552016-01-15 18:50:31 +000013524 ExprResult Val =
13525 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
13526 if (Val.isInvalid())
13527 return nullptr;
13528
13529 ValExpr = Val.get();
13530
13531 // OpenMP [2.7.1, Restrictions]
13532 // chunk_size must be a loop invariant integer expression with a positive
13533 // value.
13534 llvm::APSInt Result;
13535 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
13536 if (Result.isSigned() && !Result.isStrictlyPositive()) {
13537 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
13538 << "dist_schedule" << ChunkSize->getSourceRange();
13539 return nullptr;
13540 }
Alexey Bataev2ba67042017-11-28 21:11:44 +000013541 } else if (getOpenMPCaptureRegionForClause(
13542 DSAStack->getCurrentDirective(), OMPC_dist_schedule) !=
13543 OMPD_unknown &&
Alexey Bataevb46cdea2016-06-15 11:20:48 +000013544 !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000013545 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000013546 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev5a3af132016-03-29 08:58:54 +000013547 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
13548 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000013549 }
13550 }
13551 }
13552
13553 return new (Context)
13554 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000013555 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000013556}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013557
13558OMPClause *Sema::ActOnOpenMPDefaultmapClause(
13559 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
13560 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
13561 SourceLocation KindLoc, SourceLocation EndLoc) {
13562 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
David Majnemer9d168222016-08-05 17:44:54 +000013563 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) {
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013564 std::string Value;
13565 SourceLocation Loc;
13566 Value += "'";
13567 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
13568 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000013569 OMPC_DEFAULTMAP_MODIFIER_tofrom);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013570 Loc = MLoc;
13571 } else {
13572 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000013573 OMPC_DEFAULTMAP_scalar);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013574 Loc = KindLoc;
13575 }
13576 Value += "'";
13577 Diag(Loc, diag::err_omp_unexpected_clause_value)
13578 << Value << getOpenMPClauseName(OMPC_defaultmap);
13579 return nullptr;
13580 }
Alexey Bataev2fd0cb22017-10-05 17:51:39 +000013581 DSAStack->setDefaultDMAToFromScalar(StartLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013582
13583 return new (Context)
13584 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
13585}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013586
13587bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
13588 DeclContext *CurLexicalContext = getCurLexicalContext();
13589 if (!CurLexicalContext->isFileContext() &&
13590 !CurLexicalContext->isExternCContext() &&
Alexey Bataev502ec492017-10-03 20:00:00 +000013591 !CurLexicalContext->isExternCXXContext() &&
13592 !isa<CXXRecordDecl>(CurLexicalContext) &&
13593 !isa<ClassTemplateDecl>(CurLexicalContext) &&
13594 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) &&
13595 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013596 Diag(Loc, diag::err_omp_region_not_file_context);
13597 return false;
13598 }
Kelvin Libc38e632018-09-10 02:07:09 +000013599 ++DeclareTargetNestingLevel;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013600 return true;
13601}
13602
13603void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
Kelvin Libc38e632018-09-10 02:07:09 +000013604 assert(DeclareTargetNestingLevel > 0 &&
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013605 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
Kelvin Libc38e632018-09-10 02:07:09 +000013606 --DeclareTargetNestingLevel;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013607}
13608
David Majnemer9d168222016-08-05 17:44:54 +000013609void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope,
13610 CXXScopeSpec &ScopeSpec,
13611 const DeclarationNameInfo &Id,
13612 OMPDeclareTargetDeclAttr::MapTypeTy MT,
13613 NamedDeclSetType &SameDirectiveDecls) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013614 LookupResult Lookup(*this, Id, LookupOrdinaryName);
13615 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
13616
13617 if (Lookup.isAmbiguous())
13618 return;
13619 Lookup.suppressDiagnostics();
13620
13621 if (!Lookup.isSingleResult()) {
13622 if (TypoCorrection Corrected =
13623 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
13624 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
13625 CTK_ErrorRecovery)) {
13626 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
13627 << Id.getName());
13628 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
13629 return;
13630 }
13631
13632 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
13633 return;
13634 }
13635
13636 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
Alexey Bataev30a78212018-09-11 13:59:10 +000013637 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND) ||
13638 isa<FunctionTemplateDecl>(ND)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013639 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
13640 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
Alexey Bataev30a78212018-09-11 13:59:10 +000013641 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
13642 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
13643 cast<ValueDecl>(ND));
13644 if (!Res) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013645 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013646 ND->addAttr(A);
13647 if (ASTMutationListener *ML = Context.getASTMutationListener())
13648 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
Kelvin Li1ce87c72017-12-12 20:08:12 +000013649 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Id.getLoc());
Alexey Bataev30a78212018-09-11 13:59:10 +000013650 } else if (*Res != MT) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013651 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
13652 << Id.getName();
13653 }
Alexey Bataeve3727102018-04-18 15:57:46 +000013654 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013655 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
Alexey Bataeve3727102018-04-18 15:57:46 +000013656 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013657}
13658
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013659static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
13660 Sema &SemaRef, Decl *D) {
Alexey Bataev30a78212018-09-11 13:59:10 +000013661 if (!D || !isa<VarDecl>(D))
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013662 return;
Alexey Bataev30a78212018-09-11 13:59:10 +000013663 auto *VD = cast<VarDecl>(D);
13664 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
13665 return;
13666 SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context);
13667 SemaRef.Diag(SL, diag::note_used_here) << SR;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013668}
13669
13670static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
13671 Sema &SemaRef, DSAStackTy *Stack,
13672 ValueDecl *VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013673 return VD->hasAttr<OMPDeclareTargetDeclAttr>() ||
13674 checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(),
13675 /*FullCheck=*/false);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013676}
13677
Kelvin Li1ce87c72017-12-12 20:08:12 +000013678void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D,
13679 SourceLocation IdLoc) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013680 if (!D || D->isInvalidDecl())
13681 return;
13682 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
Stephen Kellyf2ceec42018-08-09 21:08:08 +000013683 SourceLocation SL = E ? E->getBeginLoc() : D->getLocation();
Alexey Bataeve3727102018-04-18 15:57:46 +000013684 if (auto *VD = dyn_cast<VarDecl>(D)) {
Alexey Bataevc1943e72018-07-09 19:58:08 +000013685 // Only global variables can be marked as declare target.
Alexey Bataev30a78212018-09-11 13:59:10 +000013686 if (!VD->isFileVarDecl() && !VD->isStaticLocal() &&
13687 !VD->isStaticDataMember())
Alexey Bataevc1943e72018-07-09 19:58:08 +000013688 return;
13689 // 2.10.6: threadprivate variable cannot appear in a declare target
13690 // directive.
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013691 if (DSAStack->isThreadPrivate(VD)) {
13692 Diag(SL, diag::err_omp_threadprivate_in_target);
Alexey Bataeve3727102018-04-18 15:57:46 +000013693 reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013694 return;
13695 }
13696 }
Alexey Bataev97b72212018-08-14 18:31:20 +000013697 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
13698 D = FTD->getTemplatedDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000013699 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Alexey Bataev30a78212018-09-11 13:59:10 +000013700 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
13701 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD);
13702 if (Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) {
Kelvin Li1ce87c72017-12-12 20:08:12 +000013703 assert(IdLoc.isValid() && "Source location is expected");
13704 Diag(IdLoc, diag::err_omp_function_in_link_clause);
13705 Diag(FD->getLocation(), diag::note_defined_here) << FD;
13706 return;
13707 }
13708 }
Alexey Bataev30a78212018-09-11 13:59:10 +000013709 if (auto *VD = dyn_cast<ValueDecl>(D)) {
13710 // Problem if any with var declared with incomplete type will be reported
13711 // as normal, so no need to check it here.
13712 if ((E || !VD->getType()->isIncompleteType()) &&
13713 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD))
13714 return;
13715 if (!E && !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) {
13716 // Checking declaration inside declare target region.
13717 if (isa<VarDecl>(D) || isa<FunctionDecl>(D) ||
13718 isa<FunctionTemplateDecl>(D)) {
13719 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
13720 Context, OMPDeclareTargetDeclAttr::MT_To);
13721 D->addAttr(A);
13722 if (ASTMutationListener *ML = Context.getASTMutationListener())
13723 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
13724 }
13725 return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013726 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013727 }
Alexey Bataev30a78212018-09-11 13:59:10 +000013728 if (!E)
13729 return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013730 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
13731}
Samuel Antao661c0902016-05-26 17:39:58 +000013732
13733OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
13734 SourceLocation StartLoc,
13735 SourceLocation LParenLoc,
13736 SourceLocation EndLoc) {
13737 MappableVarListInfo MVLI(VarList);
13738 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc);
13739 if (MVLI.ProcessedVarList.empty())
13740 return nullptr;
13741
13742 return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13743 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
13744 MVLI.VarComponents);
13745}
Samuel Antaoec172c62016-05-26 17:49:04 +000013746
13747OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
13748 SourceLocation StartLoc,
13749 SourceLocation LParenLoc,
13750 SourceLocation EndLoc) {
13751 MappableVarListInfo MVLI(VarList);
13752 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc);
13753 if (MVLI.ProcessedVarList.empty())
13754 return nullptr;
13755
13756 return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13757 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
13758 MVLI.VarComponents);
13759}
Carlo Bertolli2404b172016-07-13 15:37:16 +000013760
13761OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
13762 SourceLocation StartLoc,
13763 SourceLocation LParenLoc,
13764 SourceLocation EndLoc) {
Samuel Antaocc10b852016-07-28 14:23:26 +000013765 MappableVarListInfo MVLI(VarList);
13766 SmallVector<Expr *, 8> PrivateCopies;
13767 SmallVector<Expr *, 8> Inits;
13768
Alexey Bataeve3727102018-04-18 15:57:46 +000013769 for (Expr *RefExpr : VarList) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000013770 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
13771 SourceLocation ELoc;
13772 SourceRange ERange;
13773 Expr *SimpleRefExpr = RefExpr;
13774 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
13775 if (Res.second) {
13776 // It will be analyzed later.
Samuel Antaocc10b852016-07-28 14:23:26 +000013777 MVLI.ProcessedVarList.push_back(RefExpr);
13778 PrivateCopies.push_back(nullptr);
13779 Inits.push_back(nullptr);
Carlo Bertolli2404b172016-07-13 15:37:16 +000013780 }
13781 ValueDecl *D = Res.first;
13782 if (!D)
13783 continue;
13784
13785 QualType Type = D->getType();
Samuel Antaocc10b852016-07-28 14:23:26 +000013786 Type = Type.getNonReferenceType().getUnqualifiedType();
13787
13788 auto *VD = dyn_cast<VarDecl>(D);
13789
13790 // Item should be a pointer or reference to pointer.
13791 if (!Type->isPointerType()) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000013792 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
13793 << 0 << RefExpr->getSourceRange();
13794 continue;
13795 }
Samuel Antaocc10b852016-07-28 14:23:26 +000013796
13797 // Build the private variable and the expression that refers to it.
Alexey Bataev63cc8e92018-03-20 14:45:59 +000013798 auto VDPrivate =
13799 buildVarDecl(*this, ELoc, Type, D->getName(),
13800 D->hasAttrs() ? &D->getAttrs() : nullptr,
13801 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Samuel Antaocc10b852016-07-28 14:23:26 +000013802 if (VDPrivate->isInvalidDecl())
13803 continue;
13804
13805 CurContext->addDecl(VDPrivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000013806 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Samuel Antaocc10b852016-07-28 14:23:26 +000013807 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
13808
13809 // Add temporary variable to initialize the private copy of the pointer.
Alexey Bataeve3727102018-04-18 15:57:46 +000013810 VarDecl *VDInit =
Samuel Antaocc10b852016-07-28 14:23:26 +000013811 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp");
Alexey Bataeve3727102018-04-18 15:57:46 +000013812 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr(
13813 *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc());
Samuel Antaocc10b852016-07-28 14:23:26 +000013814 AddInitializerToDecl(VDPrivate,
13815 DefaultLvalueConversion(VDInitRefExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000013816 /*DirectInit=*/false);
Samuel Antaocc10b852016-07-28 14:23:26 +000013817
13818 // If required, build a capture to implement the privatization initialized
13819 // with the current list item value.
13820 DeclRefExpr *Ref = nullptr;
13821 if (!VD)
13822 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
13823 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
13824 PrivateCopies.push_back(VDPrivateRefExpr);
13825 Inits.push_back(VDInitRefExpr);
13826
13827 // We need to add a data sharing attribute for this variable to make sure it
13828 // is correctly captured. A variable that shows up in a use_device_ptr has
13829 // similar properties of a first private variable.
13830 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
13831
13832 // Create a mappable component for the list item. List items in this clause
13833 // only need a component.
13834 MVLI.VarBaseDeclarations.push_back(D);
13835 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13836 MVLI.VarComponents.back().push_back(
13837 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D));
Carlo Bertolli2404b172016-07-13 15:37:16 +000013838 }
13839
Samuel Antaocc10b852016-07-28 14:23:26 +000013840 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli2404b172016-07-13 15:37:16 +000013841 return nullptr;
13842
Samuel Antaocc10b852016-07-28 14:23:26 +000013843 return OMPUseDevicePtrClause::Create(
13844 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
13845 PrivateCopies, Inits, MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli2404b172016-07-13 15:37:16 +000013846}
Carlo Bertolli70594e92016-07-13 17:16:49 +000013847
13848OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
13849 SourceLocation StartLoc,
13850 SourceLocation LParenLoc,
13851 SourceLocation EndLoc) {
Samuel Antao6890b092016-07-28 14:25:09 +000013852 MappableVarListInfo MVLI(VarList);
Alexey Bataeve3727102018-04-18 15:57:46 +000013853 for (Expr *RefExpr : VarList) {
Kelvin Li84376252016-12-14 15:39:58 +000013854 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause.");
Carlo Bertolli70594e92016-07-13 17:16:49 +000013855 SourceLocation ELoc;
13856 SourceRange ERange;
13857 Expr *SimpleRefExpr = RefExpr;
13858 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
13859 if (Res.second) {
13860 // It will be analyzed later.
Samuel Antao6890b092016-07-28 14:25:09 +000013861 MVLI.ProcessedVarList.push_back(RefExpr);
Carlo Bertolli70594e92016-07-13 17:16:49 +000013862 }
13863 ValueDecl *D = Res.first;
13864 if (!D)
13865 continue;
13866
13867 QualType Type = D->getType();
13868 // item should be a pointer or array or reference to pointer or array
13869 if (!Type.getNonReferenceType()->isPointerType() &&
13870 !Type.getNonReferenceType()->isArrayType()) {
13871 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
13872 << 0 << RefExpr->getSourceRange();
13873 continue;
13874 }
Samuel Antao6890b092016-07-28 14:25:09 +000013875
13876 // Check if the declaration in the clause does not show up in any data
13877 // sharing attribute.
Alexey Bataeve3727102018-04-18 15:57:46 +000013878 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Samuel Antao6890b092016-07-28 14:25:09 +000013879 if (isOpenMPPrivate(DVar.CKind)) {
13880 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
13881 << getOpenMPClauseName(DVar.CKind)
13882 << getOpenMPClauseName(OMPC_is_device_ptr)
13883 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000013884 reportOriginalDsa(*this, DSAStack, D, DVar);
Samuel Antao6890b092016-07-28 14:25:09 +000013885 continue;
13886 }
13887
Alexey Bataeve3727102018-04-18 15:57:46 +000013888 const Expr *ConflictExpr;
Samuel Antao6890b092016-07-28 14:25:09 +000013889 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +000013890 D, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +000013891 [&ConflictExpr](
13892 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
13893 OpenMPClauseKind) -> bool {
13894 ConflictExpr = R.front().getAssociatedExpression();
13895 return true;
13896 })) {
13897 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
13898 Diag(ConflictExpr->getExprLoc(), diag::note_used_here)
13899 << ConflictExpr->getSourceRange();
13900 continue;
13901 }
13902
13903 // Store the components in the stack so that they can be used to check
13904 // against other clauses later on.
13905 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D);
13906 DSAStack->addMappableExpressionComponents(
13907 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
13908
13909 // Record the expression we've just processed.
13910 MVLI.ProcessedVarList.push_back(SimpleRefExpr);
13911
13912 // Create a mappable component for the list item. List items in this clause
13913 // only need a component. We use a null declaration to signal fields in
13914 // 'this'.
13915 assert((isa<DeclRefExpr>(SimpleRefExpr) ||
13916 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
13917 "Unexpected device pointer expression!");
13918 MVLI.VarBaseDeclarations.push_back(
13919 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr);
13920 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13921 MVLI.VarComponents.back().push_back(MC);
Carlo Bertolli70594e92016-07-13 17:16:49 +000013922 }
13923
Samuel Antao6890b092016-07-28 14:25:09 +000013924 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli70594e92016-07-13 17:16:49 +000013925 return nullptr;
13926
Samuel Antao6890b092016-07-28 14:25:09 +000013927 return OMPIsDevicePtrClause::Create(
13928 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
13929 MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli70594e92016-07-13 17:16:49 +000013930}