blob: 7b702250b882d84c20783ab031d6e18cdccf8501 [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 Bataev9c2e8ee2014-07-11 11:25:16 +0000679bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
Alexey Bataev35aaee62016-04-13 13:36:48 +0000680 return isOpenMPParallelDirective(DKind) || isOpenMPTaskingDirective(DKind) ||
681 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000682}
Alexey Bataeve3727102018-04-18 15:57:46 +0000683
Alexey Bataeved09d242014-05-28 05:53:51 +0000684} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000685
Alexey Bataeve3727102018-04-18 15:57:46 +0000686static const Expr *getExprAsWritten(const Expr *E) {
Bill Wendling7c44da22018-10-31 03:48:47 +0000687 if (const auto *FE = dyn_cast<FullExpr>(E))
688 E = FE->getSubExpr();
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000689
Alexey Bataeve3727102018-04-18 15:57:46 +0000690 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000691 E = MTE->GetTemporaryExpr();
692
Alexey Bataeve3727102018-04-18 15:57:46 +0000693 while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000694 E = Binder->getSubExpr();
695
Alexey Bataeve3727102018-04-18 15:57:46 +0000696 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000697 E = ICE->getSubExprAsWritten();
698 return E->IgnoreParens();
699}
700
Alexey Bataeve3727102018-04-18 15:57:46 +0000701static Expr *getExprAsWritten(Expr *E) {
702 return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E)));
703}
704
705static const ValueDecl *getCanonicalDecl(const ValueDecl *D) {
706 if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D))
707 if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000708 D = ME->getMemberDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +0000709 const auto *VD = dyn_cast<VarDecl>(D);
710 const auto *FD = dyn_cast<FieldDecl>(D);
David Majnemer9d168222016-08-05 17:44:54 +0000711 if (VD != nullptr) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000712 VD = VD->getCanonicalDecl();
713 D = VD;
714 } else {
715 assert(FD);
716 FD = FD->getCanonicalDecl();
717 D = FD;
718 }
719 return D;
720}
721
Alexey Bataeve3727102018-04-18 15:57:46 +0000722static ValueDecl *getCanonicalDecl(ValueDecl *D) {
723 return const_cast<ValueDecl *>(
724 getCanonicalDecl(const_cast<const ValueDecl *>(D)));
725}
726
727DSAStackTy::DSAVarData DSAStackTy::getDSA(iterator &Iter,
728 ValueDecl *D) const {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000729 D = getCanonicalDecl(D);
730 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000731 const auto *FD = dyn_cast<FieldDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000732 DSAVarData DVar;
Alexey Bataev4b465392017-04-26 15:06:24 +0000733 if (isStackEmpty() || Iter == Stack.back().first.rend()) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000734 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
735 // in a region but not in construct]
736 // File-scope or namespace-scope variables referenced in called routines
737 // in the region are shared unless they appear in a threadprivate
738 // directive.
Alexey Bataeve3727102018-04-18 15:57:46 +0000739 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000740 DVar.CKind = OMPC_shared;
741
742 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
743 // in a region but not in construct]
744 // Variables with static storage duration that are declared in called
745 // routines in the region are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000746 if (VD && VD->hasGlobalStorage())
747 DVar.CKind = OMPC_shared;
748
749 // Non-static data members are shared by default.
750 if (FD)
Alexey Bataev750a58b2014-03-18 12:19:12 +0000751 DVar.CKind = OMPC_shared;
752
Alexey Bataev758e55e2013-09-06 18:03:48 +0000753 return DVar;
754 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000755
Alexey Bataevec3da872014-01-31 05:15:34 +0000756 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
757 // in a Construct, C/C++, predetermined, p.1]
758 // Variables with automatic storage duration that are declared in a scope
759 // inside the construct are private.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000760 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
761 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000762 DVar.CKind = OMPC_private;
763 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000764 }
765
Alexey Bataeveffbdf12017-07-21 17:24:30 +0000766 DVar.DKind = Iter->Directive;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000767 // Explicitly specified attributes and local variables with predetermined
768 // attributes.
769 if (Iter->SharingMap.count(D)) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000770 const DSAInfo &Data = Iter->SharingMap.lookup(D);
771 DVar.RefExpr = Data.RefExpr.getPointer();
772 DVar.PrivateCopy = Data.PrivateCopy;
773 DVar.CKind = Data.Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000774 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000775 return DVar;
776 }
777
778 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
779 // in a Construct, C/C++, implicitly determined, p.1]
780 // In a parallel or task construct, the data-sharing attributes of these
781 // variables are determined by the default clause, if present.
782 switch (Iter->DefaultAttr) {
783 case DSA_shared:
784 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000785 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000786 return DVar;
787 case DSA_none:
788 return DVar;
789 case DSA_unspecified:
790 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
791 // in a Construct, implicitly determined, p.2]
792 // In a parallel construct, if no default clause is present, these
793 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000794 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000795 if (isOpenMPParallelDirective(DVar.DKind) ||
796 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000797 DVar.CKind = OMPC_shared;
798 return DVar;
799 }
800
801 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
802 // in a Construct, implicitly determined, p.4]
803 // In a task construct, if no default clause is present, a variable that in
804 // the enclosing context is determined to be shared by all implicit tasks
805 // bound to the current team is shared.
Alexey Bataev35aaee62016-04-13 13:36:48 +0000806 if (isOpenMPTaskingDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000807 DSAVarData DVarTemp;
Alexey Bataeve3727102018-04-18 15:57:46 +0000808 iterator I = Iter, E = Stack.back().first.rend();
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000809 do {
810 ++I;
Alexey Bataeved09d242014-05-28 05:53:51 +0000811 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
Alexey Bataev35aaee62016-04-13 13:36:48 +0000812 // Referenced in a Construct, implicitly determined, p.6]
Alexey Bataev758e55e2013-09-06 18:03:48 +0000813 // In a task construct, if no default clause is present, a variable
814 // whose data-sharing attribute is not determined by the rules above is
815 // firstprivate.
816 DVarTemp = getDSA(I, D);
817 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000818 DVar.RefExpr = nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000819 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000820 return DVar;
821 }
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000822 } while (I != E && !isParallelOrTaskRegion(I->Directive));
Alexey Bataev758e55e2013-09-06 18:03:48 +0000823 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000824 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000825 return DVar;
826 }
827 }
828 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
829 // in a Construct, implicitly determined, p.3]
830 // For constructs other than task, if no default clause is present, these
831 // variables inherit their data-sharing attributes from the enclosing
832 // context.
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000833 return getDSA(++Iter, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000834}
835
Alexey Bataeve3727102018-04-18 15:57:46 +0000836const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D,
837 const Expr *NewDE) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000838 assert(!isStackEmpty() && "Data sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000839 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000840 SharingMapTy &StackElem = Stack.back().first.back();
Alexey Bataev4b465392017-04-26 15:06:24 +0000841 auto It = StackElem.AlignedMap.find(D);
842 if (It == StackElem.AlignedMap.end()) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000843 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
Alexey Bataev4b465392017-04-26 15:06:24 +0000844 StackElem.AlignedMap[D] = NewDE;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000845 return nullptr;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000846 }
Alexey Bataeve3727102018-04-18 15:57:46 +0000847 assert(It->second && "Unexpected nullptr expr in the aligned map");
848 return It->second;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000849}
850
Alexey Bataeve3727102018-04-18 15:57:46 +0000851void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000852 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000853 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000854 SharingMapTy &StackElem = Stack.back().first.back();
855 StackElem.LCVMap.try_emplace(
856 D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture));
Alexey Bataev9c821032015-04-30 04:23:23 +0000857}
858
Alexey Bataeve3727102018-04-18 15:57:46 +0000859const DSAStackTy::LCDeclInfo
860DSAStackTy::isLoopControlVariable(const ValueDecl *D) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000861 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000862 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000863 const SharingMapTy &StackElem = Stack.back().first.back();
Alexey Bataev4b465392017-04-26 15:06:24 +0000864 auto It = StackElem.LCVMap.find(D);
865 if (It != StackElem.LCVMap.end())
866 return It->second;
867 return {0, nullptr};
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000868}
869
Alexey Bataeve3727102018-04-18 15:57:46 +0000870const DSAStackTy::LCDeclInfo
871DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000872 assert(!isStackEmpty() && Stack.back().first.size() > 1 &&
873 "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000874 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000875 const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
Alexey Bataev4b465392017-04-26 15:06:24 +0000876 auto It = StackElem.LCVMap.find(D);
877 if (It != StackElem.LCVMap.end())
878 return It->second;
879 return {0, nullptr};
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000880}
881
Alexey Bataeve3727102018-04-18 15:57:46 +0000882const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000883 assert(!isStackEmpty() && Stack.back().first.size() > 1 &&
884 "Data-sharing attributes stack is empty");
Alexey Bataeve3727102018-04-18 15:57:46 +0000885 const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
Alexey Bataev4b465392017-04-26 15:06:24 +0000886 if (StackElem.LCVMap.size() < I)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000887 return nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +0000888 for (const auto &Pair : StackElem.LCVMap)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000889 if (Pair.second.first == I)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000890 return Pair.first;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000891 return nullptr;
Alexey Bataev9c821032015-04-30 04:23:23 +0000892}
893
Alexey Bataeve3727102018-04-18 15:57:46 +0000894void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
Alexey Bataev90c228f2016-02-08 09:29:13 +0000895 DeclRefExpr *PrivateCopy) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000896 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000897 if (A == OMPC_threadprivate) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000898 DSAInfo &Data = Threadprivates[D];
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000899 Data.Attributes = A;
900 Data.RefExpr.setPointer(E);
901 Data.PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000902 } else {
Alexey Bataev4b465392017-04-26 15:06:24 +0000903 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataeve3727102018-04-18 15:57:46 +0000904 DSAInfo &Data = Stack.back().first.back().SharingMap[D];
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000905 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
906 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
907 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
908 (isLoopControlVariable(D).first && A == OMPC_private));
909 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
910 Data.RefExpr.setInt(/*IntVal=*/true);
911 return;
912 }
913 const bool IsLastprivate =
914 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
915 Data.Attributes = A;
916 Data.RefExpr.setPointerAndInt(E, IsLastprivate);
917 Data.PrivateCopy = PrivateCopy;
918 if (PrivateCopy) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000919 DSAInfo &Data =
920 Stack.back().first.back().SharingMap[PrivateCopy->getDecl()];
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000921 Data.Attributes = A;
922 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
923 Data.PrivateCopy = nullptr;
924 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000925 }
926}
927
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000928/// Build a variable declaration for OpenMP loop iteration variable.
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000929static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev63cc8e92018-03-20 14:45:59 +0000930 StringRef Name, const AttrVec *Attrs = nullptr,
931 DeclRefExpr *OrigRef = nullptr) {
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000932 DeclContext *DC = SemaRef.CurContext;
933 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
934 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
Alexey Bataeve3727102018-04-18 15:57:46 +0000935 auto *Decl =
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000936 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
937 if (Attrs) {
938 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
939 I != E; ++I)
940 Decl->addAttr(*I);
941 }
942 Decl->setImplicit();
Alexey Bataev63cc8e92018-03-20 14:45:59 +0000943 if (OrigRef) {
944 Decl->addAttr(
945 OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef));
946 }
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000947 return Decl;
948}
949
950static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
951 SourceLocation Loc,
952 bool RefersToCapture = false) {
953 D->setReferenced();
954 D->markUsed(S.Context);
955 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
956 SourceLocation(), D, RefersToCapture, Loc, Ty,
957 VK_LValue);
958}
959
Alexey Bataeve3727102018-04-18 15:57:46 +0000960void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000961 BinaryOperatorKind BOK) {
Alexey Bataevfa312f32017-07-21 18:48:21 +0000962 D = getCanonicalDecl(D);
963 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataevfa312f32017-07-21 18:48:21 +0000964 assert(
Richard Trieu09f14112017-07-21 21:29:35 +0000965 Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000966 "Additional reduction info may be specified only for reduction items.");
Alexey Bataeve3727102018-04-18 15:57:46 +0000967 ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D];
Alexey Bataevfa312f32017-07-21 18:48:21 +0000968 assert(ReductionData.ReductionRange.isInvalid() &&
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000969 Stack.back().first.back().Directive == OMPD_taskgroup &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000970 "Additional reduction info may be specified only once for reduction "
971 "items.");
972 ReductionData.set(BOK, SR);
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000973 Expr *&TaskgroupReductionRef =
974 Stack.back().first.back().TaskgroupReductionRef;
975 if (!TaskgroupReductionRef) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000976 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
977 SemaRef.Context.VoidPtrTy, ".task_red.");
Alexey Bataevd070a582017-10-25 15:54:04 +0000978 TaskgroupReductionRef =
979 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000980 }
Alexey Bataevfa312f32017-07-21 18:48:21 +0000981}
982
Alexey Bataeve3727102018-04-18 15:57:46 +0000983void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000984 const Expr *ReductionRef) {
Alexey Bataevfa312f32017-07-21 18:48:21 +0000985 D = getCanonicalDecl(D);
986 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataevfa312f32017-07-21 18:48:21 +0000987 assert(
Richard Trieu09f14112017-07-21 21:29:35 +0000988 Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000989 "Additional reduction info may be specified only for reduction items.");
Alexey Bataeve3727102018-04-18 15:57:46 +0000990 ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D];
Alexey Bataevfa312f32017-07-21 18:48:21 +0000991 assert(ReductionData.ReductionRange.isInvalid() &&
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000992 Stack.back().first.back().Directive == OMPD_taskgroup &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000993 "Additional reduction info may be specified only once for reduction "
994 "items.");
995 ReductionData.set(ReductionRef, SR);
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000996 Expr *&TaskgroupReductionRef =
997 Stack.back().first.back().TaskgroupReductionRef;
998 if (!TaskgroupReductionRef) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000999 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
1000 SemaRef.Context.VoidPtrTy, ".task_red.");
Alexey Bataevd070a582017-10-25 15:54:04 +00001001 TaskgroupReductionRef =
1002 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
Alexey Bataev3b1b8952017-07-25 15:53:26 +00001003 }
Alexey Bataevfa312f32017-07-21 18:48:21 +00001004}
1005
Alexey Bataeve3727102018-04-18 15:57:46 +00001006const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1007 const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK,
1008 Expr *&TaskgroupDescriptor) const {
Alexey Bataevfa312f32017-07-21 18:48:21 +00001009 D = getCanonicalDecl(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +00001010 assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1011 if (Stack.back().first.empty())
1012 return DSAVarData();
Alexey Bataeve3727102018-04-18 15:57:46 +00001013 for (iterator I = std::next(Stack.back().first.rbegin(), 1),
1014 E = Stack.back().first.rend();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001015 I != E; std::advance(I, 1)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001016 const DSAInfo &Data = I->SharingMap.lookup(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +00001017 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
Alexey Bataevfa312f32017-07-21 18:48:21 +00001018 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00001019 const ReductionData &ReductionData = I->ReductionMap.lookup(D);
Alexey Bataevfa312f32017-07-21 18:48:21 +00001020 if (!ReductionData.ReductionOp ||
1021 ReductionData.ReductionOp.is<const Expr *>())
Alexey Bataevf189cb72017-07-24 14:52:13 +00001022 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001023 SR = ReductionData.ReductionRange;
Alexey Bataevf87fa882017-07-21 19:26:22 +00001024 BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>();
Alexey Bataev88202be2017-07-27 13:20:36 +00001025 assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1026 "expression for the descriptor is not "
1027 "set.");
1028 TaskgroupDescriptor = I->TaskgroupReductionRef;
Alexey Bataevf189cb72017-07-24 14:52:13 +00001029 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
1030 Data.PrivateCopy, I->DefaultAttrLoc);
Alexey Bataevfa312f32017-07-21 18:48:21 +00001031 }
Alexey Bataevf189cb72017-07-24 14:52:13 +00001032 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001033}
1034
Alexey Bataeve3727102018-04-18 15:57:46 +00001035const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1036 const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef,
1037 Expr *&TaskgroupDescriptor) const {
Alexey Bataevfa312f32017-07-21 18:48:21 +00001038 D = getCanonicalDecl(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +00001039 assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1040 if (Stack.back().first.empty())
1041 return DSAVarData();
Alexey Bataeve3727102018-04-18 15:57:46 +00001042 for (iterator I = std::next(Stack.back().first.rbegin(), 1),
1043 E = Stack.back().first.rend();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001044 I != E; std::advance(I, 1)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001045 const DSAInfo &Data = I->SharingMap.lookup(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +00001046 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
Alexey Bataevfa312f32017-07-21 18:48:21 +00001047 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00001048 const ReductionData &ReductionData = I->ReductionMap.lookup(D);
Alexey Bataevfa312f32017-07-21 18:48:21 +00001049 if (!ReductionData.ReductionOp ||
1050 !ReductionData.ReductionOp.is<const Expr *>())
Alexey Bataevf189cb72017-07-24 14:52:13 +00001051 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001052 SR = ReductionData.ReductionRange;
1053 ReductionRef = ReductionData.ReductionOp.get<const Expr *>();
Alexey Bataev88202be2017-07-27 13:20:36 +00001054 assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1055 "expression for the descriptor is not "
1056 "set.");
1057 TaskgroupDescriptor = I->TaskgroupReductionRef;
Alexey Bataevf189cb72017-07-24 14:52:13 +00001058 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
1059 Data.PrivateCopy, I->DefaultAttrLoc);
Alexey Bataevfa312f32017-07-21 18:48:21 +00001060 }
Alexey Bataevf189cb72017-07-24 14:52:13 +00001061 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001062}
1063
Alexey Bataeve3727102018-04-18 15:57:46 +00001064bool DSAStackTy::isOpenMPLocal(VarDecl *D, iterator Iter) const {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001065 D = D->getCanonicalDecl();
Alexey Bataev852525d2018-03-02 17:17:12 +00001066 if (!isStackEmpty()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001067 iterator I = Iter, E = Stack.back().first.rend();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001068 Scope *TopScope = nullptr;
Alexey Bataev852525d2018-03-02 17:17:12 +00001069 while (I != E && !isParallelOrTaskRegion(I->Directive) &&
1070 !isOpenMPTargetExecutionDirective(I->Directive))
Alexey Bataevec3da872014-01-31 05:15:34 +00001071 ++I;
Alexey Bataeved09d242014-05-28 05:53:51 +00001072 if (I == E)
1073 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001074 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001075 Scope *CurScope = getCurScope();
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001076 while (CurScope != TopScope && !CurScope->isDeclScope(D))
Alexey Bataev758e55e2013-09-06 18:03:48 +00001077 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +00001078 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001079 }
Alexey Bataevec3da872014-01-31 05:15:34 +00001080 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001081}
1082
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001083static bool isConstNotMutableType(Sema &SemaRef, ValueDecl *D,
1084 bool *IsClassType = nullptr) {
1085 ASTContext &Context = SemaRef.getASTContext();
1086 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
1087 bool IsConstant = Type.isConstant(Context);
1088 Type = Context.getBaseElementType(Type);
1089 const CXXRecordDecl *RD =
1090 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
1091 if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
1092 if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate())
1093 RD = CTD->getTemplatedDecl();
1094 if (IsClassType)
1095 *IsClassType = RD;
1096 return IsConstant && !(SemaRef.getLangOpts().CPlusPlus && RD &&
1097 RD->hasDefinition() && RD->hasMutableFields());
1098}
1099
1100static bool rejectConstNotMutableType(Sema &SemaRef, ValueDecl *D,
1101 OpenMPClauseKind CKind,
1102 SourceLocation ELoc) {
1103 ASTContext &Context = SemaRef.getASTContext();
1104 bool IsClassType;
1105 if (isConstNotMutableType(SemaRef, D, &IsClassType)) {
1106 SemaRef.Diag(ELoc, IsClassType ? diag::err_omp_const_not_mutable_variable
1107 : diag::err_omp_const_variable)
1108 << getOpenMPClauseName(CKind);
1109 VarDecl *VD = dyn_cast<VarDecl>(D);
1110 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
1111 VarDecl::DeclarationOnly;
1112 SemaRef.Diag(D->getLocation(),
1113 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1114 << D;
1115 return true;
1116 }
1117 return false;
1118}
1119
Alexey Bataeve3727102018-04-18 15:57:46 +00001120const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
1121 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001122 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001123 DSAVarData DVar;
1124
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001125 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001126 auto TI = Threadprivates.find(D);
1127 if (TI != Threadprivates.end()) {
1128 DVar.RefExpr = TI->getSecond().RefExpr.getPointer();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001129 DVar.CKind = OMPC_threadprivate;
1130 return DVar;
Alexey Bataeve3727102018-04-18 15:57:46 +00001131 }
1132 if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
Alexey Bataev817d7f32017-11-14 21:01:01 +00001133 DVar.RefExpr = buildDeclRefExpr(
1134 SemaRef, VD, D->getType().getNonReferenceType(),
1135 VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation());
1136 DVar.CKind = OMPC_threadprivate;
1137 addDSA(D, DVar.RefExpr, OMPC_threadprivate);
Alexey Bataev852525d2018-03-02 17:17:12 +00001138 return DVar;
1139 }
1140 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1141 // in a Construct, C/C++, predetermined, p.1]
1142 // Variables appearing in threadprivate directives are threadprivate.
1143 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
1144 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1145 SemaRef.getLangOpts().OpenMPUseTLS &&
1146 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
1147 (VD && VD->getStorageClass() == SC_Register &&
1148 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
1149 DVar.RefExpr = buildDeclRefExpr(
1150 SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation());
1151 DVar.CKind = OMPC_threadprivate;
1152 addDSA(D, DVar.RefExpr, OMPC_threadprivate);
1153 return DVar;
1154 }
1155 if (SemaRef.getLangOpts().OpenMPCUDAMode && VD &&
1156 VD->isLocalVarDeclOrParm() && !isStackEmpty() &&
1157 !isLoopControlVariable(D).first) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001158 iterator IterTarget =
Alexey Bataev852525d2018-03-02 17:17:12 +00001159 std::find_if(Stack.back().first.rbegin(), Stack.back().first.rend(),
1160 [](const SharingMapTy &Data) {
1161 return isOpenMPTargetExecutionDirective(Data.Directive);
1162 });
1163 if (IterTarget != Stack.back().first.rend()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001164 iterator ParentIterTarget = std::next(IterTarget, 1);
1165 for (iterator Iter = Stack.back().first.rbegin();
1166 Iter != ParentIterTarget; std::advance(Iter, 1)) {
Alexey Bataev852525d2018-03-02 17:17:12 +00001167 if (isOpenMPLocal(VD, Iter)) {
1168 DVar.RefExpr =
1169 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1170 D->getLocation());
1171 DVar.CKind = OMPC_threadprivate;
1172 return DVar;
1173 }
Alexey Bataev852525d2018-03-02 17:17:12 +00001174 }
1175 if (!isClauseParsingMode() || IterTarget != Stack.back().first.rbegin()) {
1176 auto DSAIter = IterTarget->SharingMap.find(D);
1177 if (DSAIter != IterTarget->SharingMap.end() &&
1178 isOpenMPPrivate(DSAIter->getSecond().Attributes)) {
1179 DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer();
1180 DVar.CKind = OMPC_threadprivate;
1181 return DVar;
Alexey Bataeve3727102018-04-18 15:57:46 +00001182 }
1183 iterator End = Stack.back().first.rend();
1184 if (!SemaRef.isOpenMPCapturedByRef(
1185 D, std::distance(ParentIterTarget, End))) {
Alexey Bataev852525d2018-03-02 17:17:12 +00001186 DVar.RefExpr =
1187 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1188 IterTarget->ConstructLoc);
1189 DVar.CKind = OMPC_threadprivate;
1190 return DVar;
1191 }
1192 }
1193 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001194 }
1195
Alexey Bataev4b465392017-04-26 15:06:24 +00001196 if (isStackEmpty())
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001197 // Not in OpenMP execution region and top scope was already checked.
1198 return DVar;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001199
Alexey Bataev758e55e2013-09-06 18:03:48 +00001200 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +00001201 // in a Construct, C/C++, predetermined, p.4]
1202 // Static data members are shared.
1203 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1204 // in a Construct, C/C++, predetermined, p.7]
1205 // Variables with static storage duration that are declared in a scope
1206 // inside the construct are shared.
Alexey Bataeve3727102018-04-18 15:57:46 +00001207 auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; };
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001208 if (VD && VD->isStaticDataMember()) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001209 DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent);
Alexey Bataevdffa93a2015-12-10 08:20:58 +00001210 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
Alexey Bataevec3da872014-01-31 05:15:34 +00001211 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001212
Alexey Bataevdffa93a2015-12-10 08:20:58 +00001213 DVar.CKind = OMPC_shared;
1214 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001215 }
1216
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001217 // The predetermined shared attribute for const-qualified types having no
1218 // mutable members was removed after OpenMP 3.1.
1219 if (SemaRef.LangOpts.OpenMP <= 31) {
1220 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1221 // in a Construct, C/C++, predetermined, p.6]
1222 // Variables with const qualified type having no mutable member are
1223 // shared.
1224 if (isConstNotMutableType(SemaRef, D)) {
1225 // Variables with const-qualified type having no mutable member may be
1226 // listed in a firstprivate clause, even if they are static data members.
1227 DSAVarData DVarTemp = hasInnermostDSA(
1228 D,
1229 [](OpenMPClauseKind C) {
1230 return C == OMPC_firstprivate || C == OMPC_shared;
1231 },
1232 MatchesAlways, FromParent);
1233 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
1234 return DVarTemp;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001235
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001236 DVar.CKind = OMPC_shared;
1237 return DVar;
1238 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001239 }
1240
Alexey Bataev758e55e2013-09-06 18:03:48 +00001241 // Explicitly specified attributes and local variables with predetermined
1242 // attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +00001243 iterator I = Stack.back().first.rbegin();
1244 iterator EndI = Stack.back().first.rend();
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001245 if (FromParent && I != EndI)
1246 std::advance(I, 1);
Alexey Bataeve3727102018-04-18 15:57:46 +00001247 auto It = I->SharingMap.find(D);
1248 if (It != I->SharingMap.end()) {
1249 const DSAInfo &Data = It->getSecond();
1250 DVar.RefExpr = Data.RefExpr.getPointer();
1251 DVar.PrivateCopy = Data.PrivateCopy;
1252 DVar.CKind = Data.Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001253 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev4d4624c2017-07-20 16:47:47 +00001254 DVar.DKind = I->Directive;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001255 }
1256
1257 return DVar;
1258}
1259
Alexey Bataeve3727102018-04-18 15:57:46 +00001260const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1261 bool FromParent) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001262 if (isStackEmpty()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001263 iterator I;
Alexey Bataev4b465392017-04-26 15:06:24 +00001264 return getDSA(I, D);
1265 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001266 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +00001267 iterator StartI = Stack.back().first.rbegin();
1268 iterator EndI = Stack.back().first.rend();
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001269 if (FromParent && StartI != EndI)
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001270 std::advance(StartI, 1);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001271 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001272}
1273
Alexey Bataeve3727102018-04-18 15:57:46 +00001274const DSAStackTy::DSAVarData
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001275DSAStackTy::hasDSA(ValueDecl *D,
Alexey Bataev97d18bf2018-04-11 19:21:00 +00001276 const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1277 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +00001278 bool FromParent) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001279 if (isStackEmpty())
1280 return {};
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001281 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +00001282 iterator I = Stack.back().first.rbegin();
1283 iterator EndI = Stack.back().first.rend();
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001284 if (FromParent && I != EndI)
Alexey Bataev0e6fc1c2017-04-27 14:46:26 +00001285 std::advance(I, 1);
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001286 for (; I != EndI; std::advance(I, 1)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001287 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +00001288 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00001289 iterator NewI = I;
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001290 DSAVarData DVar = getDSA(NewI, D);
1291 if (I == NewI && CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001292 return DVar;
Alexey Bataev60859c02017-04-27 15:10:33 +00001293 }
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001294 return {};
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001295}
1296
Alexey Bataeve3727102018-04-18 15:57:46 +00001297const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
Alexey Bataev97d18bf2018-04-11 19:21:00 +00001298 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1299 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +00001300 bool FromParent) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001301 if (isStackEmpty())
1302 return {};
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001303 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +00001304 iterator StartI = Stack.back().first.rbegin();
1305 iterator EndI = Stack.back().first.rend();
Alexey Bataeve3978122016-07-19 05:06:39 +00001306 if (FromParent && StartI != EndI)
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001307 std::advance(StartI, 1);
Alexey Bataeve3978122016-07-19 05:06:39 +00001308 if (StartI == EndI || !DPred(StartI->Directive))
Alexey Bataev4b465392017-04-26 15:06:24 +00001309 return {};
Alexey Bataeve3727102018-04-18 15:57:46 +00001310 iterator NewI = StartI;
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001311 DSAVarData DVar = getDSA(NewI, D);
1312 return (NewI == StartI && CPred(DVar.CKind)) ? DVar : DSAVarData();
Alexey Bataevc5e02582014-06-16 07:08:35 +00001313}
1314
Alexey Bataevaac108a2015-06-23 04:51:00 +00001315bool DSAStackTy::hasExplicitDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00001316 const ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1317 unsigned Level, bool NotLastprivate) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001318 if (isStackEmpty())
1319 return false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001320 D = getCanonicalDecl(D);
Alexey Bataev4b465392017-04-26 15:06:24 +00001321 auto StartI = Stack.back().first.begin();
1322 auto EndI = Stack.back().first.end();
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +00001323 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +00001324 return false;
1325 std::advance(StartI, Level);
Alexey Bataeve3727102018-04-18 15:57:46 +00001326 auto I = StartI->SharingMap.find(D);
Alexey Bataev92b33652018-11-21 19:41:10 +00001327 if ((I != StartI->SharingMap.end()) &&
Alexey Bataeve3727102018-04-18 15:57:46 +00001328 I->getSecond().RefExpr.getPointer() &&
1329 CPred(I->getSecond().Attributes) &&
Alexey Bataev92b33652018-11-21 19:41:10 +00001330 (!NotLastprivate || !I->getSecond().RefExpr.getInt()))
1331 return true;
1332 // Check predetermined rules for the loop control variables.
1333 auto LI = StartI->LCVMap.find(D);
1334 if (LI != StartI->LCVMap.end())
1335 return CPred(OMPC_private);
1336 return false;
Alexey Bataevaac108a2015-06-23 04:51:00 +00001337}
1338
Samuel Antao4be30e92015-10-02 17:14:03 +00001339bool DSAStackTy::hasExplicitDirective(
Alexey Bataeve3727102018-04-18 15:57:46 +00001340 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1341 unsigned Level) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001342 if (isStackEmpty())
1343 return false;
1344 auto StartI = Stack.back().first.begin();
1345 auto EndI = Stack.back().first.end();
Samuel Antao4be30e92015-10-02 17:14:03 +00001346 if (std::distance(StartI, EndI) <= (int)Level)
1347 return false;
1348 std::advance(StartI, Level);
1349 return DPred(StartI->Directive);
1350}
1351
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001352bool DSAStackTy::hasDirective(
1353 const llvm::function_ref<bool(OpenMPDirectiveKind,
1354 const DeclarationNameInfo &, SourceLocation)>
Alexey Bataev97d18bf2018-04-11 19:21:00 +00001355 DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +00001356 bool FromParent) const {
Samuel Antaof0d79752016-05-27 15:21:27 +00001357 // We look only in the enclosing region.
Alexey Bataev4b465392017-04-26 15:06:24 +00001358 if (isStackEmpty())
Samuel Antaof0d79752016-05-27 15:21:27 +00001359 return false;
Alexey Bataev4b465392017-04-26 15:06:24 +00001360 auto StartI = std::next(Stack.back().first.rbegin());
1361 auto EndI = Stack.back().first.rend();
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001362 if (FromParent && StartI != EndI)
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001363 StartI = std::next(StartI);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001364 for (auto I = StartI, EE = EndI; I != EE; ++I) {
1365 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
1366 return true;
1367 }
1368 return false;
1369}
1370
Alexey Bataev758e55e2013-09-06 18:03:48 +00001371void Sema::InitDataSharingAttributesStack() {
1372 VarDataSharingAttributesStack = new DSAStackTy(*this);
1373}
1374
1375#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
1376
Alexey Bataev4b465392017-04-26 15:06:24 +00001377void Sema::pushOpenMPFunctionRegion() {
1378 DSAStack->pushFunction();
1379}
1380
1381void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) {
1382 DSAStack->popFunction(OldFSI);
1383}
1384
Alexey Bataeve3727102018-04-18 15:57:46 +00001385bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level) const {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001386 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1387
Alexey Bataeve3727102018-04-18 15:57:46 +00001388 ASTContext &Ctx = getASTContext();
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001389 bool IsByRef = true;
1390
1391 // Find the directive that is associated with the provided scope.
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00001392 D = cast<ValueDecl>(D->getCanonicalDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00001393 QualType Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001394
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001395 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001396 // This table summarizes how a given variable should be passed to the device
1397 // given its type and the clauses where it appears. This table is based on
1398 // the description in OpenMP 4.5 [2.10.4, target Construct] and
1399 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
1400 //
1401 // =========================================================================
1402 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
1403 // | |(tofrom:scalar)| | pvt | | | |
1404 // =========================================================================
1405 // | scl | | | | - | | bycopy|
1406 // | scl | | - | x | - | - | bycopy|
1407 // | scl | | x | - | - | - | null |
1408 // | scl | x | | | - | | byref |
1409 // | scl | x | - | x | - | - | bycopy|
1410 // | scl | x | x | - | - | - | null |
1411 // | scl | | - | - | - | x | byref |
1412 // | scl | x | - | - | - | x | byref |
1413 //
1414 // | agg | n.a. | | | - | | byref |
1415 // | agg | n.a. | - | x | - | - | byref |
1416 // | agg | n.a. | x | - | - | - | null |
1417 // | agg | n.a. | - | - | - | x | byref |
1418 // | agg | n.a. | - | - | - | x[] | byref |
1419 //
1420 // | ptr | n.a. | | | - | | bycopy|
1421 // | ptr | n.a. | - | x | - | - | bycopy|
1422 // | ptr | n.a. | x | - | - | - | null |
1423 // | ptr | n.a. | - | - | - | x | byref |
1424 // | ptr | n.a. | - | - | - | x[] | bycopy|
1425 // | ptr | n.a. | - | - | x | | bycopy|
1426 // | ptr | n.a. | - | - | x | x | bycopy|
1427 // | ptr | n.a. | - | - | x | x[] | bycopy|
1428 // =========================================================================
1429 // Legend:
1430 // scl - scalar
1431 // ptr - pointer
1432 // agg - aggregate
1433 // x - applies
1434 // - - invalid in this combination
1435 // [] - mapped with an array section
1436 // byref - should be mapped by reference
1437 // byval - should be mapped by value
1438 // null - initialize a local variable to null on the device
1439 //
1440 // Observations:
1441 // - All scalar declarations that show up in a map clause have to be passed
1442 // by reference, because they may have been mapped in the enclosing data
1443 // environment.
1444 // - If the scalar value does not fit the size of uintptr, it has to be
1445 // passed by reference, regardless the result in the table above.
1446 // - For pointers mapped by value that have either an implicit map or an
1447 // array section, the runtime library may pass the NULL value to the
1448 // device instead of the value passed to it by the compiler.
1449
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001450 if (Ty->isReferenceType())
1451 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
Samuel Antao86ace552016-04-27 22:40:57 +00001452
1453 // Locate map clauses and see if the variable being captured is referred to
1454 // in any of those clauses. Here we only care about variables, not fields,
1455 // because fields are part of aggregates.
1456 bool IsVariableUsedInMapClause = false;
1457 bool IsVariableAssociatedWithSection = false;
1458
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +00001459 DSAStack->checkMappableExprComponentListsForDeclAtLevel(
Alexey Bataeve3727102018-04-18 15:57:46 +00001460 D, Level,
1461 [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D](
1462 OMPClauseMappableExprCommon::MappableExprComponentListRef
Samuel Antao6890b092016-07-28 14:25:09 +00001463 MapExprComponents,
1464 OpenMPClauseKind WhereFoundClauseKind) {
1465 // Only the map clause information influences how a variable is
1466 // captured. E.g. is_device_ptr does not require changing the default
Samuel Antao4c8035b2016-12-12 18:00:20 +00001467 // behavior.
Samuel Antao6890b092016-07-28 14:25:09 +00001468 if (WhereFoundClauseKind != OMPC_map)
1469 return false;
Samuel Antao86ace552016-04-27 22:40:57 +00001470
1471 auto EI = MapExprComponents.rbegin();
1472 auto EE = MapExprComponents.rend();
1473
1474 assert(EI != EE && "Invalid map expression!");
1475
1476 if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
1477 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
1478
1479 ++EI;
1480 if (EI == EE)
1481 return false;
1482
1483 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
1484 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
1485 isa<MemberExpr>(EI->getAssociatedExpression())) {
1486 IsVariableAssociatedWithSection = true;
1487 // There is nothing more we need to know about this variable.
1488 return true;
1489 }
1490
1491 // Keep looking for more map info.
1492 return false;
1493 });
1494
1495 if (IsVariableUsedInMapClause) {
1496 // If variable is identified in a map clause it is always captured by
1497 // reference except if it is a pointer that is dereferenced somehow.
1498 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
1499 } else {
Alexey Bataev3f96fe62017-12-13 17:31:39 +00001500 // By default, all the data that has a scalar type is mapped by copy
1501 // (except for reduction variables).
1502 IsByRef =
Alexey Bataev60705422018-10-30 15:50:12 +00001503 (DSAStack->isForceCaptureByReferenceInTargetExecutable() &&
1504 !Ty->isAnyPointerType()) ||
Alexey Bataev3f96fe62017-12-13 17:31:39 +00001505 !Ty->isScalarType() ||
1506 DSAStack->getDefaultDMAAtLevel(Level) == DMA_tofrom_scalar ||
1507 DSAStack->hasExplicitDSA(
1508 D, [](OpenMPClauseKind K) { return K == OMPC_reduction; }, Level);
Samuel Antao86ace552016-04-27 22:40:57 +00001509 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001510 }
1511
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001512 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00001513 IsByRef =
Alexey Bataev60705422018-10-30 15:50:12 +00001514 ((DSAStack->isForceCaptureByReferenceInTargetExecutable() &&
1515 !Ty->isAnyPointerType()) ||
1516 !DSAStack->hasExplicitDSA(
1517 D,
1518 [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; },
1519 Level, /*NotLastprivate=*/true)) &&
Alexey Bataev8e769ee2017-12-22 21:01:52 +00001520 // If the variable is artificial and must be captured by value - try to
1521 // capture by value.
Alexey Bataevd2202ca2017-12-27 17:58:32 +00001522 !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() &&
1523 !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue());
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001524 }
1525
Samuel Antao86ace552016-04-27 22:40:57 +00001526 // When passing data by copy, we need to make sure it fits the uintptr size
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001527 // and alignment, because the runtime library only deals with uintptr types.
1528 // If it does not fit the uintptr size, we need to pass the data by reference
1529 // instead.
1530 if (!IsByRef &&
1531 (Ctx.getTypeSizeInChars(Ty) >
1532 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001533 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001534 IsByRef = true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001535 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001536
1537 return IsByRef;
1538}
1539
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001540unsigned Sema::getOpenMPNestingLevel() const {
1541 assert(getLangOpts().OpenMP);
1542 return DSAStack->getNestingLevel();
1543}
1544
Jonas Hahnfeld87d44262017-11-18 21:00:46 +00001545bool Sema::isInOpenMPTargetExecutionDirective() const {
1546 return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) &&
1547 !DSAStack->isClauseParsingMode()) ||
1548 DSAStack->hasDirective(
1549 [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
1550 SourceLocation) -> bool {
1551 return isOpenMPTargetExecutionDirective(K);
1552 },
1553 false);
1554}
1555
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001556VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D) {
Alexey Bataevf841bd92014-12-16 07:00:22 +00001557 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001558 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +00001559
1560 // If we are attempting to capture a global variable in a directive with
1561 // 'target' we return true so that this global is also mapped to the device.
1562 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001563 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001564 if (VD && !VD->hasLocalStorage()) {
1565 if (isInOpenMPDeclareTargetContext() &&
1566 (getCurCapturedRegion() || getCurBlock() || getCurLambda())) {
1567 // Try to mark variable as declare target if it is used in capturing
1568 // regions.
Alexey Bataev97b72212018-08-14 18:31:20 +00001569 if (!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001570 checkDeclIsAllowedInOpenMPTarget(nullptr, VD);
Alexey Bataev92327c52018-03-26 16:40:55 +00001571 return nullptr;
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001572 } else if (isInOpenMPTargetExecutionDirective()) {
1573 // If the declaration is enclosed in a 'declare target' directive,
1574 // then it should not be captured.
1575 //
Alexey Bataev97b72212018-08-14 18:31:20 +00001576 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001577 return nullptr;
1578 return VD;
1579 }
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001580 }
Alexey Bataev60705422018-10-30 15:50:12 +00001581 // Capture variables captured by reference in lambdas for target-based
1582 // directives.
1583 if (VD && !DSAStack->isClauseParsingMode()) {
1584 if (const auto *RD = VD->getType()
1585 .getCanonicalType()
1586 .getNonReferenceType()
1587 ->getAsCXXRecordDecl()) {
1588 bool SavedForceCaptureByReferenceInTargetExecutable =
1589 DSAStack->isForceCaptureByReferenceInTargetExecutable();
1590 DSAStack->setForceCaptureByReferenceInTargetExecutable(/*V=*/true);
Alexey Bataevd1840e52018-11-16 21:13:33 +00001591 if (RD->isLambda()) {
1592 llvm::DenseMap<const VarDecl *, FieldDecl *> Captures;
1593 FieldDecl *ThisCapture;
1594 RD->getCaptureFields(Captures, ThisCapture);
Alexey Bataev60705422018-10-30 15:50:12 +00001595 for (const LambdaCapture &LC : RD->captures()) {
1596 if (LC.getCaptureKind() == LCK_ByRef) {
1597 VarDecl *VD = LC.getCapturedVar();
1598 DeclContext *VDC = VD->getDeclContext();
1599 if (!VDC->Encloses(CurContext))
1600 continue;
1601 DSAStackTy::DSAVarData DVarPrivate =
1602 DSAStack->getTopDSA(VD, /*FromParent=*/false);
1603 // Do not capture already captured variables.
1604 if (!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) &&
1605 DVarPrivate.CKind == OMPC_unknown &&
1606 !DSAStack->checkMappableExprComponentListsForDecl(
1607 D, /*CurrentRegionOnly=*/true,
1608 [](OMPClauseMappableExprCommon::
1609 MappableExprComponentListRef,
1610 OpenMPClauseKind) { return true; }))
1611 MarkVariableReferenced(LC.getLocation(), LC.getCapturedVar());
1612 } else if (LC.getCaptureKind() == LCK_This) {
Alexey Bataevd1840e52018-11-16 21:13:33 +00001613 QualType ThisTy = getCurrentThisType();
1614 if (!ThisTy.isNull() &&
1615 Context.typesAreCompatible(ThisTy, ThisCapture->getType()))
1616 CheckCXXThisCapture(LC.getLocation());
Alexey Bataev60705422018-10-30 15:50:12 +00001617 }
1618 }
Alexey Bataevd1840e52018-11-16 21:13:33 +00001619 }
Alexey Bataev60705422018-10-30 15:50:12 +00001620 DSAStack->setForceCaptureByReferenceInTargetExecutable(
1621 SavedForceCaptureByReferenceInTargetExecutable);
1622 }
1623 }
Samuel Antao4be30e92015-10-02 17:14:03 +00001624
Alexey Bataev48977c32015-08-04 08:10:48 +00001625 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
1626 (!DSAStack->isClauseParsingMode() ||
1627 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001628 auto &&Info = DSAStack->isLoopControlVariable(D);
1629 if (Info.first ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001630 (VD && VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001631 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001632 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001633 return VD ? VD : Info.second;
Alexey Bataeve3727102018-04-18 15:57:46 +00001634 DSAStackTy::DSAVarData DVarPrivate =
1635 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001636 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
Alexey Bataev90c228f2016-02-08 09:29:13 +00001637 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00001638 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate,
1639 [](OpenMPDirectiveKind) { return true; },
1640 DSAStack->isClauseParsingMode());
Alexey Bataev90c228f2016-02-08 09:29:13 +00001641 if (DVarPrivate.CKind != OMPC_unknown)
1642 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001643 }
Alexey Bataev90c228f2016-02-08 09:29:13 +00001644 return nullptr;
Alexey Bataevf841bd92014-12-16 07:00:22 +00001645}
1646
Alexey Bataevdfa430f2017-12-08 15:03:50 +00001647void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex,
1648 unsigned Level) const {
1649 SmallVector<OpenMPDirectiveKind, 4> Regions;
1650 getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level));
1651 FunctionScopesIndex -= Regions.size();
1652}
1653
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00001654void Sema::startOpenMPLoop() {
1655 assert(LangOpts.OpenMP && "OpenMP must be enabled.");
1656 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective()))
1657 DSAStack->loopInit();
1658}
1659
Alexey Bataeve3727102018-04-18 15:57:46 +00001660bool Sema::isOpenMPPrivateDecl(const ValueDecl *D, unsigned Level) const {
Alexey Bataevaac108a2015-06-23 04:51:00 +00001661 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00001662 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
1663 if (DSAStack->getAssociatedLoops() > 0 &&
1664 !DSAStack->isLoopStarted()) {
1665 DSAStack->resetPossibleLoopCounter(D);
1666 DSAStack->loopStart();
1667 return true;
1668 }
1669 if ((DSAStack->getPossiblyLoopCunter() == D->getCanonicalDecl() ||
1670 DSAStack->isLoopControlVariable(D).first) &&
1671 !DSAStack->hasExplicitDSA(
1672 D, [](OpenMPClauseKind K) { return K != OMPC_private; }, Level) &&
1673 !isOpenMPSimdDirective(DSAStack->getCurrentDirective()))
1674 return true;
1675 }
Alexey Bataevaac108a2015-06-23 04:51:00 +00001676 return DSAStack->hasExplicitDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00001677 D, [](OpenMPClauseKind K) { return K == OMPC_private; }, Level) ||
Alexey Bataev3f82cfc2017-12-13 15:28:44 +00001678 (DSAStack->isClauseParsingMode() &&
1679 DSAStack->getClauseParsingMode() == OMPC_private) ||
Alexey Bataev88202be2017-07-27 13:20:36 +00001680 // Consider taskgroup reduction descriptor variable a private to avoid
1681 // possible capture in the region.
1682 (DSAStack->hasExplicitDirective(
1683 [](OpenMPDirectiveKind K) { return K == OMPD_taskgroup; },
1684 Level) &&
1685 DSAStack->isTaskgroupReductionRef(D, Level));
Alexey Bataevaac108a2015-06-23 04:51:00 +00001686}
1687
Alexey Bataeve3727102018-04-18 15:57:46 +00001688void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D,
1689 unsigned Level) {
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001690 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1691 D = getCanonicalDecl(D);
1692 OpenMPClauseKind OMPC = OMPC_unknown;
1693 for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) {
1694 const unsigned NewLevel = I - 1;
1695 if (DSAStack->hasExplicitDSA(D,
1696 [&OMPC](const OpenMPClauseKind K) {
1697 if (isOpenMPPrivate(K)) {
1698 OMPC = K;
1699 return true;
1700 }
1701 return false;
1702 },
1703 NewLevel))
1704 break;
1705 if (DSAStack->checkMappableExprComponentListsForDeclAtLevel(
1706 D, NewLevel,
1707 [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
1708 OpenMPClauseKind) { return true; })) {
1709 OMPC = OMPC_map;
1710 break;
1711 }
1712 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1713 NewLevel)) {
Alexey Bataev63cc8e92018-03-20 14:45:59 +00001714 OMPC = OMPC_map;
1715 if (D->getType()->isScalarType() &&
1716 DSAStack->getDefaultDMAAtLevel(NewLevel) !=
1717 DefaultMapAttributes::DMA_tofrom_scalar)
1718 OMPC = OMPC_firstprivate;
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001719 break;
1720 }
1721 }
1722 if (OMPC != OMPC_unknown)
1723 FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, OMPC));
1724}
1725
Alexey Bataeve3727102018-04-18 15:57:46 +00001726bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D,
1727 unsigned Level) const {
Samuel Antao4be30e92015-10-02 17:14:03 +00001728 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1729 // Return true if the current level is no longer enclosed in a target region.
1730
Alexey Bataeve3727102018-04-18 15:57:46 +00001731 const auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001732 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00001733 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1734 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +00001735}
1736
Alexey Bataeved09d242014-05-28 05:53:51 +00001737void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001738
1739void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
1740 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001741 Scope *CurScope, SourceLocation Loc) {
1742 DSAStack->push(DKind, DirName, CurScope, Loc);
Faisal Valid143a0c2017-04-01 21:30:49 +00001743 PushExpressionEvaluationContext(
1744 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001745}
1746
Alexey Bataevaac108a2015-06-23 04:51:00 +00001747void Sema::StartOpenMPClause(OpenMPClauseKind K) {
1748 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001749}
1750
Alexey Bataevaac108a2015-06-23 04:51:00 +00001751void Sema::EndOpenMPClause() {
1752 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001753}
1754
Alexey Bataev758e55e2013-09-06 18:03:48 +00001755void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001756 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
1757 // A variable of class type (or array thereof) that appears in a lastprivate
1758 // clause requires an accessible, unambiguous default constructor for the
1759 // class type, unless the list item is also specified in a firstprivate
1760 // clause.
Alexey Bataeve3727102018-04-18 15:57:46 +00001761 if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
1762 for (OMPClause *C : D->clauses()) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001763 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
1764 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeve3727102018-04-18 15:57:46 +00001765 for (Expr *DE : Clause->varlists()) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001766 if (DE->isValueDependent() || DE->isTypeDependent()) {
1767 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001768 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +00001769 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00001770 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
Alexey Bataeve3727102018-04-18 15:57:46 +00001771 auto *VD = cast<VarDecl>(DRE->getDecl());
Alexey Bataev005248a2016-02-25 05:25:57 +00001772 QualType Type = VD->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +00001773 const DSAStackTy::DSAVarData DVar =
1774 DSAStack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001775 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001776 // Generate helper private variable and initialize it with the
1777 // default value. The address of the original variable is replaced
1778 // by the address of the new private variable in CodeGen. This new
1779 // variable is not added to IdResolver, so the code in the OpenMP
1780 // region uses original variable for proper diagnostics.
Alexey Bataeve3727102018-04-18 15:57:46 +00001781 VarDecl *VDPrivate = buildVarDecl(
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001782 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev63cc8e92018-03-20 14:45:59 +00001783 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE);
Richard Smith3beb7c62017-01-12 02:27:38 +00001784 ActOnUninitializedDecl(VDPrivate);
Alexey Bataev38e89532015-04-16 04:54:05 +00001785 if (VDPrivate->isInvalidDecl())
1786 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001787 PrivateCopies.push_back(buildDeclRefExpr(
1788 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001789 } else {
1790 // The variable is also a firstprivate, so initialization sequence
1791 // for private copy is generated already.
1792 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001793 }
1794 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001795 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001796 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001797 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001798 }
1799 }
1800 }
1801
Alexey Bataev758e55e2013-09-06 18:03:48 +00001802 DSAStack->pop();
1803 DiscardCleanupsInEvaluationContext();
1804 PopExpressionEvaluationContext();
1805}
1806
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001807static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1808 Expr *NumIterations, Sema &SemaRef,
1809 Scope *S, DSAStackTy *Stack);
Alexander Musman3276a272015-03-21 10:12:56 +00001810
Alexey Bataeva769e072013-03-22 06:34:35 +00001811namespace {
1812
Alexey Bataeve3727102018-04-18 15:57:46 +00001813class VarDeclFilterCCC final : public CorrectionCandidateCallback {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001814private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001815 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001816
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001817public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001818 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001819 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001820 NamedDecl *ND = Candidate.getCorrectionDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +00001821 if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001822 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001823 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1824 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001825 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001826 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001827 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001828};
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001829
Alexey Bataeve3727102018-04-18 15:57:46 +00001830class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001831private:
1832 Sema &SemaRef;
1833
1834public:
1835 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
1836 bool ValidateCandidate(const TypoCorrection &Candidate) override {
1837 NamedDecl *ND = Candidate.getCorrectionDecl();
Kelvin Li59e3d192017-11-30 18:52:06 +00001838 if (ND && (isa<VarDecl>(ND) || isa<FunctionDecl>(ND))) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001839 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1840 SemaRef.getCurScope());
1841 }
1842 return false;
1843 }
1844};
1845
Alexey Bataeved09d242014-05-28 05:53:51 +00001846} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001847
1848ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1849 CXXScopeSpec &ScopeSpec,
1850 const DeclarationNameInfo &Id) {
1851 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1852 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1853
1854 if (Lookup.isAmbiguous())
1855 return ExprError();
1856
1857 VarDecl *VD;
1858 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001859 if (TypoCorrection Corrected = CorrectTypo(
1860 Id, LookupOrdinaryName, CurScope, nullptr,
1861 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001862 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001863 PDiag(Lookup.empty()
1864 ? diag::err_undeclared_var_use_suggest
1865 : diag::err_omp_expected_var_arg_suggest)
1866 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001867 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001868 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001869 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1870 : diag::err_omp_expected_var_arg)
1871 << Id.getName();
1872 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001873 }
Alexey Bataeve3727102018-04-18 15:57:46 +00001874 } else if (!(VD = Lookup.getAsSingle<VarDecl>())) {
1875 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
1876 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1877 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001878 }
1879 Lookup.suppressDiagnostics();
1880
1881 // OpenMP [2.9.2, Syntax, C/C++]
1882 // Variables must be file-scope, namespace-scope, or static block-scope.
1883 if (!VD->hasGlobalStorage()) {
1884 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001885 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1886 bool IsDecl =
1887 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001888 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001889 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1890 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001891 return ExprError();
1892 }
1893
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001894 VarDecl *CanonicalVD = VD->getCanonicalDecl();
George Burgess IV00f70bd2018-03-01 05:43:23 +00001895 NamedDecl *ND = CanonicalVD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001896 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1897 // A threadprivate directive for file-scope variables must appear outside
1898 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001899 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1900 !getCurLexicalContext()->isTranslationUnit()) {
1901 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001902 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1903 bool IsDecl =
1904 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1905 Diag(VD->getLocation(),
1906 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1907 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001908 return ExprError();
1909 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001910 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1911 // A threadprivate directive for static class member variables must appear
1912 // in the class definition, in the same scope in which the member
1913 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001914 if (CanonicalVD->isStaticDataMember() &&
1915 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1916 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001917 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1918 bool IsDecl =
1919 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1920 Diag(VD->getLocation(),
1921 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1922 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001923 return ExprError();
1924 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001925 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1926 // A threadprivate directive for namespace-scope variables must appear
1927 // outside any definition or declaration other than the namespace
1928 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001929 if (CanonicalVD->getDeclContext()->isNamespace() &&
1930 (!getCurLexicalContext()->isFileContext() ||
1931 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1932 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001933 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1934 bool IsDecl =
1935 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1936 Diag(VD->getLocation(),
1937 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1938 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001939 return ExprError();
1940 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001941 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1942 // A threadprivate directive for static block-scope variables must appear
1943 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001944 if (CanonicalVD->isStaticLocal() && CurScope &&
1945 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001946 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001947 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1948 bool IsDecl =
1949 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1950 Diag(VD->getLocation(),
1951 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1952 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001953 return ExprError();
1954 }
1955
1956 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1957 // A threadprivate directive must lexically precede all references to any
1958 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001959 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001960 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001961 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001962 return ExprError();
1963 }
1964
1965 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev376b4a42016-02-09 09:41:09 +00001966 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1967 SourceLocation(), VD,
1968 /*RefersToEnclosingVariableOrCapture=*/false,
1969 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001970}
1971
Alexey Bataeved09d242014-05-28 05:53:51 +00001972Sema::DeclGroupPtrTy
1973Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1974 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001975 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001976 CurContext->addDecl(D);
1977 return DeclGroupPtrTy::make(DeclGroupRef(D));
1978 }
David Blaikie0403cb12016-01-15 23:43:25 +00001979 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001980}
1981
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001982namespace {
Alexey Bataeve3727102018-04-18 15:57:46 +00001983class LocalVarRefChecker final
1984 : public ConstStmtVisitor<LocalVarRefChecker, bool> {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001985 Sema &SemaRef;
1986
1987public:
1988 bool VisitDeclRefExpr(const DeclRefExpr *E) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001989 if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001990 if (VD->hasLocalStorage()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001991 SemaRef.Diag(E->getBeginLoc(),
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001992 diag::err_omp_local_var_in_threadprivate_init)
1993 << E->getSourceRange();
1994 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1995 << VD << VD->getSourceRange();
1996 return true;
1997 }
1998 }
1999 return false;
2000 }
2001 bool VisitStmt(const Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002002 for (const Stmt *Child : S->children()) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002003 if (Child && Visit(Child))
2004 return true;
2005 }
2006 return false;
2007 }
Alexey Bataev23b69422014-06-18 07:08:49 +00002008 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002009};
2010} // namespace
2011
Alexey Bataeved09d242014-05-28 05:53:51 +00002012OMPThreadPrivateDecl *
2013Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002014 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +00002015 for (Expr *RefExpr : VarList) {
2016 auto *DE = cast<DeclRefExpr>(RefExpr);
2017 auto *VD = cast<VarDecl>(DE->getDecl());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002018 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00002019
Alexey Bataev376b4a42016-02-09 09:41:09 +00002020 // Mark variable as used.
2021 VD->setReferenced();
2022 VD->markUsed(Context);
2023
Alexey Bataevf56f98c2015-04-16 05:39:01 +00002024 QualType QType = VD->getType();
2025 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
2026 // It will be analyzed later.
2027 Vars.push_back(DE);
2028 continue;
2029 }
2030
Alexey Bataeva769e072013-03-22 06:34:35 +00002031 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
2032 // A threadprivate variable must not have an incomplete type.
2033 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002034 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00002035 continue;
2036 }
2037
2038 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
2039 // A threadprivate variable must not have a reference type.
2040 if (VD->getType()->isReferenceType()) {
2041 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00002042 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
2043 bool IsDecl =
2044 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2045 Diag(VD->getLocation(),
2046 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2047 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00002048 continue;
2049 }
2050
Samuel Antaof8b50122015-07-13 22:54:53 +00002051 // Check if this is a TLS variable. If TLS is not being supported, produce
2052 // the corresponding diagnostic.
2053 if ((VD->getTLSKind() != VarDecl::TLS_None &&
2054 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
2055 getLangOpts().OpenMPUseTLS &&
2056 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00002057 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
2058 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00002059 Diag(ILoc, diag::err_omp_var_thread_local)
2060 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00002061 bool IsDecl =
2062 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2063 Diag(VD->getLocation(),
2064 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2065 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00002066 continue;
2067 }
2068
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002069 // Check if initial value of threadprivate variable reference variable with
2070 // local storage (it is not supported by runtime).
Alexey Bataeve3727102018-04-18 15:57:46 +00002071 if (const Expr *Init = VD->getAnyInitializer()) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002072 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002073 if (Checker.Visit(Init))
2074 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002075 }
2076
Alexey Bataeved09d242014-05-28 05:53:51 +00002077 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00002078 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00002079 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
2080 Context, SourceRange(Loc, Loc)));
Alexey Bataeve3727102018-04-18 15:57:46 +00002081 if (ASTMutationListener *ML = Context.getASTMutationListener())
Alexey Bataev97720002014-11-11 04:05:39 +00002082 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00002083 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002084 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00002085 if (!Vars.empty()) {
2086 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
2087 Vars);
2088 D->setAccess(AS_public);
2089 }
2090 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00002091}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002092
Kelvin Li1408f912018-09-26 04:28:39 +00002093Sema::DeclGroupPtrTy
2094Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc,
2095 ArrayRef<OMPClause *> ClauseList) {
2096 OMPRequiresDecl *D = nullptr;
2097 if (!CurContext->isFileContext()) {
2098 Diag(Loc, diag::err_omp_invalid_scope) << "requires";
2099 } else {
2100 D = CheckOMPRequiresDecl(Loc, ClauseList);
2101 if (D) {
2102 CurContext->addDecl(D);
2103 DSAStack->addRequiresDecl(D);
2104 }
2105 }
2106 return DeclGroupPtrTy::make(DeclGroupRef(D));
2107}
2108
2109OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc,
2110 ArrayRef<OMPClause *> ClauseList) {
2111 if (!DSAStack->hasDuplicateRequiresClause(ClauseList))
2112 return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc,
2113 ClauseList);
2114 return nullptr;
2115}
2116
Alexey Bataeve3727102018-04-18 15:57:46 +00002117static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
2118 const ValueDecl *D,
2119 const DSAStackTy::DSAVarData &DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00002120 bool IsLoopIterVar = false) {
2121 if (DVar.RefExpr) {
2122 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
2123 << getOpenMPClauseName(DVar.CKind);
2124 return;
2125 }
2126 enum {
2127 PDSA_StaticMemberShared,
2128 PDSA_StaticLocalVarShared,
2129 PDSA_LoopIterVarPrivate,
2130 PDSA_LoopIterVarLinear,
2131 PDSA_LoopIterVarLastprivate,
2132 PDSA_ConstVarShared,
2133 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002134 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00002135 PDSA_LocalVarPrivate,
2136 PDSA_Implicit
2137 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00002138 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002139 auto ReportLoc = D->getLocation();
2140 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002141 if (IsLoopIterVar) {
2142 if (DVar.CKind == OMPC_private)
2143 Reason = PDSA_LoopIterVarPrivate;
2144 else if (DVar.CKind == OMPC_lastprivate)
2145 Reason = PDSA_LoopIterVarLastprivate;
2146 else
2147 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev35aaee62016-04-13 13:36:48 +00002148 } else if (isOpenMPTaskingDirective(DVar.DKind) &&
2149 DVar.CKind == OMPC_firstprivate) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002150 Reason = PDSA_TaskVarFirstprivate;
2151 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002152 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00002153 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002154 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00002155 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002156 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00002157 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002158 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00002159 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002160 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00002161 ReportHint = true;
2162 Reason = PDSA_LocalVarPrivate;
2163 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00002164 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002165 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00002166 << Reason << ReportHint
2167 << getOpenMPDirectiveName(Stack->getCurrentDirective());
2168 } else if (DVar.ImplicitDSALoc.isValid()) {
2169 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
2170 << getOpenMPClauseName(DVar.CKind);
2171 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00002172}
2173
Alexey Bataev758e55e2013-09-06 18:03:48 +00002174namespace {
Alexey Bataeve3727102018-04-18 15:57:46 +00002175class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> {
Alexey Bataev758e55e2013-09-06 18:03:48 +00002176 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00002177 Sema &SemaRef;
Alexey Bataeve3727102018-04-18 15:57:46 +00002178 bool ErrorFound = false;
2179 CapturedStmt *CS = nullptr;
2180 llvm::SmallVector<Expr *, 4> ImplicitFirstprivate;
2181 llvm::SmallVector<Expr *, 4> ImplicitMap;
2182 Sema::VarsWithInheritedDSAType VarsWithInheritedDSA;
2183 llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations;
Alexey Bataeved09d242014-05-28 05:53:51 +00002184
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00002185 void VisitSubCaptures(OMPExecutableDirective *S) {
2186 // Check implicitly captured variables.
2187 if (!S->hasAssociatedStmt() || !S->getAssociatedStmt())
2188 return;
2189 for (const CapturedStmt::Capture &Cap :
2190 S->getInnermostCapturedStmt()->captures()) {
2191 if (!Cap.capturesVariable())
2192 continue;
2193 VarDecl *VD = Cap.getCapturedVar();
2194 // Do not try to map the variable if it or its sub-component was mapped
2195 // already.
2196 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) &&
2197 Stack->checkMappableExprComponentListsForDecl(
2198 VD, /*CurrentRegionOnly=*/true,
2199 [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
2200 OpenMPClauseKind) { return true; }))
2201 continue;
2202 DeclRefExpr *DRE = buildDeclRefExpr(
2203 SemaRef, VD, VD->getType().getNonLValueExprType(SemaRef.Context),
2204 Cap.getLocation(), /*RefersToCapture=*/true);
2205 Visit(DRE);
2206 }
2207 }
2208
Alexey Bataev758e55e2013-09-06 18:03:48 +00002209public:
2210 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00002211 if (E->isTypeDependent() || E->isValueDependent() ||
2212 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
2213 return;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002214 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00002215 VD = VD->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002216 // Skip internally declared variables.
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002217 if (VD->hasLocalStorage() && !CS->capturesVariable(VD))
Alexey Bataeved09d242014-05-28 05:53:51 +00002218 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002219
Alexey Bataeve3727102018-04-18 15:57:46 +00002220 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002221 // Check if the variable has explicit DSA set and stop analysis if it so.
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00002222 if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second)
David Majnemer9d168222016-08-05 17:44:54 +00002223 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002224
Alexey Bataevafe50572017-10-06 17:00:28 +00002225 // Skip internally declared static variables.
Alexey Bataev92327c52018-03-26 16:40:55 +00002226 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
Alexey Bataev97b72212018-08-14 18:31:20 +00002227 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
Alexey Bataev92327c52018-03-26 16:40:55 +00002228 if (VD->hasGlobalStorage() && !CS->capturesVariable(VD) &&
2229 (!Res || *Res != OMPDeclareTargetDeclAttr::MT_Link))
Alexey Bataevafe50572017-10-06 17:00:28 +00002230 return;
2231
Alexey Bataeve3727102018-04-18 15:57:46 +00002232 SourceLocation ELoc = E->getExprLoc();
2233 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002234 // The default(none) clause requires that each variable that is referenced
2235 // in the construct, and does not have a predetermined data-sharing
2236 // attribute, must have its data-sharing attribute explicitly determined
2237 // by being listed in a data-sharing attribute clause.
2238 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002239 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00002240 VarsWithInheritedDSA.count(VD) == 0) {
2241 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002242 return;
2243 }
2244
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002245 if (isOpenMPTargetExecutionDirective(DKind) &&
2246 !Stack->isLoopControlVariable(VD).first) {
2247 if (!Stack->checkMappableExprComponentListsForDecl(
2248 VD, /*CurrentRegionOnly=*/true,
2249 [](OMPClauseMappableExprCommon::MappableExprComponentListRef
2250 StackComponents,
2251 OpenMPClauseKind) {
2252 // Variable is used if it has been marked as an array, array
2253 // section or the variable iself.
2254 return StackComponents.size() == 1 ||
2255 std::all_of(
2256 std::next(StackComponents.rbegin()),
2257 StackComponents.rend(),
2258 [](const OMPClauseMappableExprCommon::
2259 MappableComponent &MC) {
2260 return MC.getAssociatedDeclaration() ==
2261 nullptr &&
2262 (isa<OMPArraySectionExpr>(
2263 MC.getAssociatedExpression()) ||
2264 isa<ArraySubscriptExpr>(
2265 MC.getAssociatedExpression()));
2266 });
2267 })) {
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002268 bool IsFirstprivate = false;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002269 // By default lambdas are captured as firstprivates.
2270 if (const auto *RD =
2271 VD->getType().getNonReferenceType()->getAsCXXRecordDecl())
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002272 IsFirstprivate = RD->isLambda();
2273 IsFirstprivate =
2274 IsFirstprivate ||
2275 (VD->getType().getNonReferenceType()->isScalarType() &&
Alexey Bataev92327c52018-03-26 16:40:55 +00002276 Stack->getDefaultDMA() != DMA_tofrom_scalar && !Res);
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002277 if (IsFirstprivate)
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002278 ImplicitFirstprivate.emplace_back(E);
2279 else
2280 ImplicitMap.emplace_back(E);
2281 return;
2282 }
2283 }
2284
Alexey Bataev758e55e2013-09-06 18:03:48 +00002285 // OpenMP [2.9.3.6, Restrictions, p.2]
2286 // A list item that appears in a reduction clause of the innermost
2287 // enclosing worksharing or parallel construct may not be accessed in an
2288 // explicit task.
Alexey Bataev7ace49d2016-05-17 08:55:33 +00002289 DVar = Stack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00002290 VD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
2291 [](OpenMPDirectiveKind K) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00002292 return isOpenMPParallelDirective(K) ||
2293 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
2294 },
Alexey Bataeveffbdf12017-07-21 17:24:30 +00002295 /*FromParent=*/true);
Alexey Bataev35aaee62016-04-13 13:36:48 +00002296 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00002297 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00002298 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
Alexey Bataeve3727102018-04-18 15:57:46 +00002299 reportOriginalDsa(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002300 return;
2301 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002302
2303 // Define implicit data-sharing attributes for task.
Alexey Bataeve3727102018-04-18 15:57:46 +00002304 DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00002305 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
2306 !Stack->isLoopControlVariable(VD).first)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002307 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002308 }
2309 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002310 void VisitMemberExpr(MemberExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00002311 if (E->isTypeDependent() || E->isValueDependent() ||
2312 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
2313 return;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002314 auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002315 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
Patrick Lystere13b1e32019-01-02 19:28:48 +00002316 if (auto *TE = dyn_cast<CXXThisExpr>(E->getBase()->IgnoreParens())) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002317 if (!FD)
2318 return;
Alexey Bataeve3727102018-04-18 15:57:46 +00002319 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002320 // Check if the variable has explicit DSA set and stop analysis if it
2321 // so.
2322 if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second)
2323 return;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002324
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002325 if (isOpenMPTargetExecutionDirective(DKind) &&
2326 !Stack->isLoopControlVariable(FD).first &&
2327 !Stack->checkMappableExprComponentListsForDecl(
2328 FD, /*CurrentRegionOnly=*/true,
2329 [](OMPClauseMappableExprCommon::MappableExprComponentListRef
2330 StackComponents,
2331 OpenMPClauseKind) {
2332 return isa<CXXThisExpr>(
2333 cast<MemberExpr>(
2334 StackComponents.back().getAssociatedExpression())
2335 ->getBase()
2336 ->IgnoreParens());
2337 })) {
2338 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
2339 // A bit-field cannot appear in a map clause.
2340 //
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002341 if (FD->isBitField())
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002342 return;
Patrick Lystere13b1e32019-01-02 19:28:48 +00002343
2344 // Check to see if the member expression is referencing a class that
2345 // has already been explicitly mapped
2346 if (Stack->isClassPreviouslyMapped(TE->getType()))
2347 return;
2348
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002349 ImplicitMap.emplace_back(E);
2350 return;
2351 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002352
Alexey Bataeve3727102018-04-18 15:57:46 +00002353 SourceLocation ELoc = E->getExprLoc();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002354 // OpenMP [2.9.3.6, Restrictions, p.2]
2355 // A list item that appears in a reduction clause of the innermost
2356 // enclosing worksharing or parallel construct may not be accessed in
2357 // an explicit task.
2358 DVar = Stack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00002359 FD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
2360 [](OpenMPDirectiveKind K) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002361 return isOpenMPParallelDirective(K) ||
2362 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
2363 },
2364 /*FromParent=*/true);
2365 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
2366 ErrorFound = true;
2367 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
Alexey Bataeve3727102018-04-18 15:57:46 +00002368 reportOriginalDsa(SemaRef, Stack, FD, DVar);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002369 return;
2370 }
2371
2372 // Define implicit data-sharing attributes for task.
Alexey Bataeve3727102018-04-18 15:57:46 +00002373 DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002374 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
Alexey Bataevb40e05202018-10-24 18:53:12 +00002375 !Stack->isLoopControlVariable(FD).first) {
2376 // Check if there is a captured expression for the current field in the
2377 // region. Do not mark it as firstprivate unless there is no captured
2378 // expression.
2379 // TODO: try to make it firstprivate.
2380 if (DVar.CKind != OMPC_unknown)
2381 ImplicitFirstprivate.push_back(E);
2382 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002383 return;
2384 }
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002385 if (isOpenMPTargetExecutionDirective(DKind)) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002386 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
Alexey Bataeve3727102018-04-18 15:57:46 +00002387 if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map,
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002388 /*NoDiagnose=*/true))
Alexey Bataev27041fa2017-12-05 15:22:49 +00002389 return;
Alexey Bataeve3727102018-04-18 15:57:46 +00002390 const auto *VD = cast<ValueDecl>(
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002391 CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl());
2392 if (!Stack->checkMappableExprComponentListsForDecl(
2393 VD, /*CurrentRegionOnly=*/true,
2394 [&CurComponents](
2395 OMPClauseMappableExprCommon::MappableExprComponentListRef
2396 StackComponents,
2397 OpenMPClauseKind) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002398 auto CCI = CurComponents.rbegin();
Alexey Bataev5ec38932017-09-26 16:19:04 +00002399 auto CCE = CurComponents.rend();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002400 for (const auto &SC : llvm::reverse(StackComponents)) {
2401 // Do both expressions have the same kind?
2402 if (CCI->getAssociatedExpression()->getStmtClass() !=
2403 SC.getAssociatedExpression()->getStmtClass())
2404 if (!(isa<OMPArraySectionExpr>(
2405 SC.getAssociatedExpression()) &&
2406 isa<ArraySubscriptExpr>(
2407 CCI->getAssociatedExpression())))
2408 return false;
2409
Alexey Bataeve3727102018-04-18 15:57:46 +00002410 const Decl *CCD = CCI->getAssociatedDeclaration();
2411 const Decl *SCD = SC.getAssociatedDeclaration();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002412 CCD = CCD ? CCD->getCanonicalDecl() : nullptr;
2413 SCD = SCD ? SCD->getCanonicalDecl() : nullptr;
2414 if (SCD != CCD)
2415 return false;
2416 std::advance(CCI, 1);
Alexey Bataev5ec38932017-09-26 16:19:04 +00002417 if (CCI == CCE)
2418 break;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002419 }
2420 return true;
2421 })) {
2422 Visit(E->getBase());
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002423 }
Alexey Bataeve3727102018-04-18 15:57:46 +00002424 } else {
Alexey Bataev7fcacd82016-11-28 15:55:15 +00002425 Visit(E->getBase());
Alexey Bataeve3727102018-04-18 15:57:46 +00002426 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002427 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002428 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002429 for (OMPClause *C : S->clauses()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002430 // Skip analysis of arguments of implicitly defined firstprivate clause
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002431 // for task|target directives.
2432 // Skip analysis of arguments of implicitly defined map clause for target
2433 // directives.
2434 if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) &&
2435 C->isImplicit())) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002436 for (Stmt *CC : C->children()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002437 if (CC)
2438 Visit(CC);
2439 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002440 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002441 }
Alexey Bataevf07946e2018-10-29 20:17:42 +00002442 // Check implicitly captured variables.
2443 VisitSubCaptures(S);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002444 }
2445 void VisitStmt(Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002446 for (Stmt *C : S->children()) {
Alexey Bataev8fc7b5f2018-10-25 15:35:27 +00002447 if (C) {
Joel E. Denny0fdf5a92018-12-19 15:59:47 +00002448 // Check implicitly captured variables in the task-based directives to
2449 // check if they must be firstprivatized.
2450 Visit(C);
Alexey Bataev8fc7b5f2018-10-25 15:35:27 +00002451 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002452 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002453 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002454
Alexey Bataeve3727102018-04-18 15:57:46 +00002455 bool isErrorFound() const { return ErrorFound; }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002456 ArrayRef<Expr *> getImplicitFirstprivate() const {
2457 return ImplicitFirstprivate;
2458 }
2459 ArrayRef<Expr *> getImplicitMap() const { return ImplicitMap; }
Alexey Bataeve3727102018-04-18 15:57:46 +00002460 const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const {
Alexey Bataev4acb8592014-07-07 13:01:15 +00002461 return VarsWithInheritedDSA;
2462 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002463
Alexey Bataev7ff55242014-06-19 09:13:45 +00002464 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
2465 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00002466};
Alexey Bataeved09d242014-05-28 05:53:51 +00002467} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00002468
Alexey Bataevbae9a792014-06-27 10:37:06 +00002469void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00002470 switch (DKind) {
Kelvin Li70a12c52016-07-13 21:51:49 +00002471 case OMPD_parallel:
2472 case OMPD_parallel_for:
2473 case OMPD_parallel_for_simd:
2474 case OMPD_parallel_sections:
Carlo Bertolliba1487b2017-10-04 14:12:09 +00002475 case OMPD_teams:
Alexey Bataev999277a2017-12-06 14:31:09 +00002476 case OMPD_teams_distribute:
2477 case OMPD_teams_distribute_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002478 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Alexey Bataev2377fe92015-09-10 08:12:02 +00002479 QualType KmpInt32PtrTy =
2480 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002481 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002482 std::make_pair(".global_tid.", KmpInt32PtrTy),
2483 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2484 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00002485 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00002486 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2487 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00002488 break;
2489 }
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002490 case OMPD_target_teams:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00002491 case OMPD_target_parallel:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00002492 case OMPD_target_parallel_for:
Alexey Bataevdfa430f2017-12-08 15:03:50 +00002493 case OMPD_target_parallel_for_simd:
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00002494 case OMPD_target_teams_distribute:
2495 case OMPD_target_teams_distribute_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002496 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2497 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2498 QualType KmpInt32PtrTy =
2499 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2500 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00002501 FunctionProtoType::ExtProtoInfo EPI;
2502 EPI.Variadic = true;
2503 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2504 Sema::CapturedParamNameType Params[] = {
2505 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002506 std::make_pair(".part_id.", KmpInt32PtrTy),
2507 std::make_pair(".privates.", VoidPtrTy),
2508 std::make_pair(
2509 ".copy_fn.",
2510 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00002511 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2512 std::make_pair(StringRef(), QualType()) // __context with shared vars
2513 };
2514 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2515 Params);
Alexey Bataev0c869ef2018-01-16 15:57:07 +00002516 // Mark this captured region as inlined, because we don't use outlined
2517 // function directly.
2518 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2519 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002520 Context, AlwaysInlineAttr::Keyword_forceinline));
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002521 Sema::CapturedParamNameType ParamsTarget[] = {
2522 std::make_pair(StringRef(), QualType()) // __context with shared vars
2523 };
2524 // Start a captured region for 'target' with no implicit parameters.
2525 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2526 ParamsTarget);
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002527 Sema::CapturedParamNameType ParamsTeamsOrParallel[] = {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002528 std::make_pair(".global_tid.", KmpInt32PtrTy),
2529 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2530 std::make_pair(StringRef(), QualType()) // __context with shared vars
2531 };
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002532 // Start a captured region for 'teams' or 'parallel'. Both regions have
2533 // the same implicit parameters.
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002534 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002535 ParamsTeamsOrParallel);
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002536 break;
2537 }
Alexey Bataev8451efa2018-01-15 19:06:12 +00002538 case OMPD_target:
2539 case OMPD_target_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002540 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2541 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2542 QualType KmpInt32PtrTy =
2543 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2544 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00002545 FunctionProtoType::ExtProtoInfo EPI;
2546 EPI.Variadic = true;
2547 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2548 Sema::CapturedParamNameType Params[] = {
2549 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002550 std::make_pair(".part_id.", KmpInt32PtrTy),
2551 std::make_pair(".privates.", VoidPtrTy),
2552 std::make_pair(
2553 ".copy_fn.",
2554 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00002555 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2556 std::make_pair(StringRef(), QualType()) // __context with shared vars
2557 };
2558 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2559 Params);
2560 // Mark this captured region as inlined, because we don't use outlined
2561 // function directly.
2562 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2563 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002564 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev8451efa2018-01-15 19:06:12 +00002565 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2566 std::make_pair(StringRef(), QualType()));
2567 break;
2568 }
Kelvin Li70a12c52016-07-13 21:51:49 +00002569 case OMPD_simd:
2570 case OMPD_for:
2571 case OMPD_for_simd:
2572 case OMPD_sections:
2573 case OMPD_section:
2574 case OMPD_single:
2575 case OMPD_master:
2576 case OMPD_critical:
Kelvin Lia579b912016-07-14 02:54:56 +00002577 case OMPD_taskgroup:
2578 case OMPD_distribute:
Alexey Bataev46506272017-12-05 17:41:34 +00002579 case OMPD_distribute_simd:
Kelvin Li70a12c52016-07-13 21:51:49 +00002580 case OMPD_ordered:
2581 case OMPD_atomic:
Alexey Bataev8451efa2018-01-15 19:06:12 +00002582 case OMPD_target_data: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002583 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002584 std::make_pair(StringRef(), QualType()) // __context with shared vars
2585 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00002586 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2587 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002588 break;
2589 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002590 case OMPD_task: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002591 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2592 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2593 QualType KmpInt32PtrTy =
2594 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2595 QualType Args[] = {VoidPtrTy};
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002596 FunctionProtoType::ExtProtoInfo EPI;
2597 EPI.Variadic = true;
2598 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002599 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00002600 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002601 std::make_pair(".part_id.", KmpInt32PtrTy),
2602 std::make_pair(".privates.", VoidPtrTy),
2603 std::make_pair(
2604 ".copy_fn.",
2605 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev48591dd2016-04-20 04:01:36 +00002606 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002607 std::make_pair(StringRef(), QualType()) // __context with shared vars
2608 };
2609 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2610 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00002611 // Mark this captured region as inlined, because we don't use outlined
2612 // function directly.
2613 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2614 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002615 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002616 break;
2617 }
Alexey Bataev1e73ef32016-04-28 12:14:51 +00002618 case OMPD_taskloop:
2619 case OMPD_taskloop_simd: {
Alexey Bataev7292c292016-04-25 12:22:29 +00002620 QualType KmpInt32Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002621 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1)
2622 .withConst();
Alexey Bataev7292c292016-04-25 12:22:29 +00002623 QualType KmpUInt64Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002624 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0)
2625 .withConst();
Alexey Bataev7292c292016-04-25 12:22:29 +00002626 QualType KmpInt64Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002627 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1)
2628 .withConst();
2629 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2630 QualType KmpInt32PtrTy =
2631 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2632 QualType Args[] = {VoidPtrTy};
Alexey Bataev7292c292016-04-25 12:22:29 +00002633 FunctionProtoType::ExtProtoInfo EPI;
2634 EPI.Variadic = true;
2635 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev49f6e782015-12-01 04:18:41 +00002636 Sema::CapturedParamNameType Params[] = {
Alexey Bataev7292c292016-04-25 12:22:29 +00002637 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002638 std::make_pair(".part_id.", KmpInt32PtrTy),
2639 std::make_pair(".privates.", VoidPtrTy),
Alexey Bataev7292c292016-04-25 12:22:29 +00002640 std::make_pair(
2641 ".copy_fn.",
2642 Context.getPointerType(CopyFnType).withConst().withRestrict()),
2643 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2644 std::make_pair(".lb.", KmpUInt64Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002645 std::make_pair(".ub.", KmpUInt64Ty),
2646 std::make_pair(".st.", KmpInt64Ty),
Alexey Bataev7292c292016-04-25 12:22:29 +00002647 std::make_pair(".liter.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002648 std::make_pair(".reductions.", VoidPtrTy),
Alexey Bataev49f6e782015-12-01 04:18:41 +00002649 std::make_pair(StringRef(), QualType()) // __context with shared vars
2650 };
2651 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2652 Params);
Alexey Bataev7292c292016-04-25 12:22:29 +00002653 // Mark this captured region as inlined, because we don't use outlined
2654 // function directly.
2655 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2656 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002657 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev49f6e782015-12-01 04:18:41 +00002658 break;
2659 }
Kelvin Li4a39add2016-07-05 05:00:15 +00002660 case OMPD_distribute_parallel_for_simd:
Alexey Bataev647dd842018-01-15 20:59:40 +00002661 case OMPD_distribute_parallel_for: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002662 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli9925f152016-06-27 14:55:37 +00002663 QualType KmpInt32PtrTy =
2664 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2665 Sema::CapturedParamNameType Params[] = {
2666 std::make_pair(".global_tid.", KmpInt32PtrTy),
2667 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002668 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2669 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli9925f152016-06-27 14:55:37 +00002670 std::make_pair(StringRef(), QualType()) // __context with shared vars
2671 };
2672 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2673 Params);
2674 break;
2675 }
Alexey Bataev647dd842018-01-15 20:59:40 +00002676 case OMPD_target_teams_distribute_parallel_for:
2677 case OMPD_target_teams_distribute_parallel_for_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002678 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli52978c32018-01-03 21:12:44 +00002679 QualType KmpInt32PtrTy =
2680 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002681 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
Carlo Bertolli52978c32018-01-03 21:12:44 +00002682
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002683 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00002684 FunctionProtoType::ExtProtoInfo EPI;
2685 EPI.Variadic = true;
2686 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2687 Sema::CapturedParamNameType Params[] = {
2688 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002689 std::make_pair(".part_id.", KmpInt32PtrTy),
2690 std::make_pair(".privates.", VoidPtrTy),
2691 std::make_pair(
2692 ".copy_fn.",
2693 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00002694 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2695 std::make_pair(StringRef(), QualType()) // __context with shared vars
2696 };
2697 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2698 Params);
Alexey Bataev9f9fb0b2018-01-16 19:02:33 +00002699 // Mark this captured region as inlined, because we don't use outlined
2700 // function directly.
2701 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2702 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002703 Context, AlwaysInlineAttr::Keyword_forceinline));
Carlo Bertolli52978c32018-01-03 21:12:44 +00002704 Sema::CapturedParamNameType ParamsTarget[] = {
2705 std::make_pair(StringRef(), QualType()) // __context with shared vars
2706 };
2707 // Start a captured region for 'target' with no implicit parameters.
2708 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2709 ParamsTarget);
2710
2711 Sema::CapturedParamNameType ParamsTeams[] = {
2712 std::make_pair(".global_tid.", KmpInt32PtrTy),
2713 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2714 std::make_pair(StringRef(), QualType()) // __context with shared vars
2715 };
2716 // Start a captured region for 'target' with no implicit parameters.
2717 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2718 ParamsTeams);
2719
2720 Sema::CapturedParamNameType ParamsParallel[] = {
2721 std::make_pair(".global_tid.", KmpInt32PtrTy),
2722 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002723 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2724 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli52978c32018-01-03 21:12:44 +00002725 std::make_pair(StringRef(), QualType()) // __context with shared vars
2726 };
2727 // Start a captured region for 'teams' or 'parallel'. Both regions have
2728 // the same implicit parameters.
2729 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2730 ParamsParallel);
2731 break;
2732 }
2733
Alexey Bataev46506272017-12-05 17:41:34 +00002734 case OMPD_teams_distribute_parallel_for:
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00002735 case OMPD_teams_distribute_parallel_for_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002736 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli62fae152017-11-20 20:46:39 +00002737 QualType KmpInt32PtrTy =
2738 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2739
2740 Sema::CapturedParamNameType ParamsTeams[] = {
2741 std::make_pair(".global_tid.", KmpInt32PtrTy),
2742 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2743 std::make_pair(StringRef(), QualType()) // __context with shared vars
2744 };
2745 // Start a captured region for 'target' with no implicit parameters.
2746 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2747 ParamsTeams);
2748
2749 Sema::CapturedParamNameType ParamsParallel[] = {
2750 std::make_pair(".global_tid.", KmpInt32PtrTy),
2751 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002752 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2753 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli62fae152017-11-20 20:46:39 +00002754 std::make_pair(StringRef(), QualType()) // __context with shared vars
2755 };
2756 // Start a captured region for 'teams' or 'parallel'. Both regions have
2757 // the same implicit parameters.
2758 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2759 ParamsParallel);
2760 break;
2761 }
Alexey Bataev7828b252017-11-21 17:08:48 +00002762 case OMPD_target_update:
2763 case OMPD_target_enter_data:
2764 case OMPD_target_exit_data: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002765 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2766 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2767 QualType KmpInt32PtrTy =
2768 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2769 QualType Args[] = {VoidPtrTy};
Alexey Bataev7828b252017-11-21 17:08:48 +00002770 FunctionProtoType::ExtProtoInfo EPI;
2771 EPI.Variadic = true;
2772 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2773 Sema::CapturedParamNameType Params[] = {
2774 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002775 std::make_pair(".part_id.", KmpInt32PtrTy),
2776 std::make_pair(".privates.", VoidPtrTy),
2777 std::make_pair(
2778 ".copy_fn.",
2779 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev7828b252017-11-21 17:08:48 +00002780 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2781 std::make_pair(StringRef(), QualType()) // __context with shared vars
2782 };
2783 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2784 Params);
2785 // Mark this captured region as inlined, because we don't use outlined
2786 // function directly.
2787 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2788 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002789 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev7828b252017-11-21 17:08:48 +00002790 break;
2791 }
Alexey Bataev9959db52014-05-06 10:08:46 +00002792 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00002793 case OMPD_taskyield:
2794 case OMPD_barrier:
2795 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002796 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00002797 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00002798 case OMPD_flush:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002799 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00002800 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00002801 case OMPD_declare_target:
2802 case OMPD_end_declare_target:
Kelvin Li1408f912018-09-26 04:28:39 +00002803 case OMPD_requires:
Alexey Bataev9959db52014-05-06 10:08:46 +00002804 llvm_unreachable("OpenMP Directive is not allowed");
2805 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00002806 llvm_unreachable("Unknown OpenMP directive");
2807 }
2808}
2809
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002810int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) {
2811 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2812 getOpenMPCaptureRegions(CaptureRegions, DKind);
2813 return CaptureRegions.size();
2814}
2815
Alexey Bataev3392d762016-02-16 11:18:12 +00002816static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00002817 Expr *CaptureExpr, bool WithInit,
2818 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00002819 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00002820 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00002821 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00002822 QualType Ty = Init->getType();
2823 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002824 if (S.getLangOpts().CPlusPlus) {
Alexey Bataev4244be22016-02-11 05:35:55 +00002825 Ty = C.getLValueReferenceType(Ty);
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002826 } else {
Alexey Bataev4244be22016-02-11 05:35:55 +00002827 Ty = C.getPointerType(Ty);
2828 ExprResult Res =
2829 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
2830 if (!Res.isUsable())
2831 return nullptr;
2832 Init = Res.get();
2833 }
Alexey Bataev61205072016-03-02 04:57:40 +00002834 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00002835 }
Alexey Bataeva7206b92016-12-20 16:51:02 +00002836 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty,
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002837 CaptureExpr->getBeginLoc());
Alexey Bataev2bbf7212016-03-03 03:52:24 +00002838 if (!WithInit)
Alexey Bataeve3727102018-04-18 15:57:46 +00002839 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C));
Alexey Bataev4244be22016-02-11 05:35:55 +00002840 S.CurContext->addHiddenDecl(CED);
Richard Smith3beb7c62017-01-12 02:27:38 +00002841 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00002842 return CED;
2843}
2844
Alexey Bataev61205072016-03-02 04:57:40 +00002845static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
2846 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00002847 OMPCapturedExprDecl *CD;
Alexey Bataeve3727102018-04-18 15:57:46 +00002848 if (VarDecl *VD = S.isOpenMPCapturedDecl(D))
Alexey Bataevb7a34b62016-02-25 03:59:29 +00002849 CD = cast<OMPCapturedExprDecl>(VD);
Alexey Bataeve3727102018-04-18 15:57:46 +00002850 else
Alexey Bataev5a3af132016-03-29 08:58:54 +00002851 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
2852 /*AsExpression=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00002853 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00002854 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00002855}
2856
Alexey Bataev5a3af132016-03-29 08:58:54 +00002857static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002858 CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00002859 if (!Ref) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002860 OMPCapturedExprDecl *CD = buildCaptureDecl(
2861 S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr,
2862 /*WithInit=*/true, /*AsExpression=*/true);
Alexey Bataev5a3af132016-03-29 08:58:54 +00002863 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
2864 CaptureExpr->getExprLoc());
2865 }
2866 ExprResult Res = Ref;
2867 if (!S.getLangOpts().CPlusPlus &&
2868 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002869 Ref->getType()->isPointerType()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00002870 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002871 if (!Res.isUsable())
2872 return ExprError();
2873 }
2874 return S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00002875}
2876
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002877namespace {
2878// OpenMP directives parsed in this section are represented as a
2879// CapturedStatement with an associated statement. If a syntax error
2880// is detected during the parsing of the associated statement, the
2881// compiler must abort processing and close the CapturedStatement.
2882//
2883// Combined directives such as 'target parallel' have more than one
2884// nested CapturedStatements. This RAII ensures that we unwind out
2885// of all the nested CapturedStatements when an error is found.
2886class CaptureRegionUnwinderRAII {
2887private:
2888 Sema &S;
2889 bool &ErrorFound;
Alexey Bataeve3727102018-04-18 15:57:46 +00002890 OpenMPDirectiveKind DKind = OMPD_unknown;
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002891
2892public:
2893 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound,
2894 OpenMPDirectiveKind DKind)
2895 : S(S), ErrorFound(ErrorFound), DKind(DKind) {}
2896 ~CaptureRegionUnwinderRAII() {
2897 if (ErrorFound) {
2898 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind);
2899 while (--ThisCaptureLevel >= 0)
2900 S.ActOnCapturedRegionError();
2901 }
2902 }
2903};
2904} // namespace
2905
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002906StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
2907 ArrayRef<OMPClause *> Clauses) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002908 bool ErrorFound = false;
2909 CaptureRegionUnwinderRAII CaptureRegionUnwinder(
2910 *this, ErrorFound, DSAStack->getCurrentDirective());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002911 if (!S.isUsable()) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002912 ErrorFound = true;
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002913 return StmtError();
2914 }
Alexey Bataev993d2802015-12-28 06:23:08 +00002915
Alexey Bataev2ba67042017-11-28 21:11:44 +00002916 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2917 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective());
Alexey Bataev993d2802015-12-28 06:23:08 +00002918 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00002919 OMPScheduleClause *SC = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +00002920 SmallVector<const OMPLinearClause *, 4> LCs;
2921 SmallVector<const OMPClauseWithPreInit *, 4> PICs;
Alexey Bataev040d5402015-05-12 08:35:28 +00002922 // This is required for proper codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00002923 for (OMPClause *Clause : Clauses) {
Alexey Bataev88202be2017-07-27 13:20:36 +00002924 if (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) &&
2925 Clause->getClauseKind() == OMPC_in_reduction) {
2926 // Capture taskgroup task_reduction descriptors inside the tasking regions
2927 // with the corresponding in_reduction items.
2928 auto *IRC = cast<OMPInReductionClause>(Clause);
Alexey Bataeve3727102018-04-18 15:57:46 +00002929 for (Expr *E : IRC->taskgroup_descriptors())
Alexey Bataev88202be2017-07-27 13:20:36 +00002930 if (E)
2931 MarkDeclarationsReferencedInExpr(E);
2932 }
Alexey Bataev16dc7b62015-05-20 03:46:04 +00002933 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00002934 Clause->getClauseKind() == OMPC_copyprivate ||
2935 (getLangOpts().OpenMPUseTLS &&
2936 getASTContext().getTargetInfo().isTLSSupported() &&
2937 Clause->getClauseKind() == OMPC_copyin)) {
2938 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00002939 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeve3727102018-04-18 15:57:46 +00002940 for (Stmt *VarRef : Clause->children()) {
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002941 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00002942 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002943 }
2944 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00002945 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev2ba67042017-11-28 21:11:44 +00002946 } else if (CaptureRegions.size() > 1 ||
2947 CaptureRegions.back() != OMPD_unknown) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002948 if (auto *C = OMPClauseWithPreInit::get(Clause))
2949 PICs.push_back(C);
Alexey Bataev005248a2016-02-25 05:25:57 +00002950 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002951 if (Expr *E = C->getPostUpdateExpr())
Alexey Bataev005248a2016-02-25 05:25:57 +00002952 MarkDeclarationsReferencedInExpr(E);
2953 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002954 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002955 if (Clause->getClauseKind() == OMPC_schedule)
2956 SC = cast<OMPScheduleClause>(Clause);
2957 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00002958 OC = cast<OMPOrderedClause>(Clause);
2959 else if (Clause->getClauseKind() == OMPC_linear)
2960 LCs.push_back(cast<OMPLinearClause>(Clause));
2961 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002962 // OpenMP, 2.7.1 Loop Construct, Restrictions
2963 // The nonmonotonic modifier cannot be specified if an ordered clause is
2964 // specified.
2965 if (SC &&
2966 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
2967 SC->getSecondScheduleModifier() ==
2968 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
2969 OC) {
2970 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
2971 ? SC->getFirstScheduleModifierLoc()
2972 : SC->getSecondScheduleModifierLoc(),
2973 diag::err_omp_schedule_nonmonotonic_ordered)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00002974 << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
Alexey Bataev6402bca2015-12-28 07:25:51 +00002975 ErrorFound = true;
2976 }
Alexey Bataev993d2802015-12-28 06:23:08 +00002977 if (!LCs.empty() && OC && OC->getNumForLoops()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002978 for (const OMPLinearClause *C : LCs) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002979 Diag(C->getBeginLoc(), diag::err_omp_linear_ordered)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00002980 << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
Alexey Bataev993d2802015-12-28 06:23:08 +00002981 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002982 ErrorFound = true;
2983 }
Alexey Bataev113438c2015-12-30 12:06:23 +00002984 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
2985 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
2986 OC->getNumForLoops()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002987 Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd)
Alexey Bataev113438c2015-12-30 12:06:23 +00002988 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
2989 ErrorFound = true;
2990 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002991 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00002992 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002993 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002994 StmtResult SR = S;
Alexey Bataev2ba67042017-11-28 21:11:44 +00002995 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002996 // Mark all variables in private list clauses as used in inner region.
2997 // Required for proper codegen of combined directives.
2998 // TODO: add processing for other clauses.
Alexey Bataev2ba67042017-11-28 21:11:44 +00002999 if (ThisCaptureRegion != OMPD_unknown) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003000 for (const clang::OMPClauseWithPreInit *C : PICs) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00003001 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion();
3002 // Find the particular capture region for the clause if the
3003 // directive is a combined one with multiple capture regions.
3004 // If the directive is not a combined one, the capture region
3005 // associated with the clause is OMPD_unknown and is generated
3006 // only once.
3007 if (CaptureRegion == ThisCaptureRegion ||
3008 CaptureRegion == OMPD_unknown) {
3009 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003010 for (Decl *D : DS->decls())
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00003011 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
3012 }
3013 }
3014 }
3015 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003016 SR = ActOnCapturedRegionEnd(SR.get());
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00003017 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003018 return SR;
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00003019}
3020
Jonas Hahnfeld64a9e3c2017-02-22 06:49:10 +00003021static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion,
3022 OpenMPDirectiveKind CancelRegion,
3023 SourceLocation StartLoc) {
3024 // CancelRegion is only needed for cancel and cancellation_point.
3025 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point)
3026 return false;
3027
3028 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for ||
3029 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup)
3030 return false;
3031
3032 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region)
3033 << getOpenMPDirectiveName(CancelRegion);
3034 return true;
3035}
3036
Alexey Bataeve3727102018-04-18 15:57:46 +00003037static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003038 OpenMPDirectiveKind CurrentRegion,
3039 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003040 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003041 SourceLocation StartLoc) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003042 if (Stack->getCurScope()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003043 OpenMPDirectiveKind ParentRegion = Stack->getParentDirective();
3044 OpenMPDirectiveKind OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003045 bool NestingProhibited = false;
3046 bool CloseNesting = true;
David Majnemer9d168222016-08-05 17:44:54 +00003047 bool OrphanSeen = false;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003048 enum {
3049 NoRecommend,
3050 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00003051 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003052 ShouldBeInTargetRegion,
3053 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003054 } Recommend = NoRecommend;
Kelvin Lifd8b5742016-07-01 14:30:25 +00003055 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003056 // OpenMP [2.16, Nesting of Regions]
3057 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003058 // OpenMP [2.8.1,simd Construct, Restrictions]
Kelvin Lifd8b5742016-07-01 14:30:25 +00003059 // An ordered construct with the simd clause is the only OpenMP
3060 // construct that can appear in the simd region.
David Majnemer9d168222016-08-05 17:44:54 +00003061 // Allowing a SIMD construct nested in another SIMD construct is an
Kelvin Lifd8b5742016-07-01 14:30:25 +00003062 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
3063 // message.
3064 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
3065 ? diag::err_omp_prohibited_region_simd
3066 : diag::warn_omp_nesting_simd);
3067 return CurrentRegion != OMPD_simd;
Alexey Bataev549210e2014-06-24 04:39:47 +00003068 }
Alexey Bataev0162e452014-07-22 10:10:35 +00003069 if (ParentRegion == OMPD_atomic) {
3070 // OpenMP [2.16, Nesting of Regions]
3071 // OpenMP constructs may not be nested inside an atomic region.
3072 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
3073 return true;
3074 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003075 if (CurrentRegion == OMPD_section) {
3076 // OpenMP [2.7.2, sections Construct, Restrictions]
3077 // Orphaned section directives are prohibited. That is, the section
3078 // directives must appear within the sections construct and must not be
3079 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003080 if (ParentRegion != OMPD_sections &&
3081 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003082 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
3083 << (ParentRegion != OMPD_unknown)
3084 << getOpenMPDirectiveName(ParentRegion);
3085 return true;
3086 }
3087 return false;
3088 }
Kelvin Li2b51f722016-07-26 04:32:50 +00003089 // Allow some constructs (except teams) to be orphaned (they could be
David Majnemer9d168222016-08-05 17:44:54 +00003090 // used in functions, called from OpenMP regions with the required
Kelvin Li2b51f722016-07-26 04:32:50 +00003091 // preconditions).
Kelvin Libf594a52016-12-17 05:48:59 +00003092 if (ParentRegion == OMPD_unknown &&
3093 !isOpenMPNestingTeamsDirective(CurrentRegion))
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003094 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00003095 if (CurrentRegion == OMPD_cancellation_point ||
3096 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003097 // OpenMP [2.16, Nesting of Regions]
3098 // A cancellation point construct for which construct-type-clause is
3099 // taskgroup must be nested inside a task construct. A cancellation
3100 // point construct for which construct-type-clause is not taskgroup must
3101 // be closely nested inside an OpenMP construct that matches the type
3102 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00003103 // A cancel construct for which construct-type-clause is taskgroup must be
3104 // nested inside a task construct. A cancel construct for which
3105 // construct-type-clause is not taskgroup must be closely nested inside an
3106 // OpenMP construct that matches the type specified in
3107 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003108 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003109 !((CancelRegion == OMPD_parallel &&
3110 (ParentRegion == OMPD_parallel ||
3111 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00003112 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003113 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00003114 ParentRegion == OMPD_target_parallel_for ||
3115 ParentRegion == OMPD_distribute_parallel_for ||
Alexey Bataev16e79882017-11-22 21:12:03 +00003116 ParentRegion == OMPD_teams_distribute_parallel_for ||
3117 ParentRegion == OMPD_target_teams_distribute_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003118 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
3119 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00003120 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
3121 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003122 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00003123 // OpenMP [2.16, Nesting of Regions]
3124 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003125 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00003126 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003127 isOpenMPTaskingDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003128 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
3129 // OpenMP [2.16, Nesting of Regions]
3130 // A critical region may not be nested (closely or otherwise) inside a
3131 // critical region with the same name. Note that this restriction is not
3132 // sufficient to prevent deadlock.
3133 SourceLocation PreviousCriticalLoc;
David Majnemer9d168222016-08-05 17:44:54 +00003134 bool DeadLock = Stack->hasDirective(
3135 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
3136 const DeclarationNameInfo &DNI,
Alexey Bataeve3727102018-04-18 15:57:46 +00003137 SourceLocation Loc) {
David Majnemer9d168222016-08-05 17:44:54 +00003138 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
3139 PreviousCriticalLoc = Loc;
3140 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003141 }
3142 return false;
David Majnemer9d168222016-08-05 17:44:54 +00003143 },
3144 false /* skip top directive */);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003145 if (DeadLock) {
3146 SemaRef.Diag(StartLoc,
3147 diag::err_omp_prohibited_region_critical_same_name)
3148 << CurrentName.getName();
3149 if (PreviousCriticalLoc.isValid())
3150 SemaRef.Diag(PreviousCriticalLoc,
3151 diag::note_omp_previous_critical_region);
3152 return true;
3153 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003154 } else if (CurrentRegion == OMPD_barrier) {
3155 // OpenMP [2.16, Nesting of Regions]
3156 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003157 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003158 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3159 isOpenMPTaskingDirective(ParentRegion) ||
3160 ParentRegion == OMPD_master ||
3161 ParentRegion == OMPD_critical ||
3162 ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00003163 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Kelvin Li579e41c2016-11-30 23:51:03 +00003164 !isOpenMPParallelDirective(CurrentRegion) &&
3165 !isOpenMPTeamsDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003166 // OpenMP [2.16, Nesting of Regions]
3167 // A worksharing region may not be closely nested inside a worksharing,
3168 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003169 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3170 isOpenMPTaskingDirective(ParentRegion) ||
3171 ParentRegion == OMPD_master ||
3172 ParentRegion == OMPD_critical ||
3173 ParentRegion == OMPD_ordered;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003174 Recommend = ShouldBeInParallelRegion;
3175 } else if (CurrentRegion == OMPD_ordered) {
3176 // OpenMP [2.16, Nesting of Regions]
3177 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00003178 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003179 // An ordered region must be closely nested inside a loop region (or
3180 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003181 // OpenMP [2.8.1,simd Construct, Restrictions]
3182 // An ordered construct with the simd clause is the only OpenMP construct
3183 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003184 NestingProhibited = ParentRegion == OMPD_critical ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003185 isOpenMPTaskingDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003186 !(isOpenMPSimdDirective(ParentRegion) ||
3187 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003188 Recommend = ShouldBeInOrderedRegion;
Kelvin Libf594a52016-12-17 05:48:59 +00003189 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00003190 // OpenMP [2.16, Nesting of Regions]
3191 // If specified, a teams construct must be contained within a target
3192 // construct.
3193 NestingProhibited = ParentRegion != OMPD_target;
Kelvin Li2b51f722016-07-26 04:32:50 +00003194 OrphanSeen = ParentRegion == OMPD_unknown;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003195 Recommend = ShouldBeInTargetRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003196 }
Kelvin Libf594a52016-12-17 05:48:59 +00003197 if (!NestingProhibited &&
3198 !isOpenMPTargetExecutionDirective(CurrentRegion) &&
3199 !isOpenMPTargetDataManagementDirective(CurrentRegion) &&
3200 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00003201 // OpenMP [2.16, Nesting of Regions]
3202 // distribute, parallel, parallel sections, parallel workshare, and the
3203 // parallel loop and parallel loop SIMD constructs are the only OpenMP
3204 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003205 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
3206 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00003207 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003208 }
David Majnemer9d168222016-08-05 17:44:54 +00003209 if (!NestingProhibited &&
Kelvin Li02532872016-08-05 14:37:37 +00003210 isOpenMPNestingDistributeDirective(CurrentRegion)) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003211 // OpenMP 4.5 [2.17 Nesting of Regions]
3212 // The region associated with the distribute construct must be strictly
3213 // nested inside a teams region
Kelvin Libf594a52016-12-17 05:48:59 +00003214 NestingProhibited =
3215 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003216 Recommend = ShouldBeInTeamsRegion;
3217 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003218 if (!NestingProhibited &&
3219 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
3220 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
3221 // OpenMP 4.5 [2.17 Nesting of Regions]
3222 // If a target, target update, target data, target enter data, or
3223 // target exit data construct is encountered during execution of a
3224 // target region, the behavior is unspecified.
3225 NestingProhibited = Stack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +00003226 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
Alexey Bataeve3727102018-04-18 15:57:46 +00003227 SourceLocation) {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003228 if (isOpenMPTargetExecutionDirective(K)) {
3229 OffendingRegion = K;
3230 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003231 }
3232 return false;
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003233 },
3234 false /* don't skip top directive */);
3235 CloseNesting = false;
3236 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003237 if (NestingProhibited) {
Kelvin Li2b51f722016-07-26 04:32:50 +00003238 if (OrphanSeen) {
3239 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
3240 << getOpenMPDirectiveName(CurrentRegion) << Recommend;
3241 } else {
3242 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
3243 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
3244 << Recommend << getOpenMPDirectiveName(CurrentRegion);
3245 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003246 return true;
3247 }
3248 }
3249 return false;
3250}
3251
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003252static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
3253 ArrayRef<OMPClause *> Clauses,
3254 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
3255 bool ErrorFound = false;
3256 unsigned NamedModifiersNumber = 0;
3257 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
3258 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00003259 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00003260 for (const OMPClause *C : Clauses) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003261 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
3262 // At most one if clause without a directive-name-modifier can appear on
3263 // the directive.
3264 OpenMPDirectiveKind CurNM = IC->getNameModifier();
3265 if (FoundNameModifiers[CurNM]) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003266 S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003267 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
3268 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
3269 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003270 } else if (CurNM != OMPD_unknown) {
3271 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003272 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003273 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003274 FoundNameModifiers[CurNM] = IC;
3275 if (CurNM == OMPD_unknown)
3276 continue;
3277 // Check if the specified name modifier is allowed for the current
3278 // directive.
3279 // At most one if clause with the particular directive-name-modifier can
3280 // appear on the directive.
3281 bool MatchFound = false;
3282 for (auto NM : AllowedNameModifiers) {
3283 if (CurNM == NM) {
3284 MatchFound = true;
3285 break;
3286 }
3287 }
3288 if (!MatchFound) {
3289 S.Diag(IC->getNameModifierLoc(),
3290 diag::err_omp_wrong_if_directive_name_modifier)
3291 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
3292 ErrorFound = true;
3293 }
3294 }
3295 }
3296 // If any if clause on the directive includes a directive-name-modifier then
3297 // all if clauses on the directive must include a directive-name-modifier.
3298 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
3299 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003300 S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003301 diag::err_omp_no_more_if_clause);
3302 } else {
3303 std::string Values;
3304 std::string Sep(", ");
3305 unsigned AllowedCnt = 0;
3306 unsigned TotalAllowedNum =
3307 AllowedNameModifiers.size() - NamedModifiersNumber;
3308 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3309 ++Cnt) {
3310 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
3311 if (!FoundNameModifiers[NM]) {
3312 Values += "'";
3313 Values += getOpenMPDirectiveName(NM);
3314 Values += "'";
3315 if (AllowedCnt + 2 == TotalAllowedNum)
3316 Values += " or ";
3317 else if (AllowedCnt + 1 != TotalAllowedNum)
3318 Values += Sep;
3319 ++AllowedCnt;
3320 }
3321 }
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003322 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003323 diag::err_omp_unnamed_if_clause)
3324 << (TotalAllowedNum > 1) << Values;
3325 }
Alexey Bataeve3727102018-04-18 15:57:46 +00003326 for (SourceLocation Loc : NameModifierLoc) {
Alexey Bataevecb156a2015-09-15 17:23:56 +00003327 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3328 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003329 ErrorFound = true;
3330 }
3331 return ErrorFound;
3332}
3333
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003334StmtResult Sema::ActOnOpenMPExecutableDirective(
3335 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
3336 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3337 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003338 StmtResult Res = StmtError();
Jonas Hahnfeld64a9e3c2017-02-22 06:49:10 +00003339 // First check CancelRegion which is then used in checkNestingOfRegions.
3340 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) ||
3341 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003342 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00003343 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003344
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003345 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataeve3727102018-04-18 15:57:46 +00003346 VarsWithInheritedDSAType VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003347 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00003348 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00003349 if (AStmt && !CurContext->isDependentContext()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00003350 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
3351
3352 // Check default data sharing attributes for referenced variables.
3353 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
Arpith Chacko Jacob1f46b702017-01-23 15:38:49 +00003354 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind);
3355 Stmt *S = AStmt;
3356 while (--ThisCaptureLevel >= 0)
3357 S = cast<CapturedStmt>(S)->getCapturedStmt();
3358 DSAChecker.Visit(S);
Alexey Bataev68446b72014-07-18 07:47:19 +00003359 if (DSAChecker.isErrorFound())
3360 return StmtError();
3361 // Generate list of implicitly defined firstprivate variables.
3362 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00003363
Alexey Bataev88202be2017-07-27 13:20:36 +00003364 SmallVector<Expr *, 4> ImplicitFirstprivates(
3365 DSAChecker.getImplicitFirstprivate().begin(),
3366 DSAChecker.getImplicitFirstprivate().end());
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003367 SmallVector<Expr *, 4> ImplicitMaps(DSAChecker.getImplicitMap().begin(),
3368 DSAChecker.getImplicitMap().end());
Alexey Bataev88202be2017-07-27 13:20:36 +00003369 // Mark taskgroup task_reduction descriptors as implicitly firstprivate.
Alexey Bataeve3727102018-04-18 15:57:46 +00003370 for (OMPClause *C : Clauses) {
Alexey Bataev88202be2017-07-27 13:20:36 +00003371 if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003372 for (Expr *E : IRC->taskgroup_descriptors())
Alexey Bataev88202be2017-07-27 13:20:36 +00003373 if (E)
3374 ImplicitFirstprivates.emplace_back(E);
3375 }
3376 }
3377 if (!ImplicitFirstprivates.empty()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00003378 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
Alexey Bataev88202be2017-07-27 13:20:36 +00003379 ImplicitFirstprivates, SourceLocation(), SourceLocation(),
3380 SourceLocation())) {
Alexey Bataev68446b72014-07-18 07:47:19 +00003381 ClausesWithImplicit.push_back(Implicit);
3382 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
Alexey Bataev88202be2017-07-27 13:20:36 +00003383 ImplicitFirstprivates.size();
Alexey Bataeve3727102018-04-18 15:57:46 +00003384 } else {
Alexey Bataev68446b72014-07-18 07:47:19 +00003385 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003386 }
Alexey Bataev68446b72014-07-18 07:47:19 +00003387 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003388 if (!ImplicitMaps.empty()) {
3389 if (OMPClause *Implicit = ActOnOpenMPMapClause(
Kelvin Lief579432018-12-18 22:18:41 +00003390 llvm::None, llvm::None, OMPC_MAP_tofrom,
3391 /*IsMapTypeImplicit=*/true, SourceLocation(), SourceLocation(),
3392 ImplicitMaps, SourceLocation(), SourceLocation(),
3393 SourceLocation())) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003394 ClausesWithImplicit.emplace_back(Implicit);
3395 ErrorFound |=
3396 cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMaps.size();
Alexey Bataeve3727102018-04-18 15:57:46 +00003397 } else {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003398 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003399 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003400 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003401 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003402
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003403 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003404 switch (Kind) {
3405 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00003406 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3407 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003408 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003409 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003410 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003411 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3412 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003413 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003414 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003415 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3416 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003417 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003418 case OMPD_for_simd:
3419 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3420 EndLoc, VarsWithInheritedDSA);
3421 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003422 case OMPD_sections:
3423 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3424 EndLoc);
3425 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003426 case OMPD_section:
3427 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003428 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003429 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3430 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003431 case OMPD_single:
3432 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3433 EndLoc);
3434 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003435 case OMPD_master:
3436 assert(ClausesWithImplicit.empty() &&
3437 "No clauses are allowed for 'omp master' directive");
3438 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3439 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003440 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003441 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3442 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003443 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003444 case OMPD_parallel_for:
3445 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3446 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003447 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003448 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003449 case OMPD_parallel_for_simd:
3450 Res = ActOnOpenMPParallelForSimdDirective(
3451 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003452 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003453 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003454 case OMPD_parallel_sections:
3455 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3456 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003457 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003458 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003459 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003460 Res =
3461 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003462 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003463 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003464 case OMPD_taskyield:
3465 assert(ClausesWithImplicit.empty() &&
3466 "No clauses are allowed for 'omp taskyield' directive");
3467 assert(AStmt == nullptr &&
3468 "No associated statement allowed for 'omp taskyield' directive");
3469 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3470 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003471 case OMPD_barrier:
3472 assert(ClausesWithImplicit.empty() &&
3473 "No clauses are allowed for 'omp barrier' directive");
3474 assert(AStmt == nullptr &&
3475 "No associated statement allowed for 'omp barrier' directive");
3476 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3477 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003478 case OMPD_taskwait:
3479 assert(ClausesWithImplicit.empty() &&
3480 "No clauses are allowed for 'omp taskwait' directive");
3481 assert(AStmt == nullptr &&
3482 "No associated statement allowed for 'omp taskwait' directive");
3483 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3484 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003485 case OMPD_taskgroup:
Alexey Bataev169d96a2017-07-18 20:17:46 +00003486 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc,
3487 EndLoc);
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003488 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003489 case OMPD_flush:
3490 assert(AStmt == nullptr &&
3491 "No associated statement allowed for 'omp flush' directive");
3492 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3493 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003494 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003495 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3496 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003497 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003498 case OMPD_atomic:
3499 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3500 EndLoc);
3501 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003502 case OMPD_teams:
3503 Res =
3504 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3505 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003506 case OMPD_target:
3507 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3508 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003509 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003510 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003511 case OMPD_target_parallel:
3512 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3513 StartLoc, EndLoc);
3514 AllowedNameModifiers.push_back(OMPD_target);
3515 AllowedNameModifiers.push_back(OMPD_parallel);
3516 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003517 case OMPD_target_parallel_for:
3518 Res = ActOnOpenMPTargetParallelForDirective(
3519 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3520 AllowedNameModifiers.push_back(OMPD_target);
3521 AllowedNameModifiers.push_back(OMPD_parallel);
3522 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003523 case OMPD_cancellation_point:
3524 assert(ClausesWithImplicit.empty() &&
3525 "No clauses are allowed for 'omp cancellation point' directive");
3526 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3527 "cancellation point' directive");
3528 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3529 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003530 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003531 assert(AStmt == nullptr &&
3532 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003533 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3534 CancelRegion);
3535 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003536 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003537 case OMPD_target_data:
3538 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3539 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003540 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003541 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003542 case OMPD_target_enter_data:
3543 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00003544 EndLoc, AStmt);
Samuel Antaodf67fc42016-01-19 19:15:56 +00003545 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3546 break;
Samuel Antao72590762016-01-19 20:04:50 +00003547 case OMPD_target_exit_data:
3548 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00003549 EndLoc, AStmt);
Samuel Antao72590762016-01-19 20:04:50 +00003550 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3551 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003552 case OMPD_taskloop:
3553 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3554 EndLoc, VarsWithInheritedDSA);
3555 AllowedNameModifiers.push_back(OMPD_taskloop);
3556 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003557 case OMPD_taskloop_simd:
3558 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3559 EndLoc, VarsWithInheritedDSA);
3560 AllowedNameModifiers.push_back(OMPD_taskloop);
3561 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003562 case OMPD_distribute:
3563 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3564 EndLoc, VarsWithInheritedDSA);
3565 break;
Samuel Antao686c70c2016-05-26 17:30:50 +00003566 case OMPD_target_update:
Alexey Bataev7828b252017-11-21 17:08:48 +00003567 Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc,
3568 EndLoc, AStmt);
Samuel Antao686c70c2016-05-26 17:30:50 +00003569 AllowedNameModifiers.push_back(OMPD_target_update);
3570 break;
Carlo Bertolli9925f152016-06-27 14:55:37 +00003571 case OMPD_distribute_parallel_for:
3572 Res = ActOnOpenMPDistributeParallelForDirective(
3573 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3574 AllowedNameModifiers.push_back(OMPD_parallel);
3575 break;
Kelvin Li4a39add2016-07-05 05:00:15 +00003576 case OMPD_distribute_parallel_for_simd:
3577 Res = ActOnOpenMPDistributeParallelForSimdDirective(
3578 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3579 AllowedNameModifiers.push_back(OMPD_parallel);
3580 break;
Kelvin Li787f3fc2016-07-06 04:45:38 +00003581 case OMPD_distribute_simd:
3582 Res = ActOnOpenMPDistributeSimdDirective(
3583 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3584 break;
Kelvin Lia579b912016-07-14 02:54:56 +00003585 case OMPD_target_parallel_for_simd:
3586 Res = ActOnOpenMPTargetParallelForSimdDirective(
3587 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3588 AllowedNameModifiers.push_back(OMPD_target);
3589 AllowedNameModifiers.push_back(OMPD_parallel);
3590 break;
Kelvin Li986330c2016-07-20 22:57:10 +00003591 case OMPD_target_simd:
3592 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3593 EndLoc, VarsWithInheritedDSA);
3594 AllowedNameModifiers.push_back(OMPD_target);
3595 break;
Kelvin Li02532872016-08-05 14:37:37 +00003596 case OMPD_teams_distribute:
David Majnemer9d168222016-08-05 17:44:54 +00003597 Res = ActOnOpenMPTeamsDistributeDirective(
3598 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Kelvin Li02532872016-08-05 14:37:37 +00003599 break;
Kelvin Li4e325f72016-10-25 12:50:55 +00003600 case OMPD_teams_distribute_simd:
3601 Res = ActOnOpenMPTeamsDistributeSimdDirective(
3602 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3603 break;
Kelvin Li579e41c2016-11-30 23:51:03 +00003604 case OMPD_teams_distribute_parallel_for_simd:
3605 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective(
3606 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3607 AllowedNameModifiers.push_back(OMPD_parallel);
3608 break;
Kelvin Li7ade93f2016-12-09 03:24:30 +00003609 case OMPD_teams_distribute_parallel_for:
3610 Res = ActOnOpenMPTeamsDistributeParallelForDirective(
3611 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3612 AllowedNameModifiers.push_back(OMPD_parallel);
3613 break;
Kelvin Libf594a52016-12-17 05:48:59 +00003614 case OMPD_target_teams:
3615 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc,
3616 EndLoc);
3617 AllowedNameModifiers.push_back(OMPD_target);
3618 break;
Kelvin Li83c451e2016-12-25 04:52:54 +00003619 case OMPD_target_teams_distribute:
3620 Res = ActOnOpenMPTargetTeamsDistributeDirective(
3621 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3622 AllowedNameModifiers.push_back(OMPD_target);
3623 break;
Kelvin Li80e8f562016-12-29 22:16:30 +00003624 case OMPD_target_teams_distribute_parallel_for:
3625 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective(
3626 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3627 AllowedNameModifiers.push_back(OMPD_target);
3628 AllowedNameModifiers.push_back(OMPD_parallel);
3629 break;
Kelvin Li1851df52017-01-03 05:23:48 +00003630 case OMPD_target_teams_distribute_parallel_for_simd:
3631 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
3632 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3633 AllowedNameModifiers.push_back(OMPD_target);
3634 AllowedNameModifiers.push_back(OMPD_parallel);
3635 break;
Kelvin Lida681182017-01-10 18:08:18 +00003636 case OMPD_target_teams_distribute_simd:
3637 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective(
3638 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3639 AllowedNameModifiers.push_back(OMPD_target);
3640 break;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00003641 case OMPD_declare_target:
3642 case OMPD_end_declare_target:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003643 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003644 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003645 case OMPD_declare_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00003646 case OMPD_requires:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003647 llvm_unreachable("OpenMP Directive is not allowed");
3648 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003649 llvm_unreachable("Unknown OpenMP directive");
3650 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003651
Alexey Bataeve3727102018-04-18 15:57:46 +00003652 for (const auto &P : VarsWithInheritedDSA) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003653 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3654 << P.first << P.second->getSourceRange();
3655 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003656 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3657
3658 if (!AllowedNameModifiers.empty())
3659 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3660 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003661
Alexey Bataeved09d242014-05-28 05:53:51 +00003662 if (ErrorFound)
3663 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003664 return Res;
3665}
3666
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003667Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3668 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
Alexey Bataevd93d3762016-04-12 09:35:56 +00003669 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
Alexey Bataevecba70f2016-04-12 11:02:11 +00003670 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3671 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003672 assert(Aligneds.size() == Alignments.size());
Alexey Bataevecba70f2016-04-12 11:02:11 +00003673 assert(Linears.size() == LinModifiers.size());
3674 assert(Linears.size() == Steps.size());
Alexey Bataev587e1de2016-03-30 10:43:55 +00003675 if (!DG || DG.get().isNull())
3676 return DeclGroupPtrTy();
3677
3678 if (!DG.get().isSingleDecl()) {
Alexey Bataev20dfd772016-04-04 10:12:15 +00003679 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003680 return DG;
3681 }
Alexey Bataeve3727102018-04-18 15:57:46 +00003682 Decl *ADecl = DG.get().getSingleDecl();
Alexey Bataev587e1de2016-03-30 10:43:55 +00003683 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3684 ADecl = FTD->getTemplatedDecl();
3685
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003686 auto *FD = dyn_cast<FunctionDecl>(ADecl);
3687 if (!FD) {
3688 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003689 return DeclGroupPtrTy();
3690 }
3691
Alexey Bataev2af33e32016-04-07 12:45:37 +00003692 // OpenMP [2.8.2, declare simd construct, Description]
3693 // The parameter of the simdlen clause must be a constant positive integer
3694 // expression.
3695 ExprResult SL;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003696 if (Simdlen)
Alexey Bataev2af33e32016-04-07 12:45:37 +00003697 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003698 // OpenMP [2.8.2, declare simd construct, Description]
3699 // The special this pointer can be used as if was one of the arguments to the
3700 // function in any of the linear, aligned, or uniform clauses.
3701 // The uniform clause declares one or more arguments to have an invariant
3702 // value for all concurrent invocations of the function in the execution of a
3703 // single SIMD loop.
Alexey Bataeve3727102018-04-18 15:57:46 +00003704 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs;
3705 const Expr *UniformedLinearThis = nullptr;
3706 for (const Expr *E : Uniforms) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003707 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00003708 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3709 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003710 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3711 FD->getParamDecl(PVD->getFunctionScopeIndex())
Alexey Bataevecba70f2016-04-12 11:02:11 +00003712 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
Alexey Bataev43a919f2018-04-13 17:48:43 +00003713 UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003714 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003715 }
3716 if (isa<CXXThisExpr>(E)) {
3717 UniformedLinearThis = E;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003718 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003719 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003720 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3721 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
Alexey Bataev2af33e32016-04-07 12:45:37 +00003722 }
Alexey Bataevd93d3762016-04-12 09:35:56 +00003723 // OpenMP [2.8.2, declare simd construct, Description]
3724 // The aligned clause declares that the object to which each list item points
3725 // is aligned to the number of bytes expressed in the optional parameter of
3726 // the aligned clause.
3727 // The special this pointer can be used as if was one of the arguments to the
3728 // function in any of the linear, aligned, or uniform clauses.
3729 // The type of list items appearing in the aligned clause must be array,
3730 // pointer, reference to array, or reference to pointer.
Alexey Bataeve3727102018-04-18 15:57:46 +00003731 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs;
3732 const Expr *AlignedThis = nullptr;
3733 for (const Expr *E : Aligneds) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003734 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00003735 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3736 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3737 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevd93d3762016-04-12 09:35:56 +00003738 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3739 FD->getParamDecl(PVD->getFunctionScopeIndex())
3740 ->getCanonicalDecl() == CanonPVD) {
3741 // OpenMP [2.8.1, simd construct, Restrictions]
3742 // A list-item cannot appear in more than one aligned clause.
3743 if (AlignedArgs.count(CanonPVD) > 0) {
3744 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3745 << 1 << E->getSourceRange();
3746 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3747 diag::note_omp_explicit_dsa)
3748 << getOpenMPClauseName(OMPC_aligned);
3749 continue;
3750 }
3751 AlignedArgs[CanonPVD] = E;
3752 QualType QTy = PVD->getType()
3753 .getNonReferenceType()
3754 .getUnqualifiedType()
3755 .getCanonicalType();
3756 const Type *Ty = QTy.getTypePtrOrNull();
3757 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3758 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3759 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3760 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3761 }
3762 continue;
3763 }
3764 }
3765 if (isa<CXXThisExpr>(E)) {
3766 if (AlignedThis) {
3767 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3768 << 2 << E->getSourceRange();
3769 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3770 << getOpenMPClauseName(OMPC_aligned);
3771 }
3772 AlignedThis = E;
3773 continue;
3774 }
3775 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3776 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3777 }
3778 // The optional parameter of the aligned clause, alignment, must be a constant
3779 // positive integer expression. If no optional parameter is specified,
3780 // implementation-defined default alignments for SIMD instructions on the
3781 // target platforms are assumed.
Alexey Bataeve3727102018-04-18 15:57:46 +00003782 SmallVector<const Expr *, 4> NewAligns;
3783 for (Expr *E : Alignments) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003784 ExprResult Align;
3785 if (E)
3786 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3787 NewAligns.push_back(Align.get());
3788 }
Alexey Bataevecba70f2016-04-12 11:02:11 +00003789 // OpenMP [2.8.2, declare simd construct, Description]
3790 // The linear clause declares one or more list items to be private to a SIMD
3791 // lane and to have a linear relationship with respect to the iteration space
3792 // of a loop.
3793 // The special this pointer can be used as if was one of the arguments to the
3794 // function in any of the linear, aligned, or uniform clauses.
3795 // When a linear-step expression is specified in a linear clause it must be
3796 // either a constant integer expression or an integer-typed parameter that is
3797 // specified in a uniform clause on the directive.
Alexey Bataeve3727102018-04-18 15:57:46 +00003798 llvm::DenseMap<const Decl *, const Expr *> LinearArgs;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003799 const bool IsUniformedThis = UniformedLinearThis != nullptr;
3800 auto MI = LinModifiers.begin();
Alexey Bataeve3727102018-04-18 15:57:46 +00003801 for (const Expr *E : Linears) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003802 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3803 ++MI;
3804 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00003805 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3806 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3807 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevecba70f2016-04-12 11:02:11 +00003808 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3809 FD->getParamDecl(PVD->getFunctionScopeIndex())
3810 ->getCanonicalDecl() == CanonPVD) {
3811 // OpenMP [2.15.3.7, linear Clause, Restrictions]
3812 // A list-item cannot appear in more than one linear clause.
3813 if (LinearArgs.count(CanonPVD) > 0) {
3814 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3815 << getOpenMPClauseName(OMPC_linear)
3816 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3817 Diag(LinearArgs[CanonPVD]->getExprLoc(),
3818 diag::note_omp_explicit_dsa)
3819 << getOpenMPClauseName(OMPC_linear);
3820 continue;
3821 }
3822 // Each argument can appear in at most one uniform or linear clause.
3823 if (UniformedArgs.count(CanonPVD) > 0) {
3824 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3825 << getOpenMPClauseName(OMPC_linear)
3826 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3827 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3828 diag::note_omp_explicit_dsa)
3829 << getOpenMPClauseName(OMPC_uniform);
3830 continue;
3831 }
3832 LinearArgs[CanonPVD] = E;
3833 if (E->isValueDependent() || E->isTypeDependent() ||
3834 E->isInstantiationDependent() ||
3835 E->containsUnexpandedParameterPack())
3836 continue;
3837 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3838 PVD->getOriginalType());
3839 continue;
3840 }
3841 }
3842 if (isa<CXXThisExpr>(E)) {
3843 if (UniformedLinearThis) {
3844 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3845 << getOpenMPClauseName(OMPC_linear)
3846 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3847 << E->getSourceRange();
3848 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3849 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3850 : OMPC_linear);
3851 continue;
3852 }
3853 UniformedLinearThis = E;
3854 if (E->isValueDependent() || E->isTypeDependent() ||
3855 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3856 continue;
3857 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3858 E->getType());
3859 continue;
3860 }
3861 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3862 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3863 }
3864 Expr *Step = nullptr;
3865 Expr *NewStep = nullptr;
3866 SmallVector<Expr *, 4> NewSteps;
Alexey Bataeve3727102018-04-18 15:57:46 +00003867 for (Expr *E : Steps) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003868 // Skip the same step expression, it was checked already.
3869 if (Step == E || !E) {
3870 NewSteps.push_back(E ? NewStep : nullptr);
3871 continue;
3872 }
3873 Step = E;
Alexey Bataeve3727102018-04-18 15:57:46 +00003874 if (const auto *DRE = dyn_cast<DeclRefExpr>(Step))
3875 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3876 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevecba70f2016-04-12 11:02:11 +00003877 if (UniformedArgs.count(CanonPVD) == 0) {
3878 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
3879 << Step->getSourceRange();
3880 } else if (E->isValueDependent() || E->isTypeDependent() ||
3881 E->isInstantiationDependent() ||
3882 E->containsUnexpandedParameterPack() ||
Alexey Bataeve3727102018-04-18 15:57:46 +00003883 CanonPVD->getType()->hasIntegerRepresentation()) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003884 NewSteps.push_back(Step);
Alexey Bataeve3727102018-04-18 15:57:46 +00003885 } else {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003886 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
3887 << Step->getSourceRange();
3888 }
3889 continue;
3890 }
3891 NewStep = Step;
3892 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3893 !Step->isInstantiationDependent() &&
3894 !Step->containsUnexpandedParameterPack()) {
3895 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
3896 .get();
3897 if (NewStep)
3898 NewStep = VerifyIntegerConstantExpression(NewStep).get();
3899 }
3900 NewSteps.push_back(NewStep);
3901 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003902 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3903 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
Alexey Bataevd93d3762016-04-12 09:35:56 +00003904 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
Alexey Bataevecba70f2016-04-12 11:02:11 +00003905 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
3906 const_cast<Expr **>(Linears.data()), Linears.size(),
3907 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
3908 NewSteps.data(), NewSteps.size(), SR);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003909 ADecl->addAttr(NewAttr);
3910 return ConvertDeclToDeclGroup(ADecl);
3911}
3912
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003913StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3914 Stmt *AStmt,
3915 SourceLocation StartLoc,
3916 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003917 if (!AStmt)
3918 return StmtError();
3919
Alexey Bataeve3727102018-04-18 15:57:46 +00003920 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev9959db52014-05-06 10:08:46 +00003921 // 1.2.2 OpenMP Language Terminology
3922 // Structured block - An executable statement with a single entry at the
3923 // top and a single exit at the bottom.
3924 // The point of exit cannot be a branch out of the structured block.
3925 // longjmp() and throw() must not violate the entry/exit criteria.
3926 CS->getCapturedDecl()->setNothrow();
3927
Reid Kleckner87a31802018-03-12 21:43:02 +00003928 setFunctionHasBranchProtectedScope();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003929
Alexey Bataev25e5b442015-09-15 12:52:43 +00003930 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3931 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003932}
3933
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003934namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003935/// Helper class for checking canonical form of the OpenMP loops and
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003936/// extracting iteration space of each loop in the loop nest, that will be used
3937/// for IR generation.
3938class OpenMPIterationSpaceChecker {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003939 /// Reference to Sema.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003940 Sema &SemaRef;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003941 /// A location for diagnostics (when there is no some better location).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003942 SourceLocation DefaultLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003943 /// A location for diagnostics (when increment is not compatible).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003944 SourceLocation ConditionLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003945 /// A source location for referring to loop init later.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003946 SourceRange InitSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003947 /// A source location for referring to condition later.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003948 SourceRange ConditionSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003949 /// A source location for referring to increment later.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003950 SourceRange IncrementSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003951 /// Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003952 ValueDecl *LCDecl = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003953 /// Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003954 Expr *LCRef = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003955 /// Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003956 Expr *LB = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003957 /// Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003958 Expr *UB = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003959 /// Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003960 Expr *Step = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003961 /// This flag is true when condition is one of:
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003962 /// Var < UB
3963 /// Var <= UB
3964 /// UB > Var
3965 /// UB >= Var
Kelvin Liefbe4af2018-11-21 19:10:48 +00003966 /// This will have no value when the condition is !=
3967 llvm::Optional<bool> TestIsLessOp;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003968 /// This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003969 bool TestIsStrictOp = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003970 /// This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003971 bool SubtractStep = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003972
3973public:
3974 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003975 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003976 /// Check init-expr for canonical loop form and save loop counter
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003977 /// variable - #Var and its initialization value - #LB.
Alexey Bataeve3727102018-04-18 15:57:46 +00003978 bool checkAndSetInit(Stmt *S, bool EmitDiags = true);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003979 /// Check test-expr for canonical form, save upper-bound (#UB), flags
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003980 /// for less/greater and for strict/non-strict comparison.
Alexey Bataeve3727102018-04-18 15:57:46 +00003981 bool checkAndSetCond(Expr *S);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003982 /// Check incr-expr for canonical loop form and return true if it
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003983 /// does not conform, otherwise save loop step (#Step).
Alexey Bataeve3727102018-04-18 15:57:46 +00003984 bool checkAndSetInc(Expr *S);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003985 /// Return the loop counter variable.
Alexey Bataeve3727102018-04-18 15:57:46 +00003986 ValueDecl *getLoopDecl() const { return LCDecl; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003987 /// Return the reference expression to loop counter variable.
Alexey Bataeve3727102018-04-18 15:57:46 +00003988 Expr *getLoopDeclRefExpr() const { return LCRef; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003989 /// Source range of the loop init.
Alexey Bataeve3727102018-04-18 15:57:46 +00003990 SourceRange getInitSrcRange() const { return InitSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003991 /// Source range of the loop condition.
Alexey Bataeve3727102018-04-18 15:57:46 +00003992 SourceRange getConditionSrcRange() const { return ConditionSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003993 /// Source range of the loop increment.
Alexey Bataeve3727102018-04-18 15:57:46 +00003994 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003995 /// True if the step should be subtracted.
Alexey Bataeve3727102018-04-18 15:57:46 +00003996 bool shouldSubtractStep() const { return SubtractStep; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003997 /// Build the expression to calculate the number of iterations.
Alexey Bataeve3727102018-04-18 15:57:46 +00003998 Expr *buildNumIterations(
3999 Scope *S, const bool LimitedType,
4000 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004001 /// Build the precondition expression for the loops.
Alexey Bataeve3727102018-04-18 15:57:46 +00004002 Expr *
4003 buildPreCond(Scope *S, Expr *Cond,
4004 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004005 /// Build reference expression to the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004006 DeclRefExpr *
4007 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
4008 DSAStackTy &DSA) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004009 /// Build reference expression to the private counter be used for
Alexey Bataeva8899172015-08-06 12:30:57 +00004010 /// codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004011 Expr *buildPrivateCounterVar() const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004012 /// Build initialization of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004013 Expr *buildCounterInit() const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004014 /// Build step of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004015 Expr *buildCounterStep() const;
Alexey Bataevf138fda2018-08-13 19:04:24 +00004016 /// Build loop data with counter value for depend clauses in ordered
4017 /// directives.
4018 Expr *
4019 buildOrderedLoopData(Scope *S, Expr *Counter,
4020 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
4021 SourceLocation Loc, Expr *Inc = nullptr,
4022 OverloadedOperatorKind OOK = OO_Amp);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004023 /// Return true if any expression is dependent.
Alexey Bataeve3727102018-04-18 15:57:46 +00004024 bool dependent() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004025
4026private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004027 /// Check the right-hand side of an assignment in the increment
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004028 /// expression.
Alexey Bataeve3727102018-04-18 15:57:46 +00004029 bool checkAndSetIncRHS(Expr *RHS);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004030 /// Helper to set loop counter variable and its initializer.
Alexey Bataeve3727102018-04-18 15:57:46 +00004031 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004032 /// Helper to set upper bound.
Kelvin Liefbe4af2018-11-21 19:10:48 +00004033 bool setUB(Expr *NewUB, llvm::Optional<bool> LessOp, bool StrictOp,
4034 SourceRange SR, SourceLocation SL);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004035 /// Helper to set loop increment.
Alexey Bataeve3727102018-04-18 15:57:46 +00004036 bool setStep(Expr *NewStep, bool Subtract);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004037};
4038
Alexey Bataeve3727102018-04-18 15:57:46 +00004039bool OpenMPIterationSpaceChecker::dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004040 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004041 assert(!LB && !UB && !Step);
4042 return false;
4043 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004044 return LCDecl->getType()->isDependentType() ||
4045 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
4046 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004047}
4048
Alexey Bataeve3727102018-04-18 15:57:46 +00004049bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl,
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004050 Expr *NewLCRefExpr,
4051 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004052 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004053 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00004054 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004055 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004056 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004057 LCDecl = getCanonicalDecl(NewLCDecl);
4058 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004059 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
4060 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00004061 if ((Ctor->isCopyOrMoveConstructor() ||
4062 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4063 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004064 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004065 LB = NewLB;
4066 return false;
4067}
4068
Kelvin Liefbe4af2018-11-21 19:10:48 +00004069bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB, llvm::Optional<bool> LessOp,
4070 bool StrictOp, SourceRange SR,
4071 SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004072 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004073 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
4074 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004075 if (!NewUB)
4076 return true;
4077 UB = NewUB;
Kelvin Liefbe4af2018-11-21 19:10:48 +00004078 if (LessOp)
4079 TestIsLessOp = LessOp;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004080 TestIsStrictOp = StrictOp;
4081 ConditionSrcRange = SR;
4082 ConditionLoc = SL;
4083 return false;
4084}
4085
Alexey Bataeve3727102018-04-18 15:57:46 +00004086bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) {
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 && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004089 if (!NewStep)
4090 return true;
4091 if (!NewStep->isValueDependent()) {
4092 // Check that the step is integer expression.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004093 SourceLocation StepLoc = NewStep->getBeginLoc();
Alexey Bataev5372fb82017-08-31 23:06:52 +00004094 ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion(
4095 StepLoc, getExprAsWritten(NewStep));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004096 if (Val.isInvalid())
4097 return true;
4098 NewStep = Val.get();
4099
4100 // OpenMP [2.6, Canonical Loop Form, Restrictions]
4101 // If test-expr is of form var relational-op b and relational-op is < or
4102 // <= then incr-expr must cause var to increase on each iteration of the
4103 // loop. If test-expr is of form var relational-op b and relational-op is
4104 // > or >= then incr-expr must cause var to decrease on each iteration of
4105 // the loop.
4106 // If test-expr is of form b relational-op var and relational-op is < or
4107 // <= then incr-expr must cause var to decrease on each iteration of the
4108 // loop. If test-expr is of form b relational-op var and relational-op is
4109 // > or >= then incr-expr must cause var to increase on each iteration of
4110 // the loop.
4111 llvm::APSInt Result;
4112 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
4113 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
4114 bool IsConstNeg =
4115 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004116 bool IsConstPos =
4117 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004118 bool IsConstZero = IsConstant && !Result.getBoolValue();
Kelvin Liefbe4af2018-11-21 19:10:48 +00004119
4120 // != with increment is treated as <; != with decrement is treated as >
4121 if (!TestIsLessOp.hasValue())
4122 TestIsLessOp = IsConstPos || (IsUnsigned && !Subtract);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004123 if (UB && (IsConstZero ||
Kelvin Liefbe4af2018-11-21 19:10:48 +00004124 (TestIsLessOp.getValue() ?
4125 (IsConstNeg || (IsUnsigned && Subtract)) :
4126 (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004127 SemaRef.Diag(NewStep->getExprLoc(),
4128 diag::err_omp_loop_incr_not_compatible)
Kelvin Liefbe4af2018-11-21 19:10:48 +00004129 << LCDecl << TestIsLessOp.getValue() << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004130 SemaRef.Diag(ConditionLoc,
4131 diag::note_omp_loop_cond_requres_compatible_incr)
Kelvin Liefbe4af2018-11-21 19:10:48 +00004132 << TestIsLessOp.getValue() << ConditionSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004133 return true;
4134 }
Kelvin Liefbe4af2018-11-21 19:10:48 +00004135 if (TestIsLessOp.getValue() == Subtract) {
David Majnemer9d168222016-08-05 17:44:54 +00004136 NewStep =
4137 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep)
4138 .get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004139 Subtract = !Subtract;
4140 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004141 }
4142
4143 Step = NewStep;
4144 SubtractStep = Subtract;
4145 return false;
4146}
4147
Alexey Bataeve3727102018-04-18 15:57:46 +00004148bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004149 // Check init-expr for canonical loop form and save loop counter
4150 // variable - #Var and its initialization value - #LB.
4151 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
4152 // var = lb
4153 // integer-type var = lb
4154 // random-access-iterator-type var = lb
4155 // pointer-type var = lb
4156 //
4157 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00004158 if (EmitDiags) {
4159 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
4160 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004161 return true;
4162 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004163 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4164 if (!ExprTemp->cleanupsHaveSideEffects())
4165 S = ExprTemp->getSubExpr();
4166
Alexander Musmana5f070a2014-10-01 06:03:56 +00004167 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004168 if (Expr *E = dyn_cast<Expr>(S))
4169 S = E->IgnoreParens();
David Majnemer9d168222016-08-05 17:44:54 +00004170 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004171 if (BO->getOpcode() == BO_Assign) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004172 Expr *LHS = BO->getLHS()->IgnoreParens();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004173 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
4174 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4175 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataeve3727102018-04-18 15:57:46 +00004176 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4177 return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004178 }
4179 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4180 if (ME->isArrow() &&
4181 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
Alexey Bataeve3727102018-04-18 15:57:46 +00004182 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004183 }
4184 }
David Majnemer9d168222016-08-05 17:44:54 +00004185 } else if (auto *DS = dyn_cast<DeclStmt>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004186 if (DS->isSingleDecl()) {
David Majnemer9d168222016-08-05 17:44:54 +00004187 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00004188 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004189 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00004190 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004191 SemaRef.Diag(S->getBeginLoc(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004192 diag::ext_omp_loop_not_canonical_init)
4193 << S->getSourceRange();
Alexey Bataevf138fda2018-08-13 19:04:24 +00004194 return setLCDeclAndLB(
4195 Var,
4196 buildDeclRefExpr(SemaRef, Var,
4197 Var->getType().getNonReferenceType(),
4198 DS->getBeginLoc()),
4199 Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004200 }
4201 }
4202 }
David Majnemer9d168222016-08-05 17:44:54 +00004203 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004204 if (CE->getOperator() == OO_Equal) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004205 Expr *LHS = CE->getArg(0);
David Majnemer9d168222016-08-05 17:44:54 +00004206 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004207 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4208 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataeve3727102018-04-18 15:57:46 +00004209 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4210 return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004211 }
4212 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4213 if (ME->isArrow() &&
4214 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
Alexey Bataeve3727102018-04-18 15:57:46 +00004215 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004216 }
4217 }
4218 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004219
Alexey Bataeve3727102018-04-18 15:57:46 +00004220 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004221 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00004222 if (EmitDiags) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004223 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init)
Alexey Bataev9c821032015-04-30 04:23:23 +00004224 << S->getSourceRange();
4225 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004226 return true;
4227}
4228
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004229/// Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004230/// variable (which may be the loop variable) if possible.
Alexey Bataeve3727102018-04-18 15:57:46 +00004231static const ValueDecl *getInitLCDecl(const Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004232 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00004233 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004234 E = getExprAsWritten(E);
Alexey Bataeve3727102018-04-18 15:57:46 +00004235 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004236 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00004237 if ((Ctor->isCopyOrMoveConstructor() ||
4238 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4239 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004240 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00004241 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
4242 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004243 return getCanonicalDecl(VD);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004244 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004245 if (const auto *ME = dyn_cast_or_null<MemberExpr>(E))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004246 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4247 return getCanonicalDecl(ME->getMemberDecl());
4248 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004249}
4250
Alexey Bataeve3727102018-04-18 15:57:46 +00004251bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004252 // Check test-expr for canonical form, save upper-bound UB, flags for
4253 // less/greater and for strict/non-strict comparison.
4254 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4255 // var relational-op b
4256 // b relational-op var
4257 //
4258 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004259 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004260 return true;
4261 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004262 S = getExprAsWritten(S);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004263 SourceLocation CondLoc = S->getBeginLoc();
David Majnemer9d168222016-08-05 17:44:54 +00004264 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004265 if (BO->isRelationalOp()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004266 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4267 return setUB(BO->getRHS(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004268 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
4269 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4270 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +00004271 if (getInitLCDecl(BO->getRHS()) == LCDecl)
4272 return setUB(BO->getLHS(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004273 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
4274 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4275 BO->getSourceRange(), BO->getOperatorLoc());
Kelvin Liefbe4af2018-11-21 19:10:48 +00004276 } else if (BO->getOpcode() == BO_NE)
4277 return setUB(getInitLCDecl(BO->getLHS()) == LCDecl ?
4278 BO->getRHS() : BO->getLHS(),
4279 /*LessOp=*/llvm::None,
4280 /*StrictOp=*/true,
4281 BO->getSourceRange(), BO->getOperatorLoc());
David Majnemer9d168222016-08-05 17:44:54 +00004282 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004283 if (CE->getNumArgs() == 2) {
4284 auto Op = CE->getOperator();
4285 switch (Op) {
4286 case OO_Greater:
4287 case OO_GreaterEqual:
4288 case OO_Less:
4289 case OO_LessEqual:
Alexey Bataeve3727102018-04-18 15:57:46 +00004290 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4291 return setUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004292 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4293 CE->getOperatorLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +00004294 if (getInitLCDecl(CE->getArg(1)) == LCDecl)
4295 return setUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004296 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4297 CE->getOperatorLoc());
4298 break;
Kelvin Liefbe4af2018-11-21 19:10:48 +00004299 case OO_ExclaimEqual:
4300 return setUB(getInitLCDecl(CE->getArg(0)) == LCDecl ?
4301 CE->getArg(1) : CE->getArg(0),
4302 /*LessOp=*/llvm::None,
4303 /*StrictOp=*/true,
4304 CE->getSourceRange(),
4305 CE->getOperatorLoc());
4306 break;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004307 default:
4308 break;
4309 }
4310 }
4311 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004312 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004313 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004314 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004315 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004316 return true;
4317}
4318
Alexey Bataeve3727102018-04-18 15:57:46 +00004319bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004320 // RHS of canonical loop form increment can be:
4321 // var + incr
4322 // incr + var
4323 // var - incr
4324 //
4325 RHS = RHS->IgnoreParenImpCasts();
David Majnemer9d168222016-08-05 17:44:54 +00004326 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004327 if (BO->isAdditiveOp()) {
4328 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataeve3727102018-04-18 15:57:46 +00004329 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4330 return setStep(BO->getRHS(), !IsAdd);
4331 if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl)
4332 return setStep(BO->getLHS(), /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004333 }
David Majnemer9d168222016-08-05 17:44:54 +00004334 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004335 bool IsAdd = CE->getOperator() == OO_Plus;
4336 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004337 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4338 return setStep(CE->getArg(1), !IsAdd);
4339 if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl)
4340 return setStep(CE->getArg(0), /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004341 }
4342 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004343 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004344 return false;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004345 SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004346 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004347 return true;
4348}
4349
Alexey Bataeve3727102018-04-18 15:57:46 +00004350bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004351 // Check incr-expr for canonical loop form and return true if it
4352 // does not conform.
4353 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4354 // ++var
4355 // var++
4356 // --var
4357 // var--
4358 // var += incr
4359 // var -= incr
4360 // var = var + incr
4361 // var = incr + var
4362 // var = var - incr
4363 //
4364 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004365 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004366 return true;
4367 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004368 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4369 if (!ExprTemp->cleanupsHaveSideEffects())
4370 S = ExprTemp->getSubExpr();
4371
Alexander Musmana5f070a2014-10-01 06:03:56 +00004372 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004373 S = S->IgnoreParens();
David Majnemer9d168222016-08-05 17:44:54 +00004374 if (auto *UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004375 if (UO->isIncrementDecrementOp() &&
Alexey Bataeve3727102018-04-18 15:57:46 +00004376 getInitLCDecl(UO->getSubExpr()) == LCDecl)
4377 return setStep(SemaRef
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004378 .ActOnIntegerConstant(UO->getBeginLoc(),
David Majnemer9d168222016-08-05 17:44:54 +00004379 (UO->isDecrementOp() ? -1 : 1))
4380 .get(),
Alexey Bataeve3727102018-04-18 15:57:46 +00004381 /*Subtract=*/false);
David Majnemer9d168222016-08-05 17:44:54 +00004382 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004383 switch (BO->getOpcode()) {
4384 case BO_AddAssign:
4385 case BO_SubAssign:
Alexey Bataeve3727102018-04-18 15:57:46 +00004386 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4387 return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004388 break;
4389 case BO_Assign:
Alexey Bataeve3727102018-04-18 15:57:46 +00004390 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4391 return checkAndSetIncRHS(BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004392 break;
4393 default:
4394 break;
4395 }
David Majnemer9d168222016-08-05 17:44:54 +00004396 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004397 switch (CE->getOperator()) {
4398 case OO_PlusPlus:
4399 case OO_MinusMinus:
Alexey Bataeve3727102018-04-18 15:57:46 +00004400 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4401 return setStep(SemaRef
David Majnemer9d168222016-08-05 17:44:54 +00004402 .ActOnIntegerConstant(
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004403 CE->getBeginLoc(),
David Majnemer9d168222016-08-05 17:44:54 +00004404 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
4405 .get(),
Alexey Bataeve3727102018-04-18 15:57:46 +00004406 /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004407 break;
4408 case OO_PlusEqual:
4409 case OO_MinusEqual:
Alexey Bataeve3727102018-04-18 15:57:46 +00004410 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4411 return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004412 break;
4413 case OO_Equal:
Alexey Bataeve3727102018-04-18 15:57:46 +00004414 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4415 return checkAndSetIncRHS(CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004416 break;
4417 default:
4418 break;
4419 }
4420 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004421 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004422 return false;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004423 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004424 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004425 return true;
4426}
Alexander Musmana5f070a2014-10-01 06:03:56 +00004427
Alexey Bataev5a3af132016-03-29 08:58:54 +00004428static ExprResult
4429tryBuildCapture(Sema &SemaRef, Expr *Capture,
Alexey Bataeve3727102018-04-18 15:57:46 +00004430 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00004431 if (SemaRef.CurContext->isDependentContext())
4432 return ExprResult(Capture);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004433 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
4434 return SemaRef.PerformImplicitConversion(
4435 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
4436 /*AllowExplicit=*/true);
4437 auto I = Captures.find(Capture);
4438 if (I != Captures.end())
4439 return buildCapture(SemaRef, Capture, I->second);
4440 DeclRefExpr *Ref = nullptr;
4441 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
4442 Captures[Capture] = Ref;
4443 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004444}
4445
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004446/// Build the expression to calculate the number of iterations.
Alexey Bataeve3727102018-04-18 15:57:46 +00004447Expr *OpenMPIterationSpaceChecker::buildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004448 Scope *S, const bool LimitedType,
Alexey Bataeve3727102018-04-18 15:57:46 +00004449 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004450 ExprResult Diff;
Alexey Bataeve3727102018-04-18 15:57:46 +00004451 QualType VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004452 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004453 SemaRef.getLangOpts().CPlusPlus) {
4454 // Upper - Lower
Kelvin Liefbe4af2018-11-21 19:10:48 +00004455 Expr *UBExpr = TestIsLessOp.getValue() ? UB : LB;
4456 Expr *LBExpr = TestIsLessOp.getValue() ? LB : UB;
Alexey Bataev5a3af132016-03-29 08:58:54 +00004457 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
4458 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004459 if (!Upper || !Lower)
4460 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004461
4462 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4463
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004464 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004465 // BuildBinOp already emitted error, this one is to point user to upper
4466 // and lower bound, and to tell what is passed to 'operator-'.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004467 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004468 << Upper->getSourceRange() << Lower->getSourceRange();
4469 return nullptr;
4470 }
4471 }
4472
4473 if (!Diff.isUsable())
4474 return nullptr;
4475
4476 // Upper - Lower [- 1]
4477 if (TestIsStrictOp)
4478 Diff = SemaRef.BuildBinOp(
4479 S, DefaultLoc, BO_Sub, Diff.get(),
4480 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4481 if (!Diff.isUsable())
4482 return nullptr;
4483
4484 // Upper - Lower [- 1] + Step
Alexey Bataeve3727102018-04-18 15:57:46 +00004485 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004486 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004487 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004488 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004489 if (!Diff.isUsable())
4490 return nullptr;
4491
4492 // Parentheses (for dumping/debugging purposes only).
4493 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4494 if (!Diff.isUsable())
4495 return nullptr;
4496
4497 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004498 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004499 if (!Diff.isUsable())
4500 return nullptr;
4501
Alexander Musman174b3ca2014-10-06 11:16:29 +00004502 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004503 QualType Type = Diff.get()->getType();
Alexey Bataeve3727102018-04-18 15:57:46 +00004504 ASTContext &C = SemaRef.Context;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004505 bool UseVarType = VarType->hasIntegerRepresentation() &&
4506 C.getTypeSize(Type) > C.getTypeSize(VarType);
4507 if (!Type->isIntegerType() || UseVarType) {
4508 unsigned NewSize =
4509 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4510 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4511 : Type->hasSignedIntegerRepresentation();
4512 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00004513 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4514 Diff = SemaRef.PerformImplicitConversion(
4515 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4516 if (!Diff.isUsable())
4517 return nullptr;
4518 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004519 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004520 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00004521 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4522 if (NewSize != C.getTypeSize(Type)) {
4523 if (NewSize < C.getTypeSize(Type)) {
4524 assert(NewSize == 64 && "incorrect loop var size");
4525 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4526 << InitSrcRange << ConditionSrcRange;
4527 }
4528 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004529 NewSize, Type->hasSignedIntegerRepresentation() ||
4530 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00004531 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4532 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4533 Sema::AA_Converting, true);
4534 if (!Diff.isUsable())
4535 return nullptr;
4536 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004537 }
4538 }
4539
Alexander Musmana5f070a2014-10-01 06:03:56 +00004540 return Diff.get();
4541}
4542
Alexey Bataeve3727102018-04-18 15:57:46 +00004543Expr *OpenMPIterationSpaceChecker::buildPreCond(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004544 Scope *S, Expr *Cond,
Alexey Bataeve3727102018-04-18 15:57:46 +00004545 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004546 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4547 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4548 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004549
Alexey Bataeve3727102018-04-18 15:57:46 +00004550 ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures);
4551 ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004552 if (!NewLB.isUsable() || !NewUB.isUsable())
4553 return nullptr;
4554
Alexey Bataeve3727102018-04-18 15:57:46 +00004555 ExprResult CondExpr =
4556 SemaRef.BuildBinOp(S, DefaultLoc,
Kelvin Liefbe4af2018-11-21 19:10:48 +00004557 TestIsLessOp.getValue() ?
4558 (TestIsStrictOp ? BO_LT : BO_LE) :
4559 (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataeve3727102018-04-18 15:57:46 +00004560 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004561 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004562 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4563 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00004564 CondExpr = SemaRef.PerformImplicitConversion(
4565 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4566 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004567 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00004568 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4569 // Otherwise use original loop conditon and evaluate it in runtime.
4570 return CondExpr.isUsable() ? CondExpr.get() : Cond;
4571}
4572
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004573/// Build reference expression to the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004574DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar(
Alexey Bataevf138fda2018-08-13 19:04:24 +00004575 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
4576 DSAStackTy &DSA) const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004577 auto *VD = dyn_cast<VarDecl>(LCDecl);
4578 if (!VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004579 VD = SemaRef.isOpenMPCapturedDecl(LCDecl);
4580 DeclRefExpr *Ref = buildDeclRefExpr(
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004581 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
Alexey Bataeve3727102018-04-18 15:57:46 +00004582 const DSAStackTy::DSAVarData Data =
4583 DSA.getTopDSA(LCDecl, /*FromParent=*/false);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004584 // If the loop control decl is explicitly marked as private, do not mark it
4585 // as captured again.
4586 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4587 Captures.insert(std::make_pair(LCRef, Ref));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004588 return Ref;
4589 }
4590 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
Alexey Bataeva8899172015-08-06 12:30:57 +00004591 DefaultLoc);
4592}
4593
Alexey Bataeve3727102018-04-18 15:57:46 +00004594Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004595 if (LCDecl && !LCDecl->isInvalidDecl()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004596 QualType Type = LCDecl->getType().getNonReferenceType();
4597 VarDecl *PrivateVar = buildVarDecl(
Alexey Bataev63cc8e92018-03-20 14:45:59 +00004598 SemaRef, DefaultLoc, Type, LCDecl->getName(),
4599 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr,
4600 isa<VarDecl>(LCDecl)
4601 ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc)
4602 : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00004603 if (PrivateVar->isInvalidDecl())
4604 return nullptr;
4605 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4606 }
4607 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004608}
4609
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004610/// Build initialization of the counter to be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004611Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004612
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004613/// Build step of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004614Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004615
Alexey Bataevf138fda2018-08-13 19:04:24 +00004616Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData(
4617 Scope *S, Expr *Counter,
4618 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc,
4619 Expr *Inc, OverloadedOperatorKind OOK) {
4620 Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get();
4621 if (!Cnt)
4622 return nullptr;
4623 if (Inc) {
4624 assert((OOK == OO_Plus || OOK == OO_Minus) &&
4625 "Expected only + or - operations for depend clauses.");
4626 BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub;
4627 Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get();
4628 if (!Cnt)
4629 return nullptr;
4630 }
4631 ExprResult Diff;
4632 QualType VarType = LCDecl->getType().getNonReferenceType();
4633 if (VarType->isIntegerType() || VarType->isPointerType() ||
4634 SemaRef.getLangOpts().CPlusPlus) {
4635 // Upper - Lower
4636 Expr *Upper =
Kelvin Liefbe4af2018-11-21 19:10:48 +00004637 TestIsLessOp.getValue() ? Cnt : tryBuildCapture(SemaRef, UB, Captures).get();
Alexey Bataevf138fda2018-08-13 19:04:24 +00004638 Expr *Lower =
Kelvin Liefbe4af2018-11-21 19:10:48 +00004639 TestIsLessOp.getValue() ? tryBuildCapture(SemaRef, LB, Captures).get() : Cnt;
Alexey Bataevf138fda2018-08-13 19:04:24 +00004640 if (!Upper || !Lower)
4641 return nullptr;
4642
4643 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4644
4645 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
4646 // BuildBinOp already emitted error, this one is to point user to upper
4647 // and lower bound, and to tell what is passed to 'operator-'.
4648 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
4649 << Upper->getSourceRange() << Lower->getSourceRange();
4650 return nullptr;
4651 }
4652 }
4653
4654 if (!Diff.isUsable())
4655 return nullptr;
4656
4657 // Parentheses (for dumping/debugging purposes only).
4658 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4659 if (!Diff.isUsable())
4660 return nullptr;
4661
4662 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
4663 if (!NewStep.isUsable())
4664 return nullptr;
4665 // (Upper - Lower) / Step
4666 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
4667 if (!Diff.isUsable())
4668 return nullptr;
4669
4670 return Diff.get();
4671}
4672
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004673/// Iteration space of a single for loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004674struct LoopIterationSpace final {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004675 /// Condition of the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004676 Expr *PreCond = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004677 /// This expression calculates the number of iterations in the loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004678 /// It is always possible to calculate it before starting the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004679 Expr *NumIterations = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004680 /// The loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004681 Expr *CounterVar = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004682 /// Private loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004683 Expr *PrivateCounterVar = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004684 /// This is initializer for the initial value of #CounterVar.
Alexey Bataev8b427062016-05-25 12:36:08 +00004685 Expr *CounterInit = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004686 /// This is step for the #CounterVar used to generate its update:
Alexander Musmana5f070a2014-10-01 06:03:56 +00004687 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
Alexey Bataev8b427062016-05-25 12:36:08 +00004688 Expr *CounterStep = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004689 /// Should step be subtracted?
Alexey Bataev8b427062016-05-25 12:36:08 +00004690 bool Subtract = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004691 /// Source range of the loop init.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004692 SourceRange InitSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004693 /// Source range of the loop condition.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004694 SourceRange CondSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004695 /// Source range of the loop increment.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004696 SourceRange IncSrcRange;
4697};
4698
Alexey Bataev23b69422014-06-18 07:08:49 +00004699} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004700
Alexey Bataev9c821032015-04-30 04:23:23 +00004701void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4702 assert(getLangOpts().OpenMP && "OpenMP is not active.");
4703 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004704 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4705 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00004706 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevce901812018-12-19 18:16:37 +00004707 DSAStack->loopStart();
Alexey Bataev9c821032015-04-30 04:23:23 +00004708 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataeve3727102018-04-18 15:57:46 +00004709 if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) {
4710 if (ValueDecl *D = ISC.getLoopDecl()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004711 auto *VD = dyn_cast<VarDecl>(D);
4712 if (!VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004713 if (VarDecl *Private = isOpenMPCapturedDecl(D)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004714 VD = Private;
Alexey Bataeve3727102018-04-18 15:57:46 +00004715 } else {
4716 DeclRefExpr *Ref = buildCapture(*this, D, ISC.getLoopDeclRefExpr(),
4717 /*WithInit=*/false);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004718 VD = cast<VarDecl>(Ref->getDecl());
4719 }
4720 }
4721 DSAStack->addLoopControlVariable(D, VD);
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00004722 const Decl *LD = DSAStack->getPossiblyLoopCunter();
4723 if (LD != D->getCanonicalDecl()) {
4724 DSAStack->resetPossibleLoopCounter();
4725 if (auto *Var = dyn_cast_or_null<VarDecl>(LD))
4726 MarkDeclarationsReferencedInExpr(
4727 buildDeclRefExpr(*this, const_cast<VarDecl *>(Var),
4728 Var->getType().getNonLValueExprType(Context),
4729 ForLoc, /*RefersToCapture=*/true));
4730 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004731 }
4732 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004733 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00004734 }
4735}
4736
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004737/// Called on a for stmt to check and extract its iteration space
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004738/// for further processing (such as collapsing).
Alexey Bataeve3727102018-04-18 15:57:46 +00004739static bool checkOpenMPIterationSpace(
Alexey Bataev4acb8592014-07-07 13:01:15 +00004740 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4741 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataevf138fda2018-08-13 19:04:24 +00004742 unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr,
4743 Expr *OrderedLoopCountExpr,
Alexey Bataeve3727102018-04-18 15:57:46 +00004744 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004745 LoopIterationSpace &ResultIterSpace,
Alexey Bataeve3727102018-04-18 15:57:46 +00004746 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004747 // OpenMP [2.6, Canonical Loop Form]
4748 // for (init-expr; test-expr; incr-expr) structured-block
David Majnemer9d168222016-08-05 17:44:54 +00004749 auto *For = dyn_cast_or_null<ForStmt>(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004750 if (!For) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004751 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004752 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
Alexey Bataevf138fda2018-08-13 19:04:24 +00004753 << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount
Alexey Bataev10e775f2015-07-30 11:36:16 +00004754 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
Alexey Bataevf138fda2018-08-13 19:04:24 +00004755 if (TotalNestedLoopCount > 1) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00004756 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4757 SemaRef.Diag(DSA.getConstructLoc(),
4758 diag::note_omp_collapse_ordered_expr)
4759 << 2 << CollapseLoopCountExpr->getSourceRange()
4760 << OrderedLoopCountExpr->getSourceRange();
4761 else if (CollapseLoopCountExpr)
4762 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4763 diag::note_omp_collapse_ordered_expr)
4764 << 0 << CollapseLoopCountExpr->getSourceRange();
4765 else
4766 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4767 diag::note_omp_collapse_ordered_expr)
4768 << 1 << OrderedLoopCountExpr->getSourceRange();
4769 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004770 return true;
4771 }
4772 assert(For->getBody());
4773
4774 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4775
4776 // Check init.
Alexey Bataeve3727102018-04-18 15:57:46 +00004777 Stmt *Init = For->getInit();
4778 if (ISC.checkAndSetInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004779 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004780
4781 bool HasErrors = false;
4782
4783 // Check loop variable's type.
Alexey Bataeve3727102018-04-18 15:57:46 +00004784 if (ValueDecl *LCDecl = ISC.getLoopDecl()) {
4785 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004786
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004787 // OpenMP [2.6, Canonical Loop Form]
4788 // Var is one of the following:
4789 // A variable of signed or unsigned integer type.
4790 // For C++, a variable of a random access iterator type.
4791 // For C, a variable of a pointer type.
Alexey Bataeve3727102018-04-18 15:57:46 +00004792 QualType VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004793 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4794 !VarType->isPointerType() &&
4795 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004796 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004797 << SemaRef.getLangOpts().CPlusPlus;
4798 HasErrors = true;
4799 }
4800
4801 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4802 // a Construct
4803 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4804 // parallel for construct is (are) private.
4805 // The loop iteration variable in the associated for-loop of a simd
4806 // construct with just one associated for-loop is linear with a
4807 // constant-linear-step that is the increment of the associated for-loop.
4808 // Exclude loop var from the list of variables with implicitly defined data
4809 // sharing attributes.
4810 VarsWithImplicitDSA.erase(LCDecl);
4811
4812 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4813 // in a Construct, C/C++].
4814 // The loop iteration variable in the associated for-loop of a simd
4815 // construct with just one associated for-loop may be listed in a linear
4816 // clause with a constant-linear-step that is the increment of the
4817 // associated for-loop.
4818 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4819 // parallel for construct may be listed in a private or lastprivate clause.
4820 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4821 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4822 // declared in the loop and it is predetermined as a private.
Alexey Bataeve3727102018-04-18 15:57:46 +00004823 OpenMPClauseKind PredeterminedCKind =
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004824 isOpenMPSimdDirective(DKind)
4825 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4826 : OMPC_private;
4827 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4828 DVar.CKind != PredeterminedCKind) ||
4829 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4830 isOpenMPDistributeDirective(DKind)) &&
4831 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4832 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4833 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004834 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004835 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4836 << getOpenMPClauseName(PredeterminedCKind);
4837 if (DVar.RefExpr == nullptr)
4838 DVar.CKind = PredeterminedCKind;
Alexey Bataeve3727102018-04-18 15:57:46 +00004839 reportOriginalDsa(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004840 HasErrors = true;
4841 } else if (LoopDeclRefExpr != nullptr) {
4842 // Make the loop iteration variable private (for worksharing constructs),
4843 // linear (for simd directives with the only one associated loop) or
4844 // lastprivate (for simd directives with several collapsed or ordered
4845 // loops).
4846 if (DVar.CKind == OMPC_unknown)
Alexey Bataev7ace49d2016-05-17 08:55:33 +00004847 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate,
4848 [](OpenMPDirectiveKind) -> bool { return true; },
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004849 /*FromParent=*/false);
4850 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4851 }
4852
4853 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4854
4855 // Check test-expr.
Alexey Bataeve3727102018-04-18 15:57:46 +00004856 HasErrors |= ISC.checkAndSetCond(For->getCond());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004857
4858 // Check incr-expr.
Alexey Bataeve3727102018-04-18 15:57:46 +00004859 HasErrors |= ISC.checkAndSetInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004860 }
4861
Alexey Bataeve3727102018-04-18 15:57:46 +00004862 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004863 return HasErrors;
4864
Alexander Musmana5f070a2014-10-01 06:03:56 +00004865 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004866 ResultIterSpace.PreCond =
Alexey Bataeve3727102018-04-18 15:57:46 +00004867 ISC.buildPreCond(DSA.getCurScope(), For->getCond(), Captures);
4868 ResultIterSpace.NumIterations = ISC.buildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004869 DSA.getCurScope(),
4870 (isOpenMPWorksharingDirective(DKind) ||
4871 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4872 Captures);
Alexey Bataeve3727102018-04-18 15:57:46 +00004873 ResultIterSpace.CounterVar = ISC.buildCounterVar(Captures, DSA);
4874 ResultIterSpace.PrivateCounterVar = ISC.buildPrivateCounterVar();
4875 ResultIterSpace.CounterInit = ISC.buildCounterInit();
4876 ResultIterSpace.CounterStep = ISC.buildCounterStep();
4877 ResultIterSpace.InitSrcRange = ISC.getInitSrcRange();
4878 ResultIterSpace.CondSrcRange = ISC.getConditionSrcRange();
4879 ResultIterSpace.IncSrcRange = ISC.getIncrementSrcRange();
4880 ResultIterSpace.Subtract = ISC.shouldSubtractStep();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004881
Alexey Bataev62dbb972015-04-22 11:59:37 +00004882 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4883 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004884 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004885 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004886 ResultIterSpace.CounterInit == nullptr ||
4887 ResultIterSpace.CounterStep == nullptr);
Alexey Bataevf138fda2018-08-13 19:04:24 +00004888 if (!HasErrors && DSA.isOrderedRegion()) {
4889 if (DSA.getOrderedRegionParam().second->getNumForLoops()) {
4890 if (CurrentNestedLoopCount <
4891 DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) {
4892 DSA.getOrderedRegionParam().second->setLoopNumIterations(
4893 CurrentNestedLoopCount, ResultIterSpace.NumIterations);
4894 DSA.getOrderedRegionParam().second->setLoopCounter(
4895 CurrentNestedLoopCount, ResultIterSpace.CounterVar);
4896 }
4897 }
4898 for (auto &Pair : DSA.getDoacrossDependClauses()) {
4899 if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) {
4900 // Erroneous case - clause has some problems.
4901 continue;
4902 }
4903 if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink &&
4904 Pair.second.size() <= CurrentNestedLoopCount) {
4905 // Erroneous case - clause has some problems.
4906 Pair.first->setLoopData(CurrentNestedLoopCount, nullptr);
4907 continue;
4908 }
4909 Expr *CntValue;
4910 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
4911 CntValue = ISC.buildOrderedLoopData(
4912 DSA.getCurScope(), ResultIterSpace.CounterVar, Captures,
4913 Pair.first->getDependencyLoc());
4914 else
4915 CntValue = ISC.buildOrderedLoopData(
4916 DSA.getCurScope(), ResultIterSpace.CounterVar, Captures,
4917 Pair.first->getDependencyLoc(),
4918 Pair.second[CurrentNestedLoopCount].first,
4919 Pair.second[CurrentNestedLoopCount].second);
4920 Pair.first->setLoopData(CurrentNestedLoopCount, CntValue);
4921 }
4922 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004923
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004924 return HasErrors;
4925}
4926
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004927/// Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004928static ExprResult
Alexey Bataeve3727102018-04-18 15:57:46 +00004929buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004930 ExprResult Start,
Alexey Bataeve3727102018-04-18 15:57:46 +00004931 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004932 // Build 'VarRef = Start.
Alexey Bataeve3727102018-04-18 15:57:46 +00004933 ExprResult NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004934 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004935 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004936 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00004937 VarRef.get()->getType())) {
4938 NewStart = SemaRef.PerformImplicitConversion(
4939 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4940 /*AllowExplicit=*/true);
4941 if (!NewStart.isUsable())
4942 return ExprError();
4943 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004944
Alexey Bataeve3727102018-04-18 15:57:46 +00004945 ExprResult Init =
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004946 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4947 return Init;
4948}
4949
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004950/// Build 'VarRef = Start + Iter * Step'.
Alexey Bataeve3727102018-04-18 15:57:46 +00004951static ExprResult buildCounterUpdate(
4952 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4953 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract,
4954 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004955 // Add parentheses (for debugging purposes only).
4956 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4957 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4958 !Step.isUsable())
4959 return ExprError();
4960
Alexey Bataev5a3af132016-03-29 08:58:54 +00004961 ExprResult NewStep = Step;
4962 if (Captures)
4963 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004964 if (NewStep.isInvalid())
4965 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004966 ExprResult Update =
4967 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004968 if (!Update.isUsable())
4969 return ExprError();
4970
Alexey Bataevc0214e02016-02-16 12:13:49 +00004971 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4972 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004973 ExprResult NewStart = Start;
4974 if (Captures)
4975 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004976 if (NewStart.isInvalid())
4977 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004978
Alexey Bataevc0214e02016-02-16 12:13:49 +00004979 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4980 ExprResult SavedUpdate = Update;
4981 ExprResult UpdateVal;
4982 if (VarRef.get()->getType()->isOverloadableType() ||
4983 NewStart.get()->getType()->isOverloadableType() ||
4984 Update.get()->getType()->isOverloadableType()) {
4985 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4986 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4987 Update =
4988 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4989 if (Update.isUsable()) {
4990 UpdateVal =
4991 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4992 VarRef.get(), SavedUpdate.get());
4993 if (UpdateVal.isUsable()) {
4994 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4995 UpdateVal.get());
4996 }
4997 }
4998 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4999 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005000
Alexey Bataevc0214e02016-02-16 12:13:49 +00005001 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
5002 if (!Update.isUsable() || !UpdateVal.isUsable()) {
5003 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
5004 NewStart.get(), SavedUpdate.get());
5005 if (!Update.isUsable())
5006 return ExprError();
5007
Alexey Bataev11481f52016-02-17 10:29:05 +00005008 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
5009 VarRef.get()->getType())) {
5010 Update = SemaRef.PerformImplicitConversion(
5011 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
5012 if (!Update.isUsable())
5013 return ExprError();
5014 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00005015
5016 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
5017 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005018 return Update;
5019}
5020
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005021/// Convert integer expression \a E to make it have at least \a Bits
Alexander Musmana5f070a2014-10-01 06:03:56 +00005022/// bits.
Alexey Bataeve3727102018-04-18 15:57:46 +00005023static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00005024 if (E == nullptr)
5025 return ExprError();
Alexey Bataeve3727102018-04-18 15:57:46 +00005026 ASTContext &C = SemaRef.Context;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005027 QualType OldType = E->getType();
5028 unsigned HasBits = C.getTypeSize(OldType);
5029 if (HasBits >= Bits)
5030 return ExprResult(E);
5031 // OK to convert to signed, because new type has more bits than old.
5032 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
5033 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
5034 true);
5035}
5036
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005037/// Check if the given expression \a E is a constant integer that fits
Alexander Musmana5f070a2014-10-01 06:03:56 +00005038/// into \a Bits bits.
Alexey Bataeve3727102018-04-18 15:57:46 +00005039static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00005040 if (E == nullptr)
5041 return false;
5042 llvm::APSInt Result;
5043 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
5044 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
5045 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005046}
5047
Alexey Bataev5a3af132016-03-29 08:58:54 +00005048/// Build preinits statement for the given declarations.
5049static Stmt *buildPreInits(ASTContext &Context,
Alexey Bataevc5514062017-10-25 15:44:52 +00005050 MutableArrayRef<Decl *> PreInits) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005051 if (!PreInits.empty()) {
5052 return new (Context) DeclStmt(
5053 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
5054 SourceLocation(), SourceLocation());
5055 }
5056 return nullptr;
5057}
5058
5059/// Build preinits statement for the given declarations.
Alexey Bataevc5514062017-10-25 15:44:52 +00005060static Stmt *
5061buildPreInits(ASTContext &Context,
Alexey Bataeve3727102018-04-18 15:57:46 +00005062 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005063 if (!Captures.empty()) {
5064 SmallVector<Decl *, 16> PreInits;
Alexey Bataeve3727102018-04-18 15:57:46 +00005065 for (const auto &Pair : Captures)
Alexey Bataev5a3af132016-03-29 08:58:54 +00005066 PreInits.push_back(Pair.second->getDecl());
5067 return buildPreInits(Context, PreInits);
5068 }
5069 return nullptr;
5070}
5071
5072/// Build postupdate expression for the given list of postupdates expressions.
5073static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
5074 Expr *PostUpdate = nullptr;
5075 if (!PostUpdates.empty()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00005076 for (Expr *E : PostUpdates) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005077 Expr *ConvE = S.BuildCStyleCastExpr(
5078 E->getExprLoc(),
5079 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
5080 E->getExprLoc(), E)
5081 .get();
5082 PostUpdate = PostUpdate
5083 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
5084 PostUpdate, ConvE)
5085 .get()
5086 : ConvE;
5087 }
5088 }
5089 return PostUpdate;
5090}
5091
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005092/// Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00005093/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
5094/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005095static unsigned
Alexey Bataeve3727102018-04-18 15:57:46 +00005096checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
Alexey Bataev10e775f2015-07-30 11:36:16 +00005097 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
5098 DSAStackTy &DSA,
Alexey Bataeve3727102018-04-18 15:57:46 +00005099 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00005100 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005101 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005102 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005103 // Found 'collapse' clause - calculate collapse number.
Fangrui Song407659a2018-11-30 23:41:18 +00005104 Expr::EvalResult Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005105 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Fangrui Song407659a2018-11-30 23:41:18 +00005106 NestedLoopCount = Result.Val.getInt().getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005107 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00005108 unsigned OrderedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005109 if (OrderedLoopCountExpr) {
5110 // Found 'ordered' clause - calculate collapse number.
Fangrui Song407659a2018-11-30 23:41:18 +00005111 Expr::EvalResult EVResult;
5112 if (OrderedLoopCountExpr->EvaluateAsInt(EVResult, SemaRef.getASTContext())) {
5113 llvm::APSInt Result = EVResult.Val.getInt();
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005114 if (Result.getLimitedValue() < NestedLoopCount) {
5115 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
5116 diag::err_omp_wrong_ordered_loop_count)
5117 << OrderedLoopCountExpr->getSourceRange();
5118 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
5119 diag::note_collapse_loop_count)
5120 << CollapseLoopCountExpr->getSourceRange();
5121 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00005122 OrderedLoopCount = Result.getLimitedValue();
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005123 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005124 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005125 // This is helper routine for loop directives (e.g., 'for', 'simd',
5126 // 'for simd', etc.).
Alexey Bataeve3727102018-04-18 15:57:46 +00005127 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005128 SmallVector<LoopIterationSpace, 4> IterSpaces;
Alexey Bataevf138fda2018-08-13 19:04:24 +00005129 IterSpaces.resize(std::max(OrderedLoopCount, NestedLoopCount));
Alexander Musmana5f070a2014-10-01 06:03:56 +00005130 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005131 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataevf138fda2018-08-13 19:04:24 +00005132 if (checkOpenMPIterationSpace(
5133 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
5134 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
5135 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt],
5136 Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00005137 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005138 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005139 // OpenMP [2.8.1, simd construct, Restrictions]
5140 // All loops associated with the construct must be perfectly nested; that
5141 // is, there must be no intervening code nor any OpenMP directive between
5142 // any two loops.
5143 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005144 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00005145 for (unsigned Cnt = NestedLoopCount; Cnt < OrderedLoopCount; ++Cnt) {
5146 if (checkOpenMPIterationSpace(
5147 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
5148 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
5149 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt],
5150 Captures))
5151 return 0;
5152 if (Cnt > 0 && IterSpaces[Cnt].CounterVar) {
5153 // Handle initialization of captured loop iterator variables.
5154 auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar);
5155 if (isa<OMPCapturedExprDecl>(DRE->getDecl())) {
5156 Captures[DRE] = DRE;
5157 }
5158 }
5159 // Move on to the next nested for loop, or to the loop body.
5160 // OpenMP [2.8.1, simd construct, Restrictions]
5161 // All loops associated with the construct must be perfectly nested; that
5162 // is, there must be no intervening code nor any OpenMP directive between
5163 // any two loops.
5164 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
5165 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005166
Alexander Musmana5f070a2014-10-01 06:03:56 +00005167 Built.clear(/* size */ NestedLoopCount);
5168
5169 if (SemaRef.CurContext->isDependentContext())
5170 return NestedLoopCount;
5171
5172 // An example of what is generated for the following code:
5173 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00005174 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00005175 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00005176 // for (k = 0; k < NK; ++k)
5177 // for (j = J0; j < NJ; j+=2) {
5178 // <loop body>
5179 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005180 //
5181 // We generate the code below.
5182 // Note: the loop body may be outlined in CodeGen.
5183 // Note: some counters may be C++ classes, operator- is used to find number of
5184 // iterations and operator+= to calculate counter value.
5185 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
5186 // or i64 is currently supported).
5187 //
5188 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
5189 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
5190 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
5191 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
5192 // // similar updates for vars in clauses (e.g. 'linear')
5193 // <loop body (using local i and j)>
5194 // }
5195 // i = NI; // assign final values of counters
5196 // j = NJ;
5197 //
5198
5199 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
5200 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00005201 // Precondition tests if there is at least one iteration (all conditions are
5202 // true).
5203 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexey Bataeve3727102018-04-18 15:57:46 +00005204 Expr *N0 = IterSpaces[0].NumIterations;
5205 ExprResult LastIteration32 =
5206 widenIterationCount(/*Bits=*/32,
5207 SemaRef
5208 .PerformImplicitConversion(
5209 N0->IgnoreImpCasts(), N0->getType(),
5210 Sema::AA_Converting, /*AllowExplicit=*/true)
5211 .get(),
5212 SemaRef);
5213 ExprResult LastIteration64 = widenIterationCount(
5214 /*Bits=*/64,
5215 SemaRef
5216 .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(),
5217 Sema::AA_Converting,
5218 /*AllowExplicit=*/true)
5219 .get(),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005220 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005221
5222 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
5223 return NestedLoopCount;
5224
Alexey Bataeve3727102018-04-18 15:57:46 +00005225 ASTContext &C = SemaRef.Context;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005226 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
5227
5228 Scope *CurScope = DSA.getCurScope();
5229 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00005230 if (PreCond.isUsable()) {
Alexey Bataeva7206b92016-12-20 16:51:02 +00005231 PreCond =
5232 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd,
5233 PreCond.get(), IterSpaces[Cnt].PreCond);
Alexey Bataev62dbb972015-04-22 11:59:37 +00005234 }
Alexey Bataeve3727102018-04-18 15:57:46 +00005235 Expr *N = IterSpaces[Cnt].NumIterations;
Alexey Bataeva7206b92016-12-20 16:51:02 +00005236 SourceLocation Loc = N->getExprLoc();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005237 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
5238 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005239 LastIteration32 = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005240 CurScope, Loc, BO_Mul, LastIteration32.get(),
David Majnemer9d168222016-08-05 17:44:54 +00005241 SemaRef
5242 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5243 Sema::AA_Converting,
5244 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005245 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005246 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005247 LastIteration64 = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005248 CurScope, Loc, BO_Mul, LastIteration64.get(),
David Majnemer9d168222016-08-05 17:44:54 +00005249 SemaRef
5250 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5251 Sema::AA_Converting,
5252 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005253 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005254 }
5255
5256 // Choose either the 32-bit or 64-bit version.
5257 ExprResult LastIteration = LastIteration64;
5258 if (LastIteration32.isUsable() &&
5259 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
5260 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
Alexey Bataeve3727102018-04-18 15:57:46 +00005261 fitsInto(
5262 /*Bits=*/32,
Alexander Musmana5f070a2014-10-01 06:03:56 +00005263 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
5264 LastIteration64.get(), SemaRef)))
5265 LastIteration = LastIteration32;
Alexey Bataev7292c292016-04-25 12:22:29 +00005266 QualType VType = LastIteration.get()->getType();
5267 QualType RealVType = VType;
5268 QualType StrideVType = VType;
5269 if (isOpenMPTaskLoopDirective(DKind)) {
5270 VType =
5271 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
5272 StrideVType =
5273 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
5274 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005275
5276 if (!LastIteration.isUsable())
5277 return 0;
5278
5279 // Save the number of iterations.
5280 ExprResult NumIterations = LastIteration;
5281 {
5282 LastIteration = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005283 CurScope, LastIteration.get()->getExprLoc(), BO_Sub,
5284 LastIteration.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00005285 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5286 if (!LastIteration.isUsable())
5287 return 0;
5288 }
5289
5290 // Calculate the last iteration number beforehand instead of doing this on
5291 // each iteration. Do not do this if the number of iterations may be kfold-ed.
5292 llvm::APSInt Result;
5293 bool IsConstant =
5294 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
5295 ExprResult CalcLastIteration;
5296 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005297 ExprResult SaveRef =
5298 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005299 LastIteration = SaveRef;
5300
5301 // Prepare SaveRef + 1.
5302 NumIterations = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005303 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00005304 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5305 if (!NumIterations.isUsable())
5306 return 0;
5307 }
5308
5309 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
5310
David Majnemer9d168222016-08-05 17:44:54 +00005311 // Build variables passed into runtime, necessary for worksharing directives.
Carlo Bertolliffafe102017-04-20 00:39:39 +00005312 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005313 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5314 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005315 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005316 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
5317 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005318 SemaRef.AddInitializerToDecl(LBDecl,
5319 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5320 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005321
5322 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005323 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
5324 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005325 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
Richard Smith3beb7c62017-01-12 02:27:38 +00005326 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005327
5328 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
5329 // This will be used to implement clause 'lastprivate'.
5330 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005331 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
5332 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005333 SemaRef.AddInitializerToDecl(ILDecl,
5334 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5335 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005336
5337 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev7292c292016-04-25 12:22:29 +00005338 VarDecl *STDecl =
5339 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
5340 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005341 SemaRef.AddInitializerToDecl(STDecl,
5342 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
5343 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005344
5345 // Build expression: UB = min(UB, LastIteration)
David Majnemer9d168222016-08-05 17:44:54 +00005346 // It is necessary for CodeGen of directives with static scheduling.
Alexander Musmanc6388682014-12-15 07:07:06 +00005347 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
5348 UB.get(), LastIteration.get());
5349 ExprResult CondOp = SemaRef.ActOnConditionalOp(
Alexey Bataev86ec3fe2018-07-25 14:40:26 +00005350 LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(),
5351 LastIteration.get(), UB.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00005352 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
5353 CondOp.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005354 EUB = SemaRef.ActOnFinishFullExpr(EUB.get(), /*DiscardedValue*/ false);
Carlo Bertolli9925f152016-06-27 14:55:37 +00005355
5356 // If we have a combined directive that combines 'distribute', 'for' or
5357 // 'simd' we need to be able to access the bounds of the schedule of the
5358 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
5359 // by scheduling 'distribute' have to be passed to the schedule of 'for'.
5360 if (isOpenMPLoopBoundSharingDirective(DKind)) {
Carlo Bertolliffafe102017-04-20 00:39:39 +00005361 // Lower bound variable, initialized with zero.
5362 VarDecl *CombLBDecl =
5363 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb");
5364 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc);
5365 SemaRef.AddInitializerToDecl(
5366 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5367 /*DirectInit*/ false);
5368
5369 // Upper bound variable, initialized with last iteration number.
5370 VarDecl *CombUBDecl =
5371 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub");
5372 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc);
5373 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(),
5374 /*DirectInit*/ false);
5375
5376 ExprResult CombIsUBGreater = SemaRef.BuildBinOp(
5377 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get());
5378 ExprResult CombCondOp =
5379 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(),
5380 LastIteration.get(), CombUB.get());
5381 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(),
5382 CombCondOp.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005383 CombEUB =
5384 SemaRef.ActOnFinishFullExpr(CombEUB.get(), /*DiscardedValue*/ false);
Carlo Bertolliffafe102017-04-20 00:39:39 +00005385
Alexey Bataeve3727102018-04-18 15:57:46 +00005386 const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005387 // We expect to have at least 2 more parameters than the 'parallel'
5388 // directive does - the lower and upper bounds of the previous schedule.
5389 assert(CD->getNumParams() >= 4 &&
5390 "Unexpected number of parameters in loop combined directive");
5391
5392 // Set the proper type for the bounds given what we learned from the
5393 // enclosed loops.
Alexey Bataeve3727102018-04-18 15:57:46 +00005394 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
5395 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
Carlo Bertolli9925f152016-06-27 14:55:37 +00005396
5397 // Previous lower and upper bounds are obtained from the region
5398 // parameters.
5399 PrevLB =
5400 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
5401 PrevUB =
5402 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
5403 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005404 }
5405
5406 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005407 ExprResult IV;
Carlo Bertolliffafe102017-04-20 00:39:39 +00005408 ExprResult Init, CombInit;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005409 {
Alexey Bataev7292c292016-04-25 12:22:29 +00005410 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
5411 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
David Majnemer9d168222016-08-05 17:44:54 +00005412 Expr *RHS =
5413 (isOpenMPWorksharingDirective(DKind) ||
5414 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
5415 ? LB.get()
5416 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005417 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005418 Init = SemaRef.ActOnFinishFullExpr(Init.get(), /*DiscardedValue*/ false);
Carlo Bertolliffafe102017-04-20 00:39:39 +00005419
5420 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5421 Expr *CombRHS =
5422 (isOpenMPWorksharingDirective(DKind) ||
5423 isOpenMPTaskLoopDirective(DKind) ||
5424 isOpenMPDistributeDirective(DKind))
5425 ? CombLB.get()
5426 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
5427 CombInit =
5428 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS);
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005429 CombInit =
5430 SemaRef.ActOnFinishFullExpr(CombInit.get(), /*DiscardedValue*/ false);
Carlo Bertolliffafe102017-04-20 00:39:39 +00005431 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005432 }
5433
Alexander Musmanc6388682014-12-15 07:07:06 +00005434 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005435 SourceLocation CondLoc = AStmt->getBeginLoc();
Alexander Musmanc6388682014-12-15 07:07:06 +00005436 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005437 (isOpenMPWorksharingDirective(DKind) ||
5438 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00005439 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
5440 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
5441 NumIterations.get());
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005442 ExprResult CombDistCond;
5443 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5444 CombDistCond =
Gheorghe-Teodor Bercea9d6341f2018-10-29 19:44:25 +00005445 SemaRef.BuildBinOp(
5446 CurScope, CondLoc, BO_LT, IV.get(), NumIterations.get());
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005447 }
5448
Carlo Bertolliffafe102017-04-20 00:39:39 +00005449 ExprResult CombCond;
5450 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5451 CombCond =
5452 SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), CombUB.get());
5453 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005454 // Loop increment (IV = IV + 1)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005455 SourceLocation IncLoc = AStmt->getBeginLoc();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005456 ExprResult Inc =
5457 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
5458 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
5459 if (!Inc.isUsable())
5460 return 0;
5461 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005462 Inc = SemaRef.ActOnFinishFullExpr(Inc.get(), /*DiscardedValue*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005463 if (!Inc.isUsable())
5464 return 0;
5465
5466 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
5467 // Used for directives with static scheduling.
Carlo Bertolliffafe102017-04-20 00:39:39 +00005468 // In combined construct, add combined version that use CombLB and CombUB
5469 // base variables for the update
5470 ExprResult NextLB, NextUB, CombNextLB, CombNextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005471 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5472 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005473 // LB + ST
5474 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
5475 if (!NextLB.isUsable())
5476 return 0;
5477 // LB = LB + ST
5478 NextLB =
5479 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005480 NextLB =
5481 SemaRef.ActOnFinishFullExpr(NextLB.get(), /*DiscardedValue*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005482 if (!NextLB.isUsable())
5483 return 0;
5484 // UB + ST
5485 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
5486 if (!NextUB.isUsable())
5487 return 0;
5488 // UB = UB + ST
5489 NextUB =
5490 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005491 NextUB =
5492 SemaRef.ActOnFinishFullExpr(NextUB.get(), /*DiscardedValue*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005493 if (!NextUB.isUsable())
5494 return 0;
Carlo Bertolliffafe102017-04-20 00:39:39 +00005495 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5496 CombNextLB =
5497 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get());
5498 if (!NextLB.isUsable())
5499 return 0;
5500 // LB = LB + ST
5501 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(),
5502 CombNextLB.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005503 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get(),
5504 /*DiscardedValue*/ false);
Carlo Bertolliffafe102017-04-20 00:39:39 +00005505 if (!CombNextLB.isUsable())
5506 return 0;
5507 // UB + ST
5508 CombNextUB =
5509 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get());
5510 if (!CombNextUB.isUsable())
5511 return 0;
5512 // UB = UB + ST
5513 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(),
5514 CombNextUB.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005515 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get(),
5516 /*DiscardedValue*/ false);
Carlo Bertolliffafe102017-04-20 00:39:39 +00005517 if (!CombNextUB.isUsable())
5518 return 0;
5519 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005520 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005521
Carlo Bertolliffafe102017-04-20 00:39:39 +00005522 // Create increment expression for distribute loop when combined in a same
Carlo Bertolli8429d812017-02-17 21:29:13 +00005523 // directive with for as IV = IV + ST; ensure upper bound expression based
5524 // on PrevUB instead of NumIterations - used to implement 'for' when found
5525 // in combination with 'distribute', like in 'distribute parallel for'
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005526 SourceLocation DistIncLoc = AStmt->getBeginLoc();
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005527 ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond;
Carlo Bertolli8429d812017-02-17 21:29:13 +00005528 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5529 DistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get());
5530 assert(DistCond.isUsable() && "distribute cond expr was not built");
5531
5532 DistInc =
5533 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get());
5534 assert(DistInc.isUsable() && "distribute inc expr was not built");
5535 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(),
5536 DistInc.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005537 DistInc =
5538 SemaRef.ActOnFinishFullExpr(DistInc.get(), /*DiscardedValue*/ false);
Carlo Bertolli8429d812017-02-17 21:29:13 +00005539 assert(DistInc.isUsable() && "distribute inc expr was not built");
5540
5541 // Build expression: UB = min(UB, prevUB) for #for in composite or combined
5542 // construct
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005543 SourceLocation DistEUBLoc = AStmt->getBeginLoc();
Carlo Bertolli8429d812017-02-17 21:29:13 +00005544 ExprResult IsUBGreater =
5545 SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get());
5546 ExprResult CondOp = SemaRef.ActOnConditionalOp(
5547 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get());
5548 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(),
5549 CondOp.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005550 PrevEUB =
5551 SemaRef.ActOnFinishFullExpr(PrevEUB.get(), /*DiscardedValue*/ false);
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005552
5553 // Build IV <= PrevUB to be used in parallel for is in combination with
5554 // a distribute directive with schedule(static, 1)
5555 ParForInDistCond =
5556 SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), PrevUB.get());
Carlo Bertolli8429d812017-02-17 21:29:13 +00005557 }
5558
Alexander Musmana5f070a2014-10-01 06:03:56 +00005559 // Build updates and final values of the loop counters.
5560 bool HasErrors = false;
5561 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005562 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005563 Built.Updates.resize(NestedLoopCount);
5564 Built.Finals.resize(NestedLoopCount);
5565 {
5566 ExprResult Div;
5567 // Go from inner nested loop to outer.
5568 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5569 LoopIterationSpace &IS = IterSpaces[Cnt];
5570 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
5571 // Build: Iter = (IV / Div) % IS.NumIters
5572 // where Div is product of previous iterations' IS.NumIters.
5573 ExprResult Iter;
5574 if (Div.isUsable()) {
5575 Iter =
5576 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
5577 } else {
5578 Iter = IV;
5579 assert((Cnt == (int)NestedLoopCount - 1) &&
5580 "unusable div expected on first iteration only");
5581 }
5582
5583 if (Cnt != 0 && Iter.isUsable())
5584 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
5585 IS.NumIterations);
5586 if (!Iter.isUsable()) {
5587 HasErrors = true;
5588 break;
5589 }
5590
Alexey Bataev39f915b82015-05-08 10:41:21 +00005591 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005592 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00005593 DeclRefExpr *CounterVar = buildDeclRefExpr(
5594 SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
5595 /*RefersToCapture=*/true);
5596 ExprResult Init = buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005597 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005598 if (!Init.isUsable()) {
5599 HasErrors = true;
5600 break;
5601 }
Alexey Bataeve3727102018-04-18 15:57:46 +00005602 ExprResult Update = buildCounterUpdate(
Alexey Bataev5a3af132016-03-29 08:58:54 +00005603 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
5604 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005605 if (!Update.isUsable()) {
5606 HasErrors = true;
5607 break;
5608 }
5609
5610 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
Alexey Bataeve3727102018-04-18 15:57:46 +00005611 ExprResult Final = buildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00005612 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005613 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005614 if (!Final.isUsable()) {
5615 HasErrors = true;
5616 break;
5617 }
5618
5619 // Build Div for the next iteration: Div <- Div * IS.NumIters
5620 if (Cnt != 0) {
5621 if (Div.isUnset())
5622 Div = IS.NumIterations;
5623 else
5624 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
5625 IS.NumIterations);
5626
5627 // Add parentheses (for debugging purposes only).
5628 if (Div.isUsable())
Alexey Bataev8b427062016-05-25 12:36:08 +00005629 Div = tryBuildCapture(SemaRef, Div.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005630 if (!Div.isUsable()) {
5631 HasErrors = true;
5632 break;
5633 }
5634 }
5635 if (!Update.isUsable() || !Final.isUsable()) {
5636 HasErrors = true;
5637 break;
5638 }
5639 // Save results
5640 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00005641 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005642 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005643 Built.Updates[Cnt] = Update.get();
5644 Built.Finals[Cnt] = Final.get();
5645 }
5646 }
5647
5648 if (HasErrors)
5649 return 0;
5650
5651 // Save results
5652 Built.IterationVarRef = IV.get();
5653 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00005654 Built.NumIterations = NumIterations.get();
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005655 Built.CalcLastIteration = SemaRef
5656 .ActOnFinishFullExpr(CalcLastIteration.get(),
5657 /*DiscardedValue*/ false)
5658 .get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005659 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00005660 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005661 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005662 Built.Init = Init.get();
5663 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005664 Built.LB = LB.get();
5665 Built.UB = UB.get();
5666 Built.IL = IL.get();
5667 Built.ST = ST.get();
5668 Built.EUB = EUB.get();
5669 Built.NLB = NextLB.get();
5670 Built.NUB = NextUB.get();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005671 Built.PrevLB = PrevLB.get();
5672 Built.PrevUB = PrevUB.get();
Carlo Bertolli8429d812017-02-17 21:29:13 +00005673 Built.DistInc = DistInc.get();
5674 Built.PrevEUB = PrevEUB.get();
Carlo Bertolliffafe102017-04-20 00:39:39 +00005675 Built.DistCombinedFields.LB = CombLB.get();
5676 Built.DistCombinedFields.UB = CombUB.get();
5677 Built.DistCombinedFields.EUB = CombEUB.get();
5678 Built.DistCombinedFields.Init = CombInit.get();
5679 Built.DistCombinedFields.Cond = CombCond.get();
5680 Built.DistCombinedFields.NLB = CombNextLB.get();
5681 Built.DistCombinedFields.NUB = CombNextUB.get();
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005682 Built.DistCombinedFields.DistCond = CombDistCond.get();
5683 Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005684
Alexey Bataevabfc0692014-06-25 06:52:00 +00005685 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005686}
5687
Alexey Bataev10e775f2015-07-30 11:36:16 +00005688static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005689 auto CollapseClauses =
5690 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
5691 if (CollapseClauses.begin() != CollapseClauses.end())
5692 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005693 return nullptr;
5694}
5695
Alexey Bataev10e775f2015-07-30 11:36:16 +00005696static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005697 auto OrderedClauses =
5698 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
5699 if (OrderedClauses.begin() != OrderedClauses.end())
5700 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005701 return nullptr;
5702}
5703
Kelvin Lic5609492016-07-15 04:39:07 +00005704static bool checkSimdlenSafelenSpecified(Sema &S,
5705 const ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00005706 const OMPSafelenClause *Safelen = nullptr;
5707 const OMPSimdlenClause *Simdlen = nullptr;
Kelvin Lic5609492016-07-15 04:39:07 +00005708
Alexey Bataeve3727102018-04-18 15:57:46 +00005709 for (const OMPClause *Clause : Clauses) {
Kelvin Lic5609492016-07-15 04:39:07 +00005710 if (Clause->getClauseKind() == OMPC_safelen)
5711 Safelen = cast<OMPSafelenClause>(Clause);
5712 else if (Clause->getClauseKind() == OMPC_simdlen)
5713 Simdlen = cast<OMPSimdlenClause>(Clause);
5714 if (Safelen && Simdlen)
5715 break;
5716 }
5717
5718 if (Simdlen && Safelen) {
Alexey Bataeve3727102018-04-18 15:57:46 +00005719 const Expr *SimdlenLength = Simdlen->getSimdlen();
5720 const Expr *SafelenLength = Safelen->getSafelen();
Kelvin Lic5609492016-07-15 04:39:07 +00005721 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
5722 SimdlenLength->isInstantiationDependent() ||
5723 SimdlenLength->containsUnexpandedParameterPack())
5724 return false;
5725 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
5726 SafelenLength->isInstantiationDependent() ||
5727 SafelenLength->containsUnexpandedParameterPack())
5728 return false;
Fangrui Song407659a2018-11-30 23:41:18 +00005729 Expr::EvalResult SimdlenResult, SafelenResult;
5730 SimdlenLength->EvaluateAsInt(SimdlenResult, S.Context);
5731 SafelenLength->EvaluateAsInt(SafelenResult, S.Context);
5732 llvm::APSInt SimdlenRes = SimdlenResult.Val.getInt();
5733 llvm::APSInt SafelenRes = SafelenResult.Val.getInt();
Kelvin Lic5609492016-07-15 04:39:07 +00005734 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
5735 // If both simdlen and safelen clauses are specified, the value of the
5736 // simdlen parameter must be less than or equal to the value of the safelen
5737 // parameter.
5738 if (SimdlenRes > SafelenRes) {
5739 S.Diag(SimdlenLength->getExprLoc(),
5740 diag::err_omp_wrong_simdlen_safelen_values)
5741 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
5742 return true;
5743 }
Alexey Bataev66b15b52015-08-21 11:14:16 +00005744 }
5745 return false;
5746}
5747
Alexey Bataeve3727102018-04-18 15:57:46 +00005748StmtResult
5749Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
5750 SourceLocation StartLoc, SourceLocation EndLoc,
5751 VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005752 if (!AStmt)
5753 return StmtError();
5754
5755 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005756 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005757 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5758 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00005759 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataev10e775f2015-07-30 11:36:16 +00005760 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5761 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005762 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005763 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005764
Alexander Musmana5f070a2014-10-01 06:03:56 +00005765 assert((CurContext->isDependentContext() || B.builtAll()) &&
5766 "omp simd loop exprs were not built");
5767
Alexander Musman3276a272015-03-21 10:12:56 +00005768 if (!CurContext->isDependentContext()) {
5769 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005770 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005771 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexander Musman3276a272015-03-21 10:12:56 +00005772 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005773 B.NumIterations, *this, CurScope,
5774 DSAStack))
Alexander Musman3276a272015-03-21 10:12:56 +00005775 return StmtError();
5776 }
5777 }
5778
Kelvin Lic5609492016-07-15 04:39:07 +00005779 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005780 return StmtError();
5781
Reid Kleckner87a31802018-03-12 21:43:02 +00005782 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005783 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5784 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005785}
5786
Alexey Bataeve3727102018-04-18 15:57:46 +00005787StmtResult
5788Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
5789 SourceLocation StartLoc, SourceLocation EndLoc,
5790 VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005791 if (!AStmt)
5792 return StmtError();
5793
5794 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005795 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005796 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5797 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00005798 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataev10e775f2015-07-30 11:36:16 +00005799 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5800 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005801 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005802 return StmtError();
5803
Alexander Musmana5f070a2014-10-01 06:03:56 +00005804 assert((CurContext->isDependentContext() || B.builtAll()) &&
5805 "omp for loop exprs were not built");
5806
Alexey Bataev54acd402015-08-04 11:18:19 +00005807 if (!CurContext->isDependentContext()) {
5808 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005809 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005810 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00005811 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005812 B.NumIterations, *this, CurScope,
5813 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005814 return StmtError();
5815 }
5816 }
5817
Reid Kleckner87a31802018-03-12 21:43:02 +00005818 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005819 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005820 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005821}
5822
Alexander Musmanf82886e2014-09-18 05:12:34 +00005823StmtResult Sema::ActOnOpenMPForSimdDirective(
5824 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00005825 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005826 if (!AStmt)
5827 return StmtError();
5828
5829 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005830 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005831 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5832 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00005833 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00005834 checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00005835 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5836 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005837 if (NestedLoopCount == 0)
5838 return StmtError();
5839
Alexander Musmanc6388682014-12-15 07:07:06 +00005840 assert((CurContext->isDependentContext() || B.builtAll()) &&
5841 "omp for simd loop exprs were not built");
5842
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005843 if (!CurContext->isDependentContext()) {
5844 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005845 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005846 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005847 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005848 B.NumIterations, *this, CurScope,
5849 DSAStack))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005850 return StmtError();
5851 }
5852 }
5853
Kelvin Lic5609492016-07-15 04:39:07 +00005854 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005855 return StmtError();
5856
Reid Kleckner87a31802018-03-12 21:43:02 +00005857 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005858 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5859 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005860}
5861
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005862StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5863 Stmt *AStmt,
5864 SourceLocation StartLoc,
5865 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005866 if (!AStmt)
5867 return StmtError();
5868
5869 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005870 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00005871 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005872 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00005873 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005874 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005875 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005876 return StmtError();
5877 // All associated statements must be '#pragma omp section' except for
5878 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005879 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005880 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5881 if (SectionStmt)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005882 Diag(SectionStmt->getBeginLoc(),
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005883 diag::err_omp_sections_substmt_not_section);
5884 return StmtError();
5885 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005886 cast<OMPSectionDirective>(SectionStmt)
5887 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005888 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005889 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005890 Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005891 return StmtError();
5892 }
5893
Reid Kleckner87a31802018-03-12 21:43:02 +00005894 setFunctionHasBranchProtectedScope();
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005895
Alexey Bataev25e5b442015-09-15 12:52:43 +00005896 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5897 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005898}
5899
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005900StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5901 SourceLocation StartLoc,
5902 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005903 if (!AStmt)
5904 return StmtError();
5905
5906 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005907
Reid Kleckner87a31802018-03-12 21:43:02 +00005908 setFunctionHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00005909 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005910
Alexey Bataev25e5b442015-09-15 12:52:43 +00005911 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5912 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005913}
5914
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005915StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5916 Stmt *AStmt,
5917 SourceLocation StartLoc,
5918 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005919 if (!AStmt)
5920 return StmtError();
5921
5922 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00005923
Reid Kleckner87a31802018-03-12 21:43:02 +00005924 setFunctionHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00005925
Alexey Bataev3255bf32015-01-19 05:20:46 +00005926 // OpenMP [2.7.3, single Construct, Restrictions]
5927 // The copyprivate clause must not be used with the nowait clause.
Alexey Bataeve3727102018-04-18 15:57:46 +00005928 const OMPClause *Nowait = nullptr;
5929 const OMPClause *Copyprivate = nullptr;
5930 for (const OMPClause *Clause : Clauses) {
Alexey Bataev3255bf32015-01-19 05:20:46 +00005931 if (Clause->getClauseKind() == OMPC_nowait)
5932 Nowait = Clause;
5933 else if (Clause->getClauseKind() == OMPC_copyprivate)
5934 Copyprivate = Clause;
5935 if (Copyprivate && Nowait) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005936 Diag(Copyprivate->getBeginLoc(),
Alexey Bataev3255bf32015-01-19 05:20:46 +00005937 diag::err_omp_single_copyprivate_with_nowait);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005938 Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here);
Alexey Bataev3255bf32015-01-19 05:20:46 +00005939 return StmtError();
5940 }
5941 }
5942
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005943 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5944}
5945
Alexander Musman80c22892014-07-17 08:54:58 +00005946StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5947 SourceLocation StartLoc,
5948 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005949 if (!AStmt)
5950 return StmtError();
5951
5952 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00005953
Reid Kleckner87a31802018-03-12 21:43:02 +00005954 setFunctionHasBranchProtectedScope();
Alexander Musman80c22892014-07-17 08:54:58 +00005955
5956 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5957}
5958
Alexey Bataev28c75412015-12-15 08:19:24 +00005959StmtResult Sema::ActOnOpenMPCriticalDirective(
5960 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5961 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005962 if (!AStmt)
5963 return StmtError();
5964
5965 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005966
Alexey Bataev28c75412015-12-15 08:19:24 +00005967 bool ErrorFound = false;
5968 llvm::APSInt Hint;
5969 SourceLocation HintLoc;
5970 bool DependentHint = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00005971 for (const OMPClause *C : Clauses) {
Alexey Bataev28c75412015-12-15 08:19:24 +00005972 if (C->getClauseKind() == OMPC_hint) {
5973 if (!DirName.getName()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005974 Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name);
Alexey Bataev28c75412015-12-15 08:19:24 +00005975 ErrorFound = true;
5976 }
5977 Expr *E = cast<OMPHintClause>(C)->getHint();
5978 if (E->isTypeDependent() || E->isValueDependent() ||
Alexey Bataeve3727102018-04-18 15:57:46 +00005979 E->isInstantiationDependent()) {
Alexey Bataev28c75412015-12-15 08:19:24 +00005980 DependentHint = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00005981 } else {
Alexey Bataev28c75412015-12-15 08:19:24 +00005982 Hint = E->EvaluateKnownConstInt(Context);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005983 HintLoc = C->getBeginLoc();
Alexey Bataev28c75412015-12-15 08:19:24 +00005984 }
5985 }
5986 }
5987 if (ErrorFound)
5988 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00005989 const auto Pair = DSAStack->getCriticalWithHint(DirName);
Alexey Bataev28c75412015-12-15 08:19:24 +00005990 if (Pair.first && DirName.getName() && !DependentHint) {
5991 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5992 Diag(StartLoc, diag::err_omp_critical_with_hint);
Alexey Bataeve3727102018-04-18 15:57:46 +00005993 if (HintLoc.isValid())
Alexey Bataev28c75412015-12-15 08:19:24 +00005994 Diag(HintLoc, diag::note_omp_critical_hint_here)
5995 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +00005996 else
Alexey Bataev28c75412015-12-15 08:19:24 +00005997 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
Alexey Bataeve3727102018-04-18 15:57:46 +00005998 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005999 Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here)
Alexey Bataev28c75412015-12-15 08:19:24 +00006000 << 1
6001 << C->getHint()->EvaluateKnownConstInt(Context).toString(
6002 /*Radix=*/10, /*Signed=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +00006003 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006004 Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1;
Alexey Bataeve3727102018-04-18 15:57:46 +00006005 }
Alexey Bataev28c75412015-12-15 08:19:24 +00006006 }
6007 }
6008
Reid Kleckner87a31802018-03-12 21:43:02 +00006009 setFunctionHasBranchProtectedScope();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00006010
Alexey Bataev28c75412015-12-15 08:19:24 +00006011 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
6012 Clauses, AStmt);
6013 if (!Pair.first && DirName.getName() && !DependentHint)
6014 DSAStack->addCriticalWithHint(Dir, Hint);
6015 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00006016}
6017
Alexey Bataev4acb8592014-07-07 13:01:15 +00006018StmtResult Sema::ActOnOpenMPParallelForDirective(
6019 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00006020 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006021 if (!AStmt)
6022 return StmtError();
6023
Alexey Bataeve3727102018-04-18 15:57:46 +00006024 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev4acb8592014-07-07 13:01:15 +00006025 // 1.2.2 OpenMP Language Terminology
6026 // Structured block - An executable statement with a single entry at the
6027 // top and a single exit at the bottom.
6028 // The point of exit cannot be a branch out of the structured block.
6029 // longjmp() and throw() must not violate the entry/exit criteria.
6030 CS->getCapturedDecl()->setNothrow();
6031
Alexander Musmanc6388682014-12-15 07:07:06 +00006032 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006033 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6034 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00006035 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00006036 checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00006037 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6038 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00006039 if (NestedLoopCount == 0)
6040 return StmtError();
6041
Alexander Musmana5f070a2014-10-01 06:03:56 +00006042 assert((CurContext->isDependentContext() || B.builtAll()) &&
6043 "omp parallel for loop exprs were not built");
6044
Alexey Bataev54acd402015-08-04 11:18:19 +00006045 if (!CurContext->isDependentContext()) {
6046 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00006047 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00006048 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00006049 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006050 B.NumIterations, *this, CurScope,
6051 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00006052 return StmtError();
6053 }
6054 }
6055
Reid Kleckner87a31802018-03-12 21:43:02 +00006056 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00006057 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00006058 NestedLoopCount, Clauses, AStmt, B,
6059 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00006060}
6061
Alexander Musmane4e893b2014-09-23 09:33:00 +00006062StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
6063 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00006064 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006065 if (!AStmt)
6066 return StmtError();
6067
Alexey Bataeve3727102018-04-18 15:57:46 +00006068 auto *CS = cast<CapturedStmt>(AStmt);
Alexander Musmane4e893b2014-09-23 09:33:00 +00006069 // 1.2.2 OpenMP Language Terminology
6070 // Structured block - An executable statement with a single entry at the
6071 // top and a single exit at the bottom.
6072 // The point of exit cannot be a branch out of the structured block.
6073 // longjmp() and throw() must not violate the entry/exit criteria.
6074 CS->getCapturedDecl()->setNothrow();
6075
Alexander Musmanc6388682014-12-15 07:07:06 +00006076 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006077 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6078 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00006079 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00006080 checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00006081 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6082 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00006083 if (NestedLoopCount == 0)
6084 return StmtError();
6085
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00006086 if (!CurContext->isDependentContext()) {
6087 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00006088 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00006089 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00006090 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006091 B.NumIterations, *this, CurScope,
6092 DSAStack))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00006093 return StmtError();
6094 }
6095 }
6096
Kelvin Lic5609492016-07-15 04:39:07 +00006097 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00006098 return StmtError();
6099
Reid Kleckner87a31802018-03-12 21:43:02 +00006100 setFunctionHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00006101 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00006102 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00006103}
6104
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006105StmtResult
6106Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
6107 Stmt *AStmt, SourceLocation StartLoc,
6108 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006109 if (!AStmt)
6110 return StmtError();
6111
6112 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006113 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00006114 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006115 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00006116 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006117 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00006118 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006119 return StmtError();
6120 // All associated statements must be '#pragma omp section' except for
6121 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00006122 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006123 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
6124 if (SectionStmt)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006125 Diag(SectionStmt->getBeginLoc(),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006126 diag::err_omp_parallel_sections_substmt_not_section);
6127 return StmtError();
6128 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006129 cast<OMPSectionDirective>(SectionStmt)
6130 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006131 }
6132 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006133 Diag(AStmt->getBeginLoc(),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006134 diag::err_omp_parallel_sections_not_compound_stmt);
6135 return StmtError();
6136 }
6137
Reid Kleckner87a31802018-03-12 21:43:02 +00006138 setFunctionHasBranchProtectedScope();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006139
Alexey Bataev25e5b442015-09-15 12:52:43 +00006140 return OMPParallelSectionsDirective::Create(
6141 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006142}
6143
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006144StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
6145 Stmt *AStmt, SourceLocation StartLoc,
6146 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006147 if (!AStmt)
6148 return StmtError();
6149
David Majnemer9d168222016-08-05 17:44:54 +00006150 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006151 // 1.2.2 OpenMP Language Terminology
6152 // Structured block - An executable statement with a single entry at the
6153 // top and a single exit at the bottom.
6154 // The point of exit cannot be a branch out of the structured block.
6155 // longjmp() and throw() must not violate the entry/exit criteria.
6156 CS->getCapturedDecl()->setNothrow();
6157
Reid Kleckner87a31802018-03-12 21:43:02 +00006158 setFunctionHasBranchProtectedScope();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006159
Alexey Bataev25e5b442015-09-15 12:52:43 +00006160 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
6161 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006162}
6163
Alexey Bataev68446b72014-07-18 07:47:19 +00006164StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
6165 SourceLocation EndLoc) {
6166 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
6167}
6168
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00006169StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
6170 SourceLocation EndLoc) {
6171 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
6172}
6173
Alexey Bataev2df347a2014-07-18 10:17:07 +00006174StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
6175 SourceLocation EndLoc) {
6176 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
6177}
6178
Alexey Bataev169d96a2017-07-18 20:17:46 +00006179StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses,
6180 Stmt *AStmt,
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006181 SourceLocation StartLoc,
6182 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006183 if (!AStmt)
6184 return StmtError();
6185
6186 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006187
Reid Kleckner87a31802018-03-12 21:43:02 +00006188 setFunctionHasBranchProtectedScope();
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006189
Alexey Bataev169d96a2017-07-18 20:17:46 +00006190 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses,
Alexey Bataev3b1b8952017-07-25 15:53:26 +00006191 AStmt,
6192 DSAStack->getTaskgroupReductionRef());
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006193}
6194
Alexey Bataev6125da92014-07-21 11:26:11 +00006195StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
6196 SourceLocation StartLoc,
6197 SourceLocation EndLoc) {
6198 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
6199 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
6200}
6201
Alexey Bataev346265e2015-09-25 10:37:12 +00006202StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
6203 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00006204 SourceLocation StartLoc,
6205 SourceLocation EndLoc) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006206 const OMPClause *DependFound = nullptr;
6207 const OMPClause *DependSourceClause = nullptr;
6208 const OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00006209 bool ErrorFound = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00006210 const OMPThreadsClause *TC = nullptr;
6211 const OMPSIMDClause *SC = nullptr;
6212 for (const OMPClause *C : Clauses) {
Alexey Bataeveb482352015-12-18 05:05:56 +00006213 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
6214 DependFound = C;
6215 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
6216 if (DependSourceClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006217 Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
Alexey Bataeveb482352015-12-18 05:05:56 +00006218 << getOpenMPDirectiveName(OMPD_ordered)
6219 << getOpenMPClauseName(OMPC_depend) << 2;
6220 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006221 } else {
Alexey Bataeveb482352015-12-18 05:05:56 +00006222 DependSourceClause = C;
Alexey Bataeve3727102018-04-18 15:57:46 +00006223 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006224 if (DependSinkClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006225 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006226 << 0;
6227 ErrorFound = true;
6228 }
6229 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
6230 if (DependSourceClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006231 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006232 << 1;
6233 ErrorFound = true;
6234 }
6235 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00006236 }
Alexey Bataeve3727102018-04-18 15:57:46 +00006237 } else if (C->getClauseKind() == OMPC_threads) {
Alexey Bataev346265e2015-09-25 10:37:12 +00006238 TC = cast<OMPThreadsClause>(C);
Alexey Bataeve3727102018-04-18 15:57:46 +00006239 } else if (C->getClauseKind() == OMPC_simd) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006240 SC = cast<OMPSIMDClause>(C);
Alexey Bataeve3727102018-04-18 15:57:46 +00006241 }
Alexey Bataev346265e2015-09-25 10:37:12 +00006242 }
Alexey Bataeveb482352015-12-18 05:05:56 +00006243 if (!ErrorFound && !SC &&
6244 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006245 // OpenMP [2.8.1,simd Construct, Restrictions]
6246 // An ordered construct with the simd clause is the only OpenMP construct
6247 // that can appear in the simd region.
6248 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00006249 ErrorFound = true;
6250 } else if (DependFound && (TC || SC)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006251 Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd)
Alexey Bataeveb482352015-12-18 05:05:56 +00006252 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
6253 ErrorFound = true;
Alexey Bataevf138fda2018-08-13 19:04:24 +00006254 } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006255 Diag(DependFound->getBeginLoc(),
Alexey Bataeveb482352015-12-18 05:05:56 +00006256 diag::err_omp_ordered_directive_without_param);
6257 ErrorFound = true;
6258 } else if (TC || Clauses.empty()) {
Alexey Bataevf138fda2018-08-13 19:04:24 +00006259 if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006260 SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc;
Alexey Bataeveb482352015-12-18 05:05:56 +00006261 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
6262 << (TC != nullptr);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006263 Diag(Param->getBeginLoc(), diag::note_omp_ordered_param);
Alexey Bataeveb482352015-12-18 05:05:56 +00006264 ErrorFound = true;
6265 }
6266 }
6267 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006268 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00006269
6270 if (AStmt) {
6271 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6272
Reid Kleckner87a31802018-03-12 21:43:02 +00006273 setFunctionHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006274 }
Alexey Bataev346265e2015-09-25 10:37:12 +00006275
6276 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00006277}
6278
Alexey Bataev1d160b12015-03-13 12:27:31 +00006279namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006280/// Helper class for checking expression in 'omp atomic [update]'
Alexey Bataev1d160b12015-03-13 12:27:31 +00006281/// construct.
6282class OpenMPAtomicUpdateChecker {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006283 /// Error results for atomic update expressions.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006284 enum ExprAnalysisErrorCode {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006285 /// A statement is not an expression statement.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006286 NotAnExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006287 /// Expression is not builtin binary or unary operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006288 NotABinaryOrUnaryExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006289 /// Unary operation is not post-/pre- increment/decrement operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006290 NotAnUnaryIncDecExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006291 /// An expression is not of scalar type.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006292 NotAScalarType,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006293 /// A binary operation is not an assignment operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006294 NotAnAssignmentOp,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006295 /// RHS part of the binary operation is not a binary expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006296 NotABinaryExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006297 /// RHS part is not additive/multiplicative/shift/biwise binary
Alexey Bataev1d160b12015-03-13 12:27:31 +00006298 /// expression.
6299 NotABinaryOperator,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006300 /// RHS binary operation does not have reference to the updated LHS
Alexey Bataev1d160b12015-03-13 12:27:31 +00006301 /// part.
6302 NotAnUpdateExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006303 /// No errors is found.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006304 NoError
6305 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006306 /// Reference to Sema.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006307 Sema &SemaRef;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006308 /// A location for note diagnostics (when error is found).
Alexey Bataev1d160b12015-03-13 12:27:31 +00006309 SourceLocation NoteLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006310 /// 'x' lvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006311 Expr *X;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006312 /// 'expr' rvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006313 Expr *E;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006314 /// Helper expression of the form
Alexey Bataevb4505a72015-03-30 05:20:59 +00006315 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6316 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6317 Expr *UpdateExpr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006318 /// Is 'x' a LHS in a RHS part of full update expression. It is
Alexey Bataevb4505a72015-03-30 05:20:59 +00006319 /// important for non-associative operations.
6320 bool IsXLHSInRHSPart;
6321 BinaryOperatorKind Op;
6322 SourceLocation OpLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006323 /// true if the source expression is a postfix unary operation, false
Alexey Bataevb78ca832015-04-01 03:33:17 +00006324 /// if it is a prefix unary operation.
6325 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006326
6327public:
6328 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00006329 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00006330 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006331 /// Check specified statement that it is suitable for 'atomic update'
Alexey Bataev1d160b12015-03-13 12:27:31 +00006332 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00006333 /// expression. If DiagId and NoteId == 0, then only check is performed
6334 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006335 /// \param DiagId Diagnostic which should be emitted if error is found.
6336 /// \param NoteId Diagnostic note for the main error message.
6337 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00006338 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006339 /// Return the 'x' lvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006340 Expr *getX() const { return X; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006341 /// Return the 'expr' rvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006342 Expr *getExpr() const { return E; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006343 /// Return the update expression used in calculation of the updated
Alexey Bataevb4505a72015-03-30 05:20:59 +00006344 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6345 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6346 Expr *getUpdateExpr() const { return UpdateExpr; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006347 /// Return true if 'x' is LHS in RHS part of full update expression,
Alexey Bataevb4505a72015-03-30 05:20:59 +00006348 /// false otherwise.
6349 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
6350
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006351 /// true if the source expression is a postfix unary operation, false
Alexey Bataevb78ca832015-04-01 03:33:17 +00006352 /// if it is a prefix unary operation.
6353 bool isPostfixUpdate() const { return IsPostfixUpdate; }
6354
Alexey Bataev1d160b12015-03-13 12:27:31 +00006355private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00006356 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
6357 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00006358};
6359} // namespace
6360
6361bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
6362 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
6363 ExprAnalysisErrorCode ErrorFound = NoError;
6364 SourceLocation ErrorLoc, NoteLoc;
6365 SourceRange ErrorRange, NoteRange;
6366 // Allowed constructs are:
6367 // x = x binop expr;
6368 // x = expr binop x;
6369 if (AtomicBinOp->getOpcode() == BO_Assign) {
6370 X = AtomicBinOp->getLHS();
Alexey Bataeve3727102018-04-18 15:57:46 +00006371 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006372 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
6373 if (AtomicInnerBinOp->isMultiplicativeOp() ||
6374 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
6375 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006376 Op = AtomicInnerBinOp->getOpcode();
6377 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataeve3727102018-04-18 15:57:46 +00006378 Expr *LHS = AtomicInnerBinOp->getLHS();
6379 Expr *RHS = AtomicInnerBinOp->getRHS();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006380 llvm::FoldingSetNodeID XId, LHSId, RHSId;
6381 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
6382 /*Canonical=*/true);
6383 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
6384 /*Canonical=*/true);
6385 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
6386 /*Canonical=*/true);
6387 if (XId == LHSId) {
6388 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006389 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006390 } else if (XId == RHSId) {
6391 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006392 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006393 } else {
6394 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6395 ErrorRange = AtomicInnerBinOp->getSourceRange();
6396 NoteLoc = X->getExprLoc();
6397 NoteRange = X->getSourceRange();
6398 ErrorFound = NotAnUpdateExpression;
6399 }
6400 } else {
6401 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6402 ErrorRange = AtomicInnerBinOp->getSourceRange();
6403 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
6404 NoteRange = SourceRange(NoteLoc, NoteLoc);
6405 ErrorFound = NotABinaryOperator;
6406 }
6407 } else {
6408 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
6409 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
6410 ErrorFound = NotABinaryExpression;
6411 }
6412 } else {
6413 ErrorLoc = AtomicBinOp->getExprLoc();
6414 ErrorRange = AtomicBinOp->getSourceRange();
6415 NoteLoc = AtomicBinOp->getOperatorLoc();
6416 NoteRange = SourceRange(NoteLoc, NoteLoc);
6417 ErrorFound = NotAnAssignmentOp;
6418 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006419 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006420 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6421 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6422 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006423 }
6424 if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006425 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006426 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006427}
6428
6429bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
6430 unsigned NoteId) {
6431 ExprAnalysisErrorCode ErrorFound = NoError;
6432 SourceLocation ErrorLoc, NoteLoc;
6433 SourceRange ErrorRange, NoteRange;
6434 // Allowed constructs are:
6435 // x++;
6436 // x--;
6437 // ++x;
6438 // --x;
6439 // x binop= expr;
6440 // x = x binop expr;
6441 // x = expr binop x;
6442 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
6443 AtomicBody = AtomicBody->IgnoreParenImpCasts();
6444 if (AtomicBody->getType()->isScalarType() ||
6445 AtomicBody->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006446 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006447 AtomicBody->IgnoreParenImpCasts())) {
6448 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00006449 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006450 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00006451 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006452 E = AtomicCompAssignOp->getRHS();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006453 X = AtomicCompAssignOp->getLHS()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006454 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006455 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
6456 AtomicBody->IgnoreParenImpCasts())) {
6457 // Check for Binary Operation
David Majnemer9d168222016-08-05 17:44:54 +00006458 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
Alexey Bataevb4505a72015-03-30 05:20:59 +00006459 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006460 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
David Majnemer9d168222016-08-05 17:44:54 +00006461 AtomicBody->IgnoreParenImpCasts())) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006462 // Check for Unary Operation
6463 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006464 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006465 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
6466 OpLoc = AtomicUnaryOp->getOperatorLoc();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006467 X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006468 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
6469 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006470 } else {
6471 ErrorFound = NotAnUnaryIncDecExpression;
6472 ErrorLoc = AtomicUnaryOp->getExprLoc();
6473 ErrorRange = AtomicUnaryOp->getSourceRange();
6474 NoteLoc = AtomicUnaryOp->getOperatorLoc();
6475 NoteRange = SourceRange(NoteLoc, NoteLoc);
6476 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006477 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006478 ErrorFound = NotABinaryOrUnaryExpression;
6479 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
6480 NoteRange = ErrorRange = AtomicBody->getSourceRange();
6481 }
6482 } else {
6483 ErrorFound = NotAScalarType;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006484 NoteLoc = ErrorLoc = AtomicBody->getBeginLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006485 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6486 }
6487 } else {
6488 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006489 NoteLoc = ErrorLoc = S->getBeginLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006490 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6491 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006492 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006493 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6494 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6495 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006496 }
6497 if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006498 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006499 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006500 // Build an update expression of form 'OpaqueValueExpr(x) binop
6501 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
6502 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
6503 auto *OVEX = new (SemaRef.getASTContext())
6504 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
6505 auto *OVEExpr = new (SemaRef.getASTContext())
6506 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
Alexey Bataeve3727102018-04-18 15:57:46 +00006507 ExprResult Update =
Alexey Bataevb4505a72015-03-30 05:20:59 +00006508 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
6509 IsXLHSInRHSPart ? OVEExpr : OVEX);
6510 if (Update.isInvalid())
6511 return true;
6512 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
6513 Sema::AA_Casting);
6514 if (Update.isInvalid())
6515 return true;
6516 UpdateExpr = Update.get();
6517 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00006518 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006519}
6520
Alexey Bataev0162e452014-07-22 10:10:35 +00006521StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
6522 Stmt *AStmt,
6523 SourceLocation StartLoc,
6524 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006525 if (!AStmt)
6526 return StmtError();
6527
David Majnemer9d168222016-08-05 17:44:54 +00006528 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00006529 // 1.2.2 OpenMP Language Terminology
6530 // Structured block - An executable statement with a single entry at the
6531 // top and a single exit at the bottom.
6532 // The point of exit cannot be a branch out of the structured block.
6533 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00006534 OpenMPClauseKind AtomicKind = OMPC_unknown;
6535 SourceLocation AtomicKindLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00006536 for (const OMPClause *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00006537 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00006538 C->getClauseKind() == OMPC_update ||
6539 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00006540 if (AtomicKind != OMPC_unknown) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006541 Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00006542 << SourceRange(C->getBeginLoc(), C->getEndLoc());
Alexey Bataevdea47612014-07-23 07:46:59 +00006543 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
6544 << getOpenMPClauseName(AtomicKind);
6545 } else {
6546 AtomicKind = C->getClauseKind();
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006547 AtomicKindLoc = C->getBeginLoc();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006548 }
6549 }
6550 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006551
Alexey Bataeve3727102018-04-18 15:57:46 +00006552 Stmt *Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00006553 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
6554 Body = EWC->getSubExpr();
6555
Alexey Bataev62cec442014-11-18 10:14:22 +00006556 Expr *X = nullptr;
6557 Expr *V = nullptr;
6558 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006559 Expr *UE = nullptr;
6560 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006561 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00006562 // OpenMP [2.12.6, atomic Construct]
6563 // In the next expressions:
6564 // * x and v (as applicable) are both l-value expressions with scalar type.
6565 // * During the execution of an atomic region, multiple syntactic
6566 // occurrences of x must designate the same storage location.
6567 // * Neither of v and expr (as applicable) may access the storage location
6568 // designated by x.
6569 // * Neither of x and expr (as applicable) may access the storage location
6570 // designated by v.
6571 // * expr is an expression with scalar type.
6572 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
6573 // * binop, binop=, ++, and -- are not overloaded operators.
6574 // * The expression x binop expr must be numerically equivalent to x binop
6575 // (expr). This requirement is satisfied if the operators in expr have
6576 // precedence greater than binop, or by using parentheses around expr or
6577 // subexpressions of expr.
6578 // * The expression expr binop x must be numerically equivalent to (expr)
6579 // binop x. This requirement is satisfied if the operators in expr have
6580 // precedence equal to or greater than binop, or by using parentheses around
6581 // expr or subexpressions of expr.
6582 // * For forms that allow multiple occurrences of x, the number of times
6583 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00006584 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006585 enum {
6586 NotAnExpression,
6587 NotAnAssignmentOp,
6588 NotAScalarType,
6589 NotAnLValue,
6590 NoError
6591 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00006592 SourceLocation ErrorLoc, NoteLoc;
6593 SourceRange ErrorRange, NoteRange;
6594 // If clause is read:
6595 // v = x;
Alexey Bataeve3727102018-04-18 15:57:46 +00006596 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
6597 const auto *AtomicBinOp =
Alexey Bataev62cec442014-11-18 10:14:22 +00006598 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6599 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6600 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6601 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
6602 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6603 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
6604 if (!X->isLValue() || !V->isLValue()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006605 const Expr *NotLValueExpr = X->isLValue() ? V : X;
Alexey Bataev62cec442014-11-18 10:14:22 +00006606 ErrorFound = NotAnLValue;
6607 ErrorLoc = AtomicBinOp->getExprLoc();
6608 ErrorRange = AtomicBinOp->getSourceRange();
6609 NoteLoc = NotLValueExpr->getExprLoc();
6610 NoteRange = NotLValueExpr->getSourceRange();
6611 }
6612 } else if (!X->isInstantiationDependent() ||
6613 !V->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006614 const Expr *NotScalarExpr =
Alexey Bataev62cec442014-11-18 10:14:22 +00006615 (X->isInstantiationDependent() || X->getType()->isScalarType())
6616 ? V
6617 : X;
6618 ErrorFound = NotAScalarType;
6619 ErrorLoc = AtomicBinOp->getExprLoc();
6620 ErrorRange = AtomicBinOp->getSourceRange();
6621 NoteLoc = NotScalarExpr->getExprLoc();
6622 NoteRange = NotScalarExpr->getSourceRange();
6623 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006624 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00006625 ErrorFound = NotAnAssignmentOp;
6626 ErrorLoc = AtomicBody->getExprLoc();
6627 ErrorRange = AtomicBody->getSourceRange();
6628 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6629 : AtomicBody->getExprLoc();
6630 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6631 : AtomicBody->getSourceRange();
6632 }
6633 } else {
6634 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006635 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataev62cec442014-11-18 10:14:22 +00006636 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006637 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006638 if (ErrorFound != NoError) {
6639 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
6640 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006641 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6642 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00006643 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00006644 }
6645 if (CurContext->isDependentContext())
Alexey Bataev62cec442014-11-18 10:14:22 +00006646 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00006647 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006648 enum {
6649 NotAnExpression,
6650 NotAnAssignmentOp,
6651 NotAScalarType,
6652 NotAnLValue,
6653 NoError
6654 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006655 SourceLocation ErrorLoc, NoteLoc;
6656 SourceRange ErrorRange, NoteRange;
6657 // If clause is write:
6658 // x = expr;
Alexey Bataeve3727102018-04-18 15:57:46 +00006659 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
6660 const auto *AtomicBinOp =
Alexey Bataevf33eba62014-11-28 07:21:40 +00006661 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6662 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00006663 X = AtomicBinOp->getLHS();
6664 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006665 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6666 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
6667 if (!X->isLValue()) {
6668 ErrorFound = NotAnLValue;
6669 ErrorLoc = AtomicBinOp->getExprLoc();
6670 ErrorRange = AtomicBinOp->getSourceRange();
6671 NoteLoc = X->getExprLoc();
6672 NoteRange = X->getSourceRange();
6673 }
6674 } else if (!X->isInstantiationDependent() ||
6675 !E->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006676 const Expr *NotScalarExpr =
Alexey Bataevf33eba62014-11-28 07:21:40 +00006677 (X->isInstantiationDependent() || X->getType()->isScalarType())
6678 ? E
6679 : X;
6680 ErrorFound = NotAScalarType;
6681 ErrorLoc = AtomicBinOp->getExprLoc();
6682 ErrorRange = AtomicBinOp->getSourceRange();
6683 NoteLoc = NotScalarExpr->getExprLoc();
6684 NoteRange = NotScalarExpr->getSourceRange();
6685 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006686 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00006687 ErrorFound = NotAnAssignmentOp;
6688 ErrorLoc = AtomicBody->getExprLoc();
6689 ErrorRange = AtomicBody->getSourceRange();
6690 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6691 : AtomicBody->getExprLoc();
6692 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6693 : AtomicBody->getSourceRange();
6694 }
6695 } else {
6696 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006697 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006698 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006699 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00006700 if (ErrorFound != NoError) {
6701 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
6702 << ErrorRange;
6703 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6704 << NoteRange;
6705 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00006706 }
6707 if (CurContext->isDependentContext())
Alexey Bataevf33eba62014-11-28 07:21:40 +00006708 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006709 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006710 // If clause is update:
6711 // x++;
6712 // x--;
6713 // ++x;
6714 // --x;
6715 // x binop= expr;
6716 // x = x binop expr;
6717 // x = expr binop x;
6718 OpenMPAtomicUpdateChecker Checker(*this);
6719 if (Checker.checkStatement(
6720 Body, (AtomicKind == OMPC_update)
6721 ? diag::err_omp_atomic_update_not_expression_statement
6722 : diag::err_omp_atomic_not_expression_statement,
6723 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00006724 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006725 if (!CurContext->isDependentContext()) {
6726 E = Checker.getExpr();
6727 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006728 UE = Checker.getUpdateExpr();
6729 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00006730 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006731 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006732 enum {
6733 NotAnAssignmentOp,
6734 NotACompoundStatement,
6735 NotTwoSubstatements,
6736 NotASpecificExpression,
6737 NoError
6738 } ErrorFound = NoError;
6739 SourceLocation ErrorLoc, NoteLoc;
6740 SourceRange ErrorRange, NoteRange;
Alexey Bataeve3727102018-04-18 15:57:46 +00006741 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006742 // If clause is a capture:
6743 // v = x++;
6744 // v = x--;
6745 // v = ++x;
6746 // v = --x;
6747 // v = x binop= expr;
6748 // v = x = x binop expr;
6749 // v = x = expr binop x;
Alexey Bataeve3727102018-04-18 15:57:46 +00006750 const auto *AtomicBinOp =
Alexey Bataevb78ca832015-04-01 03:33:17 +00006751 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6752 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6753 V = AtomicBinOp->getLHS();
6754 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6755 OpenMPAtomicUpdateChecker Checker(*this);
6756 if (Checker.checkStatement(
6757 Body, diag::err_omp_atomic_capture_not_expression_statement,
6758 diag::note_omp_atomic_update))
6759 return StmtError();
6760 E = Checker.getExpr();
6761 X = Checker.getX();
6762 UE = Checker.getUpdateExpr();
6763 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6764 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00006765 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006766 ErrorLoc = AtomicBody->getExprLoc();
6767 ErrorRange = AtomicBody->getSourceRange();
6768 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6769 : AtomicBody->getExprLoc();
6770 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6771 : AtomicBody->getSourceRange();
6772 ErrorFound = NotAnAssignmentOp;
6773 }
6774 if (ErrorFound != NoError) {
6775 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6776 << ErrorRange;
6777 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6778 return StmtError();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006779 }
Alexey Bataeve3727102018-04-18 15:57:46 +00006780 if (CurContext->isDependentContext())
6781 UE = V = E = X = nullptr;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006782 } else {
6783 // If clause is a capture:
6784 // { v = x; x = expr; }
6785 // { v = x; x++; }
6786 // { v = x; x--; }
6787 // { v = x; ++x; }
6788 // { v = x; --x; }
6789 // { v = x; x binop= expr; }
6790 // { v = x; x = x binop expr; }
6791 // { v = x; x = expr binop x; }
6792 // { x++; v = x; }
6793 // { x--; v = x; }
6794 // { ++x; v = x; }
6795 // { --x; v = x; }
6796 // { x binop= expr; v = x; }
6797 // { x = x binop expr; v = x; }
6798 // { x = expr binop x; v = x; }
6799 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6800 // Check that this is { expr1; expr2; }
6801 if (CS->size() == 2) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006802 Stmt *First = CS->body_front();
6803 Stmt *Second = CS->body_back();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006804 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6805 First = EWC->getSubExpr()->IgnoreParenImpCasts();
6806 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6807 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6808 // Need to find what subexpression is 'v' and what is 'x'.
6809 OpenMPAtomicUpdateChecker Checker(*this);
6810 bool IsUpdateExprFound = !Checker.checkStatement(Second);
6811 BinaryOperator *BinOp = nullptr;
6812 if (IsUpdateExprFound) {
6813 BinOp = dyn_cast<BinaryOperator>(First);
6814 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6815 }
6816 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6817 // { v = x; x++; }
6818 // { v = x; x--; }
6819 // { v = x; ++x; }
6820 // { v = x; --x; }
6821 // { v = x; x binop= expr; }
6822 // { v = x; x = x binop expr; }
6823 // { v = x; x = expr binop x; }
6824 // Check that the first expression has form v = x.
Alexey Bataeve3727102018-04-18 15:57:46 +00006825 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006826 llvm::FoldingSetNodeID XId, PossibleXId;
6827 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6828 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6829 IsUpdateExprFound = XId == PossibleXId;
6830 if (IsUpdateExprFound) {
6831 V = BinOp->getLHS();
6832 X = Checker.getX();
6833 E = Checker.getExpr();
6834 UE = Checker.getUpdateExpr();
6835 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006836 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006837 }
6838 }
6839 if (!IsUpdateExprFound) {
6840 IsUpdateExprFound = !Checker.checkStatement(First);
6841 BinOp = nullptr;
6842 if (IsUpdateExprFound) {
6843 BinOp = dyn_cast<BinaryOperator>(Second);
6844 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6845 }
6846 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6847 // { x++; v = x; }
6848 // { x--; v = x; }
6849 // { ++x; v = x; }
6850 // { --x; v = x; }
6851 // { x binop= expr; v = x; }
6852 // { x = x binop expr; v = x; }
6853 // { x = expr binop x; v = x; }
6854 // Check that the second expression has form v = x.
Alexey Bataeve3727102018-04-18 15:57:46 +00006855 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006856 llvm::FoldingSetNodeID XId, PossibleXId;
6857 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6858 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6859 IsUpdateExprFound = XId == PossibleXId;
6860 if (IsUpdateExprFound) {
6861 V = BinOp->getLHS();
6862 X = Checker.getX();
6863 E = Checker.getExpr();
6864 UE = Checker.getUpdateExpr();
6865 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006866 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006867 }
6868 }
6869 }
6870 if (!IsUpdateExprFound) {
6871 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00006872 auto *FirstExpr = dyn_cast<Expr>(First);
6873 auto *SecondExpr = dyn_cast<Expr>(Second);
6874 if (!FirstExpr || !SecondExpr ||
6875 !(FirstExpr->isInstantiationDependent() ||
6876 SecondExpr->isInstantiationDependent())) {
6877 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6878 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006879 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00006880 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006881 : First->getBeginLoc();
Alexey Bataev5a195472015-09-04 12:55:50 +00006882 NoteRange = ErrorRange = FirstBinOp
6883 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00006884 : SourceRange(ErrorLoc, ErrorLoc);
6885 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006886 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6887 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6888 ErrorFound = NotAnAssignmentOp;
6889 NoteLoc = ErrorLoc = SecondBinOp
6890 ? SecondBinOp->getOperatorLoc()
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006891 : Second->getBeginLoc();
Alexey Bataev5a195472015-09-04 12:55:50 +00006892 NoteRange = ErrorRange =
6893 SecondBinOp ? SecondBinOp->getSourceRange()
6894 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00006895 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +00006896 Expr *PossibleXRHSInFirst =
Alexey Bataev5a195472015-09-04 12:55:50 +00006897 FirstBinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00006898 Expr *PossibleXLHSInSecond =
Alexey Bataev5a195472015-09-04 12:55:50 +00006899 SecondBinOp->getLHS()->IgnoreParenImpCasts();
6900 llvm::FoldingSetNodeID X1Id, X2Id;
6901 PossibleXRHSInFirst->Profile(X1Id, Context,
6902 /*Canonical=*/true);
6903 PossibleXLHSInSecond->Profile(X2Id, Context,
6904 /*Canonical=*/true);
6905 IsUpdateExprFound = X1Id == X2Id;
6906 if (IsUpdateExprFound) {
6907 V = FirstBinOp->getLHS();
6908 X = SecondBinOp->getLHS();
6909 E = SecondBinOp->getRHS();
6910 UE = nullptr;
6911 IsXLHSInRHSPart = false;
6912 IsPostfixUpdate = true;
6913 } else {
6914 ErrorFound = NotASpecificExpression;
6915 ErrorLoc = FirstBinOp->getExprLoc();
6916 ErrorRange = FirstBinOp->getSourceRange();
6917 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6918 NoteRange = SecondBinOp->getRHS()->getSourceRange();
6919 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006920 }
6921 }
6922 }
6923 }
6924 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006925 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006926 NoteRange = ErrorRange =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006927 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
Alexey Bataevb78ca832015-04-01 03:33:17 +00006928 ErrorFound = NotTwoSubstatements;
6929 }
6930 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006931 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006932 NoteRange = ErrorRange =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006933 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
Alexey Bataevb78ca832015-04-01 03:33:17 +00006934 ErrorFound = NotACompoundStatement;
6935 }
6936 if (ErrorFound != NoError) {
6937 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6938 << ErrorRange;
6939 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6940 return StmtError();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006941 }
Alexey Bataeve3727102018-04-18 15:57:46 +00006942 if (CurContext->isDependentContext())
6943 UE = V = E = X = nullptr;
Alexey Bataev459dec02014-07-24 06:46:57 +00006944 }
Alexey Bataevdea47612014-07-23 07:46:59 +00006945 }
Alexey Bataev0162e452014-07-22 10:10:35 +00006946
Reid Kleckner87a31802018-03-12 21:43:02 +00006947 setFunctionHasBranchProtectedScope();
Alexey Bataev0162e452014-07-22 10:10:35 +00006948
Alexey Bataev62cec442014-11-18 10:14:22 +00006949 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00006950 X, V, E, UE, IsXLHSInRHSPart,
6951 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00006952}
6953
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006954StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6955 Stmt *AStmt,
6956 SourceLocation StartLoc,
6957 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006958 if (!AStmt)
6959 return StmtError();
6960
Alexey Bataeve3727102018-04-18 15:57:46 +00006961 auto *CS = cast<CapturedStmt>(AStmt);
Samuel Antao4af1b7b2015-12-02 17:44:43 +00006962 // 1.2.2 OpenMP Language Terminology
6963 // Structured block - An executable statement with a single entry at the
6964 // top and a single exit at the bottom.
6965 // The point of exit cannot be a branch out of the structured block.
6966 // longjmp() and throw() must not violate the entry/exit criteria.
6967 CS->getCapturedDecl()->setNothrow();
Alexey Bataev8451efa2018-01-15 19:06:12 +00006968 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target);
6969 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6970 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6971 // 1.2.2 OpenMP Language Terminology
6972 // Structured block - An executable statement with a single entry at the
6973 // top and a single exit at the bottom.
6974 // The point of exit cannot be a branch out of the structured block.
6975 // longjmp() and throw() must not violate the entry/exit criteria.
6976 CS->getCapturedDecl()->setNothrow();
6977 }
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006978
Alexey Bataev13314bf2014-10-09 04:18:56 +00006979 // OpenMP [2.16, Nesting of Regions]
6980 // If specified, a teams construct must be contained within a target
6981 // construct. That target construct must contain no statements or directives
6982 // outside of the teams construct.
6983 if (DSAStack->hasInnerTeamsRegion()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006984 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true);
Alexey Bataev13314bf2014-10-09 04:18:56 +00006985 bool OMPTeamsFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006986 if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00006987 auto I = CS->body_begin();
6988 while (I != CS->body_end()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006989 const auto *OED = dyn_cast<OMPExecutableDirective>(*I);
Alexey Bataev13314bf2014-10-09 04:18:56 +00006990 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6991 OMPTeamsFound = false;
6992 break;
6993 }
6994 ++I;
6995 }
6996 assert(I != CS->body_end() && "Not found statement");
6997 S = *I;
Kelvin Li3834dce2016-06-27 19:15:43 +00006998 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +00006999 const auto *OED = dyn_cast<OMPExecutableDirective>(S);
Kelvin Li3834dce2016-06-27 19:15:43 +00007000 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
Alexey Bataev13314bf2014-10-09 04:18:56 +00007001 }
7002 if (!OMPTeamsFound) {
7003 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
7004 Diag(DSAStack->getInnerTeamsRegionLoc(),
7005 diag::note_omp_nested_teams_construct_here);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007006 Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here)
Alexey Bataev13314bf2014-10-09 04:18:56 +00007007 << isa<OMPExecutableDirective>(S);
7008 return StmtError();
7009 }
7010 }
7011
Reid Kleckner87a31802018-03-12 21:43:02 +00007012 setFunctionHasBranchProtectedScope();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00007013
7014 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
7015}
7016
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00007017StmtResult
7018Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
7019 Stmt *AStmt, SourceLocation StartLoc,
7020 SourceLocation EndLoc) {
7021 if (!AStmt)
7022 return StmtError();
7023
Alexey Bataeve3727102018-04-18 15:57:46 +00007024 auto *CS = cast<CapturedStmt>(AStmt);
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00007025 // 1.2.2 OpenMP Language Terminology
7026 // Structured block - An executable statement with a single entry at the
7027 // top and a single exit at the bottom.
7028 // The point of exit cannot be a branch out of the structured block.
7029 // longjmp() and throw() must not violate the entry/exit criteria.
7030 CS->getCapturedDecl()->setNothrow();
Alexey Bataev8451efa2018-01-15 19:06:12 +00007031 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel);
7032 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7033 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7034 // 1.2.2 OpenMP Language Terminology
7035 // Structured block - An executable statement with a single entry at the
7036 // top and a single exit at the bottom.
7037 // The point of exit cannot be a branch out of the structured block.
7038 // longjmp() and throw() must not violate the entry/exit criteria.
7039 CS->getCapturedDecl()->setNothrow();
7040 }
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00007041
Reid Kleckner87a31802018-03-12 21:43:02 +00007042 setFunctionHasBranchProtectedScope();
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00007043
7044 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
7045 AStmt);
7046}
7047
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007048StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
7049 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007050 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007051 if (!AStmt)
7052 return StmtError();
7053
Alexey Bataeve3727102018-04-18 15:57:46 +00007054 auto *CS = cast<CapturedStmt>(AStmt);
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007055 // 1.2.2 OpenMP Language Terminology
7056 // Structured block - An executable statement with a single entry at the
7057 // top and a single exit at the bottom.
7058 // The point of exit cannot be a branch out of the structured block.
7059 // longjmp() and throw() must not violate the entry/exit criteria.
7060 CS->getCapturedDecl()->setNothrow();
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00007061 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
7062 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7063 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7064 // 1.2.2 OpenMP Language Terminology
7065 // Structured block - An executable statement with a single entry at the
7066 // top and a single exit at the bottom.
7067 // The point of exit cannot be a branch out of the structured block.
7068 // longjmp() and throw() must not violate the entry/exit criteria.
7069 CS->getCapturedDecl()->setNothrow();
7070 }
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007071
7072 OMPLoopDirective::HelperExprs B;
7073 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7074 // define the nested loops number.
7075 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007076 checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00007077 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007078 VarsWithImplicitDSA, B);
7079 if (NestedLoopCount == 0)
7080 return StmtError();
7081
7082 assert((CurContext->isDependentContext() || B.builtAll()) &&
7083 "omp target parallel for loop exprs were not built");
7084
7085 if (!CurContext->isDependentContext()) {
7086 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007087 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007088 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007089 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007090 B.NumIterations, *this, CurScope,
7091 DSAStack))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007092 return StmtError();
7093 }
7094 }
7095
Reid Kleckner87a31802018-03-12 21:43:02 +00007096 setFunctionHasBranchProtectedScope();
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007097 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
7098 NestedLoopCount, Clauses, AStmt,
7099 B, DSAStack->isCancelRegion());
7100}
7101
Alexey Bataev95b64a92017-05-30 16:00:04 +00007102/// Check for existence of a map clause in the list of clauses.
7103static bool hasClauses(ArrayRef<OMPClause *> Clauses,
7104 const OpenMPClauseKind K) {
7105 return llvm::any_of(
7106 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; });
7107}
Samuel Antaodf67fc42016-01-19 19:15:56 +00007108
Alexey Bataev95b64a92017-05-30 16:00:04 +00007109template <typename... Params>
7110static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K,
7111 const Params... ClauseTypes) {
7112 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...);
Samuel Antaodf67fc42016-01-19 19:15:56 +00007113}
7114
Michael Wong65f367f2015-07-21 13:44:28 +00007115StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
7116 Stmt *AStmt,
7117 SourceLocation StartLoc,
7118 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007119 if (!AStmt)
7120 return StmtError();
7121
7122 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7123
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00007124 // OpenMP [2.10.1, Restrictions, p. 97]
7125 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00007126 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr)) {
7127 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
7128 << "'map' or 'use_device_ptr'"
David Majnemer9d168222016-08-05 17:44:54 +00007129 << getOpenMPDirectiveName(OMPD_target_data);
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00007130 return StmtError();
7131 }
7132
Reid Kleckner87a31802018-03-12 21:43:02 +00007133 setFunctionHasBranchProtectedScope();
Michael Wong65f367f2015-07-21 13:44:28 +00007134
7135 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
7136 AStmt);
7137}
7138
Samuel Antaodf67fc42016-01-19 19:15:56 +00007139StmtResult
7140Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
7141 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00007142 SourceLocation EndLoc, Stmt *AStmt) {
7143 if (!AStmt)
7144 return StmtError();
7145
Alexey Bataeve3727102018-04-18 15:57:46 +00007146 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00007147 // 1.2.2 OpenMP Language Terminology
7148 // Structured block - An executable statement with a single entry at the
7149 // top and a single exit at the bottom.
7150 // The point of exit cannot be a branch out of the structured block.
7151 // longjmp() and throw() must not violate the entry/exit criteria.
7152 CS->getCapturedDecl()->setNothrow();
7153 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data);
7154 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7155 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7156 // 1.2.2 OpenMP Language Terminology
7157 // Structured block - An executable statement with a single entry at the
7158 // top and a single exit at the bottom.
7159 // The point of exit cannot be a branch out of the structured block.
7160 // longjmp() and throw() must not violate the entry/exit criteria.
7161 CS->getCapturedDecl()->setNothrow();
7162 }
7163
Samuel Antaodf67fc42016-01-19 19:15:56 +00007164 // OpenMP [2.10.2, Restrictions, p. 99]
7165 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00007166 if (!hasClauses(Clauses, OMPC_map)) {
7167 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
7168 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data);
Samuel Antaodf67fc42016-01-19 19:15:56 +00007169 return StmtError();
7170 }
7171
Alexey Bataev7828b252017-11-21 17:08:48 +00007172 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
7173 AStmt);
Samuel Antaodf67fc42016-01-19 19:15:56 +00007174}
7175
Samuel Antao72590762016-01-19 20:04:50 +00007176StmtResult
7177Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
7178 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00007179 SourceLocation EndLoc, Stmt *AStmt) {
7180 if (!AStmt)
7181 return StmtError();
7182
Alexey Bataeve3727102018-04-18 15:57:46 +00007183 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00007184 // 1.2.2 OpenMP Language Terminology
7185 // Structured block - An executable statement with a single entry at the
7186 // top and a single exit at the bottom.
7187 // The point of exit cannot be a branch out of the structured block.
7188 // longjmp() and throw() must not violate the entry/exit criteria.
7189 CS->getCapturedDecl()->setNothrow();
7190 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data);
7191 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7192 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7193 // 1.2.2 OpenMP Language Terminology
7194 // Structured block - An executable statement with a single entry at the
7195 // top and a single exit at the bottom.
7196 // The point of exit cannot be a branch out of the structured block.
7197 // longjmp() and throw() must not violate the entry/exit criteria.
7198 CS->getCapturedDecl()->setNothrow();
7199 }
7200
Samuel Antao72590762016-01-19 20:04:50 +00007201 // OpenMP [2.10.3, Restrictions, p. 102]
7202 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00007203 if (!hasClauses(Clauses, OMPC_map)) {
7204 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
7205 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data);
Samuel Antao72590762016-01-19 20:04:50 +00007206 return StmtError();
7207 }
7208
Alexey Bataev7828b252017-11-21 17:08:48 +00007209 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
7210 AStmt);
Samuel Antao72590762016-01-19 20:04:50 +00007211}
7212
Samuel Antao686c70c2016-05-26 17:30:50 +00007213StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
7214 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00007215 SourceLocation EndLoc,
7216 Stmt *AStmt) {
7217 if (!AStmt)
7218 return StmtError();
7219
Alexey Bataeve3727102018-04-18 15:57:46 +00007220 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00007221 // 1.2.2 OpenMP Language Terminology
7222 // Structured block - An executable statement with a single entry at the
7223 // top and a single exit at the bottom.
7224 // The point of exit cannot be a branch out of the structured block.
7225 // longjmp() and throw() must not violate the entry/exit criteria.
7226 CS->getCapturedDecl()->setNothrow();
7227 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update);
7228 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7229 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7230 // 1.2.2 OpenMP Language Terminology
7231 // Structured block - An executable statement with a single entry at the
7232 // top and a single exit at the bottom.
7233 // The point of exit cannot be a branch out of the structured block.
7234 // longjmp() and throw() must not violate the entry/exit criteria.
7235 CS->getCapturedDecl()->setNothrow();
7236 }
7237
Alexey Bataev95b64a92017-05-30 16:00:04 +00007238 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) {
Samuel Antao686c70c2016-05-26 17:30:50 +00007239 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
7240 return StmtError();
7241 }
Alexey Bataev7828b252017-11-21 17:08:48 +00007242 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses,
7243 AStmt);
Samuel Antao686c70c2016-05-26 17:30:50 +00007244}
7245
Alexey Bataev13314bf2014-10-09 04:18:56 +00007246StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
7247 Stmt *AStmt, SourceLocation StartLoc,
7248 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007249 if (!AStmt)
7250 return StmtError();
7251
Alexey Bataeve3727102018-04-18 15:57:46 +00007252 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev13314bf2014-10-09 04:18:56 +00007253 // 1.2.2 OpenMP Language Terminology
7254 // Structured block - An executable statement with a single entry at the
7255 // top and a single exit at the bottom.
7256 // The point of exit cannot be a branch out of the structured block.
7257 // longjmp() and throw() must not violate the entry/exit criteria.
7258 CS->getCapturedDecl()->setNothrow();
7259
Reid Kleckner87a31802018-03-12 21:43:02 +00007260 setFunctionHasBranchProtectedScope();
Alexey Bataev13314bf2014-10-09 04:18:56 +00007261
Alexey Bataevceabd412017-11-30 18:01:54 +00007262 DSAStack->setParentTeamsRegionLoc(StartLoc);
7263
Alexey Bataev13314bf2014-10-09 04:18:56 +00007264 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
7265}
7266
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007267StmtResult
7268Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
7269 SourceLocation EndLoc,
7270 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007271 if (DSAStack->isParentNowaitRegion()) {
7272 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
7273 return StmtError();
7274 }
7275 if (DSAStack->isParentOrderedRegion()) {
7276 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
7277 return StmtError();
7278 }
7279 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
7280 CancelRegion);
7281}
7282
Alexey Bataev87933c72015-09-18 08:07:34 +00007283StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
7284 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00007285 SourceLocation EndLoc,
7286 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev80909872015-07-02 11:25:17 +00007287 if (DSAStack->isParentNowaitRegion()) {
7288 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
7289 return StmtError();
7290 }
7291 if (DSAStack->isParentOrderedRegion()) {
7292 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
7293 return StmtError();
7294 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00007295 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00007296 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
7297 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00007298}
7299
Alexey Bataev382967a2015-12-08 12:06:20 +00007300static bool checkGrainsizeNumTasksClauses(Sema &S,
7301 ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007302 const OMPClause *PrevClause = nullptr;
Alexey Bataev382967a2015-12-08 12:06:20 +00007303 bool ErrorFound = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00007304 for (const OMPClause *C : Clauses) {
Alexey Bataev382967a2015-12-08 12:06:20 +00007305 if (C->getClauseKind() == OMPC_grainsize ||
7306 C->getClauseKind() == OMPC_num_tasks) {
7307 if (!PrevClause)
7308 PrevClause = C;
7309 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007310 S.Diag(C->getBeginLoc(),
Alexey Bataev382967a2015-12-08 12:06:20 +00007311 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
7312 << getOpenMPClauseName(C->getClauseKind())
7313 << getOpenMPClauseName(PrevClause->getClauseKind());
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007314 S.Diag(PrevClause->getBeginLoc(),
Alexey Bataev382967a2015-12-08 12:06:20 +00007315 diag::note_omp_previous_grainsize_num_tasks)
7316 << getOpenMPClauseName(PrevClause->getClauseKind());
7317 ErrorFound = true;
7318 }
7319 }
7320 }
7321 return ErrorFound;
7322}
7323
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007324static bool checkReductionClauseWithNogroup(Sema &S,
7325 ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007326 const OMPClause *ReductionClause = nullptr;
7327 const OMPClause *NogroupClause = nullptr;
7328 for (const OMPClause *C : Clauses) {
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007329 if (C->getClauseKind() == OMPC_reduction) {
7330 ReductionClause = C;
7331 if (NogroupClause)
7332 break;
7333 continue;
7334 }
7335 if (C->getClauseKind() == OMPC_nogroup) {
7336 NogroupClause = C;
7337 if (ReductionClause)
7338 break;
7339 continue;
7340 }
7341 }
7342 if (ReductionClause && NogroupClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007343 S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup)
7344 << SourceRange(NogroupClause->getBeginLoc(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007345 NogroupClause->getEndLoc());
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007346 return true;
7347 }
7348 return false;
7349}
7350
Alexey Bataev49f6e782015-12-01 04:18:41 +00007351StmtResult Sema::ActOnOpenMPTaskLoopDirective(
7352 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007353 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00007354 if (!AStmt)
7355 return StmtError();
7356
7357 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7358 OMPLoopDirective::HelperExprs B;
7359 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7360 // define the nested loops number.
7361 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007362 checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007363 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00007364 VarsWithImplicitDSA, B);
7365 if (NestedLoopCount == 0)
7366 return StmtError();
7367
7368 assert((CurContext->isDependentContext() || B.builtAll()) &&
7369 "omp for loop exprs were not built");
7370
Alexey Bataev382967a2015-12-08 12:06:20 +00007371 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7372 // The grainsize clause and num_tasks clause are mutually exclusive and may
7373 // not appear on the same taskloop directive.
7374 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7375 return StmtError();
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007376 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7377 // If a reduction clause is present on the taskloop directive, the nogroup
7378 // clause must not be specified.
7379 if (checkReductionClauseWithNogroup(*this, Clauses))
7380 return StmtError();
Alexey Bataev382967a2015-12-08 12:06:20 +00007381
Reid Kleckner87a31802018-03-12 21:43:02 +00007382 setFunctionHasBranchProtectedScope();
Alexey Bataev49f6e782015-12-01 04:18:41 +00007383 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
7384 NestedLoopCount, Clauses, AStmt, B);
7385}
7386
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007387StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
7388 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007389 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007390 if (!AStmt)
7391 return StmtError();
7392
7393 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7394 OMPLoopDirective::HelperExprs B;
7395 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7396 // define the nested loops number.
7397 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007398 checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007399 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
7400 VarsWithImplicitDSA, B);
7401 if (NestedLoopCount == 0)
7402 return StmtError();
7403
7404 assert((CurContext->isDependentContext() || B.builtAll()) &&
7405 "omp for loop exprs were not built");
7406
Alexey Bataev5a3af132016-03-29 08:58:54 +00007407 if (!CurContext->isDependentContext()) {
7408 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007409 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007410 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007411 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007412 B.NumIterations, *this, CurScope,
7413 DSAStack))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007414 return StmtError();
7415 }
7416 }
7417
Alexey Bataev382967a2015-12-08 12:06:20 +00007418 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7419 // The grainsize clause and num_tasks clause are mutually exclusive and may
7420 // not appear on the same taskloop directive.
7421 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7422 return StmtError();
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007423 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7424 // If a reduction clause is present on the taskloop directive, the nogroup
7425 // clause must not be specified.
7426 if (checkReductionClauseWithNogroup(*this, Clauses))
7427 return StmtError();
Alexey Bataev438388c2017-11-22 18:34:02 +00007428 if (checkSimdlenSafelenSpecified(*this, Clauses))
7429 return StmtError();
Alexey Bataev382967a2015-12-08 12:06:20 +00007430
Reid Kleckner87a31802018-03-12 21:43:02 +00007431 setFunctionHasBranchProtectedScope();
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007432 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
7433 NestedLoopCount, Clauses, AStmt, B);
7434}
7435
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007436StmtResult Sema::ActOnOpenMPDistributeDirective(
7437 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007438 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007439 if (!AStmt)
7440 return StmtError();
7441
7442 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7443 OMPLoopDirective::HelperExprs B;
7444 // In presence of clause 'collapse' with number of loops, it will
7445 // define the nested loops number.
7446 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007447 checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007448 nullptr /*ordered not a clause on distribute*/, AStmt,
7449 *this, *DSAStack, VarsWithImplicitDSA, B);
7450 if (NestedLoopCount == 0)
7451 return StmtError();
7452
7453 assert((CurContext->isDependentContext() || B.builtAll()) &&
7454 "omp for loop exprs were not built");
7455
Reid Kleckner87a31802018-03-12 21:43:02 +00007456 setFunctionHasBranchProtectedScope();
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007457 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
7458 NestedLoopCount, Clauses, AStmt, B);
7459}
7460
Carlo Bertolli9925f152016-06-27 14:55:37 +00007461StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
7462 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007463 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Carlo Bertolli9925f152016-06-27 14:55:37 +00007464 if (!AStmt)
7465 return StmtError();
7466
Alexey Bataeve3727102018-04-18 15:57:46 +00007467 auto *CS = cast<CapturedStmt>(AStmt);
Carlo Bertolli9925f152016-06-27 14:55:37 +00007468 // 1.2.2 OpenMP Language Terminology
7469 // Structured block - An executable statement with a single entry at the
7470 // top and a single exit at the bottom.
7471 // The point of exit cannot be a branch out of the structured block.
7472 // longjmp() and throw() must not violate the entry/exit criteria.
7473 CS->getCapturedDecl()->setNothrow();
Alexey Bataev7f96c372017-11-22 17:19:31 +00007474 for (int ThisCaptureLevel =
7475 getOpenMPCaptureLevels(OMPD_distribute_parallel_for);
7476 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7477 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7478 // 1.2.2 OpenMP Language Terminology
7479 // Structured block - An executable statement with a single entry at the
7480 // top and a single exit at the bottom.
7481 // The point of exit cannot be a branch out of the structured block.
7482 // longjmp() and throw() must not violate the entry/exit criteria.
7483 CS->getCapturedDecl()->setNothrow();
7484 }
Carlo Bertolli9925f152016-06-27 14:55:37 +00007485
7486 OMPLoopDirective::HelperExprs B;
7487 // In presence of clause 'collapse' with number of loops, it will
7488 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007489 unsigned NestedLoopCount = checkOpenMPLoop(
Carlo Bertolli9925f152016-06-27 14:55:37 +00007490 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataev7f96c372017-11-22 17:19:31 +00007491 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Carlo Bertolli9925f152016-06-27 14:55:37 +00007492 VarsWithImplicitDSA, B);
7493 if (NestedLoopCount == 0)
7494 return StmtError();
7495
7496 assert((CurContext->isDependentContext() || B.builtAll()) &&
7497 "omp for loop exprs were not built");
7498
Reid Kleckner87a31802018-03-12 21:43:02 +00007499 setFunctionHasBranchProtectedScope();
Carlo Bertolli9925f152016-06-27 14:55:37 +00007500 return OMPDistributeParallelForDirective::Create(
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00007501 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7502 DSAStack->isCancelRegion());
Carlo Bertolli9925f152016-06-27 14:55:37 +00007503}
7504
Kelvin Li4a39add2016-07-05 05:00:15 +00007505StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
7506 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007507 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li4a39add2016-07-05 05:00:15 +00007508 if (!AStmt)
7509 return StmtError();
7510
Alexey Bataeve3727102018-04-18 15:57:46 +00007511 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li4a39add2016-07-05 05:00:15 +00007512 // 1.2.2 OpenMP Language Terminology
7513 // Structured block - An executable statement with a single entry at the
7514 // top and a single exit at the bottom.
7515 // The point of exit cannot be a branch out of the structured block.
7516 // longjmp() and throw() must not violate the entry/exit criteria.
7517 CS->getCapturedDecl()->setNothrow();
Alexey Bataev974acd62017-11-27 19:38:52 +00007518 for (int ThisCaptureLevel =
7519 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd);
7520 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7521 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7522 // 1.2.2 OpenMP Language Terminology
7523 // Structured block - An executable statement with a single entry at the
7524 // top and a single exit at the bottom.
7525 // The point of exit cannot be a branch out of the structured block.
7526 // longjmp() and throw() must not violate the entry/exit criteria.
7527 CS->getCapturedDecl()->setNothrow();
7528 }
Kelvin Li4a39add2016-07-05 05:00:15 +00007529
7530 OMPLoopDirective::HelperExprs B;
7531 // In presence of clause 'collapse' with number of loops, it will
7532 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007533 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li4a39add2016-07-05 05:00:15 +00007534 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev974acd62017-11-27 19:38:52 +00007535 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li4a39add2016-07-05 05:00:15 +00007536 VarsWithImplicitDSA, B);
7537 if (NestedLoopCount == 0)
7538 return StmtError();
7539
7540 assert((CurContext->isDependentContext() || B.builtAll()) &&
7541 "omp for loop exprs were not built");
7542
Alexey Bataev438388c2017-11-22 18:34:02 +00007543 if (!CurContext->isDependentContext()) {
7544 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007545 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00007546 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7547 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7548 B.NumIterations, *this, CurScope,
7549 DSAStack))
7550 return StmtError();
7551 }
7552 }
7553
Kelvin Lic5609492016-07-15 04:39:07 +00007554 if (checkSimdlenSafelenSpecified(*this, Clauses))
7555 return StmtError();
7556
Reid Kleckner87a31802018-03-12 21:43:02 +00007557 setFunctionHasBranchProtectedScope();
Kelvin Li4a39add2016-07-05 05:00:15 +00007558 return OMPDistributeParallelForSimdDirective::Create(
7559 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7560}
7561
Kelvin Li787f3fc2016-07-06 04:45:38 +00007562StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
7563 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007564 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li787f3fc2016-07-06 04:45:38 +00007565 if (!AStmt)
7566 return StmtError();
7567
Alexey Bataeve3727102018-04-18 15:57:46 +00007568 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li787f3fc2016-07-06 04:45:38 +00007569 // 1.2.2 OpenMP Language Terminology
7570 // Structured block - An executable statement with a single entry at the
7571 // top and a single exit at the bottom.
7572 // The point of exit cannot be a branch out of the structured block.
7573 // longjmp() and throw() must not violate the entry/exit criteria.
7574 CS->getCapturedDecl()->setNothrow();
Alexey Bataev617db5f2017-12-04 15:38:33 +00007575 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd);
7576 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7577 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7578 // 1.2.2 OpenMP Language Terminology
7579 // Structured block - An executable statement with a single entry at the
7580 // top and a single exit at the bottom.
7581 // The point of exit cannot be a branch out of the structured block.
7582 // longjmp() and throw() must not violate the entry/exit criteria.
7583 CS->getCapturedDecl()->setNothrow();
7584 }
Kelvin Li787f3fc2016-07-06 04:45:38 +00007585
7586 OMPLoopDirective::HelperExprs B;
7587 // In presence of clause 'collapse' with number of loops, it will
7588 // define the nested loops number.
7589 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007590 checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev617db5f2017-12-04 15:38:33 +00007591 nullptr /*ordered not a clause on distribute*/, CS, *this,
7592 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li787f3fc2016-07-06 04:45:38 +00007593 if (NestedLoopCount == 0)
7594 return StmtError();
7595
7596 assert((CurContext->isDependentContext() || B.builtAll()) &&
7597 "omp for loop exprs were not built");
7598
Alexey Bataev438388c2017-11-22 18:34:02 +00007599 if (!CurContext->isDependentContext()) {
7600 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007601 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00007602 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7603 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7604 B.NumIterations, *this, CurScope,
7605 DSAStack))
7606 return StmtError();
7607 }
7608 }
7609
Kelvin Lic5609492016-07-15 04:39:07 +00007610 if (checkSimdlenSafelenSpecified(*this, Clauses))
7611 return StmtError();
7612
Reid Kleckner87a31802018-03-12 21:43:02 +00007613 setFunctionHasBranchProtectedScope();
Kelvin Li787f3fc2016-07-06 04:45:38 +00007614 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
7615 NestedLoopCount, Clauses, AStmt, B);
7616}
7617
Kelvin Lia579b912016-07-14 02:54:56 +00007618StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
7619 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007620 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Lia579b912016-07-14 02:54:56 +00007621 if (!AStmt)
7622 return StmtError();
7623
Alexey Bataeve3727102018-04-18 15:57:46 +00007624 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Lia579b912016-07-14 02:54:56 +00007625 // 1.2.2 OpenMP Language Terminology
7626 // Structured block - An executable statement with a single entry at the
7627 // top and a single exit at the bottom.
7628 // The point of exit cannot be a branch out of the structured block.
7629 // longjmp() and throw() must not violate the entry/exit criteria.
7630 CS->getCapturedDecl()->setNothrow();
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007631 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
7632 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7633 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7634 // 1.2.2 OpenMP Language Terminology
7635 // Structured block - An executable statement with a single entry at the
7636 // top and a single exit at the bottom.
7637 // The point of exit cannot be a branch out of the structured block.
7638 // longjmp() and throw() must not violate the entry/exit criteria.
7639 CS->getCapturedDecl()->setNothrow();
7640 }
Kelvin Lia579b912016-07-14 02:54:56 +00007641
7642 OMPLoopDirective::HelperExprs B;
7643 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7644 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007645 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Lia579b912016-07-14 02:54:56 +00007646 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007647 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Kelvin Lia579b912016-07-14 02:54:56 +00007648 VarsWithImplicitDSA, B);
7649 if (NestedLoopCount == 0)
7650 return StmtError();
7651
7652 assert((CurContext->isDependentContext() || B.builtAll()) &&
7653 "omp target parallel for simd loop exprs were not built");
7654
7655 if (!CurContext->isDependentContext()) {
7656 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007657 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007658 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Lia579b912016-07-14 02:54:56 +00007659 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7660 B.NumIterations, *this, CurScope,
7661 DSAStack))
7662 return StmtError();
7663 }
7664 }
Kelvin Lic5609492016-07-15 04:39:07 +00007665 if (checkSimdlenSafelenSpecified(*this, Clauses))
Kelvin Lia579b912016-07-14 02:54:56 +00007666 return StmtError();
7667
Reid Kleckner87a31802018-03-12 21:43:02 +00007668 setFunctionHasBranchProtectedScope();
Kelvin Lia579b912016-07-14 02:54:56 +00007669 return OMPTargetParallelForSimdDirective::Create(
7670 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7671}
7672
Kelvin Li986330c2016-07-20 22:57:10 +00007673StmtResult Sema::ActOnOpenMPTargetSimdDirective(
7674 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007675 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li986330c2016-07-20 22:57:10 +00007676 if (!AStmt)
7677 return StmtError();
7678
Alexey Bataeve3727102018-04-18 15:57:46 +00007679 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li986330c2016-07-20 22:57:10 +00007680 // 1.2.2 OpenMP Language Terminology
7681 // Structured block - An executable statement with a single entry at the
7682 // top and a single exit at the bottom.
7683 // The point of exit cannot be a branch out of the structured block.
7684 // longjmp() and throw() must not violate the entry/exit criteria.
7685 CS->getCapturedDecl()->setNothrow();
Alexey Bataevf8365372017-11-17 17:57:25 +00007686 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd);
7687 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7688 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7689 // 1.2.2 OpenMP Language Terminology
7690 // Structured block - An executable statement with a single entry at the
7691 // top and a single exit at the bottom.
7692 // The point of exit cannot be a branch out of the structured block.
7693 // longjmp() and throw() must not violate the entry/exit criteria.
7694 CS->getCapturedDecl()->setNothrow();
7695 }
7696
Kelvin Li986330c2016-07-20 22:57:10 +00007697 OMPLoopDirective::HelperExprs B;
7698 // In presence of clause 'collapse' with number of loops, it will define the
7699 // nested loops number.
David Majnemer9d168222016-08-05 17:44:54 +00007700 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007701 checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
Alexey Bataevf8365372017-11-17 17:57:25 +00007702 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Kelvin Li986330c2016-07-20 22:57:10 +00007703 VarsWithImplicitDSA, B);
7704 if (NestedLoopCount == 0)
7705 return StmtError();
7706
7707 assert((CurContext->isDependentContext() || B.builtAll()) &&
7708 "omp target simd loop exprs were not built");
7709
7710 if (!CurContext->isDependentContext()) {
7711 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007712 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007713 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Li986330c2016-07-20 22:57:10 +00007714 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7715 B.NumIterations, *this, CurScope,
7716 DSAStack))
7717 return StmtError();
7718 }
7719 }
7720
7721 if (checkSimdlenSafelenSpecified(*this, Clauses))
7722 return StmtError();
7723
Reid Kleckner87a31802018-03-12 21:43:02 +00007724 setFunctionHasBranchProtectedScope();
Kelvin Li986330c2016-07-20 22:57:10 +00007725 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
7726 NestedLoopCount, Clauses, AStmt, B);
7727}
7728
Kelvin Li02532872016-08-05 14:37:37 +00007729StmtResult Sema::ActOnOpenMPTeamsDistributeDirective(
7730 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007731 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li02532872016-08-05 14:37:37 +00007732 if (!AStmt)
7733 return StmtError();
7734
Alexey Bataeve3727102018-04-18 15:57:46 +00007735 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li02532872016-08-05 14:37:37 +00007736 // 1.2.2 OpenMP Language Terminology
7737 // Structured block - An executable statement with a single entry at the
7738 // top and a single exit at the bottom.
7739 // The point of exit cannot be a branch out of the structured block.
7740 // longjmp() and throw() must not violate the entry/exit criteria.
7741 CS->getCapturedDecl()->setNothrow();
Alexey Bataev95c6dd42017-11-29 15:14:16 +00007742 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute);
7743 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7744 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7745 // 1.2.2 OpenMP Language Terminology
7746 // Structured block - An executable statement with a single entry at the
7747 // top and a single exit at the bottom.
7748 // The point of exit cannot be a branch out of the structured block.
7749 // longjmp() and throw() must not violate the entry/exit criteria.
7750 CS->getCapturedDecl()->setNothrow();
7751 }
Kelvin Li02532872016-08-05 14:37:37 +00007752
7753 OMPLoopDirective::HelperExprs B;
7754 // In presence of clause 'collapse' with number of loops, it will
7755 // define the nested loops number.
7756 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007757 checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses),
Alexey Bataev95c6dd42017-11-29 15:14:16 +00007758 nullptr /*ordered not a clause on distribute*/, CS, *this,
7759 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li02532872016-08-05 14:37:37 +00007760 if (NestedLoopCount == 0)
7761 return StmtError();
7762
7763 assert((CurContext->isDependentContext() || B.builtAll()) &&
7764 "omp teams distribute loop exprs were not built");
7765
Reid Kleckner87a31802018-03-12 21:43:02 +00007766 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007767
7768 DSAStack->setParentTeamsRegionLoc(StartLoc);
7769
David Majnemer9d168222016-08-05 17:44:54 +00007770 return OMPTeamsDistributeDirective::Create(
7771 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Kelvin Li02532872016-08-05 14:37:37 +00007772}
7773
Kelvin Li4e325f72016-10-25 12:50:55 +00007774StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective(
7775 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007776 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li4e325f72016-10-25 12:50:55 +00007777 if (!AStmt)
7778 return StmtError();
7779
Alexey Bataeve3727102018-04-18 15:57:46 +00007780 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li4e325f72016-10-25 12:50:55 +00007781 // 1.2.2 OpenMP Language Terminology
7782 // Structured block - An executable statement with a single entry at the
7783 // top and a single exit at the bottom.
7784 // The point of exit cannot be a branch out of the structured block.
7785 // longjmp() and throw() must not violate the entry/exit criteria.
7786 CS->getCapturedDecl()->setNothrow();
Alexey Bataev999277a2017-12-06 14:31:09 +00007787 for (int ThisCaptureLevel =
7788 getOpenMPCaptureLevels(OMPD_teams_distribute_simd);
7789 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7790 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7791 // 1.2.2 OpenMP Language Terminology
7792 // Structured block - An executable statement with a single entry at the
7793 // top and a single exit at the bottom.
7794 // The point of exit cannot be a branch out of the structured block.
7795 // longjmp() and throw() must not violate the entry/exit criteria.
7796 CS->getCapturedDecl()->setNothrow();
7797 }
7798
Kelvin Li4e325f72016-10-25 12:50:55 +00007799
7800 OMPLoopDirective::HelperExprs B;
7801 // In presence of clause 'collapse' with number of loops, it will
7802 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007803 unsigned NestedLoopCount = checkOpenMPLoop(
Samuel Antao4c8035b2016-12-12 18:00:20 +00007804 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev999277a2017-12-06 14:31:09 +00007805 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Samuel Antao4c8035b2016-12-12 18:00:20 +00007806 VarsWithImplicitDSA, B);
Kelvin Li4e325f72016-10-25 12:50:55 +00007807
7808 if (NestedLoopCount == 0)
7809 return StmtError();
7810
7811 assert((CurContext->isDependentContext() || B.builtAll()) &&
7812 "omp teams distribute simd loop exprs were not built");
7813
7814 if (!CurContext->isDependentContext()) {
7815 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007816 for (OMPClause *C : Clauses) {
Kelvin Li4e325f72016-10-25 12:50:55 +00007817 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7818 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7819 B.NumIterations, *this, CurScope,
7820 DSAStack))
7821 return StmtError();
7822 }
7823 }
7824
7825 if (checkSimdlenSafelenSpecified(*this, Clauses))
7826 return StmtError();
7827
Reid Kleckner87a31802018-03-12 21:43:02 +00007828 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007829
7830 DSAStack->setParentTeamsRegionLoc(StartLoc);
7831
Kelvin Li4e325f72016-10-25 12:50:55 +00007832 return OMPTeamsDistributeSimdDirective::Create(
7833 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7834}
7835
Kelvin Li579e41c2016-11-30 23:51:03 +00007836StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective(
7837 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007838 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li579e41c2016-11-30 23:51:03 +00007839 if (!AStmt)
7840 return StmtError();
7841
Alexey Bataeve3727102018-04-18 15:57:46 +00007842 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li579e41c2016-11-30 23:51:03 +00007843 // 1.2.2 OpenMP Language Terminology
7844 // Structured block - An executable statement with a single entry at the
7845 // top and a single exit at the bottom.
7846 // The point of exit cannot be a branch out of the structured block.
7847 // longjmp() and throw() must not violate the entry/exit criteria.
7848 CS->getCapturedDecl()->setNothrow();
7849
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00007850 for (int ThisCaptureLevel =
7851 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd);
7852 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7853 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7854 // 1.2.2 OpenMP Language Terminology
7855 // Structured block - An executable statement with a single entry at the
7856 // top and a single exit at the bottom.
7857 // The point of exit cannot be a branch out of the structured block.
7858 // longjmp() and throw() must not violate the entry/exit criteria.
7859 CS->getCapturedDecl()->setNothrow();
7860 }
7861
Kelvin Li579e41c2016-11-30 23:51:03 +00007862 OMPLoopDirective::HelperExprs B;
7863 // In presence of clause 'collapse' with number of loops, it will
7864 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007865 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li579e41c2016-11-30 23:51:03 +00007866 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00007867 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li579e41c2016-11-30 23:51:03 +00007868 VarsWithImplicitDSA, B);
7869
7870 if (NestedLoopCount == 0)
7871 return StmtError();
7872
7873 assert((CurContext->isDependentContext() || B.builtAll()) &&
7874 "omp for loop exprs were not built");
7875
7876 if (!CurContext->isDependentContext()) {
7877 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007878 for (OMPClause *C : Clauses) {
Kelvin Li579e41c2016-11-30 23:51:03 +00007879 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7880 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7881 B.NumIterations, *this, CurScope,
7882 DSAStack))
7883 return StmtError();
7884 }
7885 }
7886
7887 if (checkSimdlenSafelenSpecified(*this, Clauses))
7888 return StmtError();
7889
Reid Kleckner87a31802018-03-12 21:43:02 +00007890 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007891
7892 DSAStack->setParentTeamsRegionLoc(StartLoc);
7893
Kelvin Li579e41c2016-11-30 23:51:03 +00007894 return OMPTeamsDistributeParallelForSimdDirective::Create(
7895 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7896}
7897
Kelvin Li7ade93f2016-12-09 03:24:30 +00007898StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective(
7899 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007900 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li7ade93f2016-12-09 03:24:30 +00007901 if (!AStmt)
7902 return StmtError();
7903
Alexey Bataeve3727102018-04-18 15:57:46 +00007904 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li7ade93f2016-12-09 03:24:30 +00007905 // 1.2.2 OpenMP Language Terminology
7906 // Structured block - An executable statement with a single entry at the
7907 // top and a single exit at the bottom.
7908 // The point of exit cannot be a branch out of the structured block.
7909 // longjmp() and throw() must not violate the entry/exit criteria.
7910 CS->getCapturedDecl()->setNothrow();
7911
Carlo Bertolli62fae152017-11-20 20:46:39 +00007912 for (int ThisCaptureLevel =
7913 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for);
7914 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7915 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7916 // 1.2.2 OpenMP Language Terminology
7917 // Structured block - An executable statement with a single entry at the
7918 // top and a single exit at the bottom.
7919 // The point of exit cannot be a branch out of the structured block.
7920 // longjmp() and throw() must not violate the entry/exit criteria.
7921 CS->getCapturedDecl()->setNothrow();
7922 }
7923
Kelvin Li7ade93f2016-12-09 03:24:30 +00007924 OMPLoopDirective::HelperExprs B;
7925 // In presence of clause 'collapse' with number of loops, it will
7926 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007927 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li7ade93f2016-12-09 03:24:30 +00007928 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
Carlo Bertolli62fae152017-11-20 20:46:39 +00007929 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li7ade93f2016-12-09 03:24:30 +00007930 VarsWithImplicitDSA, B);
7931
7932 if (NestedLoopCount == 0)
7933 return StmtError();
7934
7935 assert((CurContext->isDependentContext() || B.builtAll()) &&
7936 "omp for loop exprs were not built");
7937
Reid Kleckner87a31802018-03-12 21:43:02 +00007938 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007939
7940 DSAStack->setParentTeamsRegionLoc(StartLoc);
7941
Kelvin Li7ade93f2016-12-09 03:24:30 +00007942 return OMPTeamsDistributeParallelForDirective::Create(
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00007943 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7944 DSAStack->isCancelRegion());
Kelvin Li7ade93f2016-12-09 03:24:30 +00007945}
7946
Kelvin Libf594a52016-12-17 05:48:59 +00007947StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses,
7948 Stmt *AStmt,
7949 SourceLocation StartLoc,
7950 SourceLocation EndLoc) {
7951 if (!AStmt)
7952 return StmtError();
7953
Alexey Bataeve3727102018-04-18 15:57:46 +00007954 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Libf594a52016-12-17 05:48:59 +00007955 // 1.2.2 OpenMP Language Terminology
7956 // Structured block - An executable statement with a single entry at the
7957 // top and a single exit at the bottom.
7958 // The point of exit cannot be a branch out of the structured block.
7959 // longjmp() and throw() must not violate the entry/exit criteria.
7960 CS->getCapturedDecl()->setNothrow();
7961
Alexey Bataevf9fc42e2017-11-22 14:25:55 +00007962 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams);
7963 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7964 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7965 // 1.2.2 OpenMP Language Terminology
7966 // Structured block - An executable statement with a single entry at the
7967 // top and a single exit at the bottom.
7968 // The point of exit cannot be a branch out of the structured block.
7969 // longjmp() and throw() must not violate the entry/exit criteria.
7970 CS->getCapturedDecl()->setNothrow();
7971 }
Reid Kleckner87a31802018-03-12 21:43:02 +00007972 setFunctionHasBranchProtectedScope();
Kelvin Libf594a52016-12-17 05:48:59 +00007973
7974 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses,
7975 AStmt);
7976}
7977
Kelvin Li83c451e2016-12-25 04:52:54 +00007978StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective(
7979 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007980 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li83c451e2016-12-25 04:52:54 +00007981 if (!AStmt)
7982 return StmtError();
7983
Alexey Bataeve3727102018-04-18 15:57:46 +00007984 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li83c451e2016-12-25 04:52:54 +00007985 // 1.2.2 OpenMP Language Terminology
7986 // Structured block - An executable statement with a single entry at the
7987 // top and a single exit at the bottom.
7988 // The point of exit cannot be a branch out of the structured block.
7989 // longjmp() and throw() must not violate the entry/exit criteria.
7990 CS->getCapturedDecl()->setNothrow();
Alexey Bataevdfa430f2017-12-08 15:03:50 +00007991 for (int ThisCaptureLevel =
7992 getOpenMPCaptureLevels(OMPD_target_teams_distribute);
7993 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7994 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7995 // 1.2.2 OpenMP Language Terminology
7996 // Structured block - An executable statement with a single entry at the
7997 // top and a single exit at the bottom.
7998 // The point of exit cannot be a branch out of the structured block.
7999 // longjmp() and throw() must not violate the entry/exit criteria.
8000 CS->getCapturedDecl()->setNothrow();
8001 }
Kelvin Li83c451e2016-12-25 04:52:54 +00008002
8003 OMPLoopDirective::HelperExprs B;
8004 // In presence of clause 'collapse' with number of loops, it will
8005 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00008006 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataevdfa430f2017-12-08 15:03:50 +00008007 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses),
8008 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li83c451e2016-12-25 04:52:54 +00008009 VarsWithImplicitDSA, B);
8010 if (NestedLoopCount == 0)
8011 return StmtError();
8012
8013 assert((CurContext->isDependentContext() || B.builtAll()) &&
8014 "omp target teams distribute loop exprs were not built");
8015
Reid Kleckner87a31802018-03-12 21:43:02 +00008016 setFunctionHasBranchProtectedScope();
Kelvin Li83c451e2016-12-25 04:52:54 +00008017 return OMPTargetTeamsDistributeDirective::Create(
8018 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
8019}
8020
Kelvin Li80e8f562016-12-29 22:16:30 +00008021StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective(
8022 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00008023 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li80e8f562016-12-29 22:16:30 +00008024 if (!AStmt)
8025 return StmtError();
8026
Alexey Bataeve3727102018-04-18 15:57:46 +00008027 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li80e8f562016-12-29 22:16:30 +00008028 // 1.2.2 OpenMP Language Terminology
8029 // Structured block - An executable statement with a single entry at the
8030 // top and a single exit at the bottom.
8031 // The point of exit cannot be a branch out of the structured block.
8032 // longjmp() and throw() must not violate the entry/exit criteria.
8033 CS->getCapturedDecl()->setNothrow();
Carlo Bertolli52978c32018-01-03 21:12:44 +00008034 for (int ThisCaptureLevel =
8035 getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for);
8036 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8037 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8038 // 1.2.2 OpenMP Language Terminology
8039 // Structured block - An executable statement with a single entry at the
8040 // top and a single exit at the bottom.
8041 // The point of exit cannot be a branch out of the structured block.
8042 // longjmp() and throw() must not violate the entry/exit criteria.
8043 CS->getCapturedDecl()->setNothrow();
8044 }
8045
Kelvin Li80e8f562016-12-29 22:16:30 +00008046 OMPLoopDirective::HelperExprs B;
8047 // In presence of clause 'collapse' with number of loops, it will
8048 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00008049 unsigned NestedLoopCount = checkOpenMPLoop(
Carlo Bertolli52978c32018-01-03 21:12:44 +00008050 OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
8051 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li80e8f562016-12-29 22:16:30 +00008052 VarsWithImplicitDSA, B);
8053 if (NestedLoopCount == 0)
8054 return StmtError();
8055
8056 assert((CurContext->isDependentContext() || B.builtAll()) &&
8057 "omp target teams distribute parallel for loop exprs were not built");
8058
Alexey Bataev647dd842018-01-15 20:59:40 +00008059 if (!CurContext->isDependentContext()) {
8060 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00008061 for (OMPClause *C : Clauses) {
Alexey Bataev647dd842018-01-15 20:59:40 +00008062 if (auto *LC = dyn_cast<OMPLinearClause>(C))
8063 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8064 B.NumIterations, *this, CurScope,
8065 DSAStack))
8066 return StmtError();
8067 }
8068 }
8069
Reid Kleckner87a31802018-03-12 21:43:02 +00008070 setFunctionHasBranchProtectedScope();
Kelvin Li80e8f562016-12-29 22:16:30 +00008071 return OMPTargetTeamsDistributeParallelForDirective::Create(
Alexey Bataev16e79882017-11-22 21:12:03 +00008072 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
8073 DSAStack->isCancelRegion());
Kelvin Li80e8f562016-12-29 22:16:30 +00008074}
8075
Kelvin Li1851df52017-01-03 05:23:48 +00008076StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
8077 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00008078 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li1851df52017-01-03 05:23:48 +00008079 if (!AStmt)
8080 return StmtError();
8081
Alexey Bataeve3727102018-04-18 15:57:46 +00008082 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li1851df52017-01-03 05:23:48 +00008083 // 1.2.2 OpenMP Language Terminology
8084 // Structured block - An executable statement with a single entry at the
8085 // top and a single exit at the bottom.
8086 // The point of exit cannot be a branch out of the structured block.
8087 // longjmp() and throw() must not violate the entry/exit criteria.
8088 CS->getCapturedDecl()->setNothrow();
Alexey Bataev647dd842018-01-15 20:59:40 +00008089 for (int ThisCaptureLevel = getOpenMPCaptureLevels(
8090 OMPD_target_teams_distribute_parallel_for_simd);
8091 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8092 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8093 // 1.2.2 OpenMP Language Terminology
8094 // Structured block - An executable statement with a single entry at the
8095 // top and a single exit at the bottom.
8096 // The point of exit cannot be a branch out of the structured block.
8097 // longjmp() and throw() must not violate the entry/exit criteria.
8098 CS->getCapturedDecl()->setNothrow();
8099 }
Kelvin Li1851df52017-01-03 05:23:48 +00008100
8101 OMPLoopDirective::HelperExprs B;
8102 // In presence of clause 'collapse' with number of loops, it will
8103 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00008104 unsigned NestedLoopCount =
8105 checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd,
Alexey Bataev647dd842018-01-15 20:59:40 +00008106 getCollapseNumberExpr(Clauses),
8107 nullptr /*ordered not a clause on distribute*/, CS, *this,
8108 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li1851df52017-01-03 05:23:48 +00008109 if (NestedLoopCount == 0)
8110 return StmtError();
8111
8112 assert((CurContext->isDependentContext() || B.builtAll()) &&
8113 "omp target teams distribute parallel for simd loop exprs were not "
8114 "built");
8115
8116 if (!CurContext->isDependentContext()) {
8117 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00008118 for (OMPClause *C : Clauses) {
Kelvin Li1851df52017-01-03 05:23:48 +00008119 if (auto *LC = dyn_cast<OMPLinearClause>(C))
8120 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8121 B.NumIterations, *this, CurScope,
8122 DSAStack))
8123 return StmtError();
8124 }
8125 }
8126
Alexey Bataev438388c2017-11-22 18:34:02 +00008127 if (checkSimdlenSafelenSpecified(*this, Clauses))
8128 return StmtError();
8129
Reid Kleckner87a31802018-03-12 21:43:02 +00008130 setFunctionHasBranchProtectedScope();
Kelvin Li1851df52017-01-03 05:23:48 +00008131 return OMPTargetTeamsDistributeParallelForSimdDirective::Create(
8132 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
8133}
8134
Kelvin Lida681182017-01-10 18:08:18 +00008135StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective(
8136 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00008137 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Lida681182017-01-10 18:08:18 +00008138 if (!AStmt)
8139 return StmtError();
8140
8141 auto *CS = cast<CapturedStmt>(AStmt);
8142 // 1.2.2 OpenMP Language Terminology
8143 // Structured block - An executable statement with a single entry at the
8144 // top and a single exit at the bottom.
8145 // The point of exit cannot be a branch out of the structured block.
8146 // longjmp() and throw() must not violate the entry/exit criteria.
8147 CS->getCapturedDecl()->setNothrow();
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00008148 for (int ThisCaptureLevel =
8149 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd);
8150 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8151 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8152 // 1.2.2 OpenMP Language Terminology
8153 // Structured block - An executable statement with a single entry at the
8154 // top and a single exit at the bottom.
8155 // The point of exit cannot be a branch out of the structured block.
8156 // longjmp() and throw() must not violate the entry/exit criteria.
8157 CS->getCapturedDecl()->setNothrow();
8158 }
Kelvin Lida681182017-01-10 18:08:18 +00008159
8160 OMPLoopDirective::HelperExprs B;
8161 // In presence of clause 'collapse' with number of loops, it will
8162 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00008163 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Lida681182017-01-10 18:08:18 +00008164 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00008165 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Lida681182017-01-10 18:08:18 +00008166 VarsWithImplicitDSA, B);
8167 if (NestedLoopCount == 0)
8168 return StmtError();
8169
8170 assert((CurContext->isDependentContext() || B.builtAll()) &&
8171 "omp target teams distribute simd loop exprs were not built");
8172
Alexey Bataev438388c2017-11-22 18:34:02 +00008173 if (!CurContext->isDependentContext()) {
8174 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00008175 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00008176 if (auto *LC = dyn_cast<OMPLinearClause>(C))
8177 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8178 B.NumIterations, *this, CurScope,
8179 DSAStack))
8180 return StmtError();
8181 }
8182 }
8183
8184 if (checkSimdlenSafelenSpecified(*this, Clauses))
8185 return StmtError();
8186
Reid Kleckner87a31802018-03-12 21:43:02 +00008187 setFunctionHasBranchProtectedScope();
Kelvin Lida681182017-01-10 18:08:18 +00008188 return OMPTargetTeamsDistributeSimdDirective::Create(
8189 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
8190}
8191
Alexey Bataeved09d242014-05-28 05:53:51 +00008192OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008193 SourceLocation StartLoc,
8194 SourceLocation LParenLoc,
8195 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008196 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008197 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00008198 case OMPC_final:
8199 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
8200 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00008201 case OMPC_num_threads:
8202 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
8203 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00008204 case OMPC_safelen:
8205 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
8206 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00008207 case OMPC_simdlen:
8208 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
8209 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00008210 case OMPC_collapse:
8211 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
8212 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00008213 case OMPC_ordered:
8214 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
8215 break;
Michael Wonge710d542015-08-07 16:16:36 +00008216 case OMPC_device:
8217 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
8218 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00008219 case OMPC_num_teams:
8220 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
8221 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008222 case OMPC_thread_limit:
8223 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
8224 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00008225 case OMPC_priority:
8226 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
8227 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008228 case OMPC_grainsize:
8229 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
8230 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00008231 case OMPC_num_tasks:
8232 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
8233 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00008234 case OMPC_hint:
8235 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
8236 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008237 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008238 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008239 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008240 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008241 case OMPC_private:
8242 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00008243 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008244 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00008245 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008246 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008247 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00008248 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008249 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008250 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008251 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00008252 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008253 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008254 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008255 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00008256 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008257 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008258 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008259 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008260 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008261 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008262 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00008263 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008264 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008265 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00008266 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008267 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008268 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008269 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008270 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00008271 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00008272 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00008273 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00008274 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00008275 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00008276 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00008277 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00008278 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00008279 case OMPC_atomic_default_mem_order:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008280 llvm_unreachable("Clause is not allowed.");
8281 }
8282 return Res;
8283}
8284
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008285// An OpenMP directive such as 'target parallel' has two captured regions:
8286// for the 'target' and 'parallel' respectively. This function returns
8287// the region in which to capture expressions associated with a clause.
8288// A return value of OMPD_unknown signifies that the expression should not
8289// be captured.
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008290static OpenMPDirectiveKind getOpenMPCaptureRegionForClause(
8291 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
8292 OpenMPDirectiveKind NameModifier = OMPD_unknown) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008293 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008294 switch (CKind) {
8295 case OMPC_if:
8296 switch (DKind) {
8297 case OMPD_target_parallel:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008298 case OMPD_target_parallel_for:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00008299 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008300 // If this clause applies to the nested 'parallel' region, capture within
8301 // the 'target' region, otherwise do not capture.
8302 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
8303 CaptureRegion = OMPD_target;
8304 break;
Carlo Bertolli52978c32018-01-03 21:12:44 +00008305 case OMPD_target_teams_distribute_parallel_for:
8306 case OMPD_target_teams_distribute_parallel_for_simd:
8307 // If this clause applies to the nested 'parallel' region, capture within
8308 // the 'teams' region, otherwise do not capture.
8309 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
8310 CaptureRegion = OMPD_teams;
8311 break;
Carlo Bertolli62fae152017-11-20 20:46:39 +00008312 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008313 case OMPD_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008314 CaptureRegion = OMPD_teams;
8315 break;
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008316 case OMPD_target_update:
Alexey Bataevfab20e42017-12-27 18:49:38 +00008317 case OMPD_target_enter_data:
8318 case OMPD_target_exit_data:
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008319 CaptureRegion = OMPD_task;
8320 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008321 case OMPD_cancel:
8322 case OMPD_parallel:
8323 case OMPD_parallel_sections:
8324 case OMPD_parallel_for:
8325 case OMPD_parallel_for_simd:
8326 case OMPD_target:
8327 case OMPD_target_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008328 case OMPD_target_teams:
8329 case OMPD_target_teams_distribute:
8330 case OMPD_target_teams_distribute_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008331 case OMPD_distribute_parallel_for:
8332 case OMPD_distribute_parallel_for_simd:
8333 case OMPD_task:
8334 case OMPD_taskloop:
8335 case OMPD_taskloop_simd:
8336 case OMPD_target_data:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008337 // Do not capture if-clause expressions.
8338 break;
8339 case OMPD_threadprivate:
8340 case OMPD_taskyield:
8341 case OMPD_barrier:
8342 case OMPD_taskwait:
8343 case OMPD_cancellation_point:
8344 case OMPD_flush:
8345 case OMPD_declare_reduction:
8346 case OMPD_declare_simd:
8347 case OMPD_declare_target:
8348 case OMPD_end_declare_target:
8349 case OMPD_teams:
8350 case OMPD_simd:
8351 case OMPD_for:
8352 case OMPD_for_simd:
8353 case OMPD_sections:
8354 case OMPD_section:
8355 case OMPD_single:
8356 case OMPD_master:
8357 case OMPD_critical:
8358 case OMPD_taskgroup:
8359 case OMPD_distribute:
8360 case OMPD_ordered:
8361 case OMPD_atomic:
8362 case OMPD_distribute_simd:
8363 case OMPD_teams_distribute:
8364 case OMPD_teams_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008365 case OMPD_requires:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008366 llvm_unreachable("Unexpected OpenMP directive with if-clause");
8367 case OMPD_unknown:
8368 llvm_unreachable("Unknown OpenMP directive");
8369 }
8370 break;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008371 case OMPC_num_threads:
8372 switch (DKind) {
8373 case OMPD_target_parallel:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008374 case OMPD_target_parallel_for:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00008375 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008376 CaptureRegion = OMPD_target;
8377 break;
Carlo Bertolli62fae152017-11-20 20:46:39 +00008378 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008379 case OMPD_teams_distribute_parallel_for_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008380 case OMPD_target_teams_distribute_parallel_for:
8381 case OMPD_target_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008382 CaptureRegion = OMPD_teams;
8383 break;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008384 case OMPD_parallel:
8385 case OMPD_parallel_sections:
8386 case OMPD_parallel_for:
8387 case OMPD_parallel_for_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008388 case OMPD_distribute_parallel_for:
8389 case OMPD_distribute_parallel_for_simd:
8390 // Do not capture num_threads-clause expressions.
8391 break;
8392 case OMPD_target_data:
8393 case OMPD_target_enter_data:
8394 case OMPD_target_exit_data:
8395 case OMPD_target_update:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008396 case OMPD_target:
8397 case OMPD_target_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008398 case OMPD_target_teams:
8399 case OMPD_target_teams_distribute:
8400 case OMPD_target_teams_distribute_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008401 case OMPD_cancel:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008402 case OMPD_task:
8403 case OMPD_taskloop:
8404 case OMPD_taskloop_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008405 case OMPD_threadprivate:
8406 case OMPD_taskyield:
8407 case OMPD_barrier:
8408 case OMPD_taskwait:
8409 case OMPD_cancellation_point:
8410 case OMPD_flush:
8411 case OMPD_declare_reduction:
8412 case OMPD_declare_simd:
8413 case OMPD_declare_target:
8414 case OMPD_end_declare_target:
8415 case OMPD_teams:
8416 case OMPD_simd:
8417 case OMPD_for:
8418 case OMPD_for_simd:
8419 case OMPD_sections:
8420 case OMPD_section:
8421 case OMPD_single:
8422 case OMPD_master:
8423 case OMPD_critical:
8424 case OMPD_taskgroup:
8425 case OMPD_distribute:
8426 case OMPD_ordered:
8427 case OMPD_atomic:
8428 case OMPD_distribute_simd:
8429 case OMPD_teams_distribute:
8430 case OMPD_teams_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008431 case OMPD_requires:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008432 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause");
8433 case OMPD_unknown:
8434 llvm_unreachable("Unknown OpenMP directive");
8435 }
8436 break;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008437 case OMPC_num_teams:
8438 switch (DKind) {
8439 case OMPD_target_teams:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008440 case OMPD_target_teams_distribute:
8441 case OMPD_target_teams_distribute_simd:
8442 case OMPD_target_teams_distribute_parallel_for:
8443 case OMPD_target_teams_distribute_parallel_for_simd:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008444 CaptureRegion = OMPD_target;
8445 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008446 case OMPD_teams_distribute_parallel_for:
8447 case OMPD_teams_distribute_parallel_for_simd:
8448 case OMPD_teams:
8449 case OMPD_teams_distribute:
8450 case OMPD_teams_distribute_simd:
8451 // Do not capture num_teams-clause expressions.
8452 break;
8453 case OMPD_distribute_parallel_for:
8454 case OMPD_distribute_parallel_for_simd:
8455 case OMPD_task:
8456 case OMPD_taskloop:
8457 case OMPD_taskloop_simd:
8458 case OMPD_target_data:
8459 case OMPD_target_enter_data:
8460 case OMPD_target_exit_data:
8461 case OMPD_target_update:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008462 case OMPD_cancel:
8463 case OMPD_parallel:
8464 case OMPD_parallel_sections:
8465 case OMPD_parallel_for:
8466 case OMPD_parallel_for_simd:
8467 case OMPD_target:
8468 case OMPD_target_simd:
8469 case OMPD_target_parallel:
8470 case OMPD_target_parallel_for:
8471 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008472 case OMPD_threadprivate:
8473 case OMPD_taskyield:
8474 case OMPD_barrier:
8475 case OMPD_taskwait:
8476 case OMPD_cancellation_point:
8477 case OMPD_flush:
8478 case OMPD_declare_reduction:
8479 case OMPD_declare_simd:
8480 case OMPD_declare_target:
8481 case OMPD_end_declare_target:
8482 case OMPD_simd:
8483 case OMPD_for:
8484 case OMPD_for_simd:
8485 case OMPD_sections:
8486 case OMPD_section:
8487 case OMPD_single:
8488 case OMPD_master:
8489 case OMPD_critical:
8490 case OMPD_taskgroup:
8491 case OMPD_distribute:
8492 case OMPD_ordered:
8493 case OMPD_atomic:
8494 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008495 case OMPD_requires:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008496 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
8497 case OMPD_unknown:
8498 llvm_unreachable("Unknown OpenMP directive");
8499 }
8500 break;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008501 case OMPC_thread_limit:
8502 switch (DKind) {
8503 case OMPD_target_teams:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008504 case OMPD_target_teams_distribute:
8505 case OMPD_target_teams_distribute_simd:
8506 case OMPD_target_teams_distribute_parallel_for:
8507 case OMPD_target_teams_distribute_parallel_for_simd:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008508 CaptureRegion = OMPD_target;
8509 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008510 case OMPD_teams_distribute_parallel_for:
8511 case OMPD_teams_distribute_parallel_for_simd:
8512 case OMPD_teams:
8513 case OMPD_teams_distribute:
8514 case OMPD_teams_distribute_simd:
8515 // Do not capture thread_limit-clause expressions.
8516 break;
8517 case OMPD_distribute_parallel_for:
8518 case OMPD_distribute_parallel_for_simd:
8519 case OMPD_task:
8520 case OMPD_taskloop:
8521 case OMPD_taskloop_simd:
8522 case OMPD_target_data:
8523 case OMPD_target_enter_data:
8524 case OMPD_target_exit_data:
8525 case OMPD_target_update:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008526 case OMPD_cancel:
8527 case OMPD_parallel:
8528 case OMPD_parallel_sections:
8529 case OMPD_parallel_for:
8530 case OMPD_parallel_for_simd:
8531 case OMPD_target:
8532 case OMPD_target_simd:
8533 case OMPD_target_parallel:
8534 case OMPD_target_parallel_for:
8535 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008536 case OMPD_threadprivate:
8537 case OMPD_taskyield:
8538 case OMPD_barrier:
8539 case OMPD_taskwait:
8540 case OMPD_cancellation_point:
8541 case OMPD_flush:
8542 case OMPD_declare_reduction:
8543 case OMPD_declare_simd:
8544 case OMPD_declare_target:
8545 case OMPD_end_declare_target:
8546 case OMPD_simd:
8547 case OMPD_for:
8548 case OMPD_for_simd:
8549 case OMPD_sections:
8550 case OMPD_section:
8551 case OMPD_single:
8552 case OMPD_master:
8553 case OMPD_critical:
8554 case OMPD_taskgroup:
8555 case OMPD_distribute:
8556 case OMPD_ordered:
8557 case OMPD_atomic:
8558 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008559 case OMPD_requires:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008560 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause");
8561 case OMPD_unknown:
8562 llvm_unreachable("Unknown OpenMP directive");
8563 }
8564 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008565 case OMPC_schedule:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008566 switch (DKind) {
Alexey Bataev2ba67042017-11-28 21:11:44 +00008567 case OMPD_parallel_for:
8568 case OMPD_parallel_for_simd:
Alexey Bataev7f96c372017-11-22 17:19:31 +00008569 case OMPD_distribute_parallel_for:
Alexey Bataev974acd62017-11-27 19:38:52 +00008570 case OMPD_distribute_parallel_for_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008571 case OMPD_teams_distribute_parallel_for:
8572 case OMPD_teams_distribute_parallel_for_simd:
8573 case OMPD_target_parallel_for:
8574 case OMPD_target_parallel_for_simd:
8575 case OMPD_target_teams_distribute_parallel_for:
8576 case OMPD_target_teams_distribute_parallel_for_simd:
Alexey Bataev7f96c372017-11-22 17:19:31 +00008577 CaptureRegion = OMPD_parallel;
8578 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008579 case OMPD_for:
8580 case OMPD_for_simd:
8581 // Do not capture schedule-clause expressions.
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008582 break;
8583 case OMPD_task:
8584 case OMPD_taskloop:
8585 case OMPD_taskloop_simd:
8586 case OMPD_target_data:
8587 case OMPD_target_enter_data:
8588 case OMPD_target_exit_data:
8589 case OMPD_target_update:
8590 case OMPD_teams:
8591 case OMPD_teams_distribute:
8592 case OMPD_teams_distribute_simd:
8593 case OMPD_target_teams_distribute:
8594 case OMPD_target_teams_distribute_simd:
8595 case OMPD_target:
8596 case OMPD_target_simd:
8597 case OMPD_target_parallel:
8598 case OMPD_cancel:
8599 case OMPD_parallel:
8600 case OMPD_parallel_sections:
8601 case OMPD_threadprivate:
8602 case OMPD_taskyield:
8603 case OMPD_barrier:
8604 case OMPD_taskwait:
8605 case OMPD_cancellation_point:
8606 case OMPD_flush:
8607 case OMPD_declare_reduction:
8608 case OMPD_declare_simd:
8609 case OMPD_declare_target:
8610 case OMPD_end_declare_target:
8611 case OMPD_simd:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008612 case OMPD_sections:
8613 case OMPD_section:
8614 case OMPD_single:
8615 case OMPD_master:
8616 case OMPD_critical:
8617 case OMPD_taskgroup:
8618 case OMPD_distribute:
8619 case OMPD_ordered:
8620 case OMPD_atomic:
8621 case OMPD_distribute_simd:
8622 case OMPD_target_teams:
Kelvin Li1408f912018-09-26 04:28:39 +00008623 case OMPD_requires:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008624 llvm_unreachable("Unexpected OpenMP directive with schedule clause");
8625 case OMPD_unknown:
8626 llvm_unreachable("Unknown OpenMP directive");
8627 }
8628 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008629 case OMPC_dist_schedule:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008630 switch (DKind) {
8631 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008632 case OMPD_teams_distribute_parallel_for_simd:
8633 case OMPD_teams_distribute:
8634 case OMPD_teams_distribute_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008635 case OMPD_target_teams_distribute_parallel_for:
8636 case OMPD_target_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008637 case OMPD_target_teams_distribute:
8638 case OMPD_target_teams_distribute_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008639 CaptureRegion = OMPD_teams;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008640 break;
8641 case OMPD_distribute_parallel_for:
8642 case OMPD_distribute_parallel_for_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008643 case OMPD_distribute:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008644 case OMPD_distribute_simd:
8645 // Do not capture thread_limit-clause expressions.
8646 break;
8647 case OMPD_parallel_for:
8648 case OMPD_parallel_for_simd:
8649 case OMPD_target_parallel_for_simd:
8650 case OMPD_target_parallel_for:
8651 case OMPD_task:
8652 case OMPD_taskloop:
8653 case OMPD_taskloop_simd:
8654 case OMPD_target_data:
8655 case OMPD_target_enter_data:
8656 case OMPD_target_exit_data:
8657 case OMPD_target_update:
8658 case OMPD_teams:
8659 case OMPD_target:
8660 case OMPD_target_simd:
8661 case OMPD_target_parallel:
8662 case OMPD_cancel:
8663 case OMPD_parallel:
8664 case OMPD_parallel_sections:
8665 case OMPD_threadprivate:
8666 case OMPD_taskyield:
8667 case OMPD_barrier:
8668 case OMPD_taskwait:
8669 case OMPD_cancellation_point:
8670 case OMPD_flush:
8671 case OMPD_declare_reduction:
8672 case OMPD_declare_simd:
8673 case OMPD_declare_target:
8674 case OMPD_end_declare_target:
8675 case OMPD_simd:
8676 case OMPD_for:
8677 case OMPD_for_simd:
8678 case OMPD_sections:
8679 case OMPD_section:
8680 case OMPD_single:
8681 case OMPD_master:
8682 case OMPD_critical:
8683 case OMPD_taskgroup:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008684 case OMPD_ordered:
8685 case OMPD_atomic:
8686 case OMPD_target_teams:
Kelvin Li1408f912018-09-26 04:28:39 +00008687 case OMPD_requires:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008688 llvm_unreachable("Unexpected OpenMP directive with schedule clause");
8689 case OMPD_unknown:
8690 llvm_unreachable("Unknown OpenMP directive");
8691 }
8692 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008693 case OMPC_device:
8694 switch (DKind) {
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008695 case OMPD_target_update:
Alexey Bataevfab20e42017-12-27 18:49:38 +00008696 case OMPD_target_enter_data:
8697 case OMPD_target_exit_data:
Alexey Bataev8451efa2018-01-15 19:06:12 +00008698 case OMPD_target:
Alexey Bataevf41c88f2018-01-16 15:05:16 +00008699 case OMPD_target_simd:
Alexey Bataev0c869ef2018-01-16 15:57:07 +00008700 case OMPD_target_teams:
Alexey Bataev54d5c7d2018-01-16 16:27:49 +00008701 case OMPD_target_parallel:
Alexey Bataev79df7562018-01-16 16:46:46 +00008702 case OMPD_target_teams_distribute:
Alexey Bataev8d16a432018-01-16 17:22:50 +00008703 case OMPD_target_teams_distribute_simd:
Alexey Bataev8ed895512018-01-16 17:41:04 +00008704 case OMPD_target_parallel_for:
Alexey Bataevd60d1ba2018-01-16 17:55:15 +00008705 case OMPD_target_parallel_for_simd:
Alexey Bataev9f9fb0b2018-01-16 19:02:33 +00008706 case OMPD_target_teams_distribute_parallel_for:
Alexey Bataev9350fc32018-01-16 19:18:24 +00008707 case OMPD_target_teams_distribute_parallel_for_simd:
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008708 CaptureRegion = OMPD_task;
8709 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008710 case OMPD_target_data:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008711 // Do not capture device-clause expressions.
8712 break;
8713 case OMPD_teams_distribute_parallel_for:
8714 case OMPD_teams_distribute_parallel_for_simd:
8715 case OMPD_teams:
8716 case OMPD_teams_distribute:
8717 case OMPD_teams_distribute_simd:
8718 case OMPD_distribute_parallel_for:
8719 case OMPD_distribute_parallel_for_simd:
8720 case OMPD_task:
8721 case OMPD_taskloop:
8722 case OMPD_taskloop_simd:
8723 case OMPD_cancel:
8724 case OMPD_parallel:
8725 case OMPD_parallel_sections:
8726 case OMPD_parallel_for:
8727 case OMPD_parallel_for_simd:
8728 case OMPD_threadprivate:
8729 case OMPD_taskyield:
8730 case OMPD_barrier:
8731 case OMPD_taskwait:
8732 case OMPD_cancellation_point:
8733 case OMPD_flush:
8734 case OMPD_declare_reduction:
8735 case OMPD_declare_simd:
8736 case OMPD_declare_target:
8737 case OMPD_end_declare_target:
8738 case OMPD_simd:
8739 case OMPD_for:
8740 case OMPD_for_simd:
8741 case OMPD_sections:
8742 case OMPD_section:
8743 case OMPD_single:
8744 case OMPD_master:
8745 case OMPD_critical:
8746 case OMPD_taskgroup:
8747 case OMPD_distribute:
8748 case OMPD_ordered:
8749 case OMPD_atomic:
8750 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008751 case OMPD_requires:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008752 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
8753 case OMPD_unknown:
8754 llvm_unreachable("Unknown OpenMP directive");
8755 }
8756 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008757 case OMPC_firstprivate:
8758 case OMPC_lastprivate:
8759 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008760 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008761 case OMPC_in_reduction:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008762 case OMPC_linear:
8763 case OMPC_default:
8764 case OMPC_proc_bind:
8765 case OMPC_final:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008766 case OMPC_safelen:
8767 case OMPC_simdlen:
8768 case OMPC_collapse:
8769 case OMPC_private:
8770 case OMPC_shared:
8771 case OMPC_aligned:
8772 case OMPC_copyin:
8773 case OMPC_copyprivate:
8774 case OMPC_ordered:
8775 case OMPC_nowait:
8776 case OMPC_untied:
8777 case OMPC_mergeable:
8778 case OMPC_threadprivate:
8779 case OMPC_flush:
8780 case OMPC_read:
8781 case OMPC_write:
8782 case OMPC_update:
8783 case OMPC_capture:
8784 case OMPC_seq_cst:
8785 case OMPC_depend:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008786 case OMPC_threads:
8787 case OMPC_simd:
8788 case OMPC_map:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008789 case OMPC_priority:
8790 case OMPC_grainsize:
8791 case OMPC_nogroup:
8792 case OMPC_num_tasks:
8793 case OMPC_hint:
8794 case OMPC_defaultmap:
8795 case OMPC_unknown:
8796 case OMPC_uniform:
8797 case OMPC_to:
8798 case OMPC_from:
8799 case OMPC_use_device_ptr:
8800 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00008801 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00008802 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00008803 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00008804 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00008805 case OMPC_atomic_default_mem_order:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008806 llvm_unreachable("Unexpected OpenMP clause.");
8807 }
8808 return CaptureRegion;
8809}
8810
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008811OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
8812 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008813 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008814 SourceLocation NameModifierLoc,
8815 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008816 SourceLocation EndLoc) {
8817 Expr *ValExpr = Condition;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008818 Stmt *HelperValStmt = nullptr;
8819 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008820 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
8821 !Condition->isInstantiationDependent() &&
8822 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00008823 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008824 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008825 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008826
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008827 ValExpr = Val.get();
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008828
8829 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
8830 CaptureRegion =
8831 getOpenMPCaptureRegionForClause(DKind, OMPC_if, NameModifier);
Alexey Bataev2ba67042017-11-28 21:11:44 +00008832 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008833 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00008834 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008835 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8836 HelperValStmt = buildPreInits(Context, Captures);
8837 }
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008838 }
8839
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008840 return new (Context)
8841 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
8842 LParenLoc, NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008843}
8844
Alexey Bataev3778b602014-07-17 07:32:53 +00008845OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
8846 SourceLocation StartLoc,
8847 SourceLocation LParenLoc,
8848 SourceLocation EndLoc) {
8849 Expr *ValExpr = Condition;
8850 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
8851 !Condition->isInstantiationDependent() &&
8852 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00008853 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataev3778b602014-07-17 07:32:53 +00008854 if (Val.isInvalid())
8855 return nullptr;
8856
Richard Smith03a4aa32016-06-23 19:02:52 +00008857 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataev3778b602014-07-17 07:32:53 +00008858 }
8859
8860 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8861}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00008862ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
8863 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00008864 if (!Op)
8865 return ExprError();
8866
8867 class IntConvertDiagnoser : public ICEConvertDiagnoser {
8868 public:
8869 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00008870 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00008871 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
8872 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008873 return S.Diag(Loc, diag::err_omp_not_integral) << T;
8874 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008875 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
8876 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008877 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
8878 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008879 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
8880 QualType T,
8881 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008882 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
8883 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008884 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
8885 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008886 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00008887 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00008888 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008889 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
8890 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008891 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
8892 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008893 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
8894 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008895 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00008896 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00008897 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008898 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
8899 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008900 llvm_unreachable("conversion functions are permitted");
8901 }
8902 } ConvertDiagnoser;
8903 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
8904}
8905
Alexey Bataeve3727102018-04-18 15:57:46 +00008906static bool isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00008907 OpenMPClauseKind CKind,
8908 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008909 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
8910 !ValExpr->isInstantiationDependent()) {
8911 SourceLocation Loc = ValExpr->getExprLoc();
8912 ExprResult Value =
8913 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
8914 if (Value.isInvalid())
8915 return false;
8916
8917 ValExpr = Value.get();
8918 // The expression must evaluate to a non-negative integer value.
8919 llvm::APSInt Result;
8920 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00008921 Result.isSigned() &&
8922 !((!StrictlyPositive && Result.isNonNegative()) ||
8923 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008924 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00008925 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
8926 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008927 return false;
8928 }
8929 }
8930 return true;
8931}
8932
Alexey Bataev568a8332014-03-06 06:15:19 +00008933OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
8934 SourceLocation StartLoc,
8935 SourceLocation LParenLoc,
8936 SourceLocation EndLoc) {
8937 Expr *ValExpr = NumThreads;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008938 Stmt *HelperValStmt = nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00008939
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008940 // OpenMP [2.5, Restrictions]
8941 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +00008942 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
Alexey Bataeva0569352015-12-01 10:17:31 +00008943 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008944 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00008945
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008946 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +00008947 OpenMPDirectiveKind CaptureRegion =
8948 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads);
8949 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008950 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00008951 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008952 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8953 HelperValStmt = buildPreInits(Context, Captures);
8954 }
8955
8956 return new (Context) OMPNumThreadsClause(
8957 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00008958}
8959
Alexey Bataev62c87d22014-03-21 04:51:18 +00008960ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008961 OpenMPClauseKind CKind,
8962 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00008963 if (!E)
8964 return ExprError();
8965 if (E->isValueDependent() || E->isTypeDependent() ||
8966 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00008967 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00008968 llvm::APSInt Result;
8969 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
8970 if (ICE.isInvalid())
8971 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008972 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
8973 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00008974 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008975 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
8976 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00008977 return ExprError();
8978 }
Alexander Musman09184fe2014-09-30 05:29:28 +00008979 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
8980 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
8981 << E->getSourceRange();
8982 return ExprError();
8983 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008984 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
8985 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00008986 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008987 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00008988 return ICE;
8989}
8990
8991OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
8992 SourceLocation LParenLoc,
8993 SourceLocation EndLoc) {
8994 // OpenMP [2.8.1, simd construct, Description]
8995 // The parameter of the safelen clause must be a constant
8996 // positive integer expression.
8997 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
8998 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008999 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00009000 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00009001 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00009002}
9003
Alexey Bataev66b15b52015-08-21 11:14:16 +00009004OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
9005 SourceLocation LParenLoc,
9006 SourceLocation EndLoc) {
9007 // OpenMP [2.8.1, simd construct, Description]
9008 // The parameter of the simdlen clause must be a constant
9009 // positive integer expression.
9010 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
9011 if (Simdlen.isInvalid())
9012 return nullptr;
9013 return new (Context)
9014 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
9015}
9016
Alexander Musman64d33f12014-06-04 07:53:32 +00009017OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
9018 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00009019 SourceLocation LParenLoc,
9020 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00009021 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00009022 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00009023 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00009024 // The parameter of the collapse clause must be a constant
9025 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00009026 ExprResult NumForLoopsResult =
9027 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
9028 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00009029 return nullptr;
9030 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00009031 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00009032}
9033
Alexey Bataev10e775f2015-07-30 11:36:16 +00009034OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
9035 SourceLocation EndLoc,
9036 SourceLocation LParenLoc,
9037 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00009038 // OpenMP [2.7.1, loop construct, Description]
9039 // OpenMP [2.8.1, simd construct, Description]
9040 // OpenMP [2.9.6, distribute construct, Description]
9041 // The parameter of the ordered clause must be a constant
9042 // positive integer expression if any.
9043 if (NumForLoops && LParenLoc.isValid()) {
9044 ExprResult NumForLoopsResult =
9045 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
9046 if (NumForLoopsResult.isInvalid())
9047 return nullptr;
9048 NumForLoops = NumForLoopsResult.get();
Alexey Bataeve3727102018-04-18 15:57:46 +00009049 } else {
Alexey Bataev346265e2015-09-25 10:37:12 +00009050 NumForLoops = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +00009051 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00009052 auto *Clause = OMPOrderedClause::Create(
9053 Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0,
9054 StartLoc, LParenLoc, EndLoc);
9055 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause);
9056 return Clause;
Alexey Bataev10e775f2015-07-30 11:36:16 +00009057}
9058
Alexey Bataeved09d242014-05-28 05:53:51 +00009059OMPClause *Sema::ActOnOpenMPSimpleClause(
9060 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
9061 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009062 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009063 switch (Kind) {
9064 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00009065 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00009066 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
9067 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009068 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009069 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00009070 Res = ActOnOpenMPProcBindClause(
9071 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
9072 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009073 break;
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009074 case OMPC_atomic_default_mem_order:
9075 Res = ActOnOpenMPAtomicDefaultMemOrderClause(
9076 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument),
9077 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
9078 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009079 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00009080 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00009081 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00009082 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009083 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00009084 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009085 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009086 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009087 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00009088 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00009089 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00009090 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00009091 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00009092 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00009093 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009094 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009095 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009096 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009097 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00009098 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009099 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009100 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009101 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00009102 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009103 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00009104 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00009105 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00009106 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009107 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009108 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00009109 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00009110 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009111 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00009112 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009113 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009114 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009115 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009116 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00009117 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00009118 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009119 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00009120 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009121 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009122 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009123 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00009124 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00009125 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00009126 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00009127 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00009128 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00009129 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009130 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009131 case OMPC_dynamic_allocators:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009132 llvm_unreachable("Clause is not allowed.");
9133 }
9134 return Res;
9135}
9136
Alexey Bataev6402bca2015-12-28 07:25:51 +00009137static std::string
9138getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
9139 ArrayRef<unsigned> Exclude = llvm::None) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009140 SmallString<256> Buffer;
9141 llvm::raw_svector_ostream Out(Buffer);
Alexey Bataev6402bca2015-12-28 07:25:51 +00009142 unsigned Bound = Last >= 2 ? Last - 2 : 0;
9143 unsigned Skipped = Exclude.size();
9144 auto S = Exclude.begin(), E = Exclude.end();
Alexey Bataeve3727102018-04-18 15:57:46 +00009145 for (unsigned I = First; I < Last; ++I) {
9146 if (std::find(S, E, I) != E) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00009147 --Skipped;
9148 continue;
9149 }
Alexey Bataeve3727102018-04-18 15:57:46 +00009150 Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'";
9151 if (I == Bound - Skipped)
9152 Out << " or ";
9153 else if (I != Bound + 1 - Skipped)
9154 Out << ", ";
Alexey Bataev6402bca2015-12-28 07:25:51 +00009155 }
Alexey Bataeve3727102018-04-18 15:57:46 +00009156 return Out.str();
Alexey Bataev6402bca2015-12-28 07:25:51 +00009157}
9158
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009159OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
9160 SourceLocation KindKwLoc,
9161 SourceLocation StartLoc,
9162 SourceLocation LParenLoc,
9163 SourceLocation EndLoc) {
9164 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00009165 static_assert(OMPC_DEFAULT_unknown > 0,
9166 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009167 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009168 << getListOfPossibleValues(OMPC_default, /*First=*/0,
9169 /*Last=*/OMPC_DEFAULT_unknown)
9170 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009171 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009172 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00009173 switch (Kind) {
9174 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009175 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009176 break;
9177 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009178 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009179 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009180 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009181 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00009182 break;
9183 }
Alexey Bataeved09d242014-05-28 05:53:51 +00009184 return new (Context)
9185 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009186}
9187
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009188OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
9189 SourceLocation KindKwLoc,
9190 SourceLocation StartLoc,
9191 SourceLocation LParenLoc,
9192 SourceLocation EndLoc) {
9193 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009194 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009195 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
9196 /*Last=*/OMPC_PROC_BIND_unknown)
9197 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009198 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009199 }
Alexey Bataeved09d242014-05-28 05:53:51 +00009200 return new (Context)
9201 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009202}
9203
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009204OMPClause *Sema::ActOnOpenMPAtomicDefaultMemOrderClause(
9205 OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc,
9206 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
9207 if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) {
9208 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
9209 << getListOfPossibleValues(
9210 OMPC_atomic_default_mem_order, /*First=*/0,
9211 /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown)
9212 << getOpenMPClauseName(OMPC_atomic_default_mem_order);
9213 return nullptr;
9214 }
9215 return new (Context) OMPAtomicDefaultMemOrderClause(Kind, KindKwLoc, StartLoc,
9216 LParenLoc, EndLoc);
9217}
9218
Alexey Bataev56dafe82014-06-20 07:16:17 +00009219OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00009220 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00009221 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00009222 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00009223 SourceLocation EndLoc) {
9224 OMPClause *Res = nullptr;
9225 switch (Kind) {
9226 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00009227 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
9228 assert(Argument.size() == NumberOfElements &&
9229 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009230 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00009231 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
9232 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
9233 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
9234 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
9235 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009236 break;
9237 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00009238 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
9239 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
9240 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
9241 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009242 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00009243 case OMPC_dist_schedule:
9244 Res = ActOnOpenMPDistScheduleClause(
9245 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
9246 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
9247 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009248 case OMPC_defaultmap:
9249 enum { Modifier, DefaultmapKind };
9250 Res = ActOnOpenMPDefaultmapClause(
9251 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
9252 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
David Majnemer9d168222016-08-05 17:44:54 +00009253 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind],
9254 EndLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009255 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00009256 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009257 case OMPC_num_threads:
9258 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009259 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009260 case OMPC_collapse:
9261 case OMPC_default:
9262 case OMPC_proc_bind:
9263 case OMPC_private:
9264 case OMPC_firstprivate:
9265 case OMPC_lastprivate:
9266 case OMPC_shared:
9267 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00009268 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00009269 case OMPC_in_reduction:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009270 case OMPC_linear:
9271 case OMPC_aligned:
9272 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009273 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009274 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00009275 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009276 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009277 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009278 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00009279 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009280 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00009281 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00009282 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00009283 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009284 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009285 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00009286 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00009287 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009288 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00009289 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009290 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009291 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009292 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009293 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00009294 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00009295 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009296 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009297 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009298 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00009299 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00009300 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00009301 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00009302 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00009303 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00009304 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009305 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009306 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009307 case OMPC_atomic_default_mem_order:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009308 llvm_unreachable("Clause is not allowed.");
9309 }
9310 return Res;
9311}
9312
Alexey Bataev6402bca2015-12-28 07:25:51 +00009313static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
9314 OpenMPScheduleClauseModifier M2,
9315 SourceLocation M1Loc, SourceLocation M2Loc) {
9316 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
9317 SmallVector<unsigned, 2> Excluded;
9318 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
9319 Excluded.push_back(M2);
9320 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
9321 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
9322 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
9323 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
9324 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
9325 << getListOfPossibleValues(OMPC_schedule,
9326 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
9327 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
9328 Excluded)
9329 << getOpenMPClauseName(OMPC_schedule);
9330 return true;
9331 }
9332 return false;
9333}
9334
Alexey Bataev56dafe82014-06-20 07:16:17 +00009335OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00009336 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00009337 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00009338 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
9339 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
9340 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
9341 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
9342 return nullptr;
9343 // OpenMP, 2.7.1, Loop Construct, Restrictions
9344 // Either the monotonic modifier or the nonmonotonic modifier can be specified
9345 // but not both.
9346 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
9347 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
9348 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
9349 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
9350 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
9351 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
9352 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
9353 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
9354 return nullptr;
9355 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00009356 if (Kind == OMPC_SCHEDULE_unknown) {
9357 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00009358 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
9359 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
9360 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
9361 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
9362 Exclude);
9363 } else {
9364 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
9365 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009366 }
9367 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
9368 << Values << getOpenMPClauseName(OMPC_schedule);
9369 return nullptr;
9370 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00009371 // OpenMP, 2.7.1, Loop Construct, Restrictions
9372 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
9373 // schedule(guided).
9374 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
9375 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
9376 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
9377 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
9378 diag::err_omp_schedule_nonmonotonic_static);
9379 return nullptr;
9380 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00009381 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00009382 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00009383 if (ChunkSize) {
9384 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
9385 !ChunkSize->isInstantiationDependent() &&
9386 !ChunkSize->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00009387 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
Alexey Bataev56dafe82014-06-20 07:16:17 +00009388 ExprResult Val =
9389 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
9390 if (Val.isInvalid())
9391 return nullptr;
9392
9393 ValExpr = Val.get();
9394
9395 // OpenMP [2.7.1, Restrictions]
9396 // chunk_size must be a loop invariant integer expression with a positive
9397 // value.
9398 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00009399 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
9400 if (Result.isSigned() && !Result.isStrictlyPositive()) {
9401 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00009402 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00009403 return nullptr;
9404 }
Alexey Bataev2ba67042017-11-28 21:11:44 +00009405 } else if (getOpenMPCaptureRegionForClause(
9406 DSAStack->getCurrentDirective(), OMPC_schedule) !=
9407 OMPD_unknown &&
Alexey Bataevb46cdea2016-06-15 11:20:48 +00009408 !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00009409 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00009410 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev5a3af132016-03-29 08:58:54 +00009411 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
9412 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009413 }
9414 }
9415 }
9416
Alexey Bataev6402bca2015-12-28 07:25:51 +00009417 return new (Context)
9418 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00009419 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009420}
9421
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009422OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
9423 SourceLocation StartLoc,
9424 SourceLocation EndLoc) {
9425 OMPClause *Res = nullptr;
9426 switch (Kind) {
9427 case OMPC_ordered:
9428 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
9429 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00009430 case OMPC_nowait:
9431 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
9432 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009433 case OMPC_untied:
9434 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
9435 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009436 case OMPC_mergeable:
9437 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
9438 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009439 case OMPC_read:
9440 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
9441 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00009442 case OMPC_write:
9443 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
9444 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00009445 case OMPC_update:
9446 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
9447 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00009448 case OMPC_capture:
9449 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
9450 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009451 case OMPC_seq_cst:
9452 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
9453 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00009454 case OMPC_threads:
9455 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
9456 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009457 case OMPC_simd:
9458 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
9459 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00009460 case OMPC_nogroup:
9461 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
9462 break;
Kelvin Li1408f912018-09-26 04:28:39 +00009463 case OMPC_unified_address:
9464 Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc);
9465 break;
Patrick Lyster4a370b92018-10-01 13:47:43 +00009466 case OMPC_unified_shared_memory:
9467 Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
9468 break;
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009469 case OMPC_reverse_offload:
9470 Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc);
9471 break;
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009472 case OMPC_dynamic_allocators:
9473 Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc);
9474 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009475 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00009476 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009477 case OMPC_num_threads:
9478 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009479 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009480 case OMPC_collapse:
9481 case OMPC_schedule:
9482 case OMPC_private:
9483 case OMPC_firstprivate:
9484 case OMPC_lastprivate:
9485 case OMPC_shared:
9486 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00009487 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00009488 case OMPC_in_reduction:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009489 case OMPC_linear:
9490 case OMPC_aligned:
9491 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009492 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009493 case OMPC_default:
9494 case OMPC_proc_bind:
9495 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00009496 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009497 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00009498 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00009499 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009500 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009501 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009502 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009503 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00009504 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009505 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00009506 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009507 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009508 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009509 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00009510 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00009511 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00009512 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00009513 case OMPC_is_device_ptr:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009514 case OMPC_atomic_default_mem_order:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009515 llvm_unreachable("Clause is not allowed.");
9516 }
9517 return Res;
9518}
9519
Alexey Bataev236070f2014-06-20 11:19:47 +00009520OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
9521 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00009522 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00009523 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
9524}
9525
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009526OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
9527 SourceLocation EndLoc) {
9528 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
9529}
9530
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009531OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
9532 SourceLocation EndLoc) {
9533 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
9534}
9535
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009536OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
9537 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009538 return new (Context) OMPReadClause(StartLoc, EndLoc);
9539}
9540
Alexey Bataevdea47612014-07-23 07:46:59 +00009541OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
9542 SourceLocation EndLoc) {
9543 return new (Context) OMPWriteClause(StartLoc, EndLoc);
9544}
9545
Alexey Bataev67a4f222014-07-23 10:25:33 +00009546OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
9547 SourceLocation EndLoc) {
9548 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
9549}
9550
Alexey Bataev459dec02014-07-24 06:46:57 +00009551OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
9552 SourceLocation EndLoc) {
9553 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
9554}
9555
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009556OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
9557 SourceLocation EndLoc) {
9558 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
9559}
9560
Alexey Bataev346265e2015-09-25 10:37:12 +00009561OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
9562 SourceLocation EndLoc) {
9563 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
9564}
9565
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009566OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
9567 SourceLocation EndLoc) {
9568 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
9569}
9570
Alexey Bataevb825de12015-12-07 10:51:44 +00009571OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
9572 SourceLocation EndLoc) {
9573 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
9574}
9575
Kelvin Li1408f912018-09-26 04:28:39 +00009576OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc,
9577 SourceLocation EndLoc) {
9578 return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc);
9579}
9580
Patrick Lyster4a370b92018-10-01 13:47:43 +00009581OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc,
9582 SourceLocation EndLoc) {
9583 return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
9584}
9585
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009586OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc,
9587 SourceLocation EndLoc) {
9588 return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc);
9589}
9590
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009591OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc,
9592 SourceLocation EndLoc) {
9593 return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc);
9594}
9595
Alexey Bataevc5e02582014-06-16 07:08:35 +00009596OMPClause *Sema::ActOnOpenMPVarListClause(
9597 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
9598 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
9599 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009600 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Kelvin Lief579432018-12-18 22:18:41 +00009601 OpenMPLinearClauseKind LinKind,
9602 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers,
9603 ArrayRef<SourceLocation> MapTypeModifiersLoc,
Samuel Antao23abd722016-01-19 20:40:49 +00009604 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
9605 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009606 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009607 switch (Kind) {
9608 case OMPC_private:
9609 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9610 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009611 case OMPC_firstprivate:
9612 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9613 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00009614 case OMPC_lastprivate:
9615 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9616 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00009617 case OMPC_shared:
9618 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
9619 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009620 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00009621 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9622 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00009623 break;
Alexey Bataev169d96a2017-07-18 20:17:46 +00009624 case OMPC_task_reduction:
9625 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9626 EndLoc, ReductionIdScopeSpec,
9627 ReductionId);
9628 break;
Alexey Bataevfa312f32017-07-21 18:48:21 +00009629 case OMPC_in_reduction:
9630 Res =
9631 ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9632 EndLoc, ReductionIdScopeSpec, ReductionId);
9633 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00009634 case OMPC_linear:
9635 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00009636 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00009637 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009638 case OMPC_aligned:
9639 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
9640 ColonLoc, EndLoc);
9641 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009642 case OMPC_copyin:
9643 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
9644 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009645 case OMPC_copyprivate:
9646 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9647 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00009648 case OMPC_flush:
9649 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
9650 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009651 case OMPC_depend:
David Majnemer9d168222016-08-05 17:44:54 +00009652 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
Kelvin Li0bff7af2015-11-23 05:32:03 +00009653 StartLoc, LParenLoc, EndLoc);
9654 break;
9655 case OMPC_map:
Kelvin Lief579432018-12-18 22:18:41 +00009656 Res = ActOnOpenMPMapClause(MapTypeModifiers, MapTypeModifiersLoc, MapType,
9657 IsMapTypeImplicit, DepLinMapLoc, ColonLoc,
9658 VarList, StartLoc, LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009659 break;
Samuel Antao661c0902016-05-26 17:39:58 +00009660 case OMPC_to:
9661 Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc);
9662 break;
Samuel Antaoec172c62016-05-26 17:49:04 +00009663 case OMPC_from:
9664 Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc);
9665 break;
Carlo Bertolli2404b172016-07-13 15:37:16 +00009666 case OMPC_use_device_ptr:
9667 Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
9668 break;
Carlo Bertolli70594e92016-07-13 17:16:49 +00009669 case OMPC_is_device_ptr:
9670 Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
9671 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009672 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00009673 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00009674 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00009675 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009676 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00009677 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009678 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009679 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009680 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009681 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00009682 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009683 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009684 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009685 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009686 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00009687 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00009688 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00009689 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009690 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00009691 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00009692 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009693 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009694 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009695 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009696 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009697 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00009698 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00009699 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009700 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00009701 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009702 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009703 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009704 case OMPC_uniform:
Kelvin Li1408f912018-09-26 04:28:39 +00009705 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00009706 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009707 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009708 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009709 case OMPC_atomic_default_mem_order:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009710 llvm_unreachable("Clause is not allowed.");
9711 }
9712 return Res;
9713}
9714
Alexey Bataev90c228f2016-02-08 09:29:13 +00009715ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00009716 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00009717 ExprResult Res = BuildDeclRefExpr(
9718 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
9719 if (!Res.isUsable())
9720 return ExprError();
9721 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
9722 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
9723 if (!Res.isUsable())
9724 return ExprError();
9725 }
9726 if (VK != VK_LValue && Res.get()->isGLValue()) {
9727 Res = DefaultLvalueConversion(Res.get());
9728 if (!Res.isUsable())
9729 return ExprError();
9730 }
9731 return Res;
9732}
9733
Alexey Bataev60da77e2016-02-29 05:54:20 +00009734static std::pair<ValueDecl *, bool>
9735getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
9736 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00009737 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
9738 RefExpr->containsUnexpandedParameterPack())
9739 return std::make_pair(nullptr, true);
9740
Alexey Bataevd985eda2016-02-10 11:29:16 +00009741 // OpenMP [3.1, C/C++]
9742 // A list item is a variable name.
9743 // OpenMP [2.9.3.3, Restrictions, p.1]
9744 // A variable that is part of another variable (as an array or
9745 // structure element) cannot appear in a private clause.
9746 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009747 enum {
9748 NoArrayExpr = -1,
9749 ArraySubscript = 0,
9750 OMPArraySection = 1
9751 } IsArrayExpr = NoArrayExpr;
9752 if (AllowArraySection) {
9753 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009754 Expr *Base = ASE->getBase()->IgnoreParenImpCasts();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009755 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
9756 Base = TempASE->getBase()->IgnoreParenImpCasts();
9757 RefExpr = Base;
9758 IsArrayExpr = ArraySubscript;
9759 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009760 Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009761 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
9762 Base = TempOASE->getBase()->IgnoreParenImpCasts();
9763 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
9764 Base = TempASE->getBase()->IgnoreParenImpCasts();
9765 RefExpr = Base;
9766 IsArrayExpr = OMPArraySection;
9767 }
9768 }
9769 ELoc = RefExpr->getExprLoc();
9770 ERange = RefExpr->getSourceRange();
9771 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00009772 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
9773 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
9774 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
9775 (S.getCurrentThisType().isNull() || !ME ||
9776 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
9777 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009778 if (IsArrayExpr != NoArrayExpr) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009779 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
9780 << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +00009781 } else {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009782 S.Diag(ELoc,
9783 AllowArraySection
9784 ? diag::err_omp_expected_var_name_member_expr_or_array_item
9785 : diag::err_omp_expected_var_name_member_expr)
9786 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
9787 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009788 return std::make_pair(nullptr, false);
9789 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +00009790 return std::make_pair(
9791 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009792}
9793
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009794OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
9795 SourceLocation StartLoc,
9796 SourceLocation LParenLoc,
9797 SourceLocation EndLoc) {
9798 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00009799 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeve3727102018-04-18 15:57:46 +00009800 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +00009801 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009802 SourceLocation ELoc;
9803 SourceRange ERange;
9804 Expr *SimpleRefExpr = RefExpr;
9805 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009806 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009807 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009808 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009809 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009810 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009811 ValueDecl *D = Res.first;
9812 if (!D)
9813 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009814
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009815 QualType Type = D->getType();
9816 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009817
9818 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9819 // A variable that appears in a private clause must not have an incomplete
9820 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009821 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009822 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009823 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009824
Joel E. Dennye6234d1422019-01-04 22:11:31 +00009825 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
9826 // A variable that is privatized must not have a const-qualified type
9827 // unless it is of class type with a mutable member. This restriction does
9828 // not apply to the firstprivate clause.
9829 //
9830 // OpenMP 3.1 [2.9.3.3, private clause, Restrictions]
9831 // A variable that appears in a private clause must not have a
9832 // const-qualified type unless it is of class type with a mutable member.
9833 if (rejectConstNotMutableType(*this, D, OMPC_private, ELoc))
9834 continue;
9835
Alexey Bataev758e55e2013-09-06 18:03:48 +00009836 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9837 // in a Construct]
9838 // Variables with the predetermined data-sharing attributes may not be
9839 // listed in data-sharing attributes clauses, except for the cases
9840 // listed below. For these exceptions only, listing a predetermined
9841 // variable in a data-sharing attribute clause is allowed and overrides
9842 // the variable's predetermined data-sharing attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +00009843 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009844 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00009845 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9846 << getOpenMPClauseName(OMPC_private);
Alexey Bataeve3727102018-04-18 15:57:46 +00009847 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009848 continue;
9849 }
9850
Alexey Bataeve3727102018-04-18 15:57:46 +00009851 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009852 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00009853 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Kelvin Libf594a52016-12-17 05:48:59 +00009854 isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009855 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
9856 << getOpenMPClauseName(OMPC_private) << Type
Kelvin Libf594a52016-12-17 05:48:59 +00009857 << getOpenMPDirectiveName(CurrDir);
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009858 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009859 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009860 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009861 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009862 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009863 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009864 continue;
9865 }
9866
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009867 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
9868 // A list item cannot appear in both a map clause and a data-sharing
9869 // attribute clause on the same construct
Alexey Bataeve3727102018-04-18 15:57:46 +00009870 if (isOpenMPTargetExecutionDirective(CurrDir)) {
Samuel Antao6890b092016-07-28 14:25:09 +00009871 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +00009872 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +00009873 VD, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +00009874 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
9875 OpenMPClauseKind WhereFoundClauseKind) -> bool {
9876 ConflictKind = WhereFoundClauseKind;
9877 return true;
9878 })) {
9879 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009880 << getOpenMPClauseName(OMPC_private)
Samuel Antao6890b092016-07-28 14:25:09 +00009881 << getOpenMPClauseName(ConflictKind)
Kelvin Libf594a52016-12-17 05:48:59 +00009882 << getOpenMPDirectiveName(CurrDir);
Alexey Bataeve3727102018-04-18 15:57:46 +00009883 reportOriginalDsa(*this, DSAStack, D, DVar);
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009884 continue;
9885 }
9886 }
9887
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009888 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
9889 // A variable of class type (or array thereof) that appears in a private
9890 // clause requires an accessible, unambiguous default constructor for the
9891 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00009892 // Generate helper private variable and initialize it with the default
9893 // value. The address of the original variable is replaced by the address of
9894 // the new private variable in CodeGen. This new variable is not added to
9895 // IdResolver, so the code in the OpenMP region uses original variable for
9896 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009897 Type = Type.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +00009898 VarDecl *VDPrivate =
Alexey Bataev63cc8e92018-03-20 14:45:59 +00009899 buildVarDecl(*this, ELoc, Type, D->getName(),
9900 D->hasAttrs() ? &D->getAttrs() : nullptr,
9901 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Richard Smith3beb7c62017-01-12 02:27:38 +00009902 ActOnUninitializedDecl(VDPrivate);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009903 if (VDPrivate->isInvalidDecl())
9904 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00009905 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009906 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009907
Alexey Bataev90c228f2016-02-08 09:29:13 +00009908 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009909 if (!VD && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00009910 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00009911 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009912 Vars.push_back((VD || CurContext->isDependentContext())
9913 ? RefExpr->IgnoreParens()
9914 : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009915 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009916 }
9917
Alexey Bataeved09d242014-05-28 05:53:51 +00009918 if (Vars.empty())
9919 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009920
Alexey Bataev03b340a2014-10-21 03:16:40 +00009921 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
9922 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009923}
9924
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009925namespace {
9926class DiagsUninitializedSeveretyRAII {
9927private:
9928 DiagnosticsEngine &Diags;
9929 SourceLocation SavedLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00009930 bool IsIgnored = false;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009931
9932public:
9933 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
9934 bool IsIgnored)
9935 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
9936 if (!IsIgnored) {
9937 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
9938 /*Map*/ diag::Severity::Ignored, Loc);
9939 }
9940 }
9941 ~DiagsUninitializedSeveretyRAII() {
9942 if (!IsIgnored)
9943 Diags.popMappings(SavedLoc);
9944 }
9945};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00009946}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009947
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009948OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
9949 SourceLocation StartLoc,
9950 SourceLocation LParenLoc,
9951 SourceLocation EndLoc) {
9952 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009953 SmallVector<Expr *, 8> PrivateCopies;
9954 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00009955 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009956 bool IsImplicitClause =
9957 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
Alexey Bataeve3727102018-04-18 15:57:46 +00009958 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009959
Alexey Bataeve3727102018-04-18 15:57:46 +00009960 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +00009961 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009962 SourceLocation ELoc;
9963 SourceRange ERange;
9964 Expr *SimpleRefExpr = RefExpr;
9965 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009966 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009967 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009968 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009969 PrivateCopies.push_back(nullptr);
9970 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009971 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009972 ValueDecl *D = Res.first;
9973 if (!D)
9974 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009975
Alexey Bataev60da77e2016-02-29 05:54:20 +00009976 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00009977 QualType Type = D->getType();
9978 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009979
9980 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9981 // A variable that appears in a private clause must not have an incomplete
9982 // type or a reference type.
9983 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00009984 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009985 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009986 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009987
9988 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
9989 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00009990 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009991 // class type.
Alexey Bataeve3727102018-04-18 15:57:46 +00009992 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009993
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009994 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00009995 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009996 if (!IsImplicitClause) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009997 DSAStackTy::DSAVarData DVar =
9998 DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00009999 TopDVar = DVar;
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010000 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010001 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010002 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
10003 // A list item that specifies a given variable may not appear in more
10004 // than one clause on the same directive, except that a variable may be
10005 // specified in both firstprivate and lastprivate clauses.
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010006 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
10007 // A list item may appear in a firstprivate or lastprivate clause but not
10008 // both.
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010009 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevb358f992017-12-01 17:40:15 +000010010 (isOpenMPDistributeDirective(CurrDir) ||
10011 DVar.CKind != OMPC_lastprivate) &&
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010012 DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010013 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +000010014 << getOpenMPClauseName(DVar.CKind)
10015 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000010016 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010017 continue;
10018 }
10019
10020 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
10021 // in a Construct]
10022 // Variables with the predetermined data-sharing attributes may not be
10023 // listed in data-sharing attributes clauses, except for the cases
10024 // listed below. For these exceptions only, listing a predetermined
10025 // variable in a data-sharing attribute clause is allowed and overrides
10026 // the variable's predetermined data-sharing attributes.
10027 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
10028 // in a Construct, C/C++, p.2]
10029 // Variables with const-qualified type having no mutable member may be
10030 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +000010031 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010032 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
10033 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +000010034 << getOpenMPClauseName(DVar.CKind)
10035 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000010036 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010037 continue;
10038 }
10039
10040 // OpenMP [2.9.3.4, Restrictions, p.2]
10041 // A list item that is private within a parallel region must not appear
10042 // in a firstprivate clause on a worksharing construct if any of the
10043 // worksharing regions arising from the worksharing construct ever bind
10044 // to any of the parallel regions arising from the parallel construct.
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010045 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
10046 // A list item that is private within a teams region must not appear in a
10047 // firstprivate clause on a distribute construct if any of the distribute
10048 // regions arising from the distribute construct ever bind to any of the
10049 // teams regions arising from the teams construct.
10050 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
10051 // A list item that appears in a reduction clause of a teams construct
10052 // must not appear in a firstprivate clause on a distribute construct if
10053 // any of the distribute regions arising from the distribute construct
10054 // ever bind to any of the teams regions arising from the teams construct.
10055 if ((isOpenMPWorksharingDirective(CurrDir) ||
10056 isOpenMPDistributeDirective(CurrDir)) &&
Kelvin Li579e41c2016-11-30 23:51:03 +000010057 !isOpenMPParallelDirective(CurrDir) &&
10058 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +000010059 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010060 if (DVar.CKind != OMPC_shared &&
10061 (isOpenMPParallelDirective(DVar.DKind) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010062 isOpenMPTeamsDirective(DVar.DKind) ||
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010063 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +000010064 Diag(ELoc, diag::err_omp_required_access)
10065 << getOpenMPClauseName(OMPC_firstprivate)
10066 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +000010067 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +000010068 continue;
10069 }
10070 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010071 // OpenMP [2.9.3.4, Restrictions, p.3]
10072 // A list item that appears in a reduction clause of a parallel construct
10073 // must not appear in a firstprivate clause on a worksharing or task
10074 // construct if any of the worksharing or task regions arising from the
10075 // worksharing or task construct ever bind to any of the parallel regions
10076 // arising from the parallel construct.
10077 // OpenMP [2.9.3.4, Restrictions, p.4]
10078 // A list item that appears in a reduction clause in worksharing
10079 // construct must not appear in a firstprivate clause in a task construct
10080 // encountered during execution of any of the worksharing regions arising
10081 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +000010082 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010083 DVar = DSAStack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +000010084 D, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
10085 [](OpenMPDirectiveKind K) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010086 return isOpenMPParallelDirective(K) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010087 isOpenMPWorksharingDirective(K) ||
10088 isOpenMPTeamsDirective(K);
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010089 },
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010090 /*FromParent=*/true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010091 if (DVar.CKind == OMPC_reduction &&
10092 (isOpenMPParallelDirective(DVar.DKind) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010093 isOpenMPWorksharingDirective(DVar.DKind) ||
10094 isOpenMPTeamsDirective(DVar.DKind))) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010095 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
10096 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataeve3727102018-04-18 15:57:46 +000010097 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010098 continue;
10099 }
10100 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +000010101
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010102 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
10103 // A list item cannot appear in both a map clause and a data-sharing
10104 // attribute clause on the same construct
Alexey Bataevb358f992017-12-01 17:40:15 +000010105 if (isOpenMPTargetExecutionDirective(CurrDir)) {
Samuel Antao6890b092016-07-28 14:25:09 +000010106 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +000010107 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +000010108 VD, /*CurrentRegionOnly=*/true,
Alexey Bataeve3727102018-04-18 15:57:46 +000010109 [&ConflictKind](
10110 OMPClauseMappableExprCommon::MappableExprComponentListRef,
10111 OpenMPClauseKind WhereFoundClauseKind) {
Samuel Antao6890b092016-07-28 14:25:09 +000010112 ConflictKind = WhereFoundClauseKind;
10113 return true;
10114 })) {
10115 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010116 << getOpenMPClauseName(OMPC_firstprivate)
Samuel Antao6890b092016-07-28 14:25:09 +000010117 << getOpenMPClauseName(ConflictKind)
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010118 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000010119 reportOriginalDsa(*this, DSAStack, D, DVar);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010120 continue;
10121 }
10122 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010123 }
10124
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010125 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000010126 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +000010127 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010128 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
10129 << getOpenMPClauseName(OMPC_firstprivate) << Type
10130 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
10131 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +000010132 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010133 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +000010134 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010135 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +000010136 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010137 continue;
10138 }
10139
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010140 Type = Type.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010141 VarDecl *VDPrivate =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000010142 buildVarDecl(*this, ELoc, Type, D->getName(),
10143 D->hasAttrs() ? &D->getAttrs() : nullptr,
10144 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010145 // Generate helper private variable and initialize it with the value of the
10146 // original variable. The address of the original variable is replaced by
10147 // the address of the new private variable in the CodeGen. This new variable
10148 // is not added to IdResolver, so the code in the OpenMP region uses
10149 // original variable for proper diagnostics and variable capturing.
10150 Expr *VDInitRefExpr = nullptr;
10151 // For arrays generate initializer for single element and replace it by the
10152 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010153 if (Type->isArrayType()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010154 VarDecl *VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +000010155 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010156 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataeve3727102018-04-18 15:57:46 +000010157 Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010158 ElemType = ElemType.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010159 VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
10160 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +000010161 InitializedEntity Entity =
10162 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010163 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
10164
10165 InitializationSequence InitSeq(*this, Entity, Kind, Init);
10166 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
10167 if (Result.isInvalid())
10168 VDPrivate->setInvalidDecl();
10169 else
10170 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010171 // Remove temp variable declaration.
10172 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010173 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +000010174 VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
10175 ".firstprivate.temp");
Alexey Bataevd985eda2016-02-10 11:29:16 +000010176 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
10177 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +000010178 AddInitializerToDecl(VDPrivate,
10179 DefaultLvalueConversion(VDInitRefExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000010180 /*DirectInit=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010181 }
10182 if (VDPrivate->isInvalidDecl()) {
10183 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +000010184 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010185 diag::note_omp_task_predetermined_firstprivate_here);
10186 }
10187 continue;
10188 }
10189 CurContext->addDecl(VDPrivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000010190 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +000010191 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
10192 RefExpr->getExprLoc());
10193 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010194 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010195 if (TopDVar.CKind == OMPC_lastprivate) {
Alexey Bataev005248a2016-02-25 05:25:57 +000010196 Ref = TopDVar.PrivateCopy;
Alexey Bataeve3727102018-04-18 15:57:46 +000010197 } else {
Alexey Bataev61205072016-03-02 04:57:40 +000010198 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataeve3727102018-04-18 15:57:46 +000010199 if (!isOpenMPCapturedDecl(D))
Alexey Bataev005248a2016-02-25 05:25:57 +000010200 ExprCaptures.push_back(Ref->getDecl());
10201 }
Alexey Bataev417089f2016-02-17 13:19:37 +000010202 }
Alexey Bataevd985eda2016-02-10 11:29:16 +000010203 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010204 Vars.push_back((VD || CurContext->isDependentContext())
10205 ? RefExpr->IgnoreParens()
10206 : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010207 PrivateCopies.push_back(VDPrivateRefExpr);
10208 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010209 }
10210
Alexey Bataeved09d242014-05-28 05:53:51 +000010211 if (Vars.empty())
10212 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010213
10214 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +000010215 Vars, PrivateCopies, Inits,
10216 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010217}
10218
Alexander Musman1bb328c2014-06-04 13:06:39 +000010219OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
10220 SourceLocation StartLoc,
10221 SourceLocation LParenLoc,
10222 SourceLocation EndLoc) {
10223 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +000010224 SmallVector<Expr *, 8> SrcExprs;
10225 SmallVector<Expr *, 8> DstExprs;
10226 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +000010227 SmallVector<Decl *, 4> ExprCaptures;
10228 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeve3727102018-04-18 15:57:46 +000010229 for (Expr *RefExpr : VarList) {
Alexander Musman1bb328c2014-06-04 13:06:39 +000010230 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +000010231 SourceLocation ELoc;
10232 SourceRange ERange;
10233 Expr *SimpleRefExpr = RefExpr;
10234 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +000010235 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +000010236 // It will be analyzed later.
10237 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +000010238 SrcExprs.push_back(nullptr);
10239 DstExprs.push_back(nullptr);
10240 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010241 }
Alexey Bataev74caaf22016-02-20 04:09:36 +000010242 ValueDecl *D = Res.first;
10243 if (!D)
10244 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +000010245
Alexey Bataev74caaf22016-02-20 04:09:36 +000010246 QualType Type = D->getType();
10247 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010248
10249 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
10250 // A variable that appears in a lastprivate clause must not have an
10251 // incomplete type or a reference type.
10252 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +000010253 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +000010254 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010255 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +000010256
Joel E. Dennye6234d1422019-01-04 22:11:31 +000010257 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
10258 // A variable that is privatized must not have a const-qualified type
10259 // unless it is of class type with a mutable member. This restriction does
10260 // not apply to the firstprivate clause.
10261 //
10262 // OpenMP 3.1 [2.9.3.5, lastprivate clause, Restrictions]
10263 // A variable that appears in a lastprivate clause must not have a
10264 // const-qualified type unless it is of class type with a mutable member.
10265 if (rejectConstNotMutableType(*this, D, OMPC_lastprivate, ELoc))
10266 continue;
10267
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010268 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexander Musman1bb328c2014-06-04 13:06:39 +000010269 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
10270 // in a Construct]
10271 // Variables with the predetermined data-sharing attributes may not be
10272 // listed in data-sharing attributes clauses, except for the cases
10273 // listed below.
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010274 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
10275 // A list item may appear in a firstprivate or lastprivate clause but not
10276 // both.
Alexey Bataeve3727102018-04-18 15:57:46 +000010277 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010278 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
Alexey Bataevb358f992017-12-01 17:40:15 +000010279 (isOpenMPDistributeDirective(CurrDir) ||
10280 DVar.CKind != OMPC_firstprivate) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +000010281 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
10282 Diag(ELoc, diag::err_omp_wrong_dsa)
10283 << getOpenMPClauseName(DVar.CKind)
10284 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000010285 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010286 continue;
10287 }
10288
Alexey Bataevf29276e2014-06-18 04:14:57 +000010289 // OpenMP [2.14.3.5, Restrictions, p.2]
10290 // A list item that is private within a parallel region, or that appears in
10291 // the reduction clause of a parallel construct, must not appear in a
10292 // lastprivate clause on a worksharing construct if any of the corresponding
10293 // worksharing regions ever binds to any of the corresponding parallel
10294 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +000010295 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +000010296 if (isOpenMPWorksharingDirective(CurrDir) &&
Kelvin Li579e41c2016-11-30 23:51:03 +000010297 !isOpenMPParallelDirective(CurrDir) &&
10298 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +000010299 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +000010300 if (DVar.CKind != OMPC_shared) {
10301 Diag(ELoc, diag::err_omp_required_access)
10302 << getOpenMPClauseName(OMPC_lastprivate)
10303 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +000010304 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +000010305 continue;
10306 }
10307 }
Alexey Bataev74caaf22016-02-20 04:09:36 +000010308
Alexander Musman1bb328c2014-06-04 13:06:39 +000010309 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +000010310 // A variable of class type (or array thereof) that appears in a
10311 // lastprivate clause requires an accessible, unambiguous default
10312 // constructor for the class type, unless the list item is also specified
10313 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +000010314 // A variable of class type (or array thereof) that appears in a
10315 // lastprivate clause requires an accessible, unambiguous copy assignment
10316 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +000010317 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010318 VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(),
10319 Type.getUnqualifiedType(), ".lastprivate.src",
10320 D->hasAttrs() ? &D->getAttrs() : nullptr);
10321 DeclRefExpr *PseudoSrcExpr =
Alexey Bataev74caaf22016-02-20 04:09:36 +000010322 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataeve3727102018-04-18 15:57:46 +000010323 VarDecl *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +000010324 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +000010325 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000010326 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +000010327 // For arrays generate assignment operation for single element and replace
10328 // it by the original array element in CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +000010329 ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
10330 PseudoDstExpr, PseudoSrcExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +000010331 if (AssignmentOp.isInvalid())
10332 continue;
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000010333 AssignmentOp =
10334 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataev38e89532015-04-16 04:54:05 +000010335 if (AssignmentOp.isInvalid())
10336 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +000010337
Alexey Bataev74caaf22016-02-20 04:09:36 +000010338 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010339 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010340 if (TopDVar.CKind == OMPC_firstprivate) {
Alexey Bataev005248a2016-02-25 05:25:57 +000010341 Ref = TopDVar.PrivateCopy;
Alexey Bataeve3727102018-04-18 15:57:46 +000010342 } else {
Alexey Bataev61205072016-03-02 04:57:40 +000010343 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000010344 if (!isOpenMPCapturedDecl(D))
Alexey Bataev005248a2016-02-25 05:25:57 +000010345 ExprCaptures.push_back(Ref->getDecl());
10346 }
10347 if (TopDVar.CKind == OMPC_firstprivate ||
Alexey Bataeve3727102018-04-18 15:57:46 +000010348 (!isOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010349 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +000010350 ExprResult RefRes = DefaultLvalueConversion(Ref);
10351 if (!RefRes.isUsable())
10352 continue;
10353 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +000010354 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
10355 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +000010356 if (!PostUpdateRes.isUsable())
10357 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +000010358 ExprPostUpdates.push_back(
10359 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +000010360 }
10361 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010362 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010363 Vars.push_back((VD || CurContext->isDependentContext())
10364 ? RefExpr->IgnoreParens()
10365 : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +000010366 SrcExprs.push_back(PseudoSrcExpr);
10367 DstExprs.push_back(PseudoDstExpr);
10368 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +000010369 }
10370
10371 if (Vars.empty())
10372 return nullptr;
10373
10374 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +000010375 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +000010376 buildPreInits(Context, ExprCaptures),
10377 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +000010378}
10379
Alexey Bataev758e55e2013-09-06 18:03:48 +000010380OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
10381 SourceLocation StartLoc,
10382 SourceLocation LParenLoc,
10383 SourceLocation EndLoc) {
10384 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +000010385 for (Expr *RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010386 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +000010387 SourceLocation ELoc;
10388 SourceRange ERange;
10389 Expr *SimpleRefExpr = RefExpr;
10390 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010391 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +000010392 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000010393 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010394 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010395 ValueDecl *D = Res.first;
10396 if (!D)
10397 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +000010398
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010399 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010400 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
10401 // in a Construct]
10402 // Variables with the predetermined data-sharing attributes may not be
10403 // listed in data-sharing attributes clauses, except for the cases
10404 // listed below. For these exceptions only, listing a predetermined
10405 // variable in a data-sharing attribute clause is allowed and overrides
10406 // the variable's predetermined data-sharing attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +000010407 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataeved09d242014-05-28 05:53:51 +000010408 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
10409 DVar.RefExpr) {
10410 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
10411 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +000010412 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010413 continue;
10414 }
10415
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010416 DeclRefExpr *Ref = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000010417 if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +000010418 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010419 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010420 Vars.push_back((VD || !Ref || CurContext->isDependentContext())
10421 ? RefExpr->IgnoreParens()
10422 : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010423 }
10424
Alexey Bataeved09d242014-05-28 05:53:51 +000010425 if (Vars.empty())
10426 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +000010427
10428 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
10429}
10430
Alexey Bataevc5e02582014-06-16 07:08:35 +000010431namespace {
10432class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
10433 DSAStackTy *Stack;
10434
10435public:
10436 bool VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010437 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
10438 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataevc5e02582014-06-16 07:08:35 +000010439 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
10440 return false;
10441 if (DVar.CKind != OMPC_unknown)
10442 return true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010443 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +000010444 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) { return true; },
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010445 /*FromParent=*/true);
Alexey Bataeve3727102018-04-18 15:57:46 +000010446 return DVarPrivate.CKind != OMPC_unknown;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010447 }
10448 return false;
10449 }
10450 bool VisitStmt(Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010451 for (Stmt *Child : S->children()) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010452 if (Child && Visit(Child))
10453 return true;
10454 }
10455 return false;
10456 }
Alexey Bataev23b69422014-06-18 07:08:49 +000010457 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +000010458};
Alexey Bataev23b69422014-06-18 07:08:49 +000010459} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +000010460
Alexey Bataev60da77e2016-02-29 05:54:20 +000010461namespace {
10462// Transform MemberExpression for specified FieldDecl of current class to
10463// DeclRefExpr to specified OMPCapturedExprDecl.
10464class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
10465 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
Alexey Bataeve3727102018-04-18 15:57:46 +000010466 ValueDecl *Field = nullptr;
10467 DeclRefExpr *CapturedExpr = nullptr;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010468
10469public:
10470 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
10471 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
10472
10473 ExprResult TransformMemberExpr(MemberExpr *E) {
10474 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
10475 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +000010476 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +000010477 return CapturedExpr;
10478 }
10479 return BaseTransform::TransformMemberExpr(E);
10480 }
10481 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
10482};
10483} // namespace
10484
Alexey Bataev97d18bf2018-04-11 19:21:00 +000010485template <typename T, typename U>
10486static T filterLookupForUDR(SmallVectorImpl<U> &Lookups,
10487 const llvm::function_ref<T(ValueDecl *)> Gen) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010488 for (U &Set : Lookups) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010489 for (auto *D : Set) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010490 if (T Res = Gen(cast<ValueDecl>(D)))
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010491 return Res;
10492 }
10493 }
10494 return T();
10495}
10496
Alexey Bataev43b90b72018-09-12 16:31:59 +000010497static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) {
10498 assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case");
10499
10500 for (auto RD : D->redecls()) {
10501 // Don't bother with extra checks if we already know this one isn't visible.
10502 if (RD == D)
10503 continue;
10504
10505 auto ND = cast<NamedDecl>(RD);
10506 if (LookupResult::isVisible(SemaRef, ND))
10507 return ND;
10508 }
10509
10510 return nullptr;
10511}
10512
10513static void
10514argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &ReductionId,
10515 SourceLocation Loc, QualType Ty,
10516 SmallVectorImpl<UnresolvedSet<8>> &Lookups) {
10517 // Find all of the associated namespaces and classes based on the
10518 // arguments we have.
10519 Sema::AssociatedNamespaceSet AssociatedNamespaces;
10520 Sema::AssociatedClassSet AssociatedClasses;
10521 OpaqueValueExpr OVE(Loc, Ty, VK_LValue);
10522 SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces,
10523 AssociatedClasses);
10524
10525 // C++ [basic.lookup.argdep]p3:
10526 // Let X be the lookup set produced by unqualified lookup (3.4.1)
10527 // and let Y be the lookup set produced by argument dependent
10528 // lookup (defined as follows). If X contains [...] then Y is
10529 // empty. Otherwise Y is the set of declarations found in the
10530 // namespaces associated with the argument types as described
10531 // below. The set of declarations found by the lookup of the name
10532 // is the union of X and Y.
10533 //
10534 // Here, we compute Y and add its members to the overloaded
10535 // candidate set.
10536 for (auto *NS : AssociatedNamespaces) {
10537 // When considering an associated namespace, the lookup is the
10538 // same as the lookup performed when the associated namespace is
10539 // used as a qualifier (3.4.3.2) except that:
10540 //
10541 // -- Any using-directives in the associated namespace are
10542 // ignored.
10543 //
10544 // -- Any namespace-scope friend functions declared in
10545 // associated classes are visible within their respective
10546 // namespaces even if they are not visible during an ordinary
10547 // lookup (11.4).
10548 DeclContext::lookup_result R = NS->lookup(ReductionId.getName());
10549 for (auto *D : R) {
10550 auto *Underlying = D;
10551 if (auto *USD = dyn_cast<UsingShadowDecl>(D))
10552 Underlying = USD->getTargetDecl();
10553
10554 if (!isa<OMPDeclareReductionDecl>(Underlying))
10555 continue;
10556
10557 if (!SemaRef.isVisible(D)) {
10558 D = findAcceptableDecl(SemaRef, D);
10559 if (!D)
10560 continue;
10561 if (auto *USD = dyn_cast<UsingShadowDecl>(D))
10562 Underlying = USD->getTargetDecl();
10563 }
10564 Lookups.emplace_back();
10565 Lookups.back().addDecl(Underlying);
10566 }
10567 }
10568}
10569
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010570static ExprResult
10571buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
10572 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
10573 const DeclarationNameInfo &ReductionId, QualType Ty,
10574 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
10575 if (ReductionIdScopeSpec.isInvalid())
10576 return ExprError();
10577 SmallVector<UnresolvedSet<8>, 4> Lookups;
10578 if (S) {
10579 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
10580 Lookup.suppressDiagnostics();
10581 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010582 NamedDecl *D = Lookup.getRepresentativeDecl();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010583 do {
10584 S = S->getParent();
10585 } while (S && !S->isDeclScope(D));
10586 if (S)
10587 S = S->getParent();
Alexey Bataev43b90b72018-09-12 16:31:59 +000010588 Lookups.emplace_back();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010589 Lookups.back().append(Lookup.begin(), Lookup.end());
10590 Lookup.clear();
10591 }
10592 } else if (auto *ULE =
10593 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
10594 Lookups.push_back(UnresolvedSet<8>());
10595 Decl *PrevD = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000010596 for (NamedDecl *D : ULE->decls()) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010597 if (D == PrevD)
10598 Lookups.push_back(UnresolvedSet<8>());
10599 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
10600 Lookups.back().addDecl(DRD);
10601 PrevD = D;
10602 }
10603 }
Alexey Bataevfdc20352017-08-25 15:43:55 +000010604 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() ||
10605 Ty->isInstantiationDependentType() ||
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010606 Ty->containsUnexpandedParameterPack() ||
Alexey Bataeve3727102018-04-18 15:57:46 +000010607 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010608 return !D->isInvalidDecl() &&
10609 (D->getType()->isDependentType() ||
10610 D->getType()->isInstantiationDependentType() ||
10611 D->getType()->containsUnexpandedParameterPack());
10612 })) {
10613 UnresolvedSet<8> ResSet;
Alexey Bataeve3727102018-04-18 15:57:46 +000010614 for (const UnresolvedSet<8> &Set : Lookups) {
Alexey Bataev43b90b72018-09-12 16:31:59 +000010615 if (Set.empty())
10616 continue;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010617 ResSet.append(Set.begin(), Set.end());
10618 // The last item marks the end of all declarations at the specified scope.
10619 ResSet.addDecl(Set[Set.size() - 1]);
10620 }
10621 return UnresolvedLookupExpr::Create(
10622 SemaRef.Context, /*NamingClass=*/nullptr,
10623 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
10624 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
10625 }
Alexey Bataev43b90b72018-09-12 16:31:59 +000010626 // Lookup inside the classes.
10627 // C++ [over.match.oper]p3:
10628 // For a unary operator @ with an operand of a type whose
10629 // cv-unqualified version is T1, and for a binary operator @ with
10630 // a left operand of a type whose cv-unqualified version is T1 and
10631 // a right operand of a type whose cv-unqualified version is T2,
10632 // three sets of candidate functions, designated member
10633 // candidates, non-member candidates and built-in candidates, are
10634 // constructed as follows:
10635 // -- If T1 is a complete class type or a class currently being
10636 // defined, the set of member candidates is the result of the
10637 // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise,
10638 // the set of member candidates is empty.
10639 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
10640 Lookup.suppressDiagnostics();
10641 if (const auto *TyRec = Ty->getAs<RecordType>()) {
10642 // Complete the type if it can be completed.
10643 // If the type is neither complete nor being defined, bail out now.
10644 if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() ||
10645 TyRec->getDecl()->getDefinition()) {
10646 Lookup.clear();
10647 SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl());
10648 if (Lookup.empty()) {
10649 Lookups.emplace_back();
10650 Lookups.back().append(Lookup.begin(), Lookup.end());
10651 }
10652 }
10653 }
10654 // Perform ADL.
10655 argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010656 if (auto *VD = filterLookupForUDR<ValueDecl *>(
10657 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
10658 if (!D->isInvalidDecl() &&
10659 SemaRef.Context.hasSameType(D->getType(), Ty))
10660 return D;
10661 return nullptr;
10662 }))
10663 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
10664 if (auto *VD = filterLookupForUDR<ValueDecl *>(
10665 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
10666 if (!D->isInvalidDecl() &&
10667 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
10668 !Ty.isMoreQualifiedThan(D->getType()))
10669 return D;
10670 return nullptr;
10671 })) {
10672 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
10673 /*DetectVirtual=*/false);
10674 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
10675 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
10676 VD->getType().getUnqualifiedType()))) {
10677 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
10678 /*DiagID=*/0) !=
10679 Sema::AR_inaccessible) {
10680 SemaRef.BuildBasePathArray(Paths, BasePath);
10681 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
10682 }
10683 }
10684 }
10685 }
10686 if (ReductionIdScopeSpec.isSet()) {
10687 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
10688 return ExprError();
10689 }
10690 return ExprEmpty();
10691}
10692
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010693namespace {
10694/// Data for the reduction-based clauses.
10695struct ReductionData {
10696 /// List of original reduction items.
10697 SmallVector<Expr *, 8> Vars;
10698 /// List of private copies of the reduction items.
10699 SmallVector<Expr *, 8> Privates;
10700 /// LHS expressions for the reduction_op expressions.
10701 SmallVector<Expr *, 8> LHSs;
10702 /// RHS expressions for the reduction_op expressions.
10703 SmallVector<Expr *, 8> RHSs;
10704 /// Reduction operation expression.
10705 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev88202be2017-07-27 13:20:36 +000010706 /// Taskgroup descriptors for the corresponding reduction items in
10707 /// in_reduction clauses.
10708 SmallVector<Expr *, 8> TaskgroupDescriptors;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010709 /// List of captures for clause.
10710 SmallVector<Decl *, 4> ExprCaptures;
10711 /// List of postupdate expressions.
10712 SmallVector<Expr *, 4> ExprPostUpdates;
10713 ReductionData() = delete;
10714 /// Reserves required memory for the reduction data.
10715 ReductionData(unsigned Size) {
10716 Vars.reserve(Size);
10717 Privates.reserve(Size);
10718 LHSs.reserve(Size);
10719 RHSs.reserve(Size);
10720 ReductionOps.reserve(Size);
Alexey Bataev88202be2017-07-27 13:20:36 +000010721 TaskgroupDescriptors.reserve(Size);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010722 ExprCaptures.reserve(Size);
10723 ExprPostUpdates.reserve(Size);
10724 }
10725 /// Stores reduction item and reduction operation only (required for dependent
10726 /// reduction item).
10727 void push(Expr *Item, Expr *ReductionOp) {
10728 Vars.emplace_back(Item);
10729 Privates.emplace_back(nullptr);
10730 LHSs.emplace_back(nullptr);
10731 RHSs.emplace_back(nullptr);
10732 ReductionOps.emplace_back(ReductionOp);
Alexey Bataev88202be2017-07-27 13:20:36 +000010733 TaskgroupDescriptors.emplace_back(nullptr);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010734 }
10735 /// Stores reduction data.
Alexey Bataev88202be2017-07-27 13:20:36 +000010736 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp,
10737 Expr *TaskgroupDescriptor) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010738 Vars.emplace_back(Item);
10739 Privates.emplace_back(Private);
10740 LHSs.emplace_back(LHS);
10741 RHSs.emplace_back(RHS);
10742 ReductionOps.emplace_back(ReductionOp);
Alexey Bataev88202be2017-07-27 13:20:36 +000010743 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010744 }
10745};
10746} // namespace
10747
Alexey Bataeve3727102018-04-18 15:57:46 +000010748static bool checkOMPArraySectionConstantForReduction(
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010749 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement,
10750 SmallVectorImpl<llvm::APSInt> &ArraySizes) {
10751 const Expr *Length = OASE->getLength();
10752 if (Length == nullptr) {
10753 // For array sections of the form [1:] or [:], we would need to analyze
10754 // the lower bound...
10755 if (OASE->getColonLoc().isValid())
10756 return false;
10757
10758 // This is an array subscript which has implicit length 1!
10759 SingleElement = true;
10760 ArraySizes.push_back(llvm::APSInt::get(1));
10761 } else {
Fangrui Song407659a2018-11-30 23:41:18 +000010762 Expr::EvalResult Result;
10763 if (!Length->EvaluateAsInt(Result, Context))
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010764 return false;
10765
Fangrui Song407659a2018-11-30 23:41:18 +000010766 llvm::APSInt ConstantLengthValue = Result.Val.getInt();
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010767 SingleElement = (ConstantLengthValue.getSExtValue() == 1);
10768 ArraySizes.push_back(ConstantLengthValue);
10769 }
10770
10771 // Get the base of this array section and walk up from there.
10772 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
10773
10774 // We require length = 1 for all array sections except the right-most to
10775 // guarantee that the memory region is contiguous and has no holes in it.
10776 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) {
10777 Length = TempOASE->getLength();
10778 if (Length == nullptr) {
10779 // For array sections of the form [1:] or [:], we would need to analyze
10780 // the lower bound...
10781 if (OASE->getColonLoc().isValid())
10782 return false;
10783
10784 // This is an array subscript which has implicit length 1!
10785 ArraySizes.push_back(llvm::APSInt::get(1));
10786 } else {
Fangrui Song407659a2018-11-30 23:41:18 +000010787 Expr::EvalResult Result;
10788 if (!Length->EvaluateAsInt(Result, Context))
10789 return false;
10790
10791 llvm::APSInt ConstantLengthValue = Result.Val.getInt();
10792 if (ConstantLengthValue.getSExtValue() != 1)
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010793 return false;
10794
10795 ArraySizes.push_back(ConstantLengthValue);
10796 }
10797 Base = TempOASE->getBase()->IgnoreParenImpCasts();
10798 }
10799
10800 // If we have a single element, we don't need to add the implicit lengths.
10801 if (!SingleElement) {
10802 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) {
10803 // Has implicit length 1!
10804 ArraySizes.push_back(llvm::APSInt::get(1));
10805 Base = TempASE->getBase()->IgnoreParenImpCasts();
10806 }
10807 }
10808
10809 // This array section can be privatized as a single value or as a constant
10810 // sized array.
10811 return true;
10812}
10813
Alexey Bataeve3727102018-04-18 15:57:46 +000010814static bool actOnOMPReductionKindClause(
Alexey Bataev169d96a2017-07-18 20:17:46 +000010815 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind,
10816 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
10817 SourceLocation ColonLoc, SourceLocation EndLoc,
10818 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010819 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010820 DeclarationName DN = ReductionId.getName();
10821 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator();
Alexey Bataevc5e02582014-06-16 07:08:35 +000010822 BinaryOperatorKind BOK = BO_Comma;
10823
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010824 ASTContext &Context = S.Context;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010825 // OpenMP [2.14.3.6, reduction clause]
10826 // C
10827 // reduction-identifier is either an identifier or one of the following
10828 // operators: +, -, *, &, |, ^, && and ||
10829 // C++
10830 // reduction-identifier is either an id-expression or one of the following
10831 // operators: +, -, *, &, |, ^, && and ||
Alexey Bataevc5e02582014-06-16 07:08:35 +000010832 switch (OOK) {
10833 case OO_Plus:
10834 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010835 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010836 break;
10837 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010838 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010839 break;
10840 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010841 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010842 break;
10843 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010844 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010845 break;
10846 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010847 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010848 break;
10849 case OO_AmpAmp:
10850 BOK = BO_LAnd;
10851 break;
10852 case OO_PipePipe:
10853 BOK = BO_LOr;
10854 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010855 case OO_New:
10856 case OO_Delete:
10857 case OO_Array_New:
10858 case OO_Array_Delete:
10859 case OO_Slash:
10860 case OO_Percent:
10861 case OO_Tilde:
10862 case OO_Exclaim:
10863 case OO_Equal:
10864 case OO_Less:
10865 case OO_Greater:
10866 case OO_LessEqual:
10867 case OO_GreaterEqual:
10868 case OO_PlusEqual:
10869 case OO_MinusEqual:
10870 case OO_StarEqual:
10871 case OO_SlashEqual:
10872 case OO_PercentEqual:
10873 case OO_CaretEqual:
10874 case OO_AmpEqual:
10875 case OO_PipeEqual:
10876 case OO_LessLess:
10877 case OO_GreaterGreater:
10878 case OO_LessLessEqual:
10879 case OO_GreaterGreaterEqual:
10880 case OO_EqualEqual:
10881 case OO_ExclaimEqual:
Richard Smithd30b23d2017-12-01 02:13:10 +000010882 case OO_Spaceship:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010883 case OO_PlusPlus:
10884 case OO_MinusMinus:
10885 case OO_Comma:
10886 case OO_ArrowStar:
10887 case OO_Arrow:
10888 case OO_Call:
10889 case OO_Subscript:
10890 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +000010891 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010892 case NUM_OVERLOADED_OPERATORS:
10893 llvm_unreachable("Unexpected reduction identifier");
10894 case OO_None:
Alexey Bataeve3727102018-04-18 15:57:46 +000010895 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010896 if (II->isStr("max"))
10897 BOK = BO_GT;
10898 else if (II->isStr("min"))
10899 BOK = BO_LT;
10900 }
10901 break;
10902 }
10903 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010904 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +000010905 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataev4d4624c2017-07-20 16:47:47 +000010906 else
10907 ReductionIdRange.setBegin(ReductionId.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +000010908 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +000010909
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010910 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
10911 bool FirstIter = true;
Alexey Bataeve3727102018-04-18 15:57:46 +000010912 for (Expr *RefExpr : VarList) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010913 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +000010914 // OpenMP [2.1, C/C++]
10915 // A list item is a variable or array section, subject to the restrictions
10916 // specified in Section 2.4 on page 42 and in each of the sections
10917 // describing clauses and directives for which a list appears.
10918 // OpenMP [2.14.3.3, Restrictions, p.1]
10919 // A variable that is part of another variable (as an array or
10920 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010921 if (!FirstIter && IR != ER)
10922 ++IR;
10923 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010924 SourceLocation ELoc;
10925 SourceRange ERange;
10926 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010927 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
Alexey Bataev60da77e2016-02-29 05:54:20 +000010928 /*AllowArraySection=*/true);
10929 if (Res.second) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010930 // Try to find 'declare reduction' corresponding construct before using
10931 // builtin/overloaded operators.
10932 QualType Type = Context.DependentTy;
10933 CXXCastPath BasePath;
10934 ExprResult DeclareReductionRef = buildDeclareReductionRef(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010935 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010936 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010937 Expr *ReductionOp = nullptr;
10938 if (S.CurContext->isDependentContext() &&
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010939 (DeclareReductionRef.isUnset() ||
10940 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010941 ReductionOp = DeclareReductionRef.get();
10942 // It will be analyzed later.
10943 RD.push(RefExpr, ReductionOp);
Alexey Bataevc5e02582014-06-16 07:08:35 +000010944 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000010945 ValueDecl *D = Res.first;
10946 if (!D)
10947 continue;
10948
Alexey Bataev88202be2017-07-27 13:20:36 +000010949 Expr *TaskgroupDescriptor = nullptr;
Alexey Bataeva1764212015-09-30 09:22:36 +000010950 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010951 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
10952 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
Alexey Bataeve3727102018-04-18 15:57:46 +000010953 if (ASE) {
Alexey Bataev31300ed2016-02-04 11:27:03 +000010954 Type = ASE->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010955 } else if (OASE) {
10956 QualType BaseType =
10957 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
10958 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
Alexey Bataeva1764212015-09-30 09:22:36 +000010959 Type = ATy->getElementType();
10960 else
10961 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +000010962 Type = Type.getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010963 } else {
Alexey Bataev60da77e2016-02-29 05:54:20 +000010964 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
Alexey Bataeve3727102018-04-18 15:57:46 +000010965 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000010966 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +000010967
Alexey Bataevc5e02582014-06-16 07:08:35 +000010968 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
10969 // A variable that appears in a private clause must not have an incomplete
10970 // type or a reference type.
Joel E. Denny3cabf732018-06-28 19:54:49 +000010971 if (S.RequireCompleteType(ELoc, D->getType(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010972 diag::err_omp_reduction_incomplete_type))
Alexey Bataevc5e02582014-06-16 07:08:35 +000010973 continue;
10974 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +000010975 // A list item that appears in a reduction clause must not be
10976 // const-qualified.
10977 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataev169d96a2017-07-18 20:17:46 +000010978 S.Diag(ELoc, diag::err_omp_const_reduction_list_item) << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010979 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010980 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10981 VarDecl::DeclarationOnly;
10982 S.Diag(D->getLocation(),
10983 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +000010984 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +000010985 }
Alexey Bataevc5e02582014-06-16 07:08:35 +000010986 continue;
10987 }
Alexey Bataevbc529672018-09-28 19:33:14 +000010988
10989 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective();
Alexey Bataevc5e02582014-06-16 07:08:35 +000010990 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
10991 // If a list-item is a reference type then it must bind to the same object
10992 // for all threads of the team.
Alexey Bataevbc529672018-09-28 19:33:14 +000010993 if (!ASE && !OASE) {
10994 if (VD) {
10995 VarDecl *VDDef = VD->getDefinition();
10996 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) {
10997 DSARefChecker Check(Stack);
10998 if (Check.Visit(VDDef->getInit())) {
10999 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg)
11000 << getOpenMPClauseName(ClauseKind) << ERange;
11001 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
11002 continue;
11003 }
Alexey Bataeva1764212015-09-30 09:22:36 +000011004 }
Alexey Bataevc5e02582014-06-16 07:08:35 +000011005 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011006
Alexey Bataevbc529672018-09-28 19:33:14 +000011007 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
11008 // in a Construct]
11009 // Variables with the predetermined data-sharing attributes may not be
11010 // listed in data-sharing attributes clauses, except for the cases
11011 // listed below. For these exceptions only, listing a predetermined
11012 // variable in a data-sharing attribute clause is allowed and overrides
11013 // the variable's predetermined data-sharing attributes.
11014 // OpenMP [2.14.3.6, Restrictions, p.3]
11015 // Any number of reduction clauses can be specified on the directive,
11016 // but a list item can appear only once in the reduction clauses for that
11017 // directive.
11018 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false);
11019 if (DVar.CKind == OMPC_reduction) {
11020 S.Diag(ELoc, diag::err_omp_once_referenced)
11021 << getOpenMPClauseName(ClauseKind);
11022 if (DVar.RefExpr)
11023 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
11024 continue;
11025 }
11026 if (DVar.CKind != OMPC_unknown) {
11027 S.Diag(ELoc, diag::err_omp_wrong_dsa)
11028 << getOpenMPClauseName(DVar.CKind)
11029 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataeve3727102018-04-18 15:57:46 +000011030 reportOriginalDsa(S, Stack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011031 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +000011032 }
Alexey Bataevbc529672018-09-28 19:33:14 +000011033
11034 // OpenMP [2.14.3.6, Restrictions, p.1]
11035 // A list item that appears in a reduction clause of a worksharing
11036 // construct must be shared in the parallel regions to which any of the
11037 // worksharing regions arising from the worksharing construct bind.
11038 if (isOpenMPWorksharingDirective(CurrDir) &&
11039 !isOpenMPParallelDirective(CurrDir) &&
11040 !isOpenMPTeamsDirective(CurrDir)) {
11041 DVar = Stack->getImplicitDSA(D, true);
11042 if (DVar.CKind != OMPC_shared) {
11043 S.Diag(ELoc, diag::err_omp_required_access)
11044 << getOpenMPClauseName(OMPC_reduction)
11045 << getOpenMPClauseName(OMPC_shared);
11046 reportOriginalDsa(S, Stack, D, DVar);
11047 continue;
11048 }
11049 }
Alexey Bataevf29276e2014-06-18 04:14:57 +000011050 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011051
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011052 // Try to find 'declare reduction' corresponding construct before using
11053 // builtin/overloaded operators.
11054 CXXCastPath BasePath;
11055 ExprResult DeclareReductionRef = buildDeclareReductionRef(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011056 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011057 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
11058 if (DeclareReductionRef.isInvalid())
11059 continue;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011060 if (S.CurContext->isDependentContext() &&
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011061 (DeclareReductionRef.isUnset() ||
11062 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011063 RD.push(RefExpr, DeclareReductionRef.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011064 continue;
11065 }
11066 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
11067 // Not allowed reduction identifier is found.
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011068 S.Diag(ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011069 diag::err_omp_unknown_reduction_identifier)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011070 << Type << ReductionIdRange;
11071 continue;
11072 }
11073
11074 // OpenMP [2.14.3.6, reduction clause, Restrictions]
11075 // The type of a list item that appears in a reduction clause must be valid
11076 // for the reduction-identifier. For a max or min reduction in C, the type
11077 // of the list item must be an allowed arithmetic data type: char, int,
11078 // float, double, or _Bool, possibly modified with long, short, signed, or
11079 // unsigned. For a max or min reduction in C++, the type of the list item
11080 // must be an allowed arithmetic data type: char, wchar_t, int, float,
11081 // double, or bool, possibly modified with long, short, signed, or unsigned.
11082 if (DeclareReductionRef.isUnset()) {
11083 if ((BOK == BO_GT || BOK == BO_LT) &&
11084 !(Type->isScalarType() ||
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011085 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
11086 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
Alexey Bataev169d96a2017-07-18 20:17:46 +000011087 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011088 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011089 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
11090 VarDecl::DeclarationOnly;
11091 S.Diag(D->getLocation(),
11092 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011093 << D;
11094 }
11095 continue;
11096 }
11097 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011098 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) {
Alexey Bataev169d96a2017-07-18 20:17:46 +000011099 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg)
11100 << getOpenMPClauseName(ClauseKind);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011101 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011102 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
11103 VarDecl::DeclarationOnly;
11104 S.Diag(D->getLocation(),
11105 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011106 << D;
11107 }
11108 continue;
11109 }
11110 }
11111
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011112 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000011113 VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs",
11114 D->hasAttrs() ? &D->getAttrs() : nullptr);
11115 VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(),
11116 D->hasAttrs() ? &D->getAttrs() : nullptr);
11117 QualType PrivateTy = Type;
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000011118
11119 // Try if we can determine constant lengths for all array sections and avoid
11120 // the VLA.
11121 bool ConstantLengthOASE = false;
11122 if (OASE) {
11123 bool SingleElement;
11124 llvm::SmallVector<llvm::APSInt, 4> ArraySizes;
Alexey Bataeve3727102018-04-18 15:57:46 +000011125 ConstantLengthOASE = checkOMPArraySectionConstantForReduction(
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000011126 Context, OASE, SingleElement, ArraySizes);
11127
11128 // If we don't have a single element, we must emit a constant array type.
11129 if (ConstantLengthOASE && !SingleElement) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011130 for (llvm::APSInt &Size : ArraySizes)
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000011131 PrivateTy = Context.getConstantArrayType(
11132 PrivateTy, Size, ArrayType::Normal, /*IndexTypeQuals=*/0);
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000011133 }
11134 }
11135
11136 if ((OASE && !ConstantLengthOASE) ||
Jonas Hahnfeld96087f32017-11-02 13:30:42 +000011137 (!OASE && !ASE &&
Alexey Bataev60da77e2016-02-29 05:54:20 +000011138 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Jonas Hahnfeld87d44262017-11-18 21:00:46 +000011139 if (!Context.getTargetInfo().isVLASupported() &&
11140 S.shouldDiagnoseTargetSupportFromOpenMP()) {
11141 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE;
11142 S.Diag(ELoc, diag::note_vla_unsupported);
11143 continue;
11144 }
David Majnemer9d168222016-08-05 17:44:54 +000011145 // For arrays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011146 // Create pseudo array type for private copy. The size for this array will
11147 // be generated during codegen.
11148 // For array subscripts or single variables Private Ty is the same as Type
11149 // (type of the variable or single array element).
11150 PrivateTy = Context.getVariableArrayType(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011151 Type,
Alexey Bataevd070a582017-10-25 15:54:04 +000011152 new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue),
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011153 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +000011154 } else if (!ASE && !OASE &&
Alexey Bataeve3727102018-04-18 15:57:46 +000011155 Context.getAsArrayType(D->getType().getNonReferenceType())) {
Alexey Bataev60da77e2016-02-29 05:54:20 +000011156 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000011157 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011158 // Private copy.
Alexey Bataeve3727102018-04-18 15:57:46 +000011159 VarDecl *PrivateVD =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000011160 buildVarDecl(S, ELoc, PrivateTy, D->getName(),
11161 D->hasAttrs() ? &D->getAttrs() : nullptr,
11162 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011163 // Add initializer for private variable.
11164 Expr *Init = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000011165 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc);
11166 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011167 if (DeclareReductionRef.isUsable()) {
11168 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
11169 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
11170 if (DRD->getInitializer()) {
11171 Init = DRDRef;
11172 RHSVD->setInit(DRDRef);
11173 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011174 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011175 } else {
11176 switch (BOK) {
11177 case BO_Add:
11178 case BO_Xor:
11179 case BO_Or:
11180 case BO_LOr:
11181 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
11182 if (Type->isScalarType() || Type->isAnyComplexType())
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011183 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011184 break;
11185 case BO_Mul:
11186 case BO_LAnd:
11187 if (Type->isScalarType() || Type->isAnyComplexType()) {
11188 // '*' and '&&' reduction ops - initializer is '1'.
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011189 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +000011190 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011191 break;
11192 case BO_And: {
11193 // '&' reduction op - initializer is '~0'.
11194 QualType OrigType = Type;
11195 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
11196 Type = ComplexTy->getElementType();
11197 if (Type->isRealFloatingType()) {
11198 llvm::APFloat InitValue =
11199 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
11200 /*isIEEE=*/true);
11201 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
11202 Type, ELoc);
11203 } else if (Type->isScalarType()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011204 uint64_t Size = Context.getTypeSize(Type);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011205 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
11206 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
11207 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
11208 }
11209 if (Init && OrigType->isAnyComplexType()) {
11210 // Init = 0xFFFF + 0xFFFFi;
11211 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011212 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011213 }
11214 Type = OrigType;
11215 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011216 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011217 case BO_LT:
11218 case BO_GT: {
11219 // 'min' reduction op - initializer is 'Largest representable number in
11220 // the reduction list item type'.
11221 // 'max' reduction op - initializer is 'Least representable number in
11222 // the reduction list item type'.
11223 if (Type->isIntegerType() || Type->isPointerType()) {
11224 bool IsSigned = Type->hasSignedIntegerRepresentation();
Alexey Bataeve3727102018-04-18 15:57:46 +000011225 uint64_t Size = Context.getTypeSize(Type);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011226 QualType IntTy =
11227 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
11228 llvm::APInt InitValue =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011229 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
11230 : llvm::APInt::getMinValue(Size)
11231 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
11232 : llvm::APInt::getMaxValue(Size);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011233 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
11234 if (Type->isPointerType()) {
11235 // Cast to pointer type.
Alexey Bataeve3727102018-04-18 15:57:46 +000011236 ExprResult CastExpr = S.BuildCStyleCastExpr(
Alexey Bataevd070a582017-10-25 15:54:04 +000011237 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011238 if (CastExpr.isInvalid())
11239 continue;
11240 Init = CastExpr.get();
11241 }
11242 } else if (Type->isRealFloatingType()) {
11243 llvm::APFloat InitValue = llvm::APFloat::getLargest(
11244 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
11245 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
11246 Type, ELoc);
11247 }
11248 break;
11249 }
11250 case BO_PtrMemD:
11251 case BO_PtrMemI:
11252 case BO_MulAssign:
11253 case BO_Div:
11254 case BO_Rem:
11255 case BO_Sub:
11256 case BO_Shl:
11257 case BO_Shr:
11258 case BO_LE:
11259 case BO_GE:
11260 case BO_EQ:
11261 case BO_NE:
Richard Smithc70f1d62017-12-14 15:16:18 +000011262 case BO_Cmp:
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011263 case BO_AndAssign:
11264 case BO_XorAssign:
11265 case BO_OrAssign:
11266 case BO_Assign:
11267 case BO_AddAssign:
11268 case BO_SubAssign:
11269 case BO_DivAssign:
11270 case BO_RemAssign:
11271 case BO_ShlAssign:
11272 case BO_ShrAssign:
11273 case BO_Comma:
11274 llvm_unreachable("Unexpected reduction operation");
11275 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011276 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011277 if (Init && DeclareReductionRef.isUnset())
11278 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false);
11279 else if (!Init)
11280 S.ActOnUninitializedDecl(RHSVD);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011281 if (RHSVD->isInvalidDecl())
11282 continue;
11283 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011284 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible)
11285 << Type << ReductionIdRange;
11286 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
11287 VarDecl::DeclarationOnly;
11288 S.Diag(D->getLocation(),
11289 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +000011290 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011291 continue;
11292 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011293 // Store initializer for single element in private copy. Will be used during
11294 // codegen.
11295 PrivateVD->setInit(RHSVD->getInit());
11296 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataeve3727102018-04-18 15:57:46 +000011297 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011298 ExprResult ReductionOp;
11299 if (DeclareReductionRef.isUsable()) {
11300 QualType RedTy = DeclareReductionRef.get()->getType();
11301 QualType PtrRedTy = Context.getPointerType(RedTy);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011302 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
11303 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011304 if (!BasePath.empty()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011305 LHS = S.DefaultLvalueConversion(LHS.get());
11306 RHS = S.DefaultLvalueConversion(RHS.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011307 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
11308 CK_UncheckedDerivedToBase, LHS.get(),
11309 &BasePath, LHS.get()->getValueKind());
11310 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
11311 CK_UncheckedDerivedToBase, RHS.get(),
11312 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011313 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011314 FunctionProtoType::ExtProtoInfo EPI;
11315 QualType Params[] = {PtrRedTy, PtrRedTy};
11316 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
11317 auto *OVE = new (Context) OpaqueValueExpr(
11318 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011319 S.DefaultLvalueConversion(DeclareReductionRef.get()).get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011320 Expr *Args[] = {LHS.get(), RHS.get()};
Bruno Riccic5885cf2018-12-21 15:20:32 +000011321 ReductionOp =
11322 CallExpr::Create(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011323 } else {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011324 ReductionOp = S.BuildBinOp(
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011325 Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, LHSDRE, RHSDRE);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011326 if (ReductionOp.isUsable()) {
11327 if (BOK != BO_LT && BOK != BO_GT) {
11328 ReductionOp =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011329 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011330 BO_Assign, LHSDRE, ReductionOp.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011331 } else {
Alexey Bataevd070a582017-10-25 15:54:04 +000011332 auto *ConditionalOp = new (Context)
11333 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE,
11334 Type, VK_LValue, OK_Ordinary);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011335 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, ConditionalOp);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011338 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +000011339 if (ReductionOp.isUsable())
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000011340 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get(),
11341 /*DiscardedValue*/ false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011342 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +000011343 if (!ReductionOp.isUsable())
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011344 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +000011345 }
11346
Alexey Bataevfa312f32017-07-21 18:48:21 +000011347 // OpenMP [2.15.4.6, Restrictions, p.2]
11348 // A list item that appears in an in_reduction clause of a task construct
11349 // must appear in a task_reduction clause of a construct associated with a
11350 // taskgroup region that includes the participating task in its taskgroup
11351 // set. The construct associated with the innermost region that meets this
11352 // condition must specify the same reduction-identifier as the in_reduction
11353 // clause.
11354 if (ClauseKind == OMPC_in_reduction) {
Alexey Bataevfa312f32017-07-21 18:48:21 +000011355 SourceRange ParentSR;
11356 BinaryOperatorKind ParentBOK;
11357 const Expr *ParentReductionOp;
Alexey Bataev88202be2017-07-27 13:20:36 +000011358 Expr *ParentBOKTD, *ParentReductionOpTD;
Alexey Bataevf189cb72017-07-24 14:52:13 +000011359 DSAStackTy::DSAVarData ParentBOKDSA =
Alexey Bataev88202be2017-07-27 13:20:36 +000011360 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK,
11361 ParentBOKTD);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011362 DSAStackTy::DSAVarData ParentReductionOpDSA =
Alexey Bataev88202be2017-07-27 13:20:36 +000011363 Stack->getTopMostTaskgroupReductionData(
11364 D, ParentSR, ParentReductionOp, ParentReductionOpTD);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011365 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown;
11366 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown;
11367 if (!IsParentBOK && !IsParentReductionOp) {
11368 S.Diag(ELoc, diag::err_omp_in_reduction_not_task_reduction);
11369 continue;
11370 }
Alexey Bataevfa312f32017-07-21 18:48:21 +000011371 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) ||
11372 (DeclareReductionRef.isUsable() && IsParentBOK) || BOK != ParentBOK ||
11373 IsParentReductionOp) {
11374 bool EmitError = true;
11375 if (IsParentReductionOp && DeclareReductionRef.isUsable()) {
11376 llvm::FoldingSetNodeID RedId, ParentRedId;
11377 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true);
11378 DeclareReductionRef.get()->Profile(RedId, Context,
11379 /*Canonical=*/true);
11380 EmitError = RedId != ParentRedId;
11381 }
11382 if (EmitError) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011383 S.Diag(ReductionId.getBeginLoc(),
Alexey Bataevfa312f32017-07-21 18:48:21 +000011384 diag::err_omp_reduction_identifier_mismatch)
11385 << ReductionIdRange << RefExpr->getSourceRange();
11386 S.Diag(ParentSR.getBegin(),
11387 diag::note_omp_previous_reduction_identifier)
Alexey Bataevf189cb72017-07-24 14:52:13 +000011388 << ParentSR
11389 << (IsParentBOK ? ParentBOKDSA.RefExpr
11390 : ParentReductionOpDSA.RefExpr)
11391 ->getSourceRange();
Alexey Bataevfa312f32017-07-21 18:48:21 +000011392 continue;
11393 }
11394 }
Alexey Bataev88202be2017-07-27 13:20:36 +000011395 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD;
11396 assert(TaskgroupDescriptor && "Taskgroup descriptor must be defined.");
Alexey Bataevfa312f32017-07-21 18:48:21 +000011397 }
11398
Alexey Bataev60da77e2016-02-29 05:54:20 +000011399 DeclRefExpr *Ref = nullptr;
11400 Expr *VarsExpr = RefExpr->IgnoreParens();
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011401 if (!VD && !S.CurContext->isDependentContext()) {
Alexey Bataev60da77e2016-02-29 05:54:20 +000011402 if (ASE || OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011403 TransformExprToCaptures RebuildToCapture(S, D);
Alexey Bataev60da77e2016-02-29 05:54:20 +000011404 VarsExpr =
11405 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
11406 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +000011407 } else {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011408 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +000011409 }
Alexey Bataeve3727102018-04-18 15:57:46 +000011410 if (!S.isOpenMPCapturedDecl(D)) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011411 RD.ExprCaptures.emplace_back(Ref->getDecl());
Alexey Bataev5a3af132016-03-29 08:58:54 +000011412 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011413 ExprResult RefRes = S.DefaultLvalueConversion(Ref);
Alexey Bataev5a3af132016-03-29 08:58:54 +000011414 if (!RefRes.isUsable())
11415 continue;
11416 ExprResult PostUpdateRes =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011417 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
11418 RefRes.get());
Alexey Bataev5a3af132016-03-29 08:58:54 +000011419 if (!PostUpdateRes.isUsable())
11420 continue;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011421 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
11422 Stack->getCurrentDirective() == OMPD_taskgroup) {
11423 S.Diag(RefExpr->getExprLoc(),
11424 diag::err_omp_reduction_non_addressable_expression)
Alexey Bataevbcd0ae02017-07-11 19:16:44 +000011425 << RefExpr->getSourceRange();
11426 continue;
11427 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011428 RD.ExprPostUpdates.emplace_back(
11429 S.IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +000011430 }
11431 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000011432 }
Alexey Bataev169d96a2017-07-18 20:17:46 +000011433 // All reduction items are still marked as reduction (to do not increase
11434 // code base size).
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011435 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011436 if (CurrDir == OMPD_taskgroup) {
11437 if (DeclareReductionRef.isUsable())
Alexey Bataev3b1b8952017-07-25 15:53:26 +000011438 Stack->addTaskgroupReductionData(D, ReductionIdRange,
11439 DeclareReductionRef.get());
Alexey Bataevf189cb72017-07-24 14:52:13 +000011440 else
Alexey Bataev3b1b8952017-07-25 15:53:26 +000011441 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011442 }
Alexey Bataev88202be2017-07-27 13:20:36 +000011443 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(),
11444 TaskgroupDescriptor);
Alexey Bataevc5e02582014-06-16 07:08:35 +000011445 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011446 return RD.Vars.empty();
11447}
Alexey Bataevc5e02582014-06-16 07:08:35 +000011448
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011449OMPClause *Sema::ActOnOpenMPReductionClause(
11450 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11451 SourceLocation ColonLoc, SourceLocation EndLoc,
11452 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11453 ArrayRef<Expr *> UnresolvedReductions) {
11454 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000011455 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList,
Alexey Bataev169d96a2017-07-18 20:17:46 +000011456 StartLoc, LParenLoc, ColonLoc, EndLoc,
11457 ReductionIdScopeSpec, ReductionId,
11458 UnresolvedReductions, RD))
Alexey Bataevc5e02582014-06-16 07:08:35 +000011459 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +000011460
Alexey Bataevc5e02582014-06-16 07:08:35 +000011461 return OMPReductionClause::Create(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011462 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11463 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
11464 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
11465 buildPreInits(Context, RD.ExprCaptures),
11466 buildPostUpdate(*this, RD.ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +000011467}
11468
Alexey Bataev169d96a2017-07-18 20:17:46 +000011469OMPClause *Sema::ActOnOpenMPTaskReductionClause(
11470 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11471 SourceLocation ColonLoc, SourceLocation EndLoc,
11472 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11473 ArrayRef<Expr *> UnresolvedReductions) {
11474 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000011475 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList,
11476 StartLoc, LParenLoc, ColonLoc, EndLoc,
11477 ReductionIdScopeSpec, ReductionId,
Alexey Bataev169d96a2017-07-18 20:17:46 +000011478 UnresolvedReductions, RD))
11479 return nullptr;
11480
11481 return OMPTaskReductionClause::Create(
11482 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11483 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
11484 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
11485 buildPreInits(Context, RD.ExprCaptures),
11486 buildPostUpdate(*this, RD.ExprPostUpdates));
11487}
11488
Alexey Bataevfa312f32017-07-21 18:48:21 +000011489OMPClause *Sema::ActOnOpenMPInReductionClause(
11490 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11491 SourceLocation ColonLoc, SourceLocation EndLoc,
11492 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11493 ArrayRef<Expr *> UnresolvedReductions) {
11494 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000011495 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList,
Alexey Bataevfa312f32017-07-21 18:48:21 +000011496 StartLoc, LParenLoc, ColonLoc, EndLoc,
11497 ReductionIdScopeSpec, ReductionId,
11498 UnresolvedReductions, RD))
11499 return nullptr;
11500
11501 return OMPInReductionClause::Create(
11502 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11503 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
Alexey Bataev88202be2017-07-27 13:20:36 +000011504 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors,
Alexey Bataevfa312f32017-07-21 18:48:21 +000011505 buildPreInits(Context, RD.ExprCaptures),
11506 buildPostUpdate(*this, RD.ExprPostUpdates));
11507}
11508
Alexey Bataevecba70f2016-04-12 11:02:11 +000011509bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
11510 SourceLocation LinLoc) {
11511 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
11512 LinKind == OMPC_LINEAR_unknown) {
11513 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
11514 return true;
11515 }
11516 return false;
11517}
11518
Alexey Bataeve3727102018-04-18 15:57:46 +000011519bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc,
Alexey Bataevecba70f2016-04-12 11:02:11 +000011520 OpenMPLinearClauseKind LinKind,
11521 QualType Type) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011522 const auto *VD = dyn_cast_or_null<VarDecl>(D);
Alexey Bataevecba70f2016-04-12 11:02:11 +000011523 // A variable must not have an incomplete type or a reference type.
11524 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
11525 return true;
11526 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
11527 !Type->isReferenceType()) {
11528 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
11529 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
11530 return true;
11531 }
11532 Type = Type.getNonReferenceType();
11533
11534 // A list item must not be const-qualified.
11535 if (Type.isConstant(Context)) {
11536 Diag(ELoc, diag::err_omp_const_variable)
11537 << getOpenMPClauseName(OMPC_linear);
11538 if (D) {
11539 bool IsDecl =
11540 !VD ||
11541 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
11542 Diag(D->getLocation(),
11543 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
11544 << D;
11545 }
11546 return true;
11547 }
11548
11549 // A list item must be of integral or pointer type.
11550 Type = Type.getUnqualifiedType().getCanonicalType();
11551 const auto *Ty = Type.getTypePtrOrNull();
11552 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
11553 !Ty->isPointerType())) {
11554 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
11555 if (D) {
11556 bool IsDecl =
11557 !VD ||
11558 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
11559 Diag(D->getLocation(),
11560 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
11561 << D;
11562 }
11563 return true;
11564 }
11565 return false;
11566}
11567
Alexey Bataev182227b2015-08-20 10:54:39 +000011568OMPClause *Sema::ActOnOpenMPLinearClause(
11569 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
11570 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
11571 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +000011572 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011573 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +000011574 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +000011575 SmallVector<Decl *, 4> ExprCaptures;
11576 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +000011577 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +000011578 LinKind = OMPC_LINEAR_val;
Alexey Bataeve3727102018-04-18 15:57:46 +000011579 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000011580 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011581 SourceLocation ELoc;
11582 SourceRange ERange;
11583 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011584 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011585 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +000011586 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000011587 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011588 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +000011589 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +000011590 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011591 ValueDecl *D = Res.first;
11592 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +000011593 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +000011594
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011595 QualType Type = D->getType();
11596 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +000011597
11598 // OpenMP [2.14.3.7, linear clause]
11599 // A list-item cannot appear in more than one linear clause.
11600 // A list-item that appears in a linear clause cannot appear in any
11601 // other data-sharing attribute clause.
Alexey Bataeve3727102018-04-18 15:57:46 +000011602 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexander Musman8dba6642014-04-22 13:09:42 +000011603 if (DVar.RefExpr) {
11604 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
11605 << getOpenMPClauseName(OMPC_linear);
Alexey Bataeve3727102018-04-18 15:57:46 +000011606 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +000011607 continue;
11608 }
11609
Alexey Bataevecba70f2016-04-12 11:02:11 +000011610 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +000011611 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +000011612 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +000011613
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011614 // Build private copy of original var.
Alexey Bataeve3727102018-04-18 15:57:46 +000011615 VarDecl *Private =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000011616 buildVarDecl(*this, ELoc, Type, D->getName(),
11617 D->hasAttrs() ? &D->getAttrs() : nullptr,
11618 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011619 DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +000011620 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011621 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011622 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011623 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000011624 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev78849fb2016-03-09 09:49:00 +000011625 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011626 if (!isOpenMPCapturedDecl(D)) {
Alexey Bataev78849fb2016-03-09 09:49:00 +000011627 ExprCaptures.push_back(Ref->getDecl());
11628 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
11629 ExprResult RefRes = DefaultLvalueConversion(Ref);
11630 if (!RefRes.isUsable())
11631 continue;
11632 ExprResult PostUpdateRes =
11633 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
11634 SimpleRefExpr, RefRes.get());
11635 if (!PostUpdateRes.isUsable())
11636 continue;
11637 ExprPostUpdates.push_back(
11638 IgnoredValueConversions(PostUpdateRes.get()).get());
11639 }
11640 }
11641 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011642 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011643 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011644 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011645 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011646 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000011647 /*DirectInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011648 DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011649
11650 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000011651 Vars.push_back((VD || CurContext->isDependentContext())
11652 ? RefExpr->IgnoreParens()
11653 : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011654 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +000011655 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +000011656 }
11657
11658 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000011659 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +000011660
11661 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +000011662 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +000011663 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
11664 !Step->isInstantiationDependent() &&
11665 !Step->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011666 SourceLocation StepLoc = Step->getBeginLoc();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +000011667 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +000011668 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000011669 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +000011670 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +000011671
Alexander Musman3276a272015-03-21 10:12:56 +000011672 // Build var to save the step value.
11673 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +000011674 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +000011675 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +000011676 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +000011677 ExprResult CalcStep =
11678 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000011679 CalcStep = ActOnFinishFullExpr(CalcStep.get(), /*DiscardedValue*/ false);
Alexander Musman3276a272015-03-21 10:12:56 +000011680
Alexander Musman8dba6642014-04-22 13:09:42 +000011681 // Warn about zero linear step (it would be probably better specified as
11682 // making corresponding variables 'const').
11683 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +000011684 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
11685 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +000011686 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
11687 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +000011688 if (!IsConstant && CalcStep.isUsable()) {
11689 // Calculate the step beforehand instead of doing this on each iteration.
11690 // (This is not used if the number of iterations may be kfold-ed).
11691 CalcStepExpr = CalcStep.get();
11692 }
Alexander Musman8dba6642014-04-22 13:09:42 +000011693 }
11694
Alexey Bataev182227b2015-08-20 10:54:39 +000011695 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
11696 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +000011697 StepExpr, CalcStepExpr,
11698 buildPreInits(Context, ExprCaptures),
11699 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +000011700}
11701
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011702static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
11703 Expr *NumIterations, Sema &SemaRef,
11704 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +000011705 // Walk the vars and build update/final expressions for the CodeGen.
11706 SmallVector<Expr *, 8> Updates;
11707 SmallVector<Expr *, 8> Finals;
11708 Expr *Step = Clause.getStep();
11709 Expr *CalcStep = Clause.getCalcStep();
11710 // OpenMP [2.14.3.7, linear clause]
11711 // If linear-step is not specified it is assumed to be 1.
Alexey Bataeve3727102018-04-18 15:57:46 +000011712 if (!Step)
Alexander Musman3276a272015-03-21 10:12:56 +000011713 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000011714 else if (CalcStep)
Alexander Musman3276a272015-03-21 10:12:56 +000011715 Step = cast<BinaryOperator>(CalcStep)->getLHS();
11716 bool HasErrors = false;
11717 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011718 auto CurPrivate = Clause.privates().begin();
Alexey Bataeve3727102018-04-18 15:57:46 +000011719 OpenMPLinearClauseKind LinKind = Clause.getModifier();
11720 for (Expr *RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011721 SourceLocation ELoc;
11722 SourceRange ERange;
11723 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011724 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange);
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011725 ValueDecl *D = Res.first;
11726 if (Res.second || !D) {
11727 Updates.push_back(nullptr);
11728 Finals.push_back(nullptr);
11729 HasErrors = true;
11730 continue;
11731 }
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011732 auto &&Info = Stack->isLoopControlVariable(D);
Alexey Bataev2b86f212017-11-29 21:31:48 +000011733 // OpenMP [2.15.11, distribute simd Construct]
11734 // A list item may not appear in a linear clause, unless it is the loop
11735 // iteration variable.
11736 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) &&
11737 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) {
11738 SemaRef.Diag(ELoc,
11739 diag::err_omp_linear_distribute_var_non_loop_iteration);
11740 Updates.push_back(nullptr);
11741 Finals.push_back(nullptr);
11742 HasErrors = true;
11743 continue;
11744 }
Alexander Musman3276a272015-03-21 10:12:56 +000011745 Expr *InitExpr = *CurInit;
11746
11747 // Build privatized reference to the current linear var.
David Majnemer9d168222016-08-05 17:44:54 +000011748 auto *DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011749 Expr *CapturedRef;
11750 if (LinKind == OMPC_LINEAR_uval)
11751 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
11752 else
11753 CapturedRef =
11754 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
11755 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
11756 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +000011757
11758 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011759 ExprResult Update;
Alexey Bataeve3727102018-04-18 15:57:46 +000011760 if (!Info.first)
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011761 Update =
Alexey Bataeve3727102018-04-18 15:57:46 +000011762 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011763 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011764 else
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011765 Update = *CurPrivate;
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011766 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000011767 /*DiscardedValue*/ false);
Alexander Musman3276a272015-03-21 10:12:56 +000011768
11769 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011770 ExprResult Final;
Alexey Bataeve3727102018-04-18 15:57:46 +000011771 if (!Info.first)
11772 Final =
11773 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
11774 InitExpr, NumIterations, Step, /*Subtract=*/false);
11775 else
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011776 Final = *CurPrivate;
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011777 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000011778 /*DiscardedValue*/ false);
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011779
Alexander Musman3276a272015-03-21 10:12:56 +000011780 if (!Update.isUsable() || !Final.isUsable()) {
11781 Updates.push_back(nullptr);
11782 Finals.push_back(nullptr);
11783 HasErrors = true;
11784 } else {
11785 Updates.push_back(Update.get());
11786 Finals.push_back(Final.get());
11787 }
Richard Trieucc3949d2016-02-18 22:34:54 +000011788 ++CurInit;
11789 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +000011790 }
11791 Clause.setUpdates(Updates);
11792 Clause.setFinals(Finals);
11793 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +000011794}
11795
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011796OMPClause *Sema::ActOnOpenMPAlignedClause(
11797 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
11798 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011799 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +000011800 for (Expr *RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +000011801 assert(RefExpr && "NULL expr in OpenMP linear clause.");
11802 SourceLocation ELoc;
11803 SourceRange ERange;
11804 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011805 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev1efd1662016-03-29 10:59:56 +000011806 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011807 // It will be analyzed later.
11808 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011809 }
Alexey Bataev1efd1662016-03-29 10:59:56 +000011810 ValueDecl *D = Res.first;
11811 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011812 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011813
Alexey Bataev1efd1662016-03-29 10:59:56 +000011814 QualType QType = D->getType();
11815 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011816
11817 // OpenMP [2.8.1, simd construct, Restrictions]
11818 // The type of list items appearing in the aligned clause must be
11819 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011820 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011821 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +000011822 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011823 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +000011824 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011825 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +000011826 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011827 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +000011828 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011829 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +000011830 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011831 continue;
11832 }
11833
11834 // OpenMP [2.8.1, simd construct, Restrictions]
11835 // A list-item cannot appear in more than one aligned clause.
Alexey Bataeve3727102018-04-18 15:57:46 +000011836 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +000011837 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011838 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
11839 << getOpenMPClauseName(OMPC_aligned);
11840 continue;
11841 }
11842
Alexey Bataev1efd1662016-03-29 10:59:56 +000011843 DeclRefExpr *Ref = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000011844 if (!VD && isOpenMPCapturedDecl(D))
Alexey Bataev1efd1662016-03-29 10:59:56 +000011845 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
11846 Vars.push_back(DefaultFunctionArrayConversion(
11847 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
11848 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011849 }
11850
11851 // OpenMP [2.8.1, simd construct, Description]
11852 // The parameter of the aligned clause, alignment, must be a constant
11853 // positive integer expression.
11854 // If no optional parameter is specified, implementation-defined default
11855 // alignments for SIMD instructions on the target platforms are assumed.
11856 if (Alignment != nullptr) {
11857 ExprResult AlignResult =
11858 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
11859 if (AlignResult.isInvalid())
11860 return nullptr;
11861 Alignment = AlignResult.get();
11862 }
11863 if (Vars.empty())
11864 return nullptr;
11865
11866 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
11867 EndLoc, Vars, Alignment);
11868}
11869
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011870OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
11871 SourceLocation StartLoc,
11872 SourceLocation LParenLoc,
11873 SourceLocation EndLoc) {
11874 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011875 SmallVector<Expr *, 8> SrcExprs;
11876 SmallVector<Expr *, 8> DstExprs;
11877 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeve3727102018-04-18 15:57:46 +000011878 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000011879 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
11880 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011881 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000011882 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011883 SrcExprs.push_back(nullptr);
11884 DstExprs.push_back(nullptr);
11885 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011886 continue;
11887 }
11888
Alexey Bataeved09d242014-05-28 05:53:51 +000011889 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011890 // OpenMP [2.1, C/C++]
11891 // A list item is a variable name.
11892 // OpenMP [2.14.4.1, Restrictions, p.1]
11893 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeve3727102018-04-18 15:57:46 +000011894 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011895 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000011896 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
11897 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011898 continue;
11899 }
11900
11901 Decl *D = DE->getDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000011902 auto *VD = cast<VarDecl>(D);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011903
11904 QualType Type = VD->getType();
11905 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
11906 // It will be analyzed later.
11907 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011908 SrcExprs.push_back(nullptr);
11909 DstExprs.push_back(nullptr);
11910 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011911 continue;
11912 }
11913
11914 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
11915 // A list item that appears in a copyin clause must be threadprivate.
11916 if (!DSAStack->isThreadPrivate(VD)) {
11917 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +000011918 << getOpenMPClauseName(OMPC_copyin)
11919 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011920 continue;
11921 }
11922
11923 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
11924 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +000011925 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011926 // operator for the class type.
Alexey Bataeve3727102018-04-18 15:57:46 +000011927 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
11928 VarDecl *SrcVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011929 buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000011930 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011931 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011932 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +000011933 VarDecl *DstVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011934 buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst",
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000011935 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011936 DeclRefExpr *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011937 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011938 // For arrays generate assignment operation for single element and replace
11939 // it by the original array element in CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +000011940 ExprResult AssignmentOp =
11941 BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr,
11942 PseudoSrcExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011943 if (AssignmentOp.isInvalid())
11944 continue;
11945 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000011946 /*DiscardedValue*/ false);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011947 if (AssignmentOp.isInvalid())
11948 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011949
11950 DSAStack->addDSA(VD, DE, OMPC_copyin);
11951 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011952 SrcExprs.push_back(PseudoSrcExpr);
11953 DstExprs.push_back(PseudoDstExpr);
11954 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011955 }
11956
Alexey Bataeved09d242014-05-28 05:53:51 +000011957 if (Vars.empty())
11958 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011959
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011960 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
11961 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011962}
11963
Alexey Bataevbae9a792014-06-27 10:37:06 +000011964OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
11965 SourceLocation StartLoc,
11966 SourceLocation LParenLoc,
11967 SourceLocation EndLoc) {
11968 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +000011969 SmallVector<Expr *, 8> SrcExprs;
11970 SmallVector<Expr *, 8> DstExprs;
11971 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeve3727102018-04-18 15:57:46 +000011972 for (Expr *RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +000011973 assert(RefExpr && "NULL expr in OpenMP linear clause.");
11974 SourceLocation ELoc;
11975 SourceRange ERange;
11976 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011977 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataeve122da12016-03-17 10:50:17 +000011978 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000011979 // It will be analyzed later.
11980 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000011981 SrcExprs.push_back(nullptr);
11982 DstExprs.push_back(nullptr);
11983 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011984 }
Alexey Bataeve122da12016-03-17 10:50:17 +000011985 ValueDecl *D = Res.first;
11986 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +000011987 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000011988
Alexey Bataeve122da12016-03-17 10:50:17 +000011989 QualType Type = D->getType();
11990 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011991
11992 // OpenMP [2.14.4.2, Restrictions, p.2]
11993 // A list item that appears in a copyprivate clause may not appear in a
11994 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +000011995 if (!VD || !DSAStack->isThreadPrivate(VD)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011996 DSAStackTy::DSAVarData DVar =
11997 DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataeva63048e2015-03-23 06:18:07 +000011998 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
11999 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000012000 Diag(ELoc, diag::err_omp_wrong_dsa)
12001 << getOpenMPClauseName(DVar.CKind)
12002 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000012003 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000012004 continue;
12005 }
12006
12007 // OpenMP [2.11.4.2, Restrictions, p.1]
12008 // All list items that appear in a copyprivate clause must be either
12009 // threadprivate or private in the enclosing context.
12010 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +000012011 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +000012012 if (DVar.CKind == OMPC_shared) {
12013 Diag(ELoc, diag::err_omp_required_access)
12014 << getOpenMPClauseName(OMPC_copyprivate)
12015 << "threadprivate or private in the enclosing context";
Alexey Bataeve3727102018-04-18 15:57:46 +000012016 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000012017 continue;
12018 }
12019 }
12020 }
12021
Alexey Bataev7a3e5852015-05-19 08:19:24 +000012022 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000012023 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +000012024 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +000012025 << getOpenMPClauseName(OMPC_copyprivate) << Type
12026 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +000012027 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +000012028 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +000012029 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +000012030 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +000012031 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +000012032 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +000012033 continue;
12034 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +000012035
Alexey Bataevbae9a792014-06-27 10:37:06 +000012036 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
12037 // A variable of class type (or array thereof) that appears in a
12038 // copyin clause requires an accessible, unambiguous copy assignment
12039 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +000012040 Type = Context.getBaseElementType(Type.getNonReferenceType())
12041 .getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000012042 VarDecl *SrcVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000012043 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src",
Alexey Bataeve122da12016-03-17 10:50:17 +000012044 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000012045 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
12046 VarDecl *DstVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000012047 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst",
Alexey Bataeve122da12016-03-17 10:50:17 +000012048 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000012049 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
12050 ExprResult AssignmentOp = BuildBinOp(
12051 DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000012052 if (AssignmentOp.isInvalid())
12053 continue;
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000012054 AssignmentOp =
12055 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataeva63048e2015-03-23 06:18:07 +000012056 if (AssignmentOp.isInvalid())
12057 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000012058
12059 // No need to mark vars as copyprivate, they are already threadprivate or
12060 // implicitly private.
Alexey Bataeve3727102018-04-18 15:57:46 +000012061 assert(VD || isOpenMPCapturedDecl(D));
Alexey Bataeve122da12016-03-17 10:50:17 +000012062 Vars.push_back(
12063 VD ? RefExpr->IgnoreParens()
12064 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +000012065 SrcExprs.push_back(PseudoSrcExpr);
12066 DstExprs.push_back(PseudoDstExpr);
12067 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +000012068 }
12069
12070 if (Vars.empty())
12071 return nullptr;
12072
Alexey Bataeva63048e2015-03-23 06:18:07 +000012073 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
12074 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +000012075}
12076
Alexey Bataev6125da92014-07-21 11:26:11 +000012077OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
12078 SourceLocation StartLoc,
12079 SourceLocation LParenLoc,
12080 SourceLocation EndLoc) {
12081 if (VarList.empty())
12082 return nullptr;
12083
12084 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
12085}
Alexey Bataevdea47612014-07-23 07:46:59 +000012086
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012087OMPClause *
12088Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
12089 SourceLocation DepLoc, SourceLocation ColonLoc,
12090 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
12091 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +000012092 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012093 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +000012094 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000012095 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +000012096 return nullptr;
12097 }
12098 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012099 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
12100 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +000012101 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012102 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000012103 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
12104 /*Last=*/OMPC_DEPEND_unknown, Except)
12105 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012106 return nullptr;
12107 }
12108 SmallVector<Expr *, 8> Vars;
Alexey Bataev8b427062016-05-25 12:36:08 +000012109 DSAStackTy::OperatorOffsetTy OpsOffs;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012110 llvm::APSInt DepCounter(/*BitWidth=*/32);
12111 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
Alexey Bataevf138fda2018-08-13 19:04:24 +000012112 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) {
12113 if (const Expr *OrderedCountExpr =
12114 DSAStack->getParentOrderedRegionParam().first) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012115 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
12116 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012117 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012118 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012119 for (Expr *RefExpr : VarList) {
Alexey Bataev17daedf2018-02-15 22:42:57 +000012120 assert(RefExpr && "NULL expr in OpenMP shared clause.");
12121 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
12122 // It will be analyzed later.
12123 Vars.push_back(RefExpr);
12124 continue;
12125 }
12126
12127 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataeve3727102018-04-18 15:57:46 +000012128 Expr *SimpleExpr = RefExpr->IgnoreParenCasts();
Alexey Bataev17daedf2018-02-15 22:42:57 +000012129 if (DepKind == OMPC_DEPEND_sink) {
Alexey Bataevf138fda2018-08-13 19:04:24 +000012130 if (DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000012131 DepCounter >= TotalDepCount) {
12132 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
12133 continue;
12134 }
12135 ++DepCounter;
12136 // OpenMP [2.13.9, Summary]
12137 // depend(dependence-type : vec), where dependence-type is:
12138 // 'sink' and where vec is the iteration vector, which has the form:
12139 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
12140 // where n is the value specified by the ordered clause in the loop
12141 // directive, xi denotes the loop iteration variable of the i-th nested
12142 // loop associated with the loop directive, and di is a constant
12143 // non-negative integer.
12144 if (CurContext->isDependentContext()) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012145 // It will be analyzed later.
12146 Vars.push_back(RefExpr);
12147 continue;
12148 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000012149 SimpleExpr = SimpleExpr->IgnoreImplicit();
12150 OverloadedOperatorKind OOK = OO_None;
12151 SourceLocation OOLoc;
12152 Expr *LHS = SimpleExpr;
12153 Expr *RHS = nullptr;
12154 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
12155 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
12156 OOLoc = BO->getOperatorLoc();
12157 LHS = BO->getLHS()->IgnoreParenImpCasts();
12158 RHS = BO->getRHS()->IgnoreParenImpCasts();
12159 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
12160 OOK = OCE->getOperator();
12161 OOLoc = OCE->getOperatorLoc();
12162 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
12163 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
12164 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
12165 OOK = MCE->getMethodDecl()
12166 ->getNameInfo()
12167 .getName()
12168 .getCXXOverloadedOperator();
12169 OOLoc = MCE->getCallee()->getExprLoc();
12170 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
12171 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012172 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000012173 SourceLocation ELoc;
12174 SourceRange ERange;
Alexey Bataevbc529672018-09-28 19:33:14 +000012175 auto Res = getPrivateItem(*this, LHS, ELoc, ERange);
Alexey Bataev17daedf2018-02-15 22:42:57 +000012176 if (Res.second) {
12177 // It will be analyzed later.
12178 Vars.push_back(RefExpr);
12179 }
12180 ValueDecl *D = Res.first;
12181 if (!D)
12182 continue;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012183
Alexey Bataev17daedf2018-02-15 22:42:57 +000012184 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
12185 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
12186 continue;
12187 }
12188 if (RHS) {
12189 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
12190 RHS, OMPC_depend, /*StrictlyPositive=*/false);
12191 if (RHSRes.isInvalid())
12192 continue;
12193 }
12194 if (!CurContext->isDependentContext() &&
Alexey Bataevf138fda2018-08-13 19:04:24 +000012195 DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000012196 DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012197 const ValueDecl *VD =
Alexey Bataev17daedf2018-02-15 22:42:57 +000012198 DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue());
Alexey Bataeve3727102018-04-18 15:57:46 +000012199 if (VD)
Alexey Bataev17daedf2018-02-15 22:42:57 +000012200 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
12201 << 1 << VD;
Alexey Bataeve3727102018-04-18 15:57:46 +000012202 else
Alexey Bataev17daedf2018-02-15 22:42:57 +000012203 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0;
Alexey Bataev17daedf2018-02-15 22:42:57 +000012204 continue;
12205 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012206 OpsOffs.emplace_back(RHS, OOK);
Alexey Bataev17daedf2018-02-15 22:42:57 +000012207 } else {
12208 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
12209 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
12210 (ASE &&
12211 !ASE->getBase()->getType().getNonReferenceType()->isPointerType() &&
12212 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
12213 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
12214 << RefExpr->getSourceRange();
12215 continue;
12216 }
12217 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
12218 getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
12219 ExprResult Res =
12220 CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RefExpr->IgnoreParenImpCasts());
12221 getDiagnostics().setSuppressAllDiagnostics(Suppress);
12222 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr)) {
12223 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
12224 << RefExpr->getSourceRange();
12225 continue;
12226 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012227 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000012228 Vars.push_back(RefExpr->IgnoreParenImpCasts());
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012229 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000012230
12231 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
12232 TotalDepCount > VarList.size() &&
Alexey Bataevf138fda2018-08-13 19:04:24 +000012233 DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000012234 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) {
12235 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
12236 << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
12237 }
12238 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
12239 Vars.empty())
12240 return nullptr;
12241
Alexey Bataev8b427062016-05-25 12:36:08 +000012242 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataevf138fda2018-08-13 19:04:24 +000012243 DepKind, DepLoc, ColonLoc, Vars,
12244 TotalDepCount.getZExtValue());
Alexey Bataev17daedf2018-02-15 22:42:57 +000012245 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) &&
12246 DSAStack->isParentOrderedRegion())
Alexey Bataev8b427062016-05-25 12:36:08 +000012247 DSAStack->addDoacrossDependClause(C, OpsOffs);
12248 return C;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012249}
Michael Wonge710d542015-08-07 16:16:36 +000012250
12251OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
12252 SourceLocation LParenLoc,
12253 SourceLocation EndLoc) {
12254 Expr *ValExpr = Device;
Alexey Bataev931e19b2017-10-02 16:32:39 +000012255 Stmt *HelperValStmt = nullptr;
Michael Wonge710d542015-08-07 16:16:36 +000012256
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012257 // OpenMP [2.9.1, Restrictions]
12258 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000012259 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
Alexey Bataeva0569352015-12-01 10:17:31 +000012260 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012261 return nullptr;
12262
Alexey Bataev931e19b2017-10-02 16:32:39 +000012263 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000012264 OpenMPDirectiveKind CaptureRegion =
12265 getOpenMPCaptureRegionForClause(DKind, OMPC_device);
12266 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000012267 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000012268 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev931e19b2017-10-02 16:32:39 +000012269 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
12270 HelperValStmt = buildPreInits(Context, Captures);
12271 }
12272
Alexey Bataev8451efa2018-01-15 19:06:12 +000012273 return new (Context) OMPDeviceClause(ValExpr, HelperValStmt, CaptureRegion,
12274 StartLoc, LParenLoc, EndLoc);
Michael Wonge710d542015-08-07 16:16:36 +000012275}
Kelvin Li0bff7af2015-11-23 05:32:03 +000012276
Alexey Bataeve3727102018-04-18 15:57:46 +000012277static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
Alexey Bataev95c23e72018-02-27 21:31:11 +000012278 DSAStackTy *Stack, QualType QTy,
12279 bool FullCheck = true) {
Kelvin Li0bff7af2015-11-23 05:32:03 +000012280 NamedDecl *ND;
12281 if (QTy->isIncompleteType(&ND)) {
12282 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
12283 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +000012284 }
Alexey Bataev95c23e72018-02-27 21:31:11 +000012285 if (FullCheck && !SemaRef.CurContext->isDependentContext() &&
12286 !QTy.isTrivialType(SemaRef.Context))
12287 SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR;
Kelvin Li0bff7af2015-11-23 05:32:03 +000012288 return true;
12289}
12290
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000012291/// Return true if it can be proven that the provided array expression
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012292/// (array section or array subscript) does NOT specify the whole size of the
12293/// array whose base type is \a BaseQTy.
Alexey Bataeve3727102018-04-18 15:57:46 +000012294static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012295 const Expr *E,
12296 QualType BaseQTy) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012297 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012298
12299 // If this is an array subscript, it refers to the whole size if the size of
12300 // the dimension is constant and equals 1. Also, an array section assumes the
12301 // format of an array subscript if no colon is used.
12302 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012303 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012304 return ATy->getSize().getSExtValue() != 1;
12305 // Size can't be evaluated statically.
12306 return false;
12307 }
12308
12309 assert(OASE && "Expecting array section if not an array subscript.");
Alexey Bataeve3727102018-04-18 15:57:46 +000012310 const Expr *LowerBound = OASE->getLowerBound();
12311 const Expr *Length = OASE->getLength();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012312
12313 // If there is a lower bound that does not evaluates to zero, we are not
David Majnemer9d168222016-08-05 17:44:54 +000012314 // covering the whole dimension.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012315 if (LowerBound) {
Fangrui Song407659a2018-11-30 23:41:18 +000012316 Expr::EvalResult Result;
12317 if (!LowerBound->EvaluateAsInt(Result, SemaRef.getASTContext()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012318 return false; // Can't get the integer value as a constant.
Fangrui Song407659a2018-11-30 23:41:18 +000012319
12320 llvm::APSInt ConstLowerBound = Result.Val.getInt();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012321 if (ConstLowerBound.getSExtValue())
12322 return true;
12323 }
12324
12325 // If we don't have a length we covering the whole dimension.
12326 if (!Length)
12327 return false;
12328
12329 // If the base is a pointer, we don't have a way to get the size of the
12330 // pointee.
12331 if (BaseQTy->isPointerType())
12332 return false;
12333
12334 // We can only check if the length is the same as the size of the dimension
12335 // if we have a constant array.
Alexey Bataeve3727102018-04-18 15:57:46 +000012336 const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012337 if (!CATy)
12338 return false;
12339
Fangrui Song407659a2018-11-30 23:41:18 +000012340 Expr::EvalResult Result;
12341 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012342 return false; // Can't get the integer value as a constant.
12343
Fangrui Song407659a2018-11-30 23:41:18 +000012344 llvm::APSInt ConstLength = Result.Val.getInt();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012345 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
12346}
12347
12348// Return true if it can be proven that the provided array expression (array
12349// section or array subscript) does NOT specify a single element of the array
12350// whose base type is \a BaseQTy.
Alexey Bataeve3727102018-04-18 15:57:46 +000012351static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
David Majnemer9d168222016-08-05 17:44:54 +000012352 const Expr *E,
12353 QualType BaseQTy) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012354 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012355
12356 // An array subscript always refer to a single element. Also, an array section
12357 // assumes the format of an array subscript if no colon is used.
12358 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
12359 return false;
12360
12361 assert(OASE && "Expecting array section if not an array subscript.");
Alexey Bataeve3727102018-04-18 15:57:46 +000012362 const Expr *Length = OASE->getLength();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012363
12364 // If we don't have a length we have to check if the array has unitary size
12365 // for this dimension. Also, we should always expect a length if the base type
12366 // is pointer.
12367 if (!Length) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012368 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012369 return ATy->getSize().getSExtValue() != 1;
12370 // We cannot assume anything.
12371 return false;
12372 }
12373
12374 // Check if the length evaluates to 1.
Fangrui Song407659a2018-11-30 23:41:18 +000012375 Expr::EvalResult Result;
12376 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012377 return false; // Can't get the integer value as a constant.
12378
Fangrui Song407659a2018-11-30 23:41:18 +000012379 llvm::APSInt ConstLength = Result.Val.getInt();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012380 return ConstLength.getSExtValue() != 1;
12381}
12382
Samuel Antao661c0902016-05-26 17:39:58 +000012383// Return the expression of the base of the mappable expression or null if it
12384// cannot be determined and do all the necessary checks to see if the expression
12385// is valid as a standalone mappable expression. In the process, record all the
Samuel Antao90927002016-04-26 14:54:23 +000012386// components of the expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000012387static const Expr *checkMapClauseExpressionBase(
Samuel Antao90927002016-04-26 14:54:23 +000012388 Sema &SemaRef, Expr *E,
Samuel Antao661c0902016-05-26 17:39:58 +000012389 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012390 OpenMPClauseKind CKind, bool NoDiagnose) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012391 SourceLocation ELoc = E->getExprLoc();
12392 SourceRange ERange = E->getSourceRange();
12393
12394 // The base of elements of list in a map clause have to be either:
12395 // - a reference to variable or field.
12396 // - a member expression.
12397 // - an array expression.
12398 //
12399 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
12400 // reference to 'r'.
12401 //
12402 // If we have:
12403 //
12404 // struct SS {
12405 // Bla S;
12406 // foo() {
12407 // #pragma omp target map (S.Arr[:12]);
12408 // }
12409 // }
12410 //
12411 // We want to retrieve the member expression 'this->S';
12412
Alexey Bataeve3727102018-04-18 15:57:46 +000012413 const Expr *RelevantExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012414
Samuel Antao5de996e2016-01-22 20:21:36 +000012415 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
12416 // If a list item is an array section, it must specify contiguous storage.
12417 //
12418 // For this restriction it is sufficient that we make sure only references
12419 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012420 // exist except in the rightmost expression (unless they cover the whole
12421 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +000012422 //
12423 // r.ArrS[3:5].Arr[6:7]
12424 //
12425 // r.ArrS[3:5].x
12426 //
12427 // but these would be valid:
12428 // r.ArrS[3].Arr[6:7]
12429 //
12430 // r.ArrS[3].x
12431
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012432 bool AllowUnitySizeArraySection = true;
12433 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +000012434
Dmitry Polukhin644a9252016-03-11 07:58:34 +000012435 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012436 E = E->IgnoreParenImpCasts();
12437
12438 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
12439 if (!isa<VarDecl>(CurE->getDecl()))
Alexey Bataev27041fa2017-12-05 15:22:49 +000012440 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012441
12442 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012443
12444 // If we got a reference to a declaration, we should not expect any array
12445 // section before that.
12446 AllowUnitySizeArraySection = false;
12447 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000012448
12449 // Record the component.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012450 CurComponents.emplace_back(CurE, CurE->getDecl());
12451 } else if (auto *CurE = dyn_cast<MemberExpr>(E)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012452 Expr *BaseE = CurE->getBase()->IgnoreParenImpCasts();
Samuel Antao5de996e2016-01-22 20:21:36 +000012453
12454 if (isa<CXXThisExpr>(BaseE))
12455 // We found a base expression: this->Val.
12456 RelevantExpr = CurE;
12457 else
12458 E = BaseE;
12459
12460 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012461 if (!NoDiagnose) {
12462 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
12463 << CurE->getSourceRange();
12464 return nullptr;
12465 }
12466 if (RelevantExpr)
12467 return nullptr;
12468 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000012469 }
12470
12471 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
12472
12473 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
12474 // A bit-field cannot appear in a map clause.
12475 //
12476 if (FD->isBitField()) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012477 if (!NoDiagnose) {
12478 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
12479 << CurE->getSourceRange() << getOpenMPClauseName(CKind);
12480 return nullptr;
12481 }
12482 if (RelevantExpr)
12483 return nullptr;
12484 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000012485 }
12486
12487 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12488 // If the type of a list item is a reference to a type T then the type
12489 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012490 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000012491
12492 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
12493 // A list item cannot be a variable that is a member of a structure with
12494 // a union type.
12495 //
Alexey Bataeve3727102018-04-18 15:57:46 +000012496 if (CurType->isUnionType()) {
12497 if (!NoDiagnose) {
12498 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
12499 << CurE->getSourceRange();
12500 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012501 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012502 continue;
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012503 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012504
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012505 // If we got a member expression, we should not expect any array section
12506 // before that:
12507 //
12508 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
12509 // If a list item is an element of a structure, only the rightmost symbol
12510 // of the variable reference can be an array section.
12511 //
12512 AllowUnitySizeArraySection = false;
12513 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000012514
12515 // Record the component.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012516 CurComponents.emplace_back(CurE, FD);
12517 } else if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012518 E = CurE->getBase()->IgnoreParenImpCasts();
12519
12520 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012521 if (!NoDiagnose) {
12522 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
12523 << 0 << CurE->getSourceRange();
12524 return nullptr;
12525 }
12526 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000012527 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012528
12529 // If we got an array subscript that express the whole dimension we
12530 // can have any array expressions before. If it only expressing part of
12531 // the dimension, we can only have unitary-size array expressions.
Alexey Bataeve3727102018-04-18 15:57:46 +000012532 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012533 E->getType()))
12534 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000012535
Patrick Lystere13b1e32019-01-02 19:28:48 +000012536 if (const auto *TE = dyn_cast<CXXThisExpr>(E)) {
12537 Expr::EvalResult Result;
12538 if (CurE->getIdx()->EvaluateAsInt(Result, SemaRef.getASTContext())) {
12539 if (!Result.Val.getInt().isNullValue()) {
12540 SemaRef.Diag(CurE->getIdx()->getExprLoc(),
12541 diag::err_omp_invalid_map_this_expr);
12542 SemaRef.Diag(CurE->getIdx()->getExprLoc(),
12543 diag::note_omp_invalid_subscript_on_this_ptr_map);
12544 }
12545 }
12546 RelevantExpr = TE;
12547 }
12548
Samuel Antao90927002016-04-26 14:54:23 +000012549 // Record the component - we don't have any declaration associated.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012550 CurComponents.emplace_back(CurE, nullptr);
12551 } else if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012552 assert(!NoDiagnose && "Array sections cannot be implicitly mapped.");
Samuel Antao5de996e2016-01-22 20:21:36 +000012553 E = CurE->getBase()->IgnoreParenImpCasts();
12554
Alexey Bataev27041fa2017-12-05 15:22:49 +000012555 QualType CurType =
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012556 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
12557
Samuel Antao5de996e2016-01-22 20:21:36 +000012558 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12559 // If the type of a list item is a reference to a type T then the type
12560 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +000012561 if (CurType->isReferenceType())
12562 CurType = CurType->getPointeeType();
12563
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012564 bool IsPointer = CurType->isAnyPointerType();
12565
12566 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012567 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
12568 << 0 << CurE->getSourceRange();
Alexey Bataev27041fa2017-12-05 15:22:49 +000012569 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012570 }
12571
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012572 bool NotWhole =
Alexey Bataeve3727102018-04-18 15:57:46 +000012573 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012574 bool NotUnity =
Alexey Bataeve3727102018-04-18 15:57:46 +000012575 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012576
Samuel Antaodab51bb2016-07-18 23:22:11 +000012577 if (AllowWholeSizeArraySection) {
12578 // Any array section is currently allowed. Allowing a whole size array
12579 // section implies allowing a unity array section as well.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012580 //
12581 // If this array section refers to the whole dimension we can still
12582 // accept other array sections before this one, except if the base is a
12583 // pointer. Otherwise, only unitary sections are accepted.
12584 if (NotWhole || IsPointer)
12585 AllowWholeSizeArraySection = false;
Samuel Antaodab51bb2016-07-18 23:22:11 +000012586 } else if (AllowUnitySizeArraySection && NotUnity) {
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012587 // A unity or whole array section is not allowed and that is not
12588 // compatible with the properties of the current array section.
12589 SemaRef.Diag(
12590 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
12591 << CurE->getSourceRange();
Alexey Bataev27041fa2017-12-05 15:22:49 +000012592 return nullptr;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012593 }
Samuel Antao90927002016-04-26 14:54:23 +000012594
Patrick Lystere13b1e32019-01-02 19:28:48 +000012595 if (const auto *TE = dyn_cast<CXXThisExpr>(E)) {
12596 Expr::EvalResult ResultR;
12597 Expr::EvalResult ResultL;
12598 if (CurE->getLength()->EvaluateAsInt(ResultR,
12599 SemaRef.getASTContext())) {
12600 if (!ResultR.Val.getInt().isOneValue()) {
12601 SemaRef.Diag(CurE->getLength()->getExprLoc(),
12602 diag::err_omp_invalid_map_this_expr);
12603 SemaRef.Diag(CurE->getLength()->getExprLoc(),
12604 diag::note_omp_invalid_length_on_this_ptr_mapping);
12605 }
12606 }
12607 if (CurE->getLowerBound() && CurE->getLowerBound()->EvaluateAsInt(
12608 ResultL, SemaRef.getASTContext())) {
12609 if (!ResultL.Val.getInt().isNullValue()) {
12610 SemaRef.Diag(CurE->getLowerBound()->getExprLoc(),
12611 diag::err_omp_invalid_map_this_expr);
12612 SemaRef.Diag(CurE->getLowerBound()->getExprLoc(),
12613 diag::note_omp_invalid_lower_bound_on_this_ptr_mapping);
12614 }
12615 }
12616 RelevantExpr = TE;
12617 }
12618
Samuel Antao90927002016-04-26 14:54:23 +000012619 // Record the component - we don't have any declaration associated.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012620 CurComponents.emplace_back(CurE, nullptr);
12621 } else {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012622 if (!NoDiagnose) {
12623 // If nothing else worked, this is not a valid map clause expression.
12624 SemaRef.Diag(
12625 ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
12626 << ERange;
12627 }
Alexey Bataev27041fa2017-12-05 15:22:49 +000012628 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012629 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012630 }
12631
12632 return RelevantExpr;
12633}
12634
12635// Return true if expression E associated with value VD has conflicts with other
12636// map information.
Alexey Bataeve3727102018-04-18 15:57:46 +000012637static bool checkMapConflicts(
12638 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E,
Samuel Antao90927002016-04-26 14:54:23 +000012639 bool CurrentRegionOnly,
Samuel Antao661c0902016-05-26 17:39:58 +000012640 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
12641 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012642 assert(VD && E);
Samuel Antao5de996e2016-01-22 20:21:36 +000012643 SourceLocation ELoc = E->getExprLoc();
12644 SourceRange ERange = E->getSourceRange();
12645
12646 // In order to easily check the conflicts we need to match each component of
12647 // the expression under test with the components of the expressions that are
12648 // already in the stack.
12649
Samuel Antao5de996e2016-01-22 20:21:36 +000012650 assert(!CurComponents.empty() && "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000012651 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000012652 "Map clause expression with unexpected base!");
12653
12654 // Variables to help detecting enclosing problems in data environment nests.
12655 bool IsEnclosedByDataEnvironmentExpr = false;
Samuel Antao90927002016-04-26 14:54:23 +000012656 const Expr *EnclosingExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012657
Samuel Antao90927002016-04-26 14:54:23 +000012658 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
12659 VD, CurrentRegionOnly,
Alexey Bataeve3727102018-04-18 15:57:46 +000012660 [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc,
12661 ERange, CKind, &EnclosingExpr,
12662 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef
12663 StackComponents,
12664 OpenMPClauseKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012665 assert(!StackComponents.empty() &&
12666 "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000012667 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000012668 "Map clause expression with unexpected base!");
Fangrui Song16fe49a2018-04-18 19:32:01 +000012669 (void)VD;
Samuel Antao5de996e2016-01-22 20:21:36 +000012670
Samuel Antao90927002016-04-26 14:54:23 +000012671 // The whole expression in the stack.
Alexey Bataeve3727102018-04-18 15:57:46 +000012672 const Expr *RE = StackComponents.front().getAssociatedExpression();
Samuel Antao90927002016-04-26 14:54:23 +000012673
Samuel Antao5de996e2016-01-22 20:21:36 +000012674 // Expressions must start from the same base. Here we detect at which
12675 // point both expressions diverge from each other and see if we can
12676 // detect if the memory referred to both expressions is contiguous and
12677 // do not overlap.
12678 auto CI = CurComponents.rbegin();
12679 auto CE = CurComponents.rend();
12680 auto SI = StackComponents.rbegin();
12681 auto SE = StackComponents.rend();
12682 for (; CI != CE && SI != SE; ++CI, ++SI) {
12683
12684 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
12685 // At most one list item can be an array item derived from a given
12686 // variable in map clauses of the same construct.
Samuel Antao90927002016-04-26 14:54:23 +000012687 if (CurrentRegionOnly &&
12688 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
12689 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
12690 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
12691 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
12692 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
Samuel Antao5de996e2016-01-22 20:21:36 +000012693 diag::err_omp_multiple_array_items_in_map_clause)
Samuel Antao90927002016-04-26 14:54:23 +000012694 << CI->getAssociatedExpression()->getSourceRange();
12695 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
12696 diag::note_used_here)
12697 << SI->getAssociatedExpression()->getSourceRange();
Samuel Antao5de996e2016-01-22 20:21:36 +000012698 return true;
12699 }
12700
12701 // Do both expressions have the same kind?
Samuel Antao90927002016-04-26 14:54:23 +000012702 if (CI->getAssociatedExpression()->getStmtClass() !=
12703 SI->getAssociatedExpression()->getStmtClass())
Samuel Antao5de996e2016-01-22 20:21:36 +000012704 break;
12705
12706 // Are we dealing with different variables/fields?
Samuel Antao90927002016-04-26 14:54:23 +000012707 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
Samuel Antao5de996e2016-01-22 20:21:36 +000012708 break;
12709 }
Kelvin Li9f645ae2016-07-18 22:49:16 +000012710 // Check if the extra components of the expressions in the enclosing
12711 // data environment are redundant for the current base declaration.
12712 // If they are, the maps completely overlap, which is legal.
12713 for (; SI != SE; ++SI) {
12714 QualType Type;
Alexey Bataeve3727102018-04-18 15:57:46 +000012715 if (const auto *ASE =
David Majnemer9d168222016-08-05 17:44:54 +000012716 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
Kelvin Li9f645ae2016-07-18 22:49:16 +000012717 Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
Alexey Bataeve3727102018-04-18 15:57:46 +000012718 } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(
David Majnemer9d168222016-08-05 17:44:54 +000012719 SI->getAssociatedExpression())) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012720 const Expr *E = OASE->getBase()->IgnoreParenImpCasts();
Kelvin Li9f645ae2016-07-18 22:49:16 +000012721 Type =
12722 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
12723 }
12724 if (Type.isNull() || Type->isAnyPointerType() ||
Alexey Bataeve3727102018-04-18 15:57:46 +000012725 checkArrayExpressionDoesNotReferToWholeSize(
Kelvin Li9f645ae2016-07-18 22:49:16 +000012726 SemaRef, SI->getAssociatedExpression(), Type))
12727 break;
12728 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012729
12730 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
12731 // List items of map clauses in the same construct must not share
12732 // original storage.
12733 //
12734 // If the expressions are exactly the same or one is a subset of the
12735 // other, it means they are sharing storage.
12736 if (CI == CE && SI == SE) {
12737 if (CurrentRegionOnly) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012738 if (CKind == OMPC_map) {
Samuel Antao661c0902016-05-26 17:39:58 +000012739 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +000012740 } else {
Samuel Antaoec172c62016-05-26 17:49:04 +000012741 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000012742 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
12743 << ERange;
12744 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012745 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12746 << RE->getSourceRange();
12747 return true;
Samuel Antao5de996e2016-01-22 20:21:36 +000012748 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012749 // If we find the same expression in the enclosing data environment,
12750 // that is legal.
12751 IsEnclosedByDataEnvironmentExpr = true;
12752 return false;
Samuel Antao5de996e2016-01-22 20:21:36 +000012753 }
12754
Samuel Antao90927002016-04-26 14:54:23 +000012755 QualType DerivedType =
12756 std::prev(CI)->getAssociatedDeclaration()->getType();
12757 SourceLocation DerivedLoc =
12758 std::prev(CI)->getAssociatedExpression()->getExprLoc();
Samuel Antao5de996e2016-01-22 20:21:36 +000012759
12760 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12761 // If the type of a list item is a reference to a type T then the type
12762 // will be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000012763 DerivedType = DerivedType.getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000012764
12765 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
12766 // A variable for which the type is pointer and an array section
12767 // derived from that variable must not appear as list items of map
12768 // clauses of the same construct.
12769 //
12770 // Also, cover one of the cases in:
12771 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
12772 // If any part of the original storage of a list item has corresponding
12773 // storage in the device data environment, all of the original storage
12774 // must have corresponding storage in the device data environment.
12775 //
12776 if (DerivedType->isAnyPointerType()) {
12777 if (CI == CE || SI == SE) {
12778 SemaRef.Diag(
12779 DerivedLoc,
12780 diag::err_omp_pointer_mapped_along_with_derived_section)
12781 << DerivedLoc;
Alexey Bataev2819260b2018-02-27 17:42:00 +000012782 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12783 << RE->getSourceRange();
12784 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +000012785 }
12786 if (CI->getAssociatedExpression()->getStmtClass() !=
Alexey Bataev2819260b2018-02-27 17:42:00 +000012787 SI->getAssociatedExpression()->getStmtClass() ||
12788 CI->getAssociatedDeclaration()->getCanonicalDecl() ==
12789 SI->getAssociatedDeclaration()->getCanonicalDecl()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012790 assert(CI != CE && SI != SE);
Alexey Bataev2819260b2018-02-27 17:42:00 +000012791 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced)
Samuel Antao5de996e2016-01-22 20:21:36 +000012792 << DerivedLoc;
Alexey Bataev2819260b2018-02-27 17:42:00 +000012793 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12794 << RE->getSourceRange();
12795 return true;
Samuel Antao5de996e2016-01-22 20:21:36 +000012796 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012797 }
12798
12799 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
12800 // List items of map clauses in the same construct must not share
12801 // original storage.
12802 //
12803 // An expression is a subset of the other.
12804 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012805 if (CKind == OMPC_map) {
Alexey Bataeve82445f2018-09-20 13:54:02 +000012806 if (CI != CE || SI != SE) {
12807 // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is
12808 // a pointer.
12809 auto Begin =
12810 CI != CE ? CurComponents.begin() : StackComponents.begin();
12811 auto End = CI != CE ? CurComponents.end() : StackComponents.end();
12812 auto It = Begin;
12813 while (It != End && !It->getAssociatedDeclaration())
12814 std::advance(It, 1);
12815 assert(It != End &&
12816 "Expected at least one component with the declaration.");
12817 if (It != Begin && It->getAssociatedDeclaration()
12818 ->getType()
12819 .getCanonicalType()
12820 ->isAnyPointerType()) {
12821 IsEnclosedByDataEnvironmentExpr = false;
12822 EnclosingExpr = nullptr;
12823 return false;
12824 }
12825 }
Samuel Antao661c0902016-05-26 17:39:58 +000012826 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +000012827 } else {
Samuel Antaoec172c62016-05-26 17:49:04 +000012828 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000012829 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
12830 << ERange;
12831 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012832 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12833 << RE->getSourceRange();
12834 return true;
12835 }
12836
12837 // The current expression uses the same base as other expression in the
Samuel Antao90927002016-04-26 14:54:23 +000012838 // data environment but does not contain it completely.
Samuel Antao5de996e2016-01-22 20:21:36 +000012839 if (!CurrentRegionOnly && SI != SE)
12840 EnclosingExpr = RE;
12841
12842 // The current expression is a subset of the expression in the data
12843 // environment.
12844 IsEnclosedByDataEnvironmentExpr |=
12845 (!CurrentRegionOnly && CI != CE && SI == SE);
12846
12847 return false;
12848 });
12849
12850 if (CurrentRegionOnly)
12851 return FoundError;
12852
12853 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
12854 // If any part of the original storage of a list item has corresponding
12855 // storage in the device data environment, all of the original storage must
12856 // have corresponding storage in the device data environment.
12857 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
12858 // If a list item is an element of a structure, and a different element of
12859 // the structure has a corresponding list item in the device data environment
12860 // prior to a task encountering the construct associated with the map clause,
Samuel Antao90927002016-04-26 14:54:23 +000012861 // then the list item must also have a corresponding list item in the device
Samuel Antao5de996e2016-01-22 20:21:36 +000012862 // data environment prior to the task encountering the construct.
12863 //
12864 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
12865 SemaRef.Diag(ELoc,
12866 diag::err_omp_original_storage_is_shared_and_does_not_contain)
12867 << ERange;
12868 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
12869 << EnclosingExpr->getSourceRange();
12870 return true;
12871 }
12872
12873 return FoundError;
12874}
12875
Samuel Antao661c0902016-05-26 17:39:58 +000012876namespace {
12877// Utility struct that gathers all the related lists associated with a mappable
12878// expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000012879struct MappableVarListInfo {
Samuel Antao661c0902016-05-26 17:39:58 +000012880 // The list of expressions.
12881 ArrayRef<Expr *> VarList;
12882 // The list of processed expressions.
12883 SmallVector<Expr *, 16> ProcessedVarList;
12884 // The mappble components for each expression.
12885 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
12886 // The base declaration of the variable.
12887 SmallVector<ValueDecl *, 16> VarBaseDeclarations;
12888
12889 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
12890 // We have a list of components and base declarations for each entry in the
12891 // variable list.
12892 VarComponents.reserve(VarList.size());
12893 VarBaseDeclarations.reserve(VarList.size());
12894 }
12895};
12896}
12897
12898// Check the validity of the provided variable list for the provided clause kind
12899// \a CKind. In the check process the valid expressions, and mappable expression
12900// components and variables are extracted and used to fill \a Vars,
12901// \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and
12902// \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'.
12903static void
12904checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS,
12905 OpenMPClauseKind CKind, MappableVarListInfo &MVLI,
12906 SourceLocation StartLoc,
12907 OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
12908 bool IsMapTypeImplicit = false) {
Samuel Antaoec172c62016-05-26 17:49:04 +000012909 // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
12910 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
Samuel Antao661c0902016-05-26 17:39:58 +000012911 "Unexpected clause kind with mappable expressions!");
Kelvin Li0bff7af2015-11-23 05:32:03 +000012912
Samuel Antao90927002016-04-26 14:54:23 +000012913 // Keep track of the mappable components and base declarations in this clause.
12914 // Each entry in the list is going to have a list of components associated. We
12915 // record each set of the components so that we can build the clause later on.
12916 // In the end we should have the same amount of declarations and component
12917 // lists.
Samuel Antao90927002016-04-26 14:54:23 +000012918
Alexey Bataeve3727102018-04-18 15:57:46 +000012919 for (Expr *RE : MVLI.VarList) {
Samuel Antaoec172c62016-05-26 17:49:04 +000012920 assert(RE && "Null expr in omp to/from/map clause");
Kelvin Li0bff7af2015-11-23 05:32:03 +000012921 SourceLocation ELoc = RE->getExprLoc();
12922
Alexey Bataeve3727102018-04-18 15:57:46 +000012923 const Expr *VE = RE->IgnoreParenLValueCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000012924
12925 if (VE->isValueDependent() || VE->isTypeDependent() ||
12926 VE->isInstantiationDependent() ||
12927 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012928 // We can only analyze this information once the missing information is
12929 // resolved.
Samuel Antao661c0902016-05-26 17:39:58 +000012930 MVLI.ProcessedVarList.push_back(RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012931 continue;
12932 }
12933
Alexey Bataeve3727102018-04-18 15:57:46 +000012934 Expr *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000012935
Samuel Antao5de996e2016-01-22 20:21:36 +000012936 if (!RE->IgnoreParenImpCasts()->isLValue()) {
Samuel Antao661c0902016-05-26 17:39:58 +000012937 SemaRef.Diag(ELoc,
12938 diag::err_omp_expected_named_var_member_or_array_expression)
Samuel Antao5de996e2016-01-22 20:21:36 +000012939 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000012940 continue;
12941 }
12942
Samuel Antao90927002016-04-26 14:54:23 +000012943 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
12944 ValueDecl *CurDeclaration = nullptr;
12945
12946 // Obtain the array or member expression bases if required. Also, fill the
12947 // components array with all the components identified in the process.
Alexey Bataeve3727102018-04-18 15:57:46 +000012948 const Expr *BE = checkMapClauseExpressionBase(
12949 SemaRef, SimpleExpr, CurComponents, CKind, /*NoDiagnose=*/false);
Samuel Antao5de996e2016-01-22 20:21:36 +000012950 if (!BE)
12951 continue;
12952
Samuel Antao90927002016-04-26 14:54:23 +000012953 assert(!CurComponents.empty() &&
12954 "Invalid mappable expression information.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000012955
Patrick Lystere13b1e32019-01-02 19:28:48 +000012956 if (const auto *TE = dyn_cast<CXXThisExpr>(BE)) {
12957 // Add store "this" pointer to class in DSAStackTy for future checking
12958 DSAS->addMappedClassesQualTypes(TE->getType());
12959 // Skip restriction checking for variable or field declarations
12960 MVLI.ProcessedVarList.push_back(RE);
12961 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
12962 MVLI.VarComponents.back().append(CurComponents.begin(),
12963 CurComponents.end());
12964 MVLI.VarBaseDeclarations.push_back(nullptr);
12965 continue;
12966 }
12967
Samuel Antao90927002016-04-26 14:54:23 +000012968 // For the following checks, we rely on the base declaration which is
12969 // expected to be associated with the last component. The declaration is
12970 // expected to be a variable or a field (if 'this' is being mapped).
12971 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
12972 assert(CurDeclaration && "Null decl on map clause.");
12973 assert(
12974 CurDeclaration->isCanonicalDecl() &&
12975 "Expecting components to have associated only canonical declarations.");
12976
12977 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
Alexey Bataeve3727102018-04-18 15:57:46 +000012978 const auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
Samuel Antao5de996e2016-01-22 20:21:36 +000012979
12980 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000012981 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000012982
12983 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
Samuel Antao661c0902016-05-26 17:39:58 +000012984 // threadprivate variables cannot appear in a map clause.
12985 // OpenMP 4.5 [2.10.5, target update Construct]
12986 // threadprivate variables cannot appear in a from clause.
12987 if (VD && DSAS->isThreadPrivate(VD)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012988 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
Samuel Antao661c0902016-05-26 17:39:58 +000012989 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
12990 << getOpenMPClauseName(CKind);
Alexey Bataeve3727102018-04-18 15:57:46 +000012991 reportOriginalDsa(SemaRef, DSAS, VD, DVar);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012992 continue;
12993 }
12994
Samuel Antao5de996e2016-01-22 20:21:36 +000012995 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
12996 // A list item cannot appear in both a map clause and a data-sharing
12997 // attribute clause on the same construct.
Kelvin Li0bff7af2015-11-23 05:32:03 +000012998
Samuel Antao5de996e2016-01-22 20:21:36 +000012999 // Check conflicts with other map clause expressions. We check the conflicts
13000 // with the current construct separately from the enclosing data
Samuel Antao661c0902016-05-26 17:39:58 +000013001 // environment, because the restrictions are different. We only have to
13002 // check conflicts across regions for the map clauses.
Alexey Bataeve3727102018-04-18 15:57:46 +000013003 if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
Samuel Antao661c0902016-05-26 17:39:58 +000013004 /*CurrentRegionOnly=*/true, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000013005 break;
Samuel Antao661c0902016-05-26 17:39:58 +000013006 if (CKind == OMPC_map &&
Alexey Bataeve3727102018-04-18 15:57:46 +000013007 checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
Samuel Antao661c0902016-05-26 17:39:58 +000013008 /*CurrentRegionOnly=*/false, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000013009 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000013010
Samuel Antao661c0902016-05-26 17:39:58 +000013011 // OpenMP 4.5 [2.10.5, target update Construct]
Samuel Antao5de996e2016-01-22 20:21:36 +000013012 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
13013 // If the type of a list item is a reference to a type T then the type will
13014 // be considered to be T for all purposes of this clause.
Alexey Bataev354df2e2018-05-02 18:44:10 +000013015 auto I = llvm::find_if(
13016 CurComponents,
13017 [](const OMPClauseMappableExprCommon::MappableComponent &MC) {
13018 return MC.getAssociatedDeclaration();
13019 });
13020 assert(I != CurComponents.end() && "Null decl on map clause.");
13021 QualType Type =
13022 I->getAssociatedDeclaration()->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000013023
Samuel Antao661c0902016-05-26 17:39:58 +000013024 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
13025 // A list item in a to or from clause must have a mappable type.
Samuel Antao5de996e2016-01-22 20:21:36 +000013026 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000013027 // A list item must have a mappable type.
Alexey Bataeve3727102018-04-18 15:57:46 +000013028 if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
Samuel Antao661c0902016-05-26 17:39:58 +000013029 DSAS, Type))
Kelvin Li0bff7af2015-11-23 05:32:03 +000013030 continue;
13031
Samuel Antao661c0902016-05-26 17:39:58 +000013032 if (CKind == OMPC_map) {
13033 // target enter data
13034 // OpenMP [2.10.2, Restrictions, p. 99]
13035 // A map-type must be specified in all map clauses and must be either
13036 // to or alloc.
13037 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
13038 if (DKind == OMPD_target_enter_data &&
13039 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
13040 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
13041 << (IsMapTypeImplicit ? 1 : 0)
13042 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
13043 << getOpenMPDirectiveName(DKind);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000013044 continue;
13045 }
Samuel Antao661c0902016-05-26 17:39:58 +000013046
13047 // target exit_data
13048 // OpenMP [2.10.3, Restrictions, p. 102]
13049 // A map-type must be specified in all map clauses and must be either
13050 // from, release, or delete.
13051 if (DKind == OMPD_target_exit_data &&
13052 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
13053 MapType == OMPC_MAP_delete)) {
13054 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
13055 << (IsMapTypeImplicit ? 1 : 0)
13056 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
13057 << getOpenMPDirectiveName(DKind);
13058 continue;
13059 }
13060
13061 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
13062 // A list item cannot appear in both a map clause and a data-sharing
13063 // attribute clause on the same construct
Alexey Bataeve3727102018-04-18 15:57:46 +000013064 if (VD && isOpenMPTargetExecutionDirective(DKind)) {
13065 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
Samuel Antao661c0902016-05-26 17:39:58 +000013066 if (isOpenMPPrivate(DVar.CKind)) {
Samuel Antao6890b092016-07-28 14:25:09 +000013067 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Samuel Antao661c0902016-05-26 17:39:58 +000013068 << getOpenMPClauseName(DVar.CKind)
Samuel Antao6890b092016-07-28 14:25:09 +000013069 << getOpenMPClauseName(OMPC_map)
Samuel Antao661c0902016-05-26 17:39:58 +000013070 << getOpenMPDirectiveName(DSAS->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000013071 reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar);
Samuel Antao661c0902016-05-26 17:39:58 +000013072 continue;
13073 }
13074 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +000013075 }
13076
Samuel Antao90927002016-04-26 14:54:23 +000013077 // Save the current expression.
Samuel Antao661c0902016-05-26 17:39:58 +000013078 MVLI.ProcessedVarList.push_back(RE);
Samuel Antao90927002016-04-26 14:54:23 +000013079
13080 // Store the components in the stack so that they can be used to check
13081 // against other clauses later on.
Samuel Antao6890b092016-07-28 14:25:09 +000013082 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents,
13083 /*WhereFoundClauseKind=*/OMPC_map);
Samuel Antao90927002016-04-26 14:54:23 +000013084
13085 // Save the components and declaration to create the clause. For purposes of
13086 // the clause creation, any component list that has has base 'this' uses
Samuel Antao686c70c2016-05-26 17:30:50 +000013087 // null as base declaration.
Samuel Antao661c0902016-05-26 17:39:58 +000013088 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13089 MVLI.VarComponents.back().append(CurComponents.begin(),
13090 CurComponents.end());
13091 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
13092 : CurDeclaration);
Kelvin Li0bff7af2015-11-23 05:32:03 +000013093 }
Samuel Antao661c0902016-05-26 17:39:58 +000013094}
13095
13096OMPClause *
Kelvin Lief579432018-12-18 22:18:41 +000013097Sema::ActOnOpenMPMapClause(ArrayRef<OpenMPMapModifierKind> MapTypeModifiers,
13098 ArrayRef<SourceLocation> MapTypeModifiersLoc,
Samuel Antao661c0902016-05-26 17:39:58 +000013099 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
13100 SourceLocation MapLoc, SourceLocation ColonLoc,
13101 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
13102 SourceLocation LParenLoc, SourceLocation EndLoc) {
13103 MappableVarListInfo MVLI(VarList);
13104 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc,
13105 MapType, IsMapTypeImplicit);
Kelvin Li0bff7af2015-11-23 05:32:03 +000013106
Kelvin Lief579432018-12-18 22:18:41 +000013107 OpenMPMapModifierKind Modifiers[] = { OMPC_MAP_MODIFIER_unknown,
13108 OMPC_MAP_MODIFIER_unknown };
13109 SourceLocation ModifiersLoc[OMPMapClause::NumberOfModifiers];
13110
13111 // Process map-type-modifiers, flag errors for duplicate modifiers.
13112 unsigned Count = 0;
13113 for (unsigned I = 0, E = MapTypeModifiers.size(); I < E; ++I) {
13114 if (MapTypeModifiers[I] != OMPC_MAP_MODIFIER_unknown &&
13115 llvm::find(Modifiers, MapTypeModifiers[I]) != std::end(Modifiers)) {
13116 Diag(MapTypeModifiersLoc[I], diag::err_omp_duplicate_map_type_modifier);
13117 continue;
13118 }
13119 assert(Count < OMPMapClause::NumberOfModifiers &&
13120 "Modifiers exceed the allowed number of map type modifiers");
13121 Modifiers[Count] = MapTypeModifiers[I];
13122 ModifiersLoc[Count] = MapTypeModifiersLoc[I];
13123 ++Count;
13124 }
13125
Samuel Antao5de996e2016-01-22 20:21:36 +000013126 // We need to produce a map clause even if we don't have variables so that
13127 // other diagnostics related with non-existing map clauses are accurate.
Samuel Antao661c0902016-05-26 17:39:58 +000013128 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13129 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
Kelvin Lief579432018-12-18 22:18:41 +000013130 MVLI.VarComponents, Modifiers, ModifiersLoc,
13131 MapType, IsMapTypeImplicit, MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000013132}
Kelvin Li099bb8c2015-11-24 20:50:12 +000013133
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013134QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
13135 TypeResult ParsedType) {
13136 assert(ParsedType.isUsable());
13137
13138 QualType ReductionType = GetTypeFromParser(ParsedType.get());
13139 if (ReductionType.isNull())
13140 return QualType();
13141
13142 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
13143 // A type name in a declare reduction directive cannot be a function type, an
13144 // array type, a reference type, or a type qualified with const, volatile or
13145 // restrict.
13146 if (ReductionType.hasQualifiers()) {
13147 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
13148 return QualType();
13149 }
13150
13151 if (ReductionType->isFunctionType()) {
13152 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
13153 return QualType();
13154 }
13155 if (ReductionType->isReferenceType()) {
13156 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
13157 return QualType();
13158 }
13159 if (ReductionType->isArrayType()) {
13160 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
13161 return QualType();
13162 }
13163 return ReductionType;
13164}
13165
13166Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
13167 Scope *S, DeclContext *DC, DeclarationName Name,
13168 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
13169 AccessSpecifier AS, Decl *PrevDeclInScope) {
13170 SmallVector<Decl *, 8> Decls;
13171 Decls.reserve(ReductionTypes.size());
13172
13173 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
Richard Smithbecb92d2017-10-10 22:33:17 +000013174 forRedeclarationInCurContext());
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013175 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
13176 // A reduction-identifier may not be re-declared in the current scope for the
13177 // same type or for a type that is compatible according to the base language
13178 // rules.
13179 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
13180 OMPDeclareReductionDecl *PrevDRD = nullptr;
13181 bool InCompoundScope = true;
13182 if (S != nullptr) {
13183 // Find previous declaration with the same name not referenced in other
13184 // declarations.
13185 FunctionScopeInfo *ParentFn = getEnclosingFunction();
13186 InCompoundScope =
13187 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
13188 LookupName(Lookup, S);
13189 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
13190 /*AllowInlineNamespace=*/false);
13191 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
Alexey Bataeve3727102018-04-18 15:57:46 +000013192 LookupResult::Filter Filter = Lookup.makeFilter();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013193 while (Filter.hasNext()) {
13194 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
13195 if (InCompoundScope) {
13196 auto I = UsedAsPrevious.find(PrevDecl);
13197 if (I == UsedAsPrevious.end())
13198 UsedAsPrevious[PrevDecl] = false;
Alexey Bataeve3727102018-04-18 15:57:46 +000013199 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope())
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013200 UsedAsPrevious[D] = true;
13201 }
13202 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
13203 PrevDecl->getLocation();
13204 }
13205 Filter.done();
13206 if (InCompoundScope) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013207 for (const auto &PrevData : UsedAsPrevious) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013208 if (!PrevData.second) {
13209 PrevDRD = PrevData.first;
13210 break;
13211 }
13212 }
13213 }
13214 } else if (PrevDeclInScope != nullptr) {
13215 auto *PrevDRDInScope = PrevDRD =
13216 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
13217 do {
13218 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
13219 PrevDRDInScope->getLocation();
13220 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
13221 } while (PrevDRDInScope != nullptr);
13222 }
Alexey Bataeve3727102018-04-18 15:57:46 +000013223 for (const auto &TyData : ReductionTypes) {
13224 const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013225 bool Invalid = false;
13226 if (I != PreviousRedeclTypes.end()) {
13227 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
13228 << TyData.first;
13229 Diag(I->second, diag::note_previous_definition);
13230 Invalid = true;
13231 }
13232 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
13233 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
13234 Name, TyData.first, PrevDRD);
13235 DC->addDecl(DRD);
13236 DRD->setAccess(AS);
13237 Decls.push_back(DRD);
13238 if (Invalid)
13239 DRD->setInvalidDecl();
13240 else
13241 PrevDRD = DRD;
13242 }
13243
13244 return DeclGroupPtrTy::make(
13245 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
13246}
13247
13248void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
13249 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13250
13251 // Enter new function scope.
13252 PushFunctionScope();
Reid Kleckner87a31802018-03-12 21:43:02 +000013253 setFunctionHasBranchProtectedScope();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013254 getCurFunction()->setHasOMPDeclareReductionCombiner();
13255
13256 if (S != nullptr)
13257 PushDeclContext(S, DRD);
13258 else
13259 CurContext = DRD;
13260
Faisal Valid143a0c2017-04-01 21:30:49 +000013261 PushExpressionEvaluationContext(
13262 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013263
13264 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013265 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
13266 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
13267 // uses semantics of argument handles by value, but it should be passed by
13268 // reference. C lang does not support references, so pass all parameters as
13269 // pointers.
13270 // Create 'T omp_in;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000013271 VarDecl *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013272 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013273 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
13274 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
13275 // uses semantics of argument handles by value, but it should be passed by
13276 // reference. C lang does not support references, so pass all parameters as
13277 // pointers.
13278 // Create 'T omp_out;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000013279 VarDecl *OmpOutParm =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013280 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
13281 if (S != nullptr) {
13282 PushOnScopeChains(OmpInParm, S);
13283 PushOnScopeChains(OmpOutParm, S);
13284 } else {
13285 DRD->addDecl(OmpInParm);
13286 DRD->addDecl(OmpOutParm);
13287 }
Alexey Bataeve6aa4692018-09-13 16:54:05 +000013288 Expr *InE =
13289 ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation());
13290 Expr *OutE =
13291 ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation());
13292 DRD->setCombinerData(InE, OutE);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013293}
13294
13295void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
13296 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13297 DiscardCleanupsInEvaluationContext();
13298 PopExpressionEvaluationContext();
13299
13300 PopDeclContext();
13301 PopFunctionScopeInfo();
13302
13303 if (Combiner != nullptr)
13304 DRD->setCombiner(Combiner);
13305 else
13306 DRD->setInvalidDecl();
13307}
13308
Alexey Bataev070f43a2017-09-06 14:49:58 +000013309VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013310 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13311
13312 // Enter new function scope.
13313 PushFunctionScope();
Reid Kleckner87a31802018-03-12 21:43:02 +000013314 setFunctionHasBranchProtectedScope();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013315
13316 if (S != nullptr)
13317 PushDeclContext(S, DRD);
13318 else
13319 CurContext = DRD;
13320
Faisal Valid143a0c2017-04-01 21:30:49 +000013321 PushExpressionEvaluationContext(
13322 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013323
13324 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013325 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
13326 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
13327 // uses semantics of argument handles by value, but it should be passed by
13328 // reference. C lang does not support references, so pass all parameters as
13329 // pointers.
13330 // Create 'T omp_priv;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000013331 VarDecl *OmpPrivParm =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013332 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013333 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
13334 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
13335 // uses semantics of argument handles by value, but it should be passed by
13336 // reference. C lang does not support references, so pass all parameters as
13337 // pointers.
13338 // Create 'T omp_orig;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000013339 VarDecl *OmpOrigParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013340 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013341 if (S != nullptr) {
13342 PushOnScopeChains(OmpPrivParm, S);
13343 PushOnScopeChains(OmpOrigParm, S);
13344 } else {
13345 DRD->addDecl(OmpPrivParm);
13346 DRD->addDecl(OmpOrigParm);
13347 }
Alexey Bataeve6aa4692018-09-13 16:54:05 +000013348 Expr *OrigE =
13349 ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation());
13350 Expr *PrivE =
13351 ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation());
13352 DRD->setInitializerData(OrigE, PrivE);
Alexey Bataev070f43a2017-09-06 14:49:58 +000013353 return OmpPrivParm;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013354}
13355
Alexey Bataev070f43a2017-09-06 14:49:58 +000013356void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer,
13357 VarDecl *OmpPrivParm) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013358 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13359 DiscardCleanupsInEvaluationContext();
13360 PopExpressionEvaluationContext();
13361
13362 PopDeclContext();
13363 PopFunctionScopeInfo();
13364
Alexey Bataev070f43a2017-09-06 14:49:58 +000013365 if (Initializer != nullptr) {
13366 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit);
13367 } else if (OmpPrivParm->hasInit()) {
13368 DRD->setInitializer(OmpPrivParm->getInit(),
13369 OmpPrivParm->isDirectInit()
13370 ? OMPDeclareReductionDecl::DirectInit
13371 : OMPDeclareReductionDecl::CopyInit);
13372 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013373 DRD->setInvalidDecl();
Alexey Bataev070f43a2017-09-06 14:49:58 +000013374 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013375}
13376
13377Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
13378 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013379 for (Decl *D : DeclReductions.get()) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013380 if (IsValid) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013381 if (S)
13382 PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S,
13383 /*AddToContext=*/false);
13384 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013385 D->setInvalidDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000013386 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013387 }
13388 return DeclReductions;
13389}
13390
David Majnemer9d168222016-08-05 17:44:54 +000013391OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
Kelvin Li099bb8c2015-11-24 20:50:12 +000013392 SourceLocation StartLoc,
13393 SourceLocation LParenLoc,
13394 SourceLocation EndLoc) {
13395 Expr *ValExpr = NumTeams;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000013396 Stmt *HelperValStmt = nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000013397
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013398 // OpenMP [teams Constrcut, Restrictions]
13399 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000013400 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
Alexey Bataeva0569352015-12-01 10:17:31 +000013401 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013402 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000013403
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000013404 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000013405 OpenMPDirectiveKind CaptureRegion =
13406 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams);
13407 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000013408 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000013409 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000013410 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
13411 HelperValStmt = buildPreInits(Context, Captures);
13412 }
13413
13414 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion,
13415 StartLoc, LParenLoc, EndLoc);
Kelvin Li099bb8c2015-11-24 20:50:12 +000013416}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013417
13418OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
13419 SourceLocation StartLoc,
13420 SourceLocation LParenLoc,
13421 SourceLocation EndLoc) {
13422 Expr *ValExpr = ThreadLimit;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000013423 Stmt *HelperValStmt = nullptr;
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013424
13425 // OpenMP [teams Constrcut, Restrictions]
13426 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000013427 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
Alexey Bataeva0569352015-12-01 10:17:31 +000013428 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013429 return nullptr;
13430
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000013431 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000013432 OpenMPDirectiveKind CaptureRegion =
13433 getOpenMPCaptureRegionForClause(DKind, OMPC_thread_limit);
13434 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000013435 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000013436 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000013437 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
13438 HelperValStmt = buildPreInits(Context, Captures);
13439 }
13440
13441 return new (Context) OMPThreadLimitClause(
13442 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013443}
Alexey Bataeva0569352015-12-01 10:17:31 +000013444
13445OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
13446 SourceLocation StartLoc,
13447 SourceLocation LParenLoc,
13448 SourceLocation EndLoc) {
13449 Expr *ValExpr = Priority;
13450
13451 // OpenMP [2.9.1, task Constrcut]
13452 // The priority-value is a non-negative numerical scalar expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000013453 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
Alexey Bataeva0569352015-12-01 10:17:31 +000013454 /*StrictlyPositive=*/false))
13455 return nullptr;
13456
13457 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
13458}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000013459
13460OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
13461 SourceLocation StartLoc,
13462 SourceLocation LParenLoc,
13463 SourceLocation EndLoc) {
13464 Expr *ValExpr = Grainsize;
13465
13466 // OpenMP [2.9.2, taskloop Constrcut]
13467 // The parameter of the grainsize clause must be a positive integer
13468 // expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000013469 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000013470 /*StrictlyPositive=*/true))
13471 return nullptr;
13472
13473 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
13474}
Alexey Bataev382967a2015-12-08 12:06:20 +000013475
13476OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
13477 SourceLocation StartLoc,
13478 SourceLocation LParenLoc,
13479 SourceLocation EndLoc) {
13480 Expr *ValExpr = NumTasks;
13481
13482 // OpenMP [2.9.2, taskloop Constrcut]
13483 // The parameter of the num_tasks clause must be a positive integer
13484 // expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000013485 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
Alexey Bataev382967a2015-12-08 12:06:20 +000013486 /*StrictlyPositive=*/true))
13487 return nullptr;
13488
13489 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
13490}
13491
Alexey Bataev28c75412015-12-15 08:19:24 +000013492OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
13493 SourceLocation LParenLoc,
13494 SourceLocation EndLoc) {
13495 // OpenMP [2.13.2, critical construct, Description]
13496 // ... where hint-expression is an integer constant expression that evaluates
13497 // to a valid lock hint.
13498 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
13499 if (HintExpr.isInvalid())
13500 return nullptr;
13501 return new (Context)
13502 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
13503}
13504
Carlo Bertollib4adf552016-01-15 18:50:31 +000013505OMPClause *Sema::ActOnOpenMPDistScheduleClause(
13506 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
13507 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
13508 SourceLocation EndLoc) {
13509 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
13510 std::string Values;
13511 Values += "'";
13512 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
13513 Values += "'";
13514 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
13515 << Values << getOpenMPClauseName(OMPC_dist_schedule);
13516 return nullptr;
13517 }
13518 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000013519 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000013520 if (ChunkSize) {
13521 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
13522 !ChunkSize->isInstantiationDependent() &&
13523 !ChunkSize->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000013524 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
Carlo Bertollib4adf552016-01-15 18:50:31 +000013525 ExprResult Val =
13526 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
13527 if (Val.isInvalid())
13528 return nullptr;
13529
13530 ValExpr = Val.get();
13531
13532 // OpenMP [2.7.1, Restrictions]
13533 // chunk_size must be a loop invariant integer expression with a positive
13534 // value.
13535 llvm::APSInt Result;
13536 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
13537 if (Result.isSigned() && !Result.isStrictlyPositive()) {
13538 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
13539 << "dist_schedule" << ChunkSize->getSourceRange();
13540 return nullptr;
13541 }
Alexey Bataev2ba67042017-11-28 21:11:44 +000013542 } else if (getOpenMPCaptureRegionForClause(
13543 DSAStack->getCurrentDirective(), OMPC_dist_schedule) !=
13544 OMPD_unknown &&
Alexey Bataevb46cdea2016-06-15 11:20:48 +000013545 !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000013546 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000013547 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev5a3af132016-03-29 08:58:54 +000013548 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
13549 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000013550 }
13551 }
13552 }
13553
13554 return new (Context)
13555 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000013556 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000013557}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013558
13559OMPClause *Sema::ActOnOpenMPDefaultmapClause(
13560 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
13561 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
13562 SourceLocation KindLoc, SourceLocation EndLoc) {
13563 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
David Majnemer9d168222016-08-05 17:44:54 +000013564 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) {
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013565 std::string Value;
13566 SourceLocation Loc;
13567 Value += "'";
13568 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
13569 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000013570 OMPC_DEFAULTMAP_MODIFIER_tofrom);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013571 Loc = MLoc;
13572 } else {
13573 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000013574 OMPC_DEFAULTMAP_scalar);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013575 Loc = KindLoc;
13576 }
13577 Value += "'";
13578 Diag(Loc, diag::err_omp_unexpected_clause_value)
13579 << Value << getOpenMPClauseName(OMPC_defaultmap);
13580 return nullptr;
13581 }
Alexey Bataev2fd0cb22017-10-05 17:51:39 +000013582 DSAStack->setDefaultDMAToFromScalar(StartLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013583
13584 return new (Context)
13585 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
13586}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013587
13588bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
13589 DeclContext *CurLexicalContext = getCurLexicalContext();
13590 if (!CurLexicalContext->isFileContext() &&
13591 !CurLexicalContext->isExternCContext() &&
Alexey Bataev502ec492017-10-03 20:00:00 +000013592 !CurLexicalContext->isExternCXXContext() &&
13593 !isa<CXXRecordDecl>(CurLexicalContext) &&
13594 !isa<ClassTemplateDecl>(CurLexicalContext) &&
13595 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) &&
13596 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013597 Diag(Loc, diag::err_omp_region_not_file_context);
13598 return false;
13599 }
Kelvin Libc38e632018-09-10 02:07:09 +000013600 ++DeclareTargetNestingLevel;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013601 return true;
13602}
13603
13604void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
Kelvin Libc38e632018-09-10 02:07:09 +000013605 assert(DeclareTargetNestingLevel > 0 &&
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013606 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
Kelvin Libc38e632018-09-10 02:07:09 +000013607 --DeclareTargetNestingLevel;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013608}
13609
David Majnemer9d168222016-08-05 17:44:54 +000013610void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope,
13611 CXXScopeSpec &ScopeSpec,
13612 const DeclarationNameInfo &Id,
13613 OMPDeclareTargetDeclAttr::MapTypeTy MT,
13614 NamedDeclSetType &SameDirectiveDecls) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013615 LookupResult Lookup(*this, Id, LookupOrdinaryName);
13616 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
13617
13618 if (Lookup.isAmbiguous())
13619 return;
13620 Lookup.suppressDiagnostics();
13621
13622 if (!Lookup.isSingleResult()) {
13623 if (TypoCorrection Corrected =
13624 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
13625 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
13626 CTK_ErrorRecovery)) {
13627 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
13628 << Id.getName());
13629 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
13630 return;
13631 }
13632
13633 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
13634 return;
13635 }
13636
13637 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
Alexey Bataev30a78212018-09-11 13:59:10 +000013638 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND) ||
13639 isa<FunctionTemplateDecl>(ND)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013640 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
13641 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
Alexey Bataev30a78212018-09-11 13:59:10 +000013642 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
13643 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
13644 cast<ValueDecl>(ND));
13645 if (!Res) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013646 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013647 ND->addAttr(A);
13648 if (ASTMutationListener *ML = Context.getASTMutationListener())
13649 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
Kelvin Li1ce87c72017-12-12 20:08:12 +000013650 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Id.getLoc());
Alexey Bataev30a78212018-09-11 13:59:10 +000013651 } else if (*Res != MT) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013652 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
13653 << Id.getName();
13654 }
Alexey Bataeve3727102018-04-18 15:57:46 +000013655 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013656 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
Alexey Bataeve3727102018-04-18 15:57:46 +000013657 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013658}
13659
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013660static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
13661 Sema &SemaRef, Decl *D) {
Alexey Bataev30a78212018-09-11 13:59:10 +000013662 if (!D || !isa<VarDecl>(D))
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013663 return;
Alexey Bataev30a78212018-09-11 13:59:10 +000013664 auto *VD = cast<VarDecl>(D);
13665 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
13666 return;
13667 SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context);
13668 SemaRef.Diag(SL, diag::note_used_here) << SR;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013669}
13670
13671static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
13672 Sema &SemaRef, DSAStackTy *Stack,
13673 ValueDecl *VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013674 return VD->hasAttr<OMPDeclareTargetDeclAttr>() ||
13675 checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(),
13676 /*FullCheck=*/false);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013677}
13678
Kelvin Li1ce87c72017-12-12 20:08:12 +000013679void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D,
13680 SourceLocation IdLoc) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013681 if (!D || D->isInvalidDecl())
13682 return;
13683 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
Stephen Kellyf2ceec42018-08-09 21:08:08 +000013684 SourceLocation SL = E ? E->getBeginLoc() : D->getLocation();
Alexey Bataeve3727102018-04-18 15:57:46 +000013685 if (auto *VD = dyn_cast<VarDecl>(D)) {
Alexey Bataevc1943e72018-07-09 19:58:08 +000013686 // Only global variables can be marked as declare target.
Alexey Bataev30a78212018-09-11 13:59:10 +000013687 if (!VD->isFileVarDecl() && !VD->isStaticLocal() &&
13688 !VD->isStaticDataMember())
Alexey Bataevc1943e72018-07-09 19:58:08 +000013689 return;
13690 // 2.10.6: threadprivate variable cannot appear in a declare target
13691 // directive.
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013692 if (DSAStack->isThreadPrivate(VD)) {
13693 Diag(SL, diag::err_omp_threadprivate_in_target);
Alexey Bataeve3727102018-04-18 15:57:46 +000013694 reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013695 return;
13696 }
13697 }
Alexey Bataev97b72212018-08-14 18:31:20 +000013698 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
13699 D = FTD->getTemplatedDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000013700 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Alexey Bataev30a78212018-09-11 13:59:10 +000013701 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
13702 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD);
13703 if (Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) {
Kelvin Li1ce87c72017-12-12 20:08:12 +000013704 assert(IdLoc.isValid() && "Source location is expected");
13705 Diag(IdLoc, diag::err_omp_function_in_link_clause);
13706 Diag(FD->getLocation(), diag::note_defined_here) << FD;
13707 return;
13708 }
13709 }
Alexey Bataev30a78212018-09-11 13:59:10 +000013710 if (auto *VD = dyn_cast<ValueDecl>(D)) {
13711 // Problem if any with var declared with incomplete type will be reported
13712 // as normal, so no need to check it here.
13713 if ((E || !VD->getType()->isIncompleteType()) &&
13714 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD))
13715 return;
13716 if (!E && !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) {
13717 // Checking declaration inside declare target region.
13718 if (isa<VarDecl>(D) || isa<FunctionDecl>(D) ||
13719 isa<FunctionTemplateDecl>(D)) {
13720 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
13721 Context, OMPDeclareTargetDeclAttr::MT_To);
13722 D->addAttr(A);
13723 if (ASTMutationListener *ML = Context.getASTMutationListener())
13724 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
13725 }
13726 return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013727 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013728 }
Alexey Bataev30a78212018-09-11 13:59:10 +000013729 if (!E)
13730 return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013731 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
13732}
Samuel Antao661c0902016-05-26 17:39:58 +000013733
13734OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
13735 SourceLocation StartLoc,
13736 SourceLocation LParenLoc,
13737 SourceLocation EndLoc) {
13738 MappableVarListInfo MVLI(VarList);
13739 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc);
13740 if (MVLI.ProcessedVarList.empty())
13741 return nullptr;
13742
13743 return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13744 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
13745 MVLI.VarComponents);
13746}
Samuel Antaoec172c62016-05-26 17:49:04 +000013747
13748OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
13749 SourceLocation StartLoc,
13750 SourceLocation LParenLoc,
13751 SourceLocation EndLoc) {
13752 MappableVarListInfo MVLI(VarList);
13753 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc);
13754 if (MVLI.ProcessedVarList.empty())
13755 return nullptr;
13756
13757 return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13758 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
13759 MVLI.VarComponents);
13760}
Carlo Bertolli2404b172016-07-13 15:37:16 +000013761
13762OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
13763 SourceLocation StartLoc,
13764 SourceLocation LParenLoc,
13765 SourceLocation EndLoc) {
Samuel Antaocc10b852016-07-28 14:23:26 +000013766 MappableVarListInfo MVLI(VarList);
13767 SmallVector<Expr *, 8> PrivateCopies;
13768 SmallVector<Expr *, 8> Inits;
13769
Alexey Bataeve3727102018-04-18 15:57:46 +000013770 for (Expr *RefExpr : VarList) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000013771 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
13772 SourceLocation ELoc;
13773 SourceRange ERange;
13774 Expr *SimpleRefExpr = RefExpr;
13775 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
13776 if (Res.second) {
13777 // It will be analyzed later.
Samuel Antaocc10b852016-07-28 14:23:26 +000013778 MVLI.ProcessedVarList.push_back(RefExpr);
13779 PrivateCopies.push_back(nullptr);
13780 Inits.push_back(nullptr);
Carlo Bertolli2404b172016-07-13 15:37:16 +000013781 }
13782 ValueDecl *D = Res.first;
13783 if (!D)
13784 continue;
13785
13786 QualType Type = D->getType();
Samuel Antaocc10b852016-07-28 14:23:26 +000013787 Type = Type.getNonReferenceType().getUnqualifiedType();
13788
13789 auto *VD = dyn_cast<VarDecl>(D);
13790
13791 // Item should be a pointer or reference to pointer.
13792 if (!Type->isPointerType()) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000013793 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
13794 << 0 << RefExpr->getSourceRange();
13795 continue;
13796 }
Samuel Antaocc10b852016-07-28 14:23:26 +000013797
13798 // Build the private variable and the expression that refers to it.
Alexey Bataev63cc8e92018-03-20 14:45:59 +000013799 auto VDPrivate =
13800 buildVarDecl(*this, ELoc, Type, D->getName(),
13801 D->hasAttrs() ? &D->getAttrs() : nullptr,
13802 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Samuel Antaocc10b852016-07-28 14:23:26 +000013803 if (VDPrivate->isInvalidDecl())
13804 continue;
13805
13806 CurContext->addDecl(VDPrivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000013807 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Samuel Antaocc10b852016-07-28 14:23:26 +000013808 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
13809
13810 // Add temporary variable to initialize the private copy of the pointer.
Alexey Bataeve3727102018-04-18 15:57:46 +000013811 VarDecl *VDInit =
Samuel Antaocc10b852016-07-28 14:23:26 +000013812 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp");
Alexey Bataeve3727102018-04-18 15:57:46 +000013813 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr(
13814 *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc());
Samuel Antaocc10b852016-07-28 14:23:26 +000013815 AddInitializerToDecl(VDPrivate,
13816 DefaultLvalueConversion(VDInitRefExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000013817 /*DirectInit=*/false);
Samuel Antaocc10b852016-07-28 14:23:26 +000013818
13819 // If required, build a capture to implement the privatization initialized
13820 // with the current list item value.
13821 DeclRefExpr *Ref = nullptr;
13822 if (!VD)
13823 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
13824 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
13825 PrivateCopies.push_back(VDPrivateRefExpr);
13826 Inits.push_back(VDInitRefExpr);
13827
13828 // We need to add a data sharing attribute for this variable to make sure it
13829 // is correctly captured. A variable that shows up in a use_device_ptr has
13830 // similar properties of a first private variable.
13831 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
13832
13833 // Create a mappable component for the list item. List items in this clause
13834 // only need a component.
13835 MVLI.VarBaseDeclarations.push_back(D);
13836 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13837 MVLI.VarComponents.back().push_back(
13838 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D));
Carlo Bertolli2404b172016-07-13 15:37:16 +000013839 }
13840
Samuel Antaocc10b852016-07-28 14:23:26 +000013841 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli2404b172016-07-13 15:37:16 +000013842 return nullptr;
13843
Samuel Antaocc10b852016-07-28 14:23:26 +000013844 return OMPUseDevicePtrClause::Create(
13845 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
13846 PrivateCopies, Inits, MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli2404b172016-07-13 15:37:16 +000013847}
Carlo Bertolli70594e92016-07-13 17:16:49 +000013848
13849OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
13850 SourceLocation StartLoc,
13851 SourceLocation LParenLoc,
13852 SourceLocation EndLoc) {
Samuel Antao6890b092016-07-28 14:25:09 +000013853 MappableVarListInfo MVLI(VarList);
Alexey Bataeve3727102018-04-18 15:57:46 +000013854 for (Expr *RefExpr : VarList) {
Kelvin Li84376252016-12-14 15:39:58 +000013855 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause.");
Carlo Bertolli70594e92016-07-13 17:16:49 +000013856 SourceLocation ELoc;
13857 SourceRange ERange;
13858 Expr *SimpleRefExpr = RefExpr;
13859 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
13860 if (Res.second) {
13861 // It will be analyzed later.
Samuel Antao6890b092016-07-28 14:25:09 +000013862 MVLI.ProcessedVarList.push_back(RefExpr);
Carlo Bertolli70594e92016-07-13 17:16:49 +000013863 }
13864 ValueDecl *D = Res.first;
13865 if (!D)
13866 continue;
13867
13868 QualType Type = D->getType();
13869 // item should be a pointer or array or reference to pointer or array
13870 if (!Type.getNonReferenceType()->isPointerType() &&
13871 !Type.getNonReferenceType()->isArrayType()) {
13872 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
13873 << 0 << RefExpr->getSourceRange();
13874 continue;
13875 }
Samuel Antao6890b092016-07-28 14:25:09 +000013876
13877 // Check if the declaration in the clause does not show up in any data
13878 // sharing attribute.
Alexey Bataeve3727102018-04-18 15:57:46 +000013879 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Samuel Antao6890b092016-07-28 14:25:09 +000013880 if (isOpenMPPrivate(DVar.CKind)) {
13881 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
13882 << getOpenMPClauseName(DVar.CKind)
13883 << getOpenMPClauseName(OMPC_is_device_ptr)
13884 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000013885 reportOriginalDsa(*this, DSAStack, D, DVar);
Samuel Antao6890b092016-07-28 14:25:09 +000013886 continue;
13887 }
13888
Alexey Bataeve3727102018-04-18 15:57:46 +000013889 const Expr *ConflictExpr;
Samuel Antao6890b092016-07-28 14:25:09 +000013890 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +000013891 D, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +000013892 [&ConflictExpr](
13893 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
13894 OpenMPClauseKind) -> bool {
13895 ConflictExpr = R.front().getAssociatedExpression();
13896 return true;
13897 })) {
13898 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
13899 Diag(ConflictExpr->getExprLoc(), diag::note_used_here)
13900 << ConflictExpr->getSourceRange();
13901 continue;
13902 }
13903
13904 // Store the components in the stack so that they can be used to check
13905 // against other clauses later on.
13906 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D);
13907 DSAStack->addMappableExpressionComponents(
13908 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
13909
13910 // Record the expression we've just processed.
13911 MVLI.ProcessedVarList.push_back(SimpleRefExpr);
13912
13913 // Create a mappable component for the list item. List items in this clause
13914 // only need a component. We use a null declaration to signal fields in
13915 // 'this'.
13916 assert((isa<DeclRefExpr>(SimpleRefExpr) ||
13917 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
13918 "Unexpected device pointer expression!");
13919 MVLI.VarBaseDeclarations.push_back(
13920 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr);
13921 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13922 MVLI.VarComponents.back().push_back(MC);
Carlo Bertolli70594e92016-07-13 17:16:49 +000013923 }
13924
Samuel Antao6890b092016-07-28 14:25:09 +000013925 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli70594e92016-07-13 17:16:49 +000013926 return nullptr;
13927
Samuel Antao6890b092016-07-28 14:25:09 +000013928 return OMPIsDevicePtrClause::Create(
13929 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
13930 MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli70594e92016-07-13 17:16:49 +000013931}