blob: e08e77603e91312746ac11f3af2e435e4f457a0a [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
Alexey Bataeva769e072013-03-22 06:34:35 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000010/// This file implements semantic analysis for OpenMP directives and
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000011/// clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000012///
13//===----------------------------------------------------------------------===//
14
Alexey Bataevb08f89f2015-08-14 12:25:37 +000015#include "TreeTransform.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000016#include "clang/AST/ASTContext.h"
Alexey Bataev97720002014-11-11 04:05:39 +000017#include "clang/AST/ASTMutationListener.h"
Alexey Bataeva839ddd2016-03-17 10:19:46 +000018#include "clang/AST/CXXInheritance.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000019#include "clang/AST/Decl.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000020#include "clang/AST/DeclCXX.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000021#include "clang/AST/DeclOpenMP.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000022#include "clang/AST/StmtCXX.h"
23#include "clang/AST/StmtOpenMP.h"
24#include "clang/AST/StmtVisitor.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000025#include "clang/Basic/OpenMPKinds.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000026#include "clang/Sema/Initialization.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000027#include "clang/Sema/Lookup.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000028#include "clang/Sema/Scope.h"
29#include "clang/Sema/ScopeInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000030#include "clang/Sema/SemaInternal.h"
Alexey Bataevfa312f32017-07-21 18:48:21 +000031#include "llvm/ADT/PointerEmbeddedInt.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000032using namespace clang;
33
Alexey Bataev758e55e2013-09-06 18:03:48 +000034//===----------------------------------------------------------------------===//
35// Stack of data-sharing attributes for variables
36//===----------------------------------------------------------------------===//
37
Alexey Bataeve3727102018-04-18 15:57:46 +000038static const Expr *checkMapClauseExpressionBase(
Alexey Bataevf47c4b42017-09-26 13:47:31 +000039 Sema &SemaRef, Expr *E,
40 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
Alexey Bataevb7a9b742017-12-05 19:20:09 +000041 OpenMPClauseKind CKind, bool NoDiagnose);
Alexey Bataevf47c4b42017-09-26 13:47:31 +000042
Alexey Bataev758e55e2013-09-06 18:03:48 +000043namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000044/// Default data sharing attributes, which can be applied to directive.
Alexey Bataev758e55e2013-09-06 18:03:48 +000045enum DefaultDataSharingAttributes {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000046 DSA_unspecified = 0, /// Data sharing attribute not specified.
47 DSA_none = 1 << 0, /// Default data sharing attribute 'none'.
48 DSA_shared = 1 << 1, /// Default data sharing attribute 'shared'.
Alexey Bataev2fd0cb22017-10-05 17:51:39 +000049};
50
51/// Attributes of the defaultmap clause.
52enum DefaultMapAttributes {
53 DMA_unspecified, /// Default mapping is not specified.
54 DMA_tofrom_scalar, /// Default mapping is 'tofrom:scalar'.
Alexey Bataev758e55e2013-09-06 18:03:48 +000055};
Alexey Bataev7ff55242014-06-19 09:13:45 +000056
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000057/// Stack for tracking declarations used in OpenMP directives and
Alexey Bataev758e55e2013-09-06 18:03:48 +000058/// clauses and their data-sharing attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +000059class DSAStackTy {
Alexey Bataev758e55e2013-09-06 18:03:48 +000060public:
Alexey Bataeve3727102018-04-18 15:57:46 +000061 struct DSAVarData {
Alexey Bataev7ace49d2016-05-17 08:55:33 +000062 OpenMPDirectiveKind DKind = OMPD_unknown;
63 OpenMPClauseKind CKind = OMPC_unknown;
Alexey Bataeve3727102018-04-18 15:57:46 +000064 const Expr *RefExpr = nullptr;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000065 DeclRefExpr *PrivateCopy = nullptr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000066 SourceLocation ImplicitDSALoc;
Alexey Bataev4d4624c2017-07-20 16:47:47 +000067 DSAVarData() = default;
Alexey Bataeve3727102018-04-18 15:57:46 +000068 DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
69 const Expr *RefExpr, DeclRefExpr *PrivateCopy,
70 SourceLocation ImplicitDSALoc)
Alexey Bataevf189cb72017-07-24 14:52:13 +000071 : DKind(DKind), CKind(CKind), RefExpr(RefExpr),
72 PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000073 };
Alexey Bataeve3727102018-04-18 15:57:46 +000074 using OperatorOffsetTy =
75 llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>;
Alexey Bataevf138fda2018-08-13 19:04:24 +000076 using DoacrossDependMapTy =
77 llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>;
Alexey Bataeved09d242014-05-28 05:53:51 +000078
Alexey Bataev758e55e2013-09-06 18:03:48 +000079private:
Alexey Bataeve3727102018-04-18 15:57:46 +000080 struct DSAInfo {
Alexey Bataev7ace49d2016-05-17 08:55:33 +000081 OpenMPClauseKind Attributes = OMPC_unknown;
82 /// Pointer to a reference expression and a flag which shows that the
83 /// variable is marked as lastprivate(true) or not (false).
Alexey Bataeve3727102018-04-18 15:57:46 +000084 llvm::PointerIntPair<const Expr *, 1, bool> RefExpr;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000085 DeclRefExpr *PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +000086 };
Alexey Bataeve3727102018-04-18 15:57:46 +000087 using DeclSAMapTy = llvm::SmallDenseMap<const ValueDecl *, DSAInfo, 8>;
88 using AlignedMapTy = llvm::SmallDenseMap<const ValueDecl *, const Expr *, 8>;
89 using LCDeclInfo = std::pair<unsigned, VarDecl *>;
90 using LoopControlVariablesMapTy =
91 llvm::SmallDenseMap<const ValueDecl *, LCDeclInfo, 8>;
Samuel Antao6890b092016-07-28 14:25:09 +000092 /// Struct that associates a component with the clause kind where they are
93 /// found.
94 struct MappedExprComponentTy {
95 OMPClauseMappableExprCommon::MappableExprComponentLists Components;
96 OpenMPClauseKind Kind = OMPC_unknown;
97 };
Alexey Bataeve3727102018-04-18 15:57:46 +000098 using MappedExprComponentsTy =
99 llvm::DenseMap<const ValueDecl *, MappedExprComponentTy>;
100 using CriticalsWithHintsTy =
101 llvm::StringMap<std::pair<const OMPCriticalDirective *, llvm::APSInt>>;
Alexey Bataevfa312f32017-07-21 18:48:21 +0000102 struct ReductionData {
Alexey Bataeve3727102018-04-18 15:57:46 +0000103 using BOKPtrType = llvm::PointerEmbeddedInt<BinaryOperatorKind, 16>;
Alexey Bataevfa312f32017-07-21 18:48:21 +0000104 SourceRange ReductionRange;
Alexey Bataevf87fa882017-07-21 19:26:22 +0000105 llvm::PointerUnion<const Expr *, BOKPtrType> ReductionOp;
Alexey Bataevfa312f32017-07-21 18:48:21 +0000106 ReductionData() = default;
107 void set(BinaryOperatorKind BO, SourceRange RR) {
108 ReductionRange = RR;
109 ReductionOp = BO;
110 }
111 void set(const Expr *RefExpr, SourceRange RR) {
112 ReductionRange = RR;
113 ReductionOp = RefExpr;
114 }
115 };
Alexey Bataeve3727102018-04-18 15:57:46 +0000116 using DeclReductionMapTy =
117 llvm::SmallDenseMap<const ValueDecl *, ReductionData, 4>;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000118
Alexey Bataeve3727102018-04-18 15:57:46 +0000119 struct SharingMapTy {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000120 DeclSAMapTy SharingMap;
Alexey Bataevfa312f32017-07-21 18:48:21 +0000121 DeclReductionMapTy ReductionMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000122 AlignedMapTy AlignedMap;
Samuel Antao90927002016-04-26 14:54:23 +0000123 MappedExprComponentsTy MappedExprComponents;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000124 LoopControlVariablesMapTy LCVMap;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000125 DefaultDataSharingAttributes DefaultAttr = DSA_unspecified;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000126 SourceLocation DefaultAttrLoc;
Alexey Bataev2fd0cb22017-10-05 17:51:39 +0000127 DefaultMapAttributes DefaultMapAttr = DMA_unspecified;
128 SourceLocation DefaultMapAttrLoc;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000129 OpenMPDirectiveKind Directive = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000130 DeclarationNameInfo DirectiveName;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000131 Scope *CurScope = nullptr;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000132 SourceLocation ConstructLoc;
Alexey Bataev8b427062016-05-25 12:36:08 +0000133 /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to
134 /// get the data (loop counters etc.) about enclosing loop-based construct.
135 /// This data is required during codegen.
136 DoacrossDependMapTy DoacrossDepends;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000137 /// first argument (Expr *) contains optional argument of the
Alexey Bataev346265e2015-09-25 10:37:12 +0000138 /// 'ordered' clause, the second one is true if the regions has 'ordered'
139 /// clause, false otherwise.
Alexey Bataevf138fda2018-08-13 19:04:24 +0000140 llvm::Optional<std::pair<const Expr *, OMPOrderedClause *>> OrderedRegion;
Alexey Bataev6ab5bb12018-10-29 15:01:58 +0000141 unsigned AssociatedLoops = 1;
142 const Decl *PossiblyLoopCounter = nullptr;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000143 bool NowaitRegion = false;
144 bool CancelRegion = false;
Alexey Bataev6ab5bb12018-10-29 15:01:58 +0000145 bool LoopStart = false;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000146 SourceLocation InnerTeamsRegionLoc;
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000147 /// Reference to the taskgroup task_reduction reference expression.
148 Expr *TaskgroupReductionRef = nullptr;
Alexey Bataeved09d242014-05-28 05:53:51 +0000149 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000150 Scope *CurScope, SourceLocation Loc)
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000151 : Directive(DKind), DirectiveName(Name), CurScope(CurScope),
152 ConstructLoc(Loc) {}
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000153 SharingMapTy() = default;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000154 };
155
Alexey Bataeve3727102018-04-18 15:57:46 +0000156 using StackTy = SmallVector<SharingMapTy, 4>;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000157
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000158 /// Stack of used declaration and their data-sharing attributes.
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000159 DeclSAMapTy Threadprivates;
Alexey Bataev4b465392017-04-26 15:06:24 +0000160 const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr;
161 SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000162 /// true, if check for DSA must be from parent directive, false, if
Alexey Bataev39f915b82015-05-08 10:41:21 +0000163 /// from current directive.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000164 OpenMPClauseKind ClauseKindMode = OMPC_unknown;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000165 Sema &SemaRef;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000166 bool ForceCapturing = false;
Alexey Bataev28c75412015-12-15 08:19:24 +0000167 CriticalsWithHintsTy Criticals;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000168
Alexey Bataeve3727102018-04-18 15:57:46 +0000169 using iterator = StackTy::const_reverse_iterator;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000170
Alexey Bataeve3727102018-04-18 15:57:46 +0000171 DSAVarData getDSA(iterator &Iter, ValueDecl *D) const;
Alexey Bataevec3da872014-01-31 05:15:34 +0000172
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000173 /// Checks if the variable is a local for OpenMP region.
Alexey Bataeve3727102018-04-18 15:57:46 +0000174 bool isOpenMPLocal(VarDecl *D, iterator Iter) const;
Alexey Bataeved09d242014-05-28 05:53:51 +0000175
Alexey Bataev4b465392017-04-26 15:06:24 +0000176 bool isStackEmpty() const {
177 return Stack.empty() ||
178 Stack.back().second != CurrentNonCapturingFunctionScope ||
179 Stack.back().first.empty();
180 }
181
Kelvin Li1408f912018-09-26 04:28:39 +0000182 /// Vector of previously declared requires directives
183 SmallVector<const OMPRequiresDecl *, 2> RequiresDecls;
184
Alexey Bataev758e55e2013-09-06 18:03:48 +0000185public:
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000186 explicit DSAStackTy(Sema &S) : SemaRef(S) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000187
Alexey Bataevaac108a2015-06-23 04:51:00 +0000188 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
Alexey Bataev3f82cfc2017-12-13 15:28:44 +0000189 OpenMPClauseKind getClauseParsingMode() const {
190 assert(isClauseParsingMode() && "Must be in clause parsing mode.");
191 return ClauseKindMode;
192 }
Alexey Bataevaac108a2015-06-23 04:51:00 +0000193 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000194
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000195 bool isForceVarCapturing() const { return ForceCapturing; }
196 void setForceVarCapturing(bool V) { ForceCapturing = V; }
197
Alexey Bataev758e55e2013-09-06 18:03:48 +0000198 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000199 Scope *CurScope, SourceLocation Loc) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000200 if (Stack.empty() ||
201 Stack.back().second != CurrentNonCapturingFunctionScope)
202 Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope);
203 Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc);
204 Stack.back().first.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000205 }
206
207 void pop() {
Alexey Bataev4b465392017-04-26 15:06:24 +0000208 assert(!Stack.back().first.empty() &&
209 "Data-sharing attributes stack is empty!");
210 Stack.back().first.pop_back();
211 }
212
Alexey Bataev6ab5bb12018-10-29 15:01:58 +0000213 /// Marks that we're started loop parsing.
214 void loopInit() {
215 assert(isOpenMPLoopDirective(getCurrentDirective()) &&
216 "Expected loop-based directive.");
217 Stack.back().first.back().LoopStart = true;
218 }
219 /// Start capturing of the variables in the loop context.
220 void loopStart() {
221 assert(isOpenMPLoopDirective(getCurrentDirective()) &&
222 "Expected loop-based directive.");
223 Stack.back().first.back().LoopStart = false;
224 }
225 /// true, if variables are captured, false otherwise.
226 bool isLoopStarted() const {
227 assert(isOpenMPLoopDirective(getCurrentDirective()) &&
228 "Expected loop-based directive.");
229 return !Stack.back().first.back().LoopStart;
230 }
231 /// Marks (or clears) declaration as possibly loop counter.
232 void resetPossibleLoopCounter(const Decl *D = nullptr) {
233 Stack.back().first.back().PossiblyLoopCounter =
234 D ? D->getCanonicalDecl() : D;
235 }
236 /// Gets the possible loop counter decl.
237 const Decl *getPossiblyLoopCunter() const {
238 return Stack.back().first.back().PossiblyLoopCounter;
239 }
Alexey Bataev4b465392017-04-26 15:06:24 +0000240 /// Start new OpenMP region stack in new non-capturing function.
241 void pushFunction() {
242 const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction();
243 assert(!isa<CapturingScopeInfo>(CurFnScope));
244 CurrentNonCapturingFunctionScope = CurFnScope;
245 }
246 /// Pop region stack for non-capturing function.
247 void popFunction(const FunctionScopeInfo *OldFSI) {
248 if (!Stack.empty() && Stack.back().second == OldFSI) {
249 assert(Stack.back().first.empty());
250 Stack.pop_back();
251 }
252 CurrentNonCapturingFunctionScope = nullptr;
253 for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) {
254 if (!isa<CapturingScopeInfo>(FSI)) {
255 CurrentNonCapturingFunctionScope = FSI;
256 break;
257 }
258 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000259 }
260
Alexey Bataeve3727102018-04-18 15:57:46 +0000261 void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) {
Alexey Bataev43a919f2018-04-13 17:48:43 +0000262 Criticals.try_emplace(D->getDirectiveName().getAsString(), D, Hint);
Alexey Bataev28c75412015-12-15 08:19:24 +0000263 }
Alexey Bataeve3727102018-04-18 15:57:46 +0000264 const std::pair<const OMPCriticalDirective *, llvm::APSInt>
Alexey Bataev28c75412015-12-15 08:19:24 +0000265 getCriticalWithHint(const DeclarationNameInfo &Name) const {
266 auto I = Criticals.find(Name.getAsString());
267 if (I != Criticals.end())
268 return I->second;
269 return std::make_pair(nullptr, llvm::APSInt());
270 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000271 /// If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000272 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000273 /// for diagnostics.
Alexey Bataeve3727102018-04-18 15:57:46 +0000274 const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000275
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000276 /// Register specified variable as loop control variable.
Alexey Bataeve3727102018-04-18 15:57:46 +0000277 void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000278 /// Check if the specified variable is a loop control variable for
Alexey Bataev9c821032015-04-30 04:23:23 +0000279 /// current region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000280 /// \return The index of the loop control variable in the list of associated
281 /// for-loops (from outer to inner).
Alexey Bataeve3727102018-04-18 15:57:46 +0000282 const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000283 /// Check if the specified variable is a loop control variable for
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000284 /// parent region.
285 /// \return The index of the loop control variable in the list of associated
286 /// for-loops (from outer to inner).
Alexey Bataeve3727102018-04-18 15:57:46 +0000287 const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000288 /// Get the loop control variable for the I-th loop (or nullptr) in
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000289 /// parent directive.
Alexey Bataeve3727102018-04-18 15:57:46 +0000290 const ValueDecl *getParentLoopControlVariable(unsigned I) const;
Alexey Bataev9c821032015-04-30 04:23:23 +0000291
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000292 /// Adds explicit data sharing attribute to the specified declaration.
Alexey Bataeve3727102018-04-18 15:57:46 +0000293 void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
Alexey Bataev90c228f2016-02-08 09:29:13 +0000294 DeclRefExpr *PrivateCopy = nullptr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000295
Alexey Bataevfa312f32017-07-21 18:48:21 +0000296 /// Adds additional information for the reduction items with the reduction id
297 /// represented as an operator.
Alexey Bataeve3727102018-04-18 15:57:46 +0000298 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000299 BinaryOperatorKind BOK);
Alexey Bataevfa312f32017-07-21 18:48:21 +0000300 /// Adds additional information for the reduction items with the reduction id
301 /// represented as reduction identifier.
Alexey Bataeve3727102018-04-18 15:57:46 +0000302 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000303 const Expr *ReductionRef);
Alexey Bataevfa312f32017-07-21 18:48:21 +0000304 /// Returns the location and reduction operation from the innermost parent
305 /// region for the given \p D.
Alexey Bataeve3727102018-04-18 15:57:46 +0000306 const DSAVarData
307 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
308 BinaryOperatorKind &BOK,
309 Expr *&TaskgroupDescriptor) const;
Alexey Bataevfa312f32017-07-21 18:48:21 +0000310 /// Returns the location and reduction operation from the innermost parent
311 /// region for the given \p D.
Alexey Bataeve3727102018-04-18 15:57:46 +0000312 const DSAVarData
313 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
314 const Expr *&ReductionRef,
315 Expr *&TaskgroupDescriptor) const;
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000316 /// Return reduction reference expression for the current taskgroup.
317 Expr *getTaskgroupReductionRef() const {
318 assert(Stack.back().first.back().Directive == OMPD_taskgroup &&
319 "taskgroup reference expression requested for non taskgroup "
320 "directive.");
321 return Stack.back().first.back().TaskgroupReductionRef;
322 }
Alexey Bataev88202be2017-07-27 13:20:36 +0000323 /// Checks if the given \p VD declaration is actually a taskgroup reduction
324 /// descriptor variable at the \p Level of OpenMP regions.
Alexey Bataeve3727102018-04-18 15:57:46 +0000325 bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const {
Alexey Bataev88202be2017-07-27 13:20:36 +0000326 return Stack.back().first[Level].TaskgroupReductionRef &&
327 cast<DeclRefExpr>(Stack.back().first[Level].TaskgroupReductionRef)
328 ->getDecl() == VD;
329 }
Alexey Bataevfa312f32017-07-21 18:48:21 +0000330
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000331 /// Returns data sharing attributes from top of the stack for the
Alexey Bataev758e55e2013-09-06 18:03:48 +0000332 /// specified declaration.
Alexey Bataeve3727102018-04-18 15:57:46 +0000333 const DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000334 /// Returns data-sharing attributes for the specified declaration.
Alexey Bataeve3727102018-04-18 15:57:46 +0000335 const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000336 /// Checks if the specified variables has data-sharing attributes which
Alexey Bataevf29276e2014-06-18 04:14:57 +0000337 /// match specified \a CPred predicate in any directive which matches \a DPred
338 /// predicate.
Alexey Bataeve3727102018-04-18 15:57:46 +0000339 const DSAVarData
340 hasDSA(ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
341 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
342 bool FromParent) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000343 /// Checks if the specified variables has data-sharing attributes which
Alexey Bataevf29276e2014-06-18 04:14:57 +0000344 /// match specified \a CPred predicate in any innermost directive which
345 /// matches \a DPred predicate.
Alexey Bataeve3727102018-04-18 15:57:46 +0000346 const DSAVarData
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000347 hasInnermostDSA(ValueDecl *D,
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000348 const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
349 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +0000350 bool FromParent) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000351 /// Checks if the specified variables has explicit data-sharing
Alexey Bataevaac108a2015-06-23 04:51:00 +0000352 /// attributes which match specified \a CPred predicate at the specified
353 /// OpenMP region.
Alexey Bataeve3727102018-04-18 15:57:46 +0000354 bool hasExplicitDSA(const ValueDecl *D,
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000355 const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
Alexey Bataeve3727102018-04-18 15:57:46 +0000356 unsigned Level, bool NotLastprivate = false) const;
Samuel Antao4be30e92015-10-02 17:14:03 +0000357
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000358 /// Returns true if the directive at level \Level matches in the
Samuel Antao4be30e92015-10-02 17:14:03 +0000359 /// specified \a DPred predicate.
360 bool hasExplicitDirective(
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000361 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +0000362 unsigned Level) const;
Samuel Antao4be30e92015-10-02 17:14:03 +0000363
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000364 /// Finds a directive which matches specified \a DPred predicate.
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000365 bool hasDirective(
366 const llvm::function_ref<bool(
367 OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)>
368 DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +0000369 bool FromParent) const;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000370
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000371 /// Returns currently analyzed directive.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000372 OpenMPDirectiveKind getCurrentDirective() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000373 return isStackEmpty() ? OMPD_unknown : Stack.back().first.back().Directive;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000374 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000375 /// Returns directive kind at specified level.
Alexey Bataevdfa430f2017-12-08 15:03:50 +0000376 OpenMPDirectiveKind getDirective(unsigned Level) const {
377 assert(!isStackEmpty() && "No directive at specified level.");
378 return Stack.back().first[Level].Directive;
379 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000380 /// Returns parent directive.
Alexey Bataev549210e2014-06-24 04:39:47 +0000381 OpenMPDirectiveKind getParentDirective() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000382 if (isStackEmpty() || Stack.back().first.size() == 1)
383 return OMPD_unknown;
384 return std::next(Stack.back().first.rbegin())->Directive;
Alexey Bataev549210e2014-06-24 04:39:47 +0000385 }
Alexey Bataev6ab5bb12018-10-29 15:01:58 +0000386
Kelvin Li1408f912018-09-26 04:28:39 +0000387 /// Add requires decl to internal vector
388 void addRequiresDecl(OMPRequiresDecl *RD) {
389 RequiresDecls.push_back(RD);
390 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000391
Kelvin Li1408f912018-09-26 04:28:39 +0000392 /// Checks for a duplicate clause amongst previously declared requires
393 /// directives
394 bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const {
395 bool IsDuplicate = false;
396 for (OMPClause *CNew : ClauseList) {
397 for (const OMPRequiresDecl *D : RequiresDecls) {
398 for (const OMPClause *CPrev : D->clauselists()) {
399 if (CNew->getClauseKind() == CPrev->getClauseKind()) {
400 SemaRef.Diag(CNew->getBeginLoc(),
401 diag::err_omp_requires_clause_redeclaration)
402 << getOpenMPClauseName(CNew->getClauseKind());
403 SemaRef.Diag(CPrev->getBeginLoc(),
404 diag::note_omp_requires_previous_clause)
405 << getOpenMPClauseName(CPrev->getClauseKind());
406 IsDuplicate = true;
407 }
408 }
409 }
410 }
411 return IsDuplicate;
412 }
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +0000413
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000414 /// Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000415 void setDefaultDSANone(SourceLocation Loc) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000416 assert(!isStackEmpty());
417 Stack.back().first.back().DefaultAttr = DSA_none;
418 Stack.back().first.back().DefaultAttrLoc = Loc;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000419 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000420 /// Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000421 void setDefaultDSAShared(SourceLocation Loc) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000422 assert(!isStackEmpty());
423 Stack.back().first.back().DefaultAttr = DSA_shared;
424 Stack.back().first.back().DefaultAttrLoc = Loc;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000425 }
Alexey Bataev2fd0cb22017-10-05 17:51:39 +0000426 /// Set default data mapping attribute to 'tofrom:scalar'.
427 void setDefaultDMAToFromScalar(SourceLocation Loc) {
428 assert(!isStackEmpty());
429 Stack.back().first.back().DefaultMapAttr = DMA_tofrom_scalar;
430 Stack.back().first.back().DefaultMapAttrLoc = Loc;
431 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000432
433 DefaultDataSharingAttributes getDefaultDSA() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000434 return isStackEmpty() ? DSA_unspecified
435 : Stack.back().first.back().DefaultAttr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000436 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000437 SourceLocation getDefaultDSALocation() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000438 return isStackEmpty() ? SourceLocation()
439 : Stack.back().first.back().DefaultAttrLoc;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000440 }
Alexey Bataev2fd0cb22017-10-05 17:51:39 +0000441 DefaultMapAttributes getDefaultDMA() const {
442 return isStackEmpty() ? DMA_unspecified
443 : Stack.back().first.back().DefaultMapAttr;
444 }
445 DefaultMapAttributes getDefaultDMAAtLevel(unsigned Level) const {
446 return Stack.back().first[Level].DefaultMapAttr;
447 }
448 SourceLocation getDefaultDMALocation() const {
449 return isStackEmpty() ? SourceLocation()
450 : Stack.back().first.back().DefaultMapAttrLoc;
451 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000452
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000453 /// Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000454 bool isThreadPrivate(VarDecl *D) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000455 const DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000456 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000457 }
458
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000459 /// Marks current region as ordered (it has an 'ordered' clause).
Alexey Bataevf138fda2018-08-13 19:04:24 +0000460 void setOrderedRegion(bool IsOrdered, const Expr *Param,
461 OMPOrderedClause *Clause) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000462 assert(!isStackEmpty());
Alexey Bataevf138fda2018-08-13 19:04:24 +0000463 if (IsOrdered)
464 Stack.back().first.back().OrderedRegion.emplace(Param, Clause);
465 else
466 Stack.back().first.back().OrderedRegion.reset();
467 }
468 /// Returns true, if region is ordered (has associated 'ordered' clause),
469 /// false - otherwise.
470 bool isOrderedRegion() const {
471 if (isStackEmpty())
472 return false;
473 return Stack.back().first.rbegin()->OrderedRegion.hasValue();
474 }
475 /// Returns optional parameter for the ordered region.
476 std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const {
477 if (isStackEmpty() ||
478 !Stack.back().first.rbegin()->OrderedRegion.hasValue())
479 return std::make_pair(nullptr, nullptr);
480 return Stack.back().first.rbegin()->OrderedRegion.getValue();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000481 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000482 /// Returns true, if parent region is ordered (has associated
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000483 /// 'ordered' clause), false - otherwise.
484 bool isParentOrderedRegion() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000485 if (isStackEmpty() || Stack.back().first.size() == 1)
486 return false;
Alexey Bataevf138fda2018-08-13 19:04:24 +0000487 return std::next(Stack.back().first.rbegin())->OrderedRegion.hasValue();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000488 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000489 /// Returns optional parameter for the ordered region.
Alexey Bataevf138fda2018-08-13 19:04:24 +0000490 std::pair<const Expr *, OMPOrderedClause *>
491 getParentOrderedRegionParam() const {
492 if (isStackEmpty() || Stack.back().first.size() == 1 ||
493 !std::next(Stack.back().first.rbegin())->OrderedRegion.hasValue())
494 return std::make_pair(nullptr, nullptr);
495 return std::next(Stack.back().first.rbegin())->OrderedRegion.getValue();
Alexey Bataev346265e2015-09-25 10:37:12 +0000496 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000497 /// Marks current region as nowait (it has a 'nowait' clause).
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000498 void setNowaitRegion(bool IsNowait = true) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000499 assert(!isStackEmpty());
500 Stack.back().first.back().NowaitRegion = IsNowait;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000501 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000502 /// Returns true, if parent region is nowait (has associated
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000503 /// 'nowait' clause), false - otherwise.
504 bool isParentNowaitRegion() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000505 if (isStackEmpty() || Stack.back().first.size() == 1)
506 return false;
507 return std::next(Stack.back().first.rbegin())->NowaitRegion;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000508 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000509 /// Marks parent region as cancel region.
Alexey Bataev25e5b442015-09-15 12:52:43 +0000510 void setParentCancelRegion(bool Cancel = true) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000511 if (!isStackEmpty() && Stack.back().first.size() > 1) {
512 auto &StackElemRef = *std::next(Stack.back().first.rbegin());
513 StackElemRef.CancelRegion |= StackElemRef.CancelRegion || Cancel;
514 }
Alexey Bataev25e5b442015-09-15 12:52:43 +0000515 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000516 /// Return true if current region has inner cancel construct.
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000517 bool isCancelRegion() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000518 return isStackEmpty() ? false : Stack.back().first.back().CancelRegion;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000519 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000520
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000521 /// Set collapse value for the region.
Alexey Bataev4b465392017-04-26 15:06:24 +0000522 void setAssociatedLoops(unsigned Val) {
523 assert(!isStackEmpty());
524 Stack.back().first.back().AssociatedLoops = Val;
525 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000526 /// Return collapse value for region.
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000527 unsigned getAssociatedLoops() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000528 return isStackEmpty() ? 0 : Stack.back().first.back().AssociatedLoops;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000529 }
Alexey Bataev9c821032015-04-30 04:23:23 +0000530
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000531 /// Marks current target region as one with closely nested teams
Alexey Bataev13314bf2014-10-09 04:18:56 +0000532 /// region.
533 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000534 if (!isStackEmpty() && Stack.back().first.size() > 1) {
535 std::next(Stack.back().first.rbegin())->InnerTeamsRegionLoc =
536 TeamsRegionLoc;
537 }
Alexey Bataev13314bf2014-10-09 04:18:56 +0000538 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000539 /// Returns true, if current region has closely nested teams region.
Alexey Bataev13314bf2014-10-09 04:18:56 +0000540 bool hasInnerTeamsRegion() const {
541 return getInnerTeamsRegionLoc().isValid();
542 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000543 /// Returns location of the nested teams region (if any).
Alexey Bataev13314bf2014-10-09 04:18:56 +0000544 SourceLocation getInnerTeamsRegionLoc() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000545 return isStackEmpty() ? SourceLocation()
546 : Stack.back().first.back().InnerTeamsRegionLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000547 }
548
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000549 Scope *getCurScope() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000550 return isStackEmpty() ? nullptr : Stack.back().first.back().CurScope;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000551 }
Alexey Bataeve3727102018-04-18 15:57:46 +0000552 SourceLocation getConstructLoc() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000553 return isStackEmpty() ? SourceLocation()
554 : Stack.back().first.back().ConstructLoc;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000555 }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000556
Samuel Antao4c8035b2016-12-12 18:00:20 +0000557 /// Do the check specified in \a Check to all component lists and return true
558 /// if any issue is found.
Samuel Antao90927002016-04-26 14:54:23 +0000559 bool checkMappableExprComponentListsForDecl(
Alexey Bataeve3727102018-04-18 15:57:46 +0000560 const ValueDecl *VD, bool CurrentRegionOnly,
Samuel Antao6890b092016-07-28 14:25:09 +0000561 const llvm::function_ref<
562 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000563 OpenMPClauseKind)>
Alexey Bataeve3727102018-04-18 15:57:46 +0000564 Check) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000565 if (isStackEmpty())
566 return false;
567 auto SI = Stack.back().first.rbegin();
568 auto SE = Stack.back().first.rend();
Samuel Antao5de996e2016-01-22 20:21:36 +0000569
570 if (SI == SE)
571 return false;
572
Alexey Bataeve3727102018-04-18 15:57:46 +0000573 if (CurrentRegionOnly)
Samuel Antao5de996e2016-01-22 20:21:36 +0000574 SE = std::next(SI);
Alexey Bataeve3727102018-04-18 15:57:46 +0000575 else
576 std::advance(SI, 1);
Samuel Antao5de996e2016-01-22 20:21:36 +0000577
578 for (; SI != SE; ++SI) {
Samuel Antao90927002016-04-26 14:54:23 +0000579 auto MI = SI->MappedExprComponents.find(VD);
580 if (MI != SI->MappedExprComponents.end())
Alexey Bataeve3727102018-04-18 15:57:46 +0000581 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
582 MI->second.Components)
Samuel Antao6890b092016-07-28 14:25:09 +0000583 if (Check(L, MI->second.Kind))
Samuel Antao5de996e2016-01-22 20:21:36 +0000584 return true;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000585 }
Samuel Antao5de996e2016-01-22 20:21:36 +0000586 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000587 }
588
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +0000589 /// Do the check specified in \a Check to all component lists at a given level
590 /// and return true if any issue is found.
591 bool checkMappableExprComponentListsForDeclAtLevel(
Alexey Bataeve3727102018-04-18 15:57:46 +0000592 const ValueDecl *VD, unsigned Level,
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +0000593 const llvm::function_ref<
594 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000595 OpenMPClauseKind)>
Alexey Bataeve3727102018-04-18 15:57:46 +0000596 Check) const {
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +0000597 if (isStackEmpty())
598 return false;
599
600 auto StartI = Stack.back().first.begin();
601 auto EndI = Stack.back().first.end();
602 if (std::distance(StartI, EndI) <= (int)Level)
603 return false;
604 std::advance(StartI, Level);
605
606 auto MI = StartI->MappedExprComponents.find(VD);
607 if (MI != StartI->MappedExprComponents.end())
Alexey Bataeve3727102018-04-18 15:57:46 +0000608 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
609 MI->second.Components)
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +0000610 if (Check(L, MI->second.Kind))
611 return true;
612 return false;
613 }
614
Samuel Antao4c8035b2016-12-12 18:00:20 +0000615 /// Create a new mappable expression component list associated with a given
616 /// declaration and initialize it with the provided list of components.
Samuel Antao90927002016-04-26 14:54:23 +0000617 void addMappableExpressionComponents(
Alexey Bataeve3727102018-04-18 15:57:46 +0000618 const ValueDecl *VD,
Samuel Antao6890b092016-07-28 14:25:09 +0000619 OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
620 OpenMPClauseKind WhereFoundClauseKind) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000621 assert(!isStackEmpty() &&
Samuel Antao90927002016-04-26 14:54:23 +0000622 "Not expecting to retrieve components from a empty stack!");
Alexey Bataeve3727102018-04-18 15:57:46 +0000623 MappedExprComponentTy &MEC =
624 Stack.back().first.back().MappedExprComponents[VD];
Samuel Antao90927002016-04-26 14:54:23 +0000625 // Create new entry and append the new components there.
Samuel Antao6890b092016-07-28 14:25:09 +0000626 MEC.Components.resize(MEC.Components.size() + 1);
627 MEC.Components.back().append(Components.begin(), Components.end());
628 MEC.Kind = WhereFoundClauseKind;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000629 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000630
631 unsigned getNestingLevel() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000632 assert(!isStackEmpty());
633 return Stack.back().first.size() - 1;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000634 }
Alexey Bataeve3727102018-04-18 15:57:46 +0000635 void addDoacrossDependClause(OMPDependClause *C,
636 const OperatorOffsetTy &OpsOffs) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000637 assert(!isStackEmpty() && Stack.back().first.size() > 1);
Alexey Bataeve3727102018-04-18 15:57:46 +0000638 SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
Alexey Bataev4b465392017-04-26 15:06:24 +0000639 assert(isOpenMPWorksharingDirective(StackElem.Directive));
Alexey Bataeve3727102018-04-18 15:57:46 +0000640 StackElem.DoacrossDepends.try_emplace(C, OpsOffs);
Alexey Bataev8b427062016-05-25 12:36:08 +0000641 }
642 llvm::iterator_range<DoacrossDependMapTy::const_iterator>
643 getDoacrossDependClauses() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000644 assert(!isStackEmpty());
Alexey Bataeve3727102018-04-18 15:57:46 +0000645 const SharingMapTy &StackElem = Stack.back().first.back();
Alexey Bataev4b465392017-04-26 15:06:24 +0000646 if (isOpenMPWorksharingDirective(StackElem.Directive)) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000647 const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends;
Alexey Bataev8b427062016-05-25 12:36:08 +0000648 return llvm::make_range(Ref.begin(), Ref.end());
649 }
Alexey Bataev4b465392017-04-26 15:06:24 +0000650 return llvm::make_range(StackElem.DoacrossDepends.end(),
651 StackElem.DoacrossDepends.end());
Alexey Bataev8b427062016-05-25 12:36:08 +0000652 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000653};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000654bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
Alexey Bataev35aaee62016-04-13 13:36:48 +0000655 return isOpenMPParallelDirective(DKind) || isOpenMPTaskingDirective(DKind) ||
656 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000657}
Alexey Bataeve3727102018-04-18 15:57:46 +0000658
Alexey Bataeved09d242014-05-28 05:53:51 +0000659} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000660
Alexey Bataeve3727102018-04-18 15:57:46 +0000661static const Expr *getExprAsWritten(const Expr *E) {
662 if (const auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000663 E = ExprTemp->getSubExpr();
664
Alexey Bataeve3727102018-04-18 15:57:46 +0000665 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000666 E = MTE->GetTemporaryExpr();
667
Alexey Bataeve3727102018-04-18 15:57:46 +0000668 while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000669 E = Binder->getSubExpr();
670
Alexey Bataeve3727102018-04-18 15:57:46 +0000671 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000672 E = ICE->getSubExprAsWritten();
673 return E->IgnoreParens();
674}
675
Alexey Bataeve3727102018-04-18 15:57:46 +0000676static Expr *getExprAsWritten(Expr *E) {
677 return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E)));
678}
679
680static const ValueDecl *getCanonicalDecl(const ValueDecl *D) {
681 if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D))
682 if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000683 D = ME->getMemberDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +0000684 const auto *VD = dyn_cast<VarDecl>(D);
685 const auto *FD = dyn_cast<FieldDecl>(D);
David Majnemer9d168222016-08-05 17:44:54 +0000686 if (VD != nullptr) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000687 VD = VD->getCanonicalDecl();
688 D = VD;
689 } else {
690 assert(FD);
691 FD = FD->getCanonicalDecl();
692 D = FD;
693 }
694 return D;
695}
696
Alexey Bataeve3727102018-04-18 15:57:46 +0000697static ValueDecl *getCanonicalDecl(ValueDecl *D) {
698 return const_cast<ValueDecl *>(
699 getCanonicalDecl(const_cast<const ValueDecl *>(D)));
700}
701
702DSAStackTy::DSAVarData DSAStackTy::getDSA(iterator &Iter,
703 ValueDecl *D) const {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000704 D = getCanonicalDecl(D);
705 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000706 const auto *FD = dyn_cast<FieldDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000707 DSAVarData DVar;
Alexey Bataev4b465392017-04-26 15:06:24 +0000708 if (isStackEmpty() || Iter == Stack.back().first.rend()) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000709 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
710 // in a region but not in construct]
711 // File-scope or namespace-scope variables referenced in called routines
712 // in the region are shared unless they appear in a threadprivate
713 // directive.
Alexey Bataeve3727102018-04-18 15:57:46 +0000714 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000715 DVar.CKind = OMPC_shared;
716
717 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
718 // in a region but not in construct]
719 // Variables with static storage duration that are declared in called
720 // routines in the region are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000721 if (VD && VD->hasGlobalStorage())
722 DVar.CKind = OMPC_shared;
723
724 // Non-static data members are shared by default.
725 if (FD)
Alexey Bataev750a58b2014-03-18 12:19:12 +0000726 DVar.CKind = OMPC_shared;
727
Alexey Bataev758e55e2013-09-06 18:03:48 +0000728 return DVar;
729 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000730
Alexey Bataevec3da872014-01-31 05:15:34 +0000731 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
732 // in a Construct, C/C++, predetermined, p.1]
733 // Variables with automatic storage duration that are declared in a scope
734 // inside the construct are private.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000735 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
736 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000737 DVar.CKind = OMPC_private;
738 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000739 }
740
Alexey Bataeveffbdf12017-07-21 17:24:30 +0000741 DVar.DKind = Iter->Directive;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000742 // Explicitly specified attributes and local variables with predetermined
743 // attributes.
744 if (Iter->SharingMap.count(D)) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000745 const DSAInfo &Data = Iter->SharingMap.lookup(D);
746 DVar.RefExpr = Data.RefExpr.getPointer();
747 DVar.PrivateCopy = Data.PrivateCopy;
748 DVar.CKind = Data.Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000749 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000750 return DVar;
751 }
752
753 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
754 // in a Construct, C/C++, implicitly determined, p.1]
755 // In a parallel or task construct, the data-sharing attributes of these
756 // variables are determined by the default clause, if present.
757 switch (Iter->DefaultAttr) {
758 case DSA_shared:
759 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000760 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000761 return DVar;
762 case DSA_none:
763 return DVar;
764 case DSA_unspecified:
765 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
766 // in a Construct, implicitly determined, p.2]
767 // In a parallel construct, if no default clause is present, these
768 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000769 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000770 if (isOpenMPParallelDirective(DVar.DKind) ||
771 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000772 DVar.CKind = OMPC_shared;
773 return DVar;
774 }
775
776 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
777 // in a Construct, implicitly determined, p.4]
778 // In a task construct, if no default clause is present, a variable that in
779 // the enclosing context is determined to be shared by all implicit tasks
780 // bound to the current team is shared.
Alexey Bataev35aaee62016-04-13 13:36:48 +0000781 if (isOpenMPTaskingDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000782 DSAVarData DVarTemp;
Alexey Bataeve3727102018-04-18 15:57:46 +0000783 iterator I = Iter, E = Stack.back().first.rend();
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000784 do {
785 ++I;
Alexey Bataeved09d242014-05-28 05:53:51 +0000786 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
Alexey Bataev35aaee62016-04-13 13:36:48 +0000787 // Referenced in a Construct, implicitly determined, p.6]
Alexey Bataev758e55e2013-09-06 18:03:48 +0000788 // In a task construct, if no default clause is present, a variable
789 // whose data-sharing attribute is not determined by the rules above is
790 // firstprivate.
791 DVarTemp = getDSA(I, D);
792 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000793 DVar.RefExpr = nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000794 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000795 return DVar;
796 }
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000797 } while (I != E && !isParallelOrTaskRegion(I->Directive));
Alexey Bataev758e55e2013-09-06 18:03:48 +0000798 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000799 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000800 return DVar;
801 }
802 }
803 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
804 // in a Construct, implicitly determined, p.3]
805 // For constructs other than task, if no default clause is present, these
806 // variables inherit their data-sharing attributes from the enclosing
807 // context.
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000808 return getDSA(++Iter, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000809}
810
Alexey Bataeve3727102018-04-18 15:57:46 +0000811const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D,
812 const Expr *NewDE) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000813 assert(!isStackEmpty() && "Data sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000814 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000815 SharingMapTy &StackElem = Stack.back().first.back();
Alexey Bataev4b465392017-04-26 15:06:24 +0000816 auto It = StackElem.AlignedMap.find(D);
817 if (It == StackElem.AlignedMap.end()) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000818 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
Alexey Bataev4b465392017-04-26 15:06:24 +0000819 StackElem.AlignedMap[D] = NewDE;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000820 return nullptr;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000821 }
Alexey Bataeve3727102018-04-18 15:57:46 +0000822 assert(It->second && "Unexpected nullptr expr in the aligned map");
823 return It->second;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000824}
825
Alexey Bataeve3727102018-04-18 15:57:46 +0000826void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000827 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000828 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000829 SharingMapTy &StackElem = Stack.back().first.back();
830 StackElem.LCVMap.try_emplace(
831 D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture));
Alexey Bataev9c821032015-04-30 04:23:23 +0000832}
833
Alexey Bataeve3727102018-04-18 15:57:46 +0000834const DSAStackTy::LCDeclInfo
835DSAStackTy::isLoopControlVariable(const ValueDecl *D) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000836 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000837 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000838 const SharingMapTy &StackElem = Stack.back().first.back();
Alexey Bataev4b465392017-04-26 15:06:24 +0000839 auto It = StackElem.LCVMap.find(D);
840 if (It != StackElem.LCVMap.end())
841 return It->second;
842 return {0, nullptr};
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000843}
844
Alexey Bataeve3727102018-04-18 15:57:46 +0000845const DSAStackTy::LCDeclInfo
846DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000847 assert(!isStackEmpty() && Stack.back().first.size() > 1 &&
848 "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000849 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000850 const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
Alexey Bataev4b465392017-04-26 15:06:24 +0000851 auto It = StackElem.LCVMap.find(D);
852 if (It != StackElem.LCVMap.end())
853 return It->second;
854 return {0, nullptr};
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000855}
856
Alexey Bataeve3727102018-04-18 15:57:46 +0000857const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000858 assert(!isStackEmpty() && Stack.back().first.size() > 1 &&
859 "Data-sharing attributes stack is empty");
Alexey Bataeve3727102018-04-18 15:57:46 +0000860 const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
Alexey Bataev4b465392017-04-26 15:06:24 +0000861 if (StackElem.LCVMap.size() < I)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000862 return nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +0000863 for (const auto &Pair : StackElem.LCVMap)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000864 if (Pair.second.first == I)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000865 return Pair.first;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000866 return nullptr;
Alexey Bataev9c821032015-04-30 04:23:23 +0000867}
868
Alexey Bataeve3727102018-04-18 15:57:46 +0000869void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
Alexey Bataev90c228f2016-02-08 09:29:13 +0000870 DeclRefExpr *PrivateCopy) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000871 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000872 if (A == OMPC_threadprivate) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000873 DSAInfo &Data = Threadprivates[D];
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000874 Data.Attributes = A;
875 Data.RefExpr.setPointer(E);
876 Data.PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000877 } else {
Alexey Bataev4b465392017-04-26 15:06:24 +0000878 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataeve3727102018-04-18 15:57:46 +0000879 DSAInfo &Data = Stack.back().first.back().SharingMap[D];
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000880 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
881 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
882 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
883 (isLoopControlVariable(D).first && A == OMPC_private));
884 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
885 Data.RefExpr.setInt(/*IntVal=*/true);
886 return;
887 }
888 const bool IsLastprivate =
889 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
890 Data.Attributes = A;
891 Data.RefExpr.setPointerAndInt(E, IsLastprivate);
892 Data.PrivateCopy = PrivateCopy;
893 if (PrivateCopy) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000894 DSAInfo &Data =
895 Stack.back().first.back().SharingMap[PrivateCopy->getDecl()];
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000896 Data.Attributes = A;
897 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
898 Data.PrivateCopy = nullptr;
899 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000900 }
901}
902
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000903/// Build a variable declaration for OpenMP loop iteration variable.
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000904static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev63cc8e92018-03-20 14:45:59 +0000905 StringRef Name, const AttrVec *Attrs = nullptr,
906 DeclRefExpr *OrigRef = nullptr) {
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000907 DeclContext *DC = SemaRef.CurContext;
908 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
909 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
Alexey Bataeve3727102018-04-18 15:57:46 +0000910 auto *Decl =
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000911 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
912 if (Attrs) {
913 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
914 I != E; ++I)
915 Decl->addAttr(*I);
916 }
917 Decl->setImplicit();
Alexey Bataev63cc8e92018-03-20 14:45:59 +0000918 if (OrigRef) {
919 Decl->addAttr(
920 OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef));
921 }
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000922 return Decl;
923}
924
925static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
926 SourceLocation Loc,
927 bool RefersToCapture = false) {
928 D->setReferenced();
929 D->markUsed(S.Context);
930 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
931 SourceLocation(), D, RefersToCapture, Loc, Ty,
932 VK_LValue);
933}
934
Alexey Bataeve3727102018-04-18 15:57:46 +0000935void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000936 BinaryOperatorKind BOK) {
Alexey Bataevfa312f32017-07-21 18:48:21 +0000937 D = getCanonicalDecl(D);
938 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataevfa312f32017-07-21 18:48:21 +0000939 assert(
Richard Trieu09f14112017-07-21 21:29:35 +0000940 Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000941 "Additional reduction info may be specified only for reduction items.");
Alexey Bataeve3727102018-04-18 15:57:46 +0000942 ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D];
Alexey Bataevfa312f32017-07-21 18:48:21 +0000943 assert(ReductionData.ReductionRange.isInvalid() &&
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000944 Stack.back().first.back().Directive == OMPD_taskgroup &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000945 "Additional reduction info may be specified only once for reduction "
946 "items.");
947 ReductionData.set(BOK, SR);
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000948 Expr *&TaskgroupReductionRef =
949 Stack.back().first.back().TaskgroupReductionRef;
950 if (!TaskgroupReductionRef) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000951 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
952 SemaRef.Context.VoidPtrTy, ".task_red.");
Alexey Bataevd070a582017-10-25 15:54:04 +0000953 TaskgroupReductionRef =
954 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000955 }
Alexey Bataevfa312f32017-07-21 18:48:21 +0000956}
957
Alexey Bataeve3727102018-04-18 15:57:46 +0000958void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000959 const Expr *ReductionRef) {
Alexey Bataevfa312f32017-07-21 18:48:21 +0000960 D = getCanonicalDecl(D);
961 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataevfa312f32017-07-21 18:48:21 +0000962 assert(
Richard Trieu09f14112017-07-21 21:29:35 +0000963 Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000964 "Additional reduction info may be specified only for reduction items.");
Alexey Bataeve3727102018-04-18 15:57:46 +0000965 ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D];
Alexey Bataevfa312f32017-07-21 18:48:21 +0000966 assert(ReductionData.ReductionRange.isInvalid() &&
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000967 Stack.back().first.back().Directive == OMPD_taskgroup &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000968 "Additional reduction info may be specified only once for reduction "
969 "items.");
970 ReductionData.set(ReductionRef, SR);
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000971 Expr *&TaskgroupReductionRef =
972 Stack.back().first.back().TaskgroupReductionRef;
973 if (!TaskgroupReductionRef) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000974 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
975 SemaRef.Context.VoidPtrTy, ".task_red.");
Alexey Bataevd070a582017-10-25 15:54:04 +0000976 TaskgroupReductionRef =
977 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000978 }
Alexey Bataevfa312f32017-07-21 18:48:21 +0000979}
980
Alexey Bataeve3727102018-04-18 15:57:46 +0000981const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
982 const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK,
983 Expr *&TaskgroupDescriptor) const {
Alexey Bataevfa312f32017-07-21 18:48:21 +0000984 D = getCanonicalDecl(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +0000985 assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
986 if (Stack.back().first.empty())
987 return DSAVarData();
Alexey Bataeve3727102018-04-18 15:57:46 +0000988 for (iterator I = std::next(Stack.back().first.rbegin(), 1),
989 E = Stack.back().first.rend();
Alexey Bataevfa312f32017-07-21 18:48:21 +0000990 I != E; std::advance(I, 1)) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000991 const DSAInfo &Data = I->SharingMap.lookup(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +0000992 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
Alexey Bataevfa312f32017-07-21 18:48:21 +0000993 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +0000994 const ReductionData &ReductionData = I->ReductionMap.lookup(D);
Alexey Bataevfa312f32017-07-21 18:48:21 +0000995 if (!ReductionData.ReductionOp ||
996 ReductionData.ReductionOp.is<const Expr *>())
Alexey Bataevf189cb72017-07-24 14:52:13 +0000997 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +0000998 SR = ReductionData.ReductionRange;
Alexey Bataevf87fa882017-07-21 19:26:22 +0000999 BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>();
Alexey Bataev88202be2017-07-27 13:20:36 +00001000 assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1001 "expression for the descriptor is not "
1002 "set.");
1003 TaskgroupDescriptor = I->TaskgroupReductionRef;
Alexey Bataevf189cb72017-07-24 14:52:13 +00001004 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
1005 Data.PrivateCopy, I->DefaultAttrLoc);
Alexey Bataevfa312f32017-07-21 18:48:21 +00001006 }
Alexey Bataevf189cb72017-07-24 14:52:13 +00001007 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001008}
1009
Alexey Bataeve3727102018-04-18 15:57:46 +00001010const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1011 const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef,
1012 Expr *&TaskgroupDescriptor) const {
Alexey Bataevfa312f32017-07-21 18:48:21 +00001013 D = getCanonicalDecl(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +00001014 assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1015 if (Stack.back().first.empty())
1016 return DSAVarData();
Alexey Bataeve3727102018-04-18 15:57:46 +00001017 for (iterator I = std::next(Stack.back().first.rbegin(), 1),
1018 E = Stack.back().first.rend();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001019 I != E; std::advance(I, 1)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001020 const DSAInfo &Data = I->SharingMap.lookup(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +00001021 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
Alexey Bataevfa312f32017-07-21 18:48:21 +00001022 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00001023 const ReductionData &ReductionData = I->ReductionMap.lookup(D);
Alexey Bataevfa312f32017-07-21 18:48:21 +00001024 if (!ReductionData.ReductionOp ||
1025 !ReductionData.ReductionOp.is<const Expr *>())
Alexey Bataevf189cb72017-07-24 14:52:13 +00001026 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001027 SR = ReductionData.ReductionRange;
1028 ReductionRef = ReductionData.ReductionOp.get<const Expr *>();
Alexey Bataev88202be2017-07-27 13:20:36 +00001029 assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1030 "expression for the descriptor is not "
1031 "set.");
1032 TaskgroupDescriptor = I->TaskgroupReductionRef;
Alexey Bataevf189cb72017-07-24 14:52:13 +00001033 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
1034 Data.PrivateCopy, I->DefaultAttrLoc);
Alexey Bataevfa312f32017-07-21 18:48:21 +00001035 }
Alexey Bataevf189cb72017-07-24 14:52:13 +00001036 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001037}
1038
Alexey Bataeve3727102018-04-18 15:57:46 +00001039bool DSAStackTy::isOpenMPLocal(VarDecl *D, iterator Iter) const {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001040 D = D->getCanonicalDecl();
Alexey Bataev852525d2018-03-02 17:17:12 +00001041 if (!isStackEmpty()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001042 iterator I = Iter, E = Stack.back().first.rend();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001043 Scope *TopScope = nullptr;
Alexey Bataev852525d2018-03-02 17:17:12 +00001044 while (I != E && !isParallelOrTaskRegion(I->Directive) &&
1045 !isOpenMPTargetExecutionDirective(I->Directive))
Alexey Bataevec3da872014-01-31 05:15:34 +00001046 ++I;
Alexey Bataeved09d242014-05-28 05:53:51 +00001047 if (I == E)
1048 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001049 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001050 Scope *CurScope = getCurScope();
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001051 while (CurScope != TopScope && !CurScope->isDeclScope(D))
Alexey Bataev758e55e2013-09-06 18:03:48 +00001052 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +00001053 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001054 }
Alexey Bataevec3da872014-01-31 05:15:34 +00001055 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001056}
1057
Alexey Bataeve3727102018-04-18 15:57:46 +00001058const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
1059 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001060 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001061 DSAVarData DVar;
1062
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001063 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001064 auto TI = Threadprivates.find(D);
1065 if (TI != Threadprivates.end()) {
1066 DVar.RefExpr = TI->getSecond().RefExpr.getPointer();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001067 DVar.CKind = OMPC_threadprivate;
1068 return DVar;
Alexey Bataeve3727102018-04-18 15:57:46 +00001069 }
1070 if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
Alexey Bataev817d7f32017-11-14 21:01:01 +00001071 DVar.RefExpr = buildDeclRefExpr(
1072 SemaRef, VD, D->getType().getNonReferenceType(),
1073 VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation());
1074 DVar.CKind = OMPC_threadprivate;
1075 addDSA(D, DVar.RefExpr, OMPC_threadprivate);
Alexey Bataev852525d2018-03-02 17:17:12 +00001076 return DVar;
1077 }
1078 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1079 // in a Construct, C/C++, predetermined, p.1]
1080 // Variables appearing in threadprivate directives are threadprivate.
1081 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
1082 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1083 SemaRef.getLangOpts().OpenMPUseTLS &&
1084 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
1085 (VD && VD->getStorageClass() == SC_Register &&
1086 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
1087 DVar.RefExpr = buildDeclRefExpr(
1088 SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation());
1089 DVar.CKind = OMPC_threadprivate;
1090 addDSA(D, DVar.RefExpr, OMPC_threadprivate);
1091 return DVar;
1092 }
1093 if (SemaRef.getLangOpts().OpenMPCUDAMode && VD &&
1094 VD->isLocalVarDeclOrParm() && !isStackEmpty() &&
1095 !isLoopControlVariable(D).first) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001096 iterator IterTarget =
Alexey Bataev852525d2018-03-02 17:17:12 +00001097 std::find_if(Stack.back().first.rbegin(), Stack.back().first.rend(),
1098 [](const SharingMapTy &Data) {
1099 return isOpenMPTargetExecutionDirective(Data.Directive);
1100 });
1101 if (IterTarget != Stack.back().first.rend()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001102 iterator ParentIterTarget = std::next(IterTarget, 1);
1103 for (iterator Iter = Stack.back().first.rbegin();
1104 Iter != ParentIterTarget; std::advance(Iter, 1)) {
Alexey Bataev852525d2018-03-02 17:17:12 +00001105 if (isOpenMPLocal(VD, Iter)) {
1106 DVar.RefExpr =
1107 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1108 D->getLocation());
1109 DVar.CKind = OMPC_threadprivate;
1110 return DVar;
1111 }
Alexey Bataev852525d2018-03-02 17:17:12 +00001112 }
1113 if (!isClauseParsingMode() || IterTarget != Stack.back().first.rbegin()) {
1114 auto DSAIter = IterTarget->SharingMap.find(D);
1115 if (DSAIter != IterTarget->SharingMap.end() &&
1116 isOpenMPPrivate(DSAIter->getSecond().Attributes)) {
1117 DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer();
1118 DVar.CKind = OMPC_threadprivate;
1119 return DVar;
Alexey Bataeve3727102018-04-18 15:57:46 +00001120 }
1121 iterator End = Stack.back().first.rend();
1122 if (!SemaRef.isOpenMPCapturedByRef(
1123 D, std::distance(ParentIterTarget, End))) {
Alexey Bataev852525d2018-03-02 17:17:12 +00001124 DVar.RefExpr =
1125 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1126 IterTarget->ConstructLoc);
1127 DVar.CKind = OMPC_threadprivate;
1128 return DVar;
1129 }
1130 }
1131 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001132 }
1133
Alexey Bataev4b465392017-04-26 15:06:24 +00001134 if (isStackEmpty())
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001135 // Not in OpenMP execution region and top scope was already checked.
1136 return DVar;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001137
Alexey Bataev758e55e2013-09-06 18:03:48 +00001138 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +00001139 // in a Construct, C/C++, predetermined, p.4]
1140 // Static data members are shared.
1141 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1142 // in a Construct, C/C++, predetermined, p.7]
1143 // Variables with static storage duration that are declared in a scope
1144 // inside the construct are shared.
Alexey Bataeve3727102018-04-18 15:57:46 +00001145 auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; };
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001146 if (VD && VD->isStaticDataMember()) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001147 DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent);
Alexey Bataevdffa93a2015-12-10 08:20:58 +00001148 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
Alexey Bataevec3da872014-01-31 05:15:34 +00001149 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001150
Alexey Bataevdffa93a2015-12-10 08:20:58 +00001151 DVar.CKind = OMPC_shared;
1152 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001153 }
1154
1155 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00001156 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
1157 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001158 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1159 // in a Construct, C/C++, predetermined, p.6]
1160 // Variables with const qualified type having no mutable member are
1161 // shared.
Alexey Bataeve3727102018-04-18 15:57:46 +00001162 const CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +00001163 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +00001164 if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
1165 if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate())
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00001166 RD = CTD->getTemplatedDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001167 if (IsConstant &&
Alexey Bataev4bcad7f2016-02-10 10:50:12 +00001168 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() &&
1169 RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001170 // Variables with const-qualified type having no mutable member may be
1171 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataeve3727102018-04-18 15:57:46 +00001172 DSAVarData DVarTemp =
1173 hasDSA(D, [](OpenMPClauseKind C) { return C == OMPC_firstprivate; },
1174 MatchesAlways, FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001175 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
Alexey Bataev9a757382018-02-16 19:16:54 +00001176 return DVarTemp;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001177
Alexey Bataev758e55e2013-09-06 18:03:48 +00001178 DVar.CKind = OMPC_shared;
1179 return DVar;
1180 }
1181
Alexey Bataev758e55e2013-09-06 18:03:48 +00001182 // Explicitly specified attributes and local variables with predetermined
1183 // attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +00001184 iterator I = Stack.back().first.rbegin();
1185 iterator EndI = Stack.back().first.rend();
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001186 if (FromParent && I != EndI)
1187 std::advance(I, 1);
Alexey Bataeve3727102018-04-18 15:57:46 +00001188 auto It = I->SharingMap.find(D);
1189 if (It != I->SharingMap.end()) {
1190 const DSAInfo &Data = It->getSecond();
1191 DVar.RefExpr = Data.RefExpr.getPointer();
1192 DVar.PrivateCopy = Data.PrivateCopy;
1193 DVar.CKind = Data.Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001194 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev4d4624c2017-07-20 16:47:47 +00001195 DVar.DKind = I->Directive;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001196 }
1197
1198 return DVar;
1199}
1200
Alexey Bataeve3727102018-04-18 15:57:46 +00001201const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1202 bool FromParent) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001203 if (isStackEmpty()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001204 iterator I;
Alexey Bataev4b465392017-04-26 15:06:24 +00001205 return getDSA(I, D);
1206 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001207 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +00001208 iterator StartI = Stack.back().first.rbegin();
1209 iterator EndI = Stack.back().first.rend();
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001210 if (FromParent && StartI != EndI)
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001211 std::advance(StartI, 1);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001212 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001213}
1214
Alexey Bataeve3727102018-04-18 15:57:46 +00001215const DSAStackTy::DSAVarData
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001216DSAStackTy::hasDSA(ValueDecl *D,
Alexey Bataev97d18bf2018-04-11 19:21:00 +00001217 const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1218 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +00001219 bool FromParent) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001220 if (isStackEmpty())
1221 return {};
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001222 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +00001223 iterator I = Stack.back().first.rbegin();
1224 iterator EndI = Stack.back().first.rend();
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001225 if (FromParent && I != EndI)
Alexey Bataev0e6fc1c2017-04-27 14:46:26 +00001226 std::advance(I, 1);
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001227 for (; I != EndI; std::advance(I, 1)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001228 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +00001229 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00001230 iterator NewI = I;
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001231 DSAVarData DVar = getDSA(NewI, D);
1232 if (I == NewI && CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001233 return DVar;
Alexey Bataev60859c02017-04-27 15:10:33 +00001234 }
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001235 return {};
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001236}
1237
Alexey Bataeve3727102018-04-18 15:57:46 +00001238const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
Alexey Bataev97d18bf2018-04-11 19:21:00 +00001239 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1240 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +00001241 bool FromParent) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001242 if (isStackEmpty())
1243 return {};
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001244 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +00001245 iterator StartI = Stack.back().first.rbegin();
1246 iterator EndI = Stack.back().first.rend();
Alexey Bataeve3978122016-07-19 05:06:39 +00001247 if (FromParent && StartI != EndI)
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001248 std::advance(StartI, 1);
Alexey Bataeve3978122016-07-19 05:06:39 +00001249 if (StartI == EndI || !DPred(StartI->Directive))
Alexey Bataev4b465392017-04-26 15:06:24 +00001250 return {};
Alexey Bataeve3727102018-04-18 15:57:46 +00001251 iterator NewI = StartI;
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001252 DSAVarData DVar = getDSA(NewI, D);
1253 return (NewI == StartI && CPred(DVar.CKind)) ? DVar : DSAVarData();
Alexey Bataevc5e02582014-06-16 07:08:35 +00001254}
1255
Alexey Bataevaac108a2015-06-23 04:51:00 +00001256bool DSAStackTy::hasExplicitDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00001257 const ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1258 unsigned Level, bool NotLastprivate) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001259 if (isStackEmpty())
1260 return false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001261 D = getCanonicalDecl(D);
Alexey Bataev4b465392017-04-26 15:06:24 +00001262 auto StartI = Stack.back().first.begin();
1263 auto EndI = Stack.back().first.end();
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +00001264 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +00001265 return false;
1266 std::advance(StartI, Level);
Alexey Bataeve3727102018-04-18 15:57:46 +00001267 auto I = StartI->SharingMap.find(D);
1268 return (I != StartI->SharingMap.end()) &&
1269 I->getSecond().RefExpr.getPointer() &&
1270 CPred(I->getSecond().Attributes) &&
1271 (!NotLastprivate || !I->getSecond().RefExpr.getInt());
Alexey Bataevaac108a2015-06-23 04:51:00 +00001272}
1273
Samuel Antao4be30e92015-10-02 17:14:03 +00001274bool DSAStackTy::hasExplicitDirective(
Alexey Bataeve3727102018-04-18 15:57:46 +00001275 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1276 unsigned Level) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001277 if (isStackEmpty())
1278 return false;
1279 auto StartI = Stack.back().first.begin();
1280 auto EndI = Stack.back().first.end();
Samuel Antao4be30e92015-10-02 17:14:03 +00001281 if (std::distance(StartI, EndI) <= (int)Level)
1282 return false;
1283 std::advance(StartI, Level);
1284 return DPred(StartI->Directive);
1285}
1286
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001287bool DSAStackTy::hasDirective(
1288 const llvm::function_ref<bool(OpenMPDirectiveKind,
1289 const DeclarationNameInfo &, SourceLocation)>
Alexey Bataev97d18bf2018-04-11 19:21:00 +00001290 DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +00001291 bool FromParent) const {
Samuel Antaof0d79752016-05-27 15:21:27 +00001292 // We look only in the enclosing region.
Alexey Bataev4b465392017-04-26 15:06:24 +00001293 if (isStackEmpty())
Samuel Antaof0d79752016-05-27 15:21:27 +00001294 return false;
Alexey Bataev4b465392017-04-26 15:06:24 +00001295 auto StartI = std::next(Stack.back().first.rbegin());
1296 auto EndI = Stack.back().first.rend();
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001297 if (FromParent && StartI != EndI)
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001298 StartI = std::next(StartI);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001299 for (auto I = StartI, EE = EndI; I != EE; ++I) {
1300 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
1301 return true;
1302 }
1303 return false;
1304}
1305
Alexey Bataev758e55e2013-09-06 18:03:48 +00001306void Sema::InitDataSharingAttributesStack() {
1307 VarDataSharingAttributesStack = new DSAStackTy(*this);
1308}
1309
1310#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
1311
Alexey Bataev4b465392017-04-26 15:06:24 +00001312void Sema::pushOpenMPFunctionRegion() {
1313 DSAStack->pushFunction();
1314}
1315
1316void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) {
1317 DSAStack->popFunction(OldFSI);
1318}
1319
Alexey Bataeve3727102018-04-18 15:57:46 +00001320bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level) const {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001321 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1322
Alexey Bataeve3727102018-04-18 15:57:46 +00001323 ASTContext &Ctx = getASTContext();
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001324 bool IsByRef = true;
1325
1326 // Find the directive that is associated with the provided scope.
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00001327 D = cast<ValueDecl>(D->getCanonicalDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00001328 QualType Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001329
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001330 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001331 // This table summarizes how a given variable should be passed to the device
1332 // given its type and the clauses where it appears. This table is based on
1333 // the description in OpenMP 4.5 [2.10.4, target Construct] and
1334 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
1335 //
1336 // =========================================================================
1337 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
1338 // | |(tofrom:scalar)| | pvt | | | |
1339 // =========================================================================
1340 // | scl | | | | - | | bycopy|
1341 // | scl | | - | x | - | - | bycopy|
1342 // | scl | | x | - | - | - | null |
1343 // | scl | x | | | - | | byref |
1344 // | scl | x | - | x | - | - | bycopy|
1345 // | scl | x | x | - | - | - | null |
1346 // | scl | | - | - | - | x | byref |
1347 // | scl | x | - | - | - | x | byref |
1348 //
1349 // | agg | n.a. | | | - | | byref |
1350 // | agg | n.a. | - | x | - | - | byref |
1351 // | agg | n.a. | x | - | - | - | null |
1352 // | agg | n.a. | - | - | - | x | byref |
1353 // | agg | n.a. | - | - | - | x[] | byref |
1354 //
1355 // | ptr | n.a. | | | - | | bycopy|
1356 // | ptr | n.a. | - | x | - | - | bycopy|
1357 // | ptr | n.a. | x | - | - | - | null |
1358 // | ptr | n.a. | - | - | - | x | byref |
1359 // | ptr | n.a. | - | - | - | x[] | bycopy|
1360 // | ptr | n.a. | - | - | x | | bycopy|
1361 // | ptr | n.a. | - | - | x | x | bycopy|
1362 // | ptr | n.a. | - | - | x | x[] | bycopy|
1363 // =========================================================================
1364 // Legend:
1365 // scl - scalar
1366 // ptr - pointer
1367 // agg - aggregate
1368 // x - applies
1369 // - - invalid in this combination
1370 // [] - mapped with an array section
1371 // byref - should be mapped by reference
1372 // byval - should be mapped by value
1373 // null - initialize a local variable to null on the device
1374 //
1375 // Observations:
1376 // - All scalar declarations that show up in a map clause have to be passed
1377 // by reference, because they may have been mapped in the enclosing data
1378 // environment.
1379 // - If the scalar value does not fit the size of uintptr, it has to be
1380 // passed by reference, regardless the result in the table above.
1381 // - For pointers mapped by value that have either an implicit map or an
1382 // array section, the runtime library may pass the NULL value to the
1383 // device instead of the value passed to it by the compiler.
1384
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001385 if (Ty->isReferenceType())
1386 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
Samuel Antao86ace552016-04-27 22:40:57 +00001387
1388 // Locate map clauses and see if the variable being captured is referred to
1389 // in any of those clauses. Here we only care about variables, not fields,
1390 // because fields are part of aggregates.
1391 bool IsVariableUsedInMapClause = false;
1392 bool IsVariableAssociatedWithSection = false;
1393
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +00001394 DSAStack->checkMappableExprComponentListsForDeclAtLevel(
Alexey Bataeve3727102018-04-18 15:57:46 +00001395 D, Level,
1396 [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D](
1397 OMPClauseMappableExprCommon::MappableExprComponentListRef
Samuel Antao6890b092016-07-28 14:25:09 +00001398 MapExprComponents,
1399 OpenMPClauseKind WhereFoundClauseKind) {
1400 // Only the map clause information influences how a variable is
1401 // captured. E.g. is_device_ptr does not require changing the default
Samuel Antao4c8035b2016-12-12 18:00:20 +00001402 // behavior.
Samuel Antao6890b092016-07-28 14:25:09 +00001403 if (WhereFoundClauseKind != OMPC_map)
1404 return false;
Samuel Antao86ace552016-04-27 22:40:57 +00001405
1406 auto EI = MapExprComponents.rbegin();
1407 auto EE = MapExprComponents.rend();
1408
1409 assert(EI != EE && "Invalid map expression!");
1410
1411 if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
1412 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
1413
1414 ++EI;
1415 if (EI == EE)
1416 return false;
1417
1418 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
1419 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
1420 isa<MemberExpr>(EI->getAssociatedExpression())) {
1421 IsVariableAssociatedWithSection = true;
1422 // There is nothing more we need to know about this variable.
1423 return true;
1424 }
1425
1426 // Keep looking for more map info.
1427 return false;
1428 });
1429
1430 if (IsVariableUsedInMapClause) {
1431 // If variable is identified in a map clause it is always captured by
1432 // reference except if it is a pointer that is dereferenced somehow.
1433 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
1434 } else {
Alexey Bataev3f96fe62017-12-13 17:31:39 +00001435 // By default, all the data that has a scalar type is mapped by copy
1436 // (except for reduction variables).
1437 IsByRef =
1438 !Ty->isScalarType() ||
1439 DSAStack->getDefaultDMAAtLevel(Level) == DMA_tofrom_scalar ||
1440 DSAStack->hasExplicitDSA(
1441 D, [](OpenMPClauseKind K) { return K == OMPC_reduction; }, Level);
Samuel Antao86ace552016-04-27 22:40:57 +00001442 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001443 }
1444
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001445 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00001446 IsByRef =
1447 !DSAStack->hasExplicitDSA(
1448 D,
1449 [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; },
1450 Level, /*NotLastprivate=*/true) &&
1451 // If the variable is artificial and must be captured by value - try to
1452 // capture by value.
Alexey Bataevd2202ca2017-12-27 17:58:32 +00001453 !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() &&
1454 !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue());
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001455 }
1456
Samuel Antao86ace552016-04-27 22:40:57 +00001457 // When passing data by copy, we need to make sure it fits the uintptr size
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001458 // and alignment, because the runtime library only deals with uintptr types.
1459 // If it does not fit the uintptr size, we need to pass the data by reference
1460 // instead.
1461 if (!IsByRef &&
1462 (Ctx.getTypeSizeInChars(Ty) >
1463 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001464 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001465 IsByRef = true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001466 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001467
1468 return IsByRef;
1469}
1470
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001471unsigned Sema::getOpenMPNestingLevel() const {
1472 assert(getLangOpts().OpenMP);
1473 return DSAStack->getNestingLevel();
1474}
1475
Jonas Hahnfeld87d44262017-11-18 21:00:46 +00001476bool Sema::isInOpenMPTargetExecutionDirective() const {
1477 return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) &&
1478 !DSAStack->isClauseParsingMode()) ||
1479 DSAStack->hasDirective(
1480 [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
1481 SourceLocation) -> bool {
1482 return isOpenMPTargetExecutionDirective(K);
1483 },
1484 false);
1485}
1486
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001487VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D) {
Alexey Bataevf841bd92014-12-16 07:00:22 +00001488 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001489 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +00001490
1491 // If we are attempting to capture a global variable in a directive with
1492 // 'target' we return true so that this global is also mapped to the device.
1493 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001494 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001495 if (VD && !VD->hasLocalStorage()) {
1496 if (isInOpenMPDeclareTargetContext() &&
1497 (getCurCapturedRegion() || getCurBlock() || getCurLambda())) {
1498 // Try to mark variable as declare target if it is used in capturing
1499 // regions.
Alexey Bataev97b72212018-08-14 18:31:20 +00001500 if (!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001501 checkDeclIsAllowedInOpenMPTarget(nullptr, VD);
Alexey Bataev92327c52018-03-26 16:40:55 +00001502 return nullptr;
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001503 } else if (isInOpenMPTargetExecutionDirective()) {
1504 // If the declaration is enclosed in a 'declare target' directive,
1505 // then it should not be captured.
1506 //
Alexey Bataev97b72212018-08-14 18:31:20 +00001507 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001508 return nullptr;
1509 return VD;
1510 }
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001511 }
Samuel Antao4be30e92015-10-02 17:14:03 +00001512
Alexey Bataev48977c32015-08-04 08:10:48 +00001513 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
1514 (!DSAStack->isClauseParsingMode() ||
1515 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001516 auto &&Info = DSAStack->isLoopControlVariable(D);
1517 if (Info.first ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001518 (VD && VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001519 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001520 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001521 return VD ? VD : Info.second;
Alexey Bataeve3727102018-04-18 15:57:46 +00001522 DSAStackTy::DSAVarData DVarPrivate =
1523 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001524 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
Alexey Bataev90c228f2016-02-08 09:29:13 +00001525 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00001526 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate,
1527 [](OpenMPDirectiveKind) { return true; },
1528 DSAStack->isClauseParsingMode());
Alexey Bataev90c228f2016-02-08 09:29:13 +00001529 if (DVarPrivate.CKind != OMPC_unknown)
1530 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001531 }
Alexey Bataev90c228f2016-02-08 09:29:13 +00001532 return nullptr;
Alexey Bataevf841bd92014-12-16 07:00:22 +00001533}
1534
Alexey Bataevdfa430f2017-12-08 15:03:50 +00001535void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex,
1536 unsigned Level) const {
1537 SmallVector<OpenMPDirectiveKind, 4> Regions;
1538 getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level));
1539 FunctionScopesIndex -= Regions.size();
1540}
1541
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00001542void Sema::startOpenMPLoop() {
1543 assert(LangOpts.OpenMP && "OpenMP must be enabled.");
1544 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective()))
1545 DSAStack->loopInit();
1546}
1547
Alexey Bataeve3727102018-04-18 15:57:46 +00001548bool Sema::isOpenMPPrivateDecl(const ValueDecl *D, unsigned Level) const {
Alexey Bataevaac108a2015-06-23 04:51:00 +00001549 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00001550 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
1551 if (DSAStack->getAssociatedLoops() > 0 &&
1552 !DSAStack->isLoopStarted()) {
1553 DSAStack->resetPossibleLoopCounter(D);
1554 DSAStack->loopStart();
1555 return true;
1556 }
1557 if ((DSAStack->getPossiblyLoopCunter() == D->getCanonicalDecl() ||
1558 DSAStack->isLoopControlVariable(D).first) &&
1559 !DSAStack->hasExplicitDSA(
1560 D, [](OpenMPClauseKind K) { return K != OMPC_private; }, Level) &&
1561 !isOpenMPSimdDirective(DSAStack->getCurrentDirective()))
1562 return true;
1563 }
Alexey Bataevaac108a2015-06-23 04:51:00 +00001564 return DSAStack->hasExplicitDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00001565 D, [](OpenMPClauseKind K) { return K == OMPC_private; }, Level) ||
Alexey Bataev3f82cfc2017-12-13 15:28:44 +00001566 (DSAStack->isClauseParsingMode() &&
1567 DSAStack->getClauseParsingMode() == OMPC_private) ||
Alexey Bataev88202be2017-07-27 13:20:36 +00001568 // Consider taskgroup reduction descriptor variable a private to avoid
1569 // possible capture in the region.
1570 (DSAStack->hasExplicitDirective(
1571 [](OpenMPDirectiveKind K) { return K == OMPD_taskgroup; },
1572 Level) &&
1573 DSAStack->isTaskgroupReductionRef(D, Level));
Alexey Bataevaac108a2015-06-23 04:51:00 +00001574}
1575
Alexey Bataeve3727102018-04-18 15:57:46 +00001576void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D,
1577 unsigned Level) {
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001578 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1579 D = getCanonicalDecl(D);
1580 OpenMPClauseKind OMPC = OMPC_unknown;
1581 for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) {
1582 const unsigned NewLevel = I - 1;
1583 if (DSAStack->hasExplicitDSA(D,
1584 [&OMPC](const OpenMPClauseKind K) {
1585 if (isOpenMPPrivate(K)) {
1586 OMPC = K;
1587 return true;
1588 }
1589 return false;
1590 },
1591 NewLevel))
1592 break;
1593 if (DSAStack->checkMappableExprComponentListsForDeclAtLevel(
1594 D, NewLevel,
1595 [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
1596 OpenMPClauseKind) { return true; })) {
1597 OMPC = OMPC_map;
1598 break;
1599 }
1600 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1601 NewLevel)) {
Alexey Bataev63cc8e92018-03-20 14:45:59 +00001602 OMPC = OMPC_map;
1603 if (D->getType()->isScalarType() &&
1604 DSAStack->getDefaultDMAAtLevel(NewLevel) !=
1605 DefaultMapAttributes::DMA_tofrom_scalar)
1606 OMPC = OMPC_firstprivate;
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001607 break;
1608 }
1609 }
1610 if (OMPC != OMPC_unknown)
1611 FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, OMPC));
1612}
1613
Alexey Bataeve3727102018-04-18 15:57:46 +00001614bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D,
1615 unsigned Level) const {
Samuel Antao4be30e92015-10-02 17:14:03 +00001616 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1617 // Return true if the current level is no longer enclosed in a target region.
1618
Alexey Bataeve3727102018-04-18 15:57:46 +00001619 const auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001620 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00001621 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1622 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +00001623}
1624
Alexey Bataeved09d242014-05-28 05:53:51 +00001625void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001626
1627void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
1628 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001629 Scope *CurScope, SourceLocation Loc) {
1630 DSAStack->push(DKind, DirName, CurScope, Loc);
Faisal Valid143a0c2017-04-01 21:30:49 +00001631 PushExpressionEvaluationContext(
1632 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001633}
1634
Alexey Bataevaac108a2015-06-23 04:51:00 +00001635void Sema::StartOpenMPClause(OpenMPClauseKind K) {
1636 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001637}
1638
Alexey Bataevaac108a2015-06-23 04:51:00 +00001639void Sema::EndOpenMPClause() {
1640 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001641}
1642
Alexey Bataev758e55e2013-09-06 18:03:48 +00001643void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001644 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
1645 // A variable of class type (or array thereof) that appears in a lastprivate
1646 // clause requires an accessible, unambiguous default constructor for the
1647 // class type, unless the list item is also specified in a firstprivate
1648 // clause.
Alexey Bataeve3727102018-04-18 15:57:46 +00001649 if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
1650 for (OMPClause *C : D->clauses()) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001651 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
1652 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeve3727102018-04-18 15:57:46 +00001653 for (Expr *DE : Clause->varlists()) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001654 if (DE->isValueDependent() || DE->isTypeDependent()) {
1655 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001656 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +00001657 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00001658 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
Alexey Bataeve3727102018-04-18 15:57:46 +00001659 auto *VD = cast<VarDecl>(DRE->getDecl());
Alexey Bataev005248a2016-02-25 05:25:57 +00001660 QualType Type = VD->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +00001661 const DSAStackTy::DSAVarData DVar =
1662 DSAStack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001663 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001664 // Generate helper private variable and initialize it with the
1665 // default value. The address of the original variable is replaced
1666 // by the address of the new private variable in CodeGen. This new
1667 // variable is not added to IdResolver, so the code in the OpenMP
1668 // region uses original variable for proper diagnostics.
Alexey Bataeve3727102018-04-18 15:57:46 +00001669 VarDecl *VDPrivate = buildVarDecl(
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001670 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev63cc8e92018-03-20 14:45:59 +00001671 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE);
Richard Smith3beb7c62017-01-12 02:27:38 +00001672 ActOnUninitializedDecl(VDPrivate);
Alexey Bataev38e89532015-04-16 04:54:05 +00001673 if (VDPrivate->isInvalidDecl())
1674 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001675 PrivateCopies.push_back(buildDeclRefExpr(
1676 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001677 } else {
1678 // The variable is also a firstprivate, so initialization sequence
1679 // for private copy is generated already.
1680 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001681 }
1682 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001683 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001684 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001685 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001686 }
1687 }
1688 }
1689
Alexey Bataev758e55e2013-09-06 18:03:48 +00001690 DSAStack->pop();
1691 DiscardCleanupsInEvaluationContext();
1692 PopExpressionEvaluationContext();
1693}
1694
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001695static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1696 Expr *NumIterations, Sema &SemaRef,
1697 Scope *S, DSAStackTy *Stack);
Alexander Musman3276a272015-03-21 10:12:56 +00001698
Alexey Bataeva769e072013-03-22 06:34:35 +00001699namespace {
1700
Alexey Bataeve3727102018-04-18 15:57:46 +00001701class VarDeclFilterCCC final : public CorrectionCandidateCallback {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001702private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001703 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001704
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001705public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001706 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001707 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001708 NamedDecl *ND = Candidate.getCorrectionDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +00001709 if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001710 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001711 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1712 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001713 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001714 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001715 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001716};
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001717
Alexey Bataeve3727102018-04-18 15:57:46 +00001718class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001719private:
1720 Sema &SemaRef;
1721
1722public:
1723 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
1724 bool ValidateCandidate(const TypoCorrection &Candidate) override {
1725 NamedDecl *ND = Candidate.getCorrectionDecl();
Kelvin Li59e3d192017-11-30 18:52:06 +00001726 if (ND && (isa<VarDecl>(ND) || isa<FunctionDecl>(ND))) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001727 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1728 SemaRef.getCurScope());
1729 }
1730 return false;
1731 }
1732};
1733
Alexey Bataeved09d242014-05-28 05:53:51 +00001734} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001735
1736ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1737 CXXScopeSpec &ScopeSpec,
1738 const DeclarationNameInfo &Id) {
1739 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1740 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1741
1742 if (Lookup.isAmbiguous())
1743 return ExprError();
1744
1745 VarDecl *VD;
1746 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001747 if (TypoCorrection Corrected = CorrectTypo(
1748 Id, LookupOrdinaryName, CurScope, nullptr,
1749 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001750 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001751 PDiag(Lookup.empty()
1752 ? diag::err_undeclared_var_use_suggest
1753 : diag::err_omp_expected_var_arg_suggest)
1754 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001755 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001756 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001757 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1758 : diag::err_omp_expected_var_arg)
1759 << Id.getName();
1760 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001761 }
Alexey Bataeve3727102018-04-18 15:57:46 +00001762 } else if (!(VD = Lookup.getAsSingle<VarDecl>())) {
1763 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
1764 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1765 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001766 }
1767 Lookup.suppressDiagnostics();
1768
1769 // OpenMP [2.9.2, Syntax, C/C++]
1770 // Variables must be file-scope, namespace-scope, or static block-scope.
1771 if (!VD->hasGlobalStorage()) {
1772 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001773 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1774 bool IsDecl =
1775 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001776 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001777 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1778 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001779 return ExprError();
1780 }
1781
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001782 VarDecl *CanonicalVD = VD->getCanonicalDecl();
George Burgess IV00f70bd2018-03-01 05:43:23 +00001783 NamedDecl *ND = CanonicalVD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001784 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1785 // A threadprivate directive for file-scope variables must appear outside
1786 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001787 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1788 !getCurLexicalContext()->isTranslationUnit()) {
1789 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001790 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1791 bool IsDecl =
1792 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1793 Diag(VD->getLocation(),
1794 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1795 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001796 return ExprError();
1797 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001798 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1799 // A threadprivate directive for static class member variables must appear
1800 // in the class definition, in the same scope in which the member
1801 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001802 if (CanonicalVD->isStaticDataMember() &&
1803 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1804 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001805 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1806 bool IsDecl =
1807 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1808 Diag(VD->getLocation(),
1809 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1810 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001811 return ExprError();
1812 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001813 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1814 // A threadprivate directive for namespace-scope variables must appear
1815 // outside any definition or declaration other than the namespace
1816 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001817 if (CanonicalVD->getDeclContext()->isNamespace() &&
1818 (!getCurLexicalContext()->isFileContext() ||
1819 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1820 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001821 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1822 bool IsDecl =
1823 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1824 Diag(VD->getLocation(),
1825 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1826 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001827 return ExprError();
1828 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001829 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1830 // A threadprivate directive for static block-scope variables must appear
1831 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001832 if (CanonicalVD->isStaticLocal() && CurScope &&
1833 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001834 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001835 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1836 bool IsDecl =
1837 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1838 Diag(VD->getLocation(),
1839 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1840 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001841 return ExprError();
1842 }
1843
1844 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1845 // A threadprivate directive must lexically precede all references to any
1846 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001847 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001848 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001849 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001850 return ExprError();
1851 }
1852
1853 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev376b4a42016-02-09 09:41:09 +00001854 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1855 SourceLocation(), VD,
1856 /*RefersToEnclosingVariableOrCapture=*/false,
1857 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001858}
1859
Alexey Bataeved09d242014-05-28 05:53:51 +00001860Sema::DeclGroupPtrTy
1861Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1862 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001863 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001864 CurContext->addDecl(D);
1865 return DeclGroupPtrTy::make(DeclGroupRef(D));
1866 }
David Blaikie0403cb12016-01-15 23:43:25 +00001867 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001868}
1869
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001870namespace {
Alexey Bataeve3727102018-04-18 15:57:46 +00001871class LocalVarRefChecker final
1872 : public ConstStmtVisitor<LocalVarRefChecker, bool> {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001873 Sema &SemaRef;
1874
1875public:
1876 bool VisitDeclRefExpr(const DeclRefExpr *E) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001877 if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001878 if (VD->hasLocalStorage()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001879 SemaRef.Diag(E->getBeginLoc(),
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001880 diag::err_omp_local_var_in_threadprivate_init)
1881 << E->getSourceRange();
1882 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1883 << VD << VD->getSourceRange();
1884 return true;
1885 }
1886 }
1887 return false;
1888 }
1889 bool VisitStmt(const Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001890 for (const Stmt *Child : S->children()) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001891 if (Child && Visit(Child))
1892 return true;
1893 }
1894 return false;
1895 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001896 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001897};
1898} // namespace
1899
Alexey Bataeved09d242014-05-28 05:53:51 +00001900OMPThreadPrivateDecl *
1901Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001902 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +00001903 for (Expr *RefExpr : VarList) {
1904 auto *DE = cast<DeclRefExpr>(RefExpr);
1905 auto *VD = cast<VarDecl>(DE->getDecl());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001906 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001907
Alexey Bataev376b4a42016-02-09 09:41:09 +00001908 // Mark variable as used.
1909 VD->setReferenced();
1910 VD->markUsed(Context);
1911
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001912 QualType QType = VD->getType();
1913 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1914 // It will be analyzed later.
1915 Vars.push_back(DE);
1916 continue;
1917 }
1918
Alexey Bataeva769e072013-03-22 06:34:35 +00001919 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1920 // A threadprivate variable must not have an incomplete type.
1921 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001922 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001923 continue;
1924 }
1925
1926 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1927 // A threadprivate variable must not have a reference type.
1928 if (VD->getType()->isReferenceType()) {
1929 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001930 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1931 bool IsDecl =
1932 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1933 Diag(VD->getLocation(),
1934 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1935 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001936 continue;
1937 }
1938
Samuel Antaof8b50122015-07-13 22:54:53 +00001939 // Check if this is a TLS variable. If TLS is not being supported, produce
1940 // the corresponding diagnostic.
1941 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1942 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1943 getLangOpts().OpenMPUseTLS &&
1944 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001945 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1946 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001947 Diag(ILoc, diag::err_omp_var_thread_local)
1948 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001949 bool IsDecl =
1950 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1951 Diag(VD->getLocation(),
1952 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1953 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001954 continue;
1955 }
1956
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001957 // Check if initial value of threadprivate variable reference variable with
1958 // local storage (it is not supported by runtime).
Alexey Bataeve3727102018-04-18 15:57:46 +00001959 if (const Expr *Init = VD->getAnyInitializer()) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001960 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001961 if (Checker.Visit(Init))
1962 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001963 }
1964
Alexey Bataeved09d242014-05-28 05:53:51 +00001965 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001966 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001967 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1968 Context, SourceRange(Loc, Loc)));
Alexey Bataeve3727102018-04-18 15:57:46 +00001969 if (ASTMutationListener *ML = Context.getASTMutationListener())
Alexey Bataev97720002014-11-11 04:05:39 +00001970 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001971 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001972 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001973 if (!Vars.empty()) {
1974 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1975 Vars);
1976 D->setAccess(AS_public);
1977 }
1978 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001979}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001980
Kelvin Li1408f912018-09-26 04:28:39 +00001981Sema::DeclGroupPtrTy
1982Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc,
1983 ArrayRef<OMPClause *> ClauseList) {
1984 OMPRequiresDecl *D = nullptr;
1985 if (!CurContext->isFileContext()) {
1986 Diag(Loc, diag::err_omp_invalid_scope) << "requires";
1987 } else {
1988 D = CheckOMPRequiresDecl(Loc, ClauseList);
1989 if (D) {
1990 CurContext->addDecl(D);
1991 DSAStack->addRequiresDecl(D);
1992 }
1993 }
1994 return DeclGroupPtrTy::make(DeclGroupRef(D));
1995}
1996
1997OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc,
1998 ArrayRef<OMPClause *> ClauseList) {
1999 if (!DSAStack->hasDuplicateRequiresClause(ClauseList))
2000 return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc,
2001 ClauseList);
2002 return nullptr;
2003}
2004
Alexey Bataeve3727102018-04-18 15:57:46 +00002005static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
2006 const ValueDecl *D,
2007 const DSAStackTy::DSAVarData &DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00002008 bool IsLoopIterVar = false) {
2009 if (DVar.RefExpr) {
2010 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
2011 << getOpenMPClauseName(DVar.CKind);
2012 return;
2013 }
2014 enum {
2015 PDSA_StaticMemberShared,
2016 PDSA_StaticLocalVarShared,
2017 PDSA_LoopIterVarPrivate,
2018 PDSA_LoopIterVarLinear,
2019 PDSA_LoopIterVarLastprivate,
2020 PDSA_ConstVarShared,
2021 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002022 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00002023 PDSA_LocalVarPrivate,
2024 PDSA_Implicit
2025 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00002026 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002027 auto ReportLoc = D->getLocation();
2028 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002029 if (IsLoopIterVar) {
2030 if (DVar.CKind == OMPC_private)
2031 Reason = PDSA_LoopIterVarPrivate;
2032 else if (DVar.CKind == OMPC_lastprivate)
2033 Reason = PDSA_LoopIterVarLastprivate;
2034 else
2035 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev35aaee62016-04-13 13:36:48 +00002036 } else if (isOpenMPTaskingDirective(DVar.DKind) &&
2037 DVar.CKind == OMPC_firstprivate) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002038 Reason = PDSA_TaskVarFirstprivate;
2039 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002040 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00002041 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002042 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00002043 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002044 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00002045 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002046 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00002047 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002048 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00002049 ReportHint = true;
2050 Reason = PDSA_LocalVarPrivate;
2051 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00002052 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002053 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00002054 << Reason << ReportHint
2055 << getOpenMPDirectiveName(Stack->getCurrentDirective());
2056 } else if (DVar.ImplicitDSALoc.isValid()) {
2057 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
2058 << getOpenMPClauseName(DVar.CKind);
2059 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00002060}
2061
Alexey Bataev758e55e2013-09-06 18:03:48 +00002062namespace {
Alexey Bataeve3727102018-04-18 15:57:46 +00002063class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> {
Alexey Bataev758e55e2013-09-06 18:03:48 +00002064 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00002065 Sema &SemaRef;
Alexey Bataeve3727102018-04-18 15:57:46 +00002066 bool ErrorFound = false;
2067 CapturedStmt *CS = nullptr;
2068 llvm::SmallVector<Expr *, 4> ImplicitFirstprivate;
2069 llvm::SmallVector<Expr *, 4> ImplicitMap;
2070 Sema::VarsWithInheritedDSAType VarsWithInheritedDSA;
2071 llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations;
Alexey Bataeved09d242014-05-28 05:53:51 +00002072
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00002073 void VisitSubCaptures(OMPExecutableDirective *S) {
2074 // Check implicitly captured variables.
2075 if (!S->hasAssociatedStmt() || !S->getAssociatedStmt())
2076 return;
2077 for (const CapturedStmt::Capture &Cap :
2078 S->getInnermostCapturedStmt()->captures()) {
2079 if (!Cap.capturesVariable())
2080 continue;
2081 VarDecl *VD = Cap.getCapturedVar();
2082 // Do not try to map the variable if it or its sub-component was mapped
2083 // already.
2084 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) &&
2085 Stack->checkMappableExprComponentListsForDecl(
2086 VD, /*CurrentRegionOnly=*/true,
2087 [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
2088 OpenMPClauseKind) { return true; }))
2089 continue;
2090 DeclRefExpr *DRE = buildDeclRefExpr(
2091 SemaRef, VD, VD->getType().getNonLValueExprType(SemaRef.Context),
2092 Cap.getLocation(), /*RefersToCapture=*/true);
2093 Visit(DRE);
2094 }
2095 }
2096
Alexey Bataev758e55e2013-09-06 18:03:48 +00002097public:
2098 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00002099 if (E->isTypeDependent() || E->isValueDependent() ||
2100 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
2101 return;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002102 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00002103 VD = VD->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002104 // Skip internally declared variables.
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002105 if (VD->hasLocalStorage() && !CS->capturesVariable(VD))
Alexey Bataeved09d242014-05-28 05:53:51 +00002106 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002107
Alexey Bataeve3727102018-04-18 15:57:46 +00002108 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002109 // Check if the variable has explicit DSA set and stop analysis if it so.
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00002110 if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second)
David Majnemer9d168222016-08-05 17:44:54 +00002111 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002112
Alexey Bataevafe50572017-10-06 17:00:28 +00002113 // Skip internally declared static variables.
Alexey Bataev92327c52018-03-26 16:40:55 +00002114 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
Alexey Bataev97b72212018-08-14 18:31:20 +00002115 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
Alexey Bataev92327c52018-03-26 16:40:55 +00002116 if (VD->hasGlobalStorage() && !CS->capturesVariable(VD) &&
2117 (!Res || *Res != OMPDeclareTargetDeclAttr::MT_Link))
Alexey Bataevafe50572017-10-06 17:00:28 +00002118 return;
2119
Alexey Bataeve3727102018-04-18 15:57:46 +00002120 SourceLocation ELoc = E->getExprLoc();
2121 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002122 // The default(none) clause requires that each variable that is referenced
2123 // in the construct, and does not have a predetermined data-sharing
2124 // attribute, must have its data-sharing attribute explicitly determined
2125 // by being listed in a data-sharing attribute clause.
2126 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002127 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00002128 VarsWithInheritedDSA.count(VD) == 0) {
2129 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002130 return;
2131 }
2132
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002133 if (isOpenMPTargetExecutionDirective(DKind) &&
2134 !Stack->isLoopControlVariable(VD).first) {
2135 if (!Stack->checkMappableExprComponentListsForDecl(
2136 VD, /*CurrentRegionOnly=*/true,
2137 [](OMPClauseMappableExprCommon::MappableExprComponentListRef
2138 StackComponents,
2139 OpenMPClauseKind) {
2140 // Variable is used if it has been marked as an array, array
2141 // section or the variable iself.
2142 return StackComponents.size() == 1 ||
2143 std::all_of(
2144 std::next(StackComponents.rbegin()),
2145 StackComponents.rend(),
2146 [](const OMPClauseMappableExprCommon::
2147 MappableComponent &MC) {
2148 return MC.getAssociatedDeclaration() ==
2149 nullptr &&
2150 (isa<OMPArraySectionExpr>(
2151 MC.getAssociatedExpression()) ||
2152 isa<ArraySubscriptExpr>(
2153 MC.getAssociatedExpression()));
2154 });
2155 })) {
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002156 bool IsFirstprivate = false;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002157 // By default lambdas are captured as firstprivates.
2158 if (const auto *RD =
2159 VD->getType().getNonReferenceType()->getAsCXXRecordDecl())
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002160 IsFirstprivate = RD->isLambda();
2161 IsFirstprivate =
2162 IsFirstprivate ||
2163 (VD->getType().getNonReferenceType()->isScalarType() &&
Alexey Bataev92327c52018-03-26 16:40:55 +00002164 Stack->getDefaultDMA() != DMA_tofrom_scalar && !Res);
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002165 if (IsFirstprivate)
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002166 ImplicitFirstprivate.emplace_back(E);
2167 else
2168 ImplicitMap.emplace_back(E);
2169 return;
2170 }
2171 }
2172
Alexey Bataev758e55e2013-09-06 18:03:48 +00002173 // OpenMP [2.9.3.6, Restrictions, p.2]
2174 // A list item that appears in a reduction clause of the innermost
2175 // enclosing worksharing or parallel construct may not be accessed in an
2176 // explicit task.
Alexey Bataev7ace49d2016-05-17 08:55:33 +00002177 DVar = Stack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00002178 VD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
2179 [](OpenMPDirectiveKind K) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00002180 return isOpenMPParallelDirective(K) ||
2181 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
2182 },
Alexey Bataeveffbdf12017-07-21 17:24:30 +00002183 /*FromParent=*/true);
Alexey Bataev35aaee62016-04-13 13:36:48 +00002184 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00002185 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00002186 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
Alexey Bataeve3727102018-04-18 15:57:46 +00002187 reportOriginalDsa(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002188 return;
2189 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002190
2191 // Define implicit data-sharing attributes for task.
Alexey Bataeve3727102018-04-18 15:57:46 +00002192 DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00002193 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
2194 !Stack->isLoopControlVariable(VD).first)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002195 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002196 }
2197 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002198 void VisitMemberExpr(MemberExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00002199 if (E->isTypeDependent() || E->isValueDependent() ||
2200 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
2201 return;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002202 auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002203 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002204 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002205 if (!FD)
2206 return;
Alexey Bataeve3727102018-04-18 15:57:46 +00002207 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002208 // Check if the variable has explicit DSA set and stop analysis if it
2209 // so.
2210 if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second)
2211 return;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002212
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002213 if (isOpenMPTargetExecutionDirective(DKind) &&
2214 !Stack->isLoopControlVariable(FD).first &&
2215 !Stack->checkMappableExprComponentListsForDecl(
2216 FD, /*CurrentRegionOnly=*/true,
2217 [](OMPClauseMappableExprCommon::MappableExprComponentListRef
2218 StackComponents,
2219 OpenMPClauseKind) {
2220 return isa<CXXThisExpr>(
2221 cast<MemberExpr>(
2222 StackComponents.back().getAssociatedExpression())
2223 ->getBase()
2224 ->IgnoreParens());
2225 })) {
2226 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
2227 // A bit-field cannot appear in a map clause.
2228 //
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002229 if (FD->isBitField())
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002230 return;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002231 ImplicitMap.emplace_back(E);
2232 return;
2233 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002234
Alexey Bataeve3727102018-04-18 15:57:46 +00002235 SourceLocation ELoc = E->getExprLoc();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002236 // OpenMP [2.9.3.6, Restrictions, p.2]
2237 // A list item that appears in a reduction clause of the innermost
2238 // enclosing worksharing or parallel construct may not be accessed in
2239 // an explicit task.
2240 DVar = Stack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00002241 FD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
2242 [](OpenMPDirectiveKind K) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002243 return isOpenMPParallelDirective(K) ||
2244 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
2245 },
2246 /*FromParent=*/true);
2247 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
2248 ErrorFound = true;
2249 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
Alexey Bataeve3727102018-04-18 15:57:46 +00002250 reportOriginalDsa(SemaRef, Stack, FD, DVar);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002251 return;
2252 }
2253
2254 // Define implicit data-sharing attributes for task.
Alexey Bataeve3727102018-04-18 15:57:46 +00002255 DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002256 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
Alexey Bataevb40e05202018-10-24 18:53:12 +00002257 !Stack->isLoopControlVariable(FD).first) {
2258 // Check if there is a captured expression for the current field in the
2259 // region. Do not mark it as firstprivate unless there is no captured
2260 // expression.
2261 // TODO: try to make it firstprivate.
2262 if (DVar.CKind != OMPC_unknown)
2263 ImplicitFirstprivate.push_back(E);
2264 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002265 return;
2266 }
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002267 if (isOpenMPTargetExecutionDirective(DKind)) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002268 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
Alexey Bataeve3727102018-04-18 15:57:46 +00002269 if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map,
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002270 /*NoDiagnose=*/true))
Alexey Bataev27041fa2017-12-05 15:22:49 +00002271 return;
Alexey Bataeve3727102018-04-18 15:57:46 +00002272 const auto *VD = cast<ValueDecl>(
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002273 CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl());
2274 if (!Stack->checkMappableExprComponentListsForDecl(
2275 VD, /*CurrentRegionOnly=*/true,
2276 [&CurComponents](
2277 OMPClauseMappableExprCommon::MappableExprComponentListRef
2278 StackComponents,
2279 OpenMPClauseKind) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002280 auto CCI = CurComponents.rbegin();
Alexey Bataev5ec38932017-09-26 16:19:04 +00002281 auto CCE = CurComponents.rend();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002282 for (const auto &SC : llvm::reverse(StackComponents)) {
2283 // Do both expressions have the same kind?
2284 if (CCI->getAssociatedExpression()->getStmtClass() !=
2285 SC.getAssociatedExpression()->getStmtClass())
2286 if (!(isa<OMPArraySectionExpr>(
2287 SC.getAssociatedExpression()) &&
2288 isa<ArraySubscriptExpr>(
2289 CCI->getAssociatedExpression())))
2290 return false;
2291
Alexey Bataeve3727102018-04-18 15:57:46 +00002292 const Decl *CCD = CCI->getAssociatedDeclaration();
2293 const Decl *SCD = SC.getAssociatedDeclaration();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002294 CCD = CCD ? CCD->getCanonicalDecl() : nullptr;
2295 SCD = SCD ? SCD->getCanonicalDecl() : nullptr;
2296 if (SCD != CCD)
2297 return false;
2298 std::advance(CCI, 1);
Alexey Bataev5ec38932017-09-26 16:19:04 +00002299 if (CCI == CCE)
2300 break;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002301 }
2302 return true;
2303 })) {
2304 Visit(E->getBase());
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002305 }
Alexey Bataeve3727102018-04-18 15:57:46 +00002306 } else {
Alexey Bataev7fcacd82016-11-28 15:55:15 +00002307 Visit(E->getBase());
Alexey Bataeve3727102018-04-18 15:57:46 +00002308 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002309 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002310 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002311 for (OMPClause *C : S->clauses()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002312 // Skip analysis of arguments of implicitly defined firstprivate clause
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002313 // for task|target directives.
2314 // Skip analysis of arguments of implicitly defined map clause for target
2315 // directives.
2316 if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) &&
2317 C->isImplicit())) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002318 for (Stmt *CC : C->children()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002319 if (CC)
2320 Visit(CC);
2321 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002322 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002323 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002324 }
2325 void VisitStmt(Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002326 for (Stmt *C : S->children()) {
Alexey Bataev8fc7b5f2018-10-25 15:35:27 +00002327 if (C) {
2328 if (auto *OED = dyn_cast<OMPExecutableDirective>(C)) {
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00002329 // Check implicitly captured variables in the task-based directives to
Alexey Bataev8fc7b5f2018-10-25 15:35:27 +00002330 // check if they must be firstprivatized.
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00002331 VisitSubCaptures(OED);
Alexey Bataev8fc7b5f2018-10-25 15:35:27 +00002332 } else {
2333 Visit(C);
2334 }
2335 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002336 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002337 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002338
Alexey Bataeve3727102018-04-18 15:57:46 +00002339 bool isErrorFound() const { return ErrorFound; }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002340 ArrayRef<Expr *> getImplicitFirstprivate() const {
2341 return ImplicitFirstprivate;
2342 }
2343 ArrayRef<Expr *> getImplicitMap() const { return ImplicitMap; }
Alexey Bataeve3727102018-04-18 15:57:46 +00002344 const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const {
Alexey Bataev4acb8592014-07-07 13:01:15 +00002345 return VarsWithInheritedDSA;
2346 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002347
Alexey Bataev7ff55242014-06-19 09:13:45 +00002348 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
2349 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00002350};
Alexey Bataeved09d242014-05-28 05:53:51 +00002351} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00002352
Alexey Bataevbae9a792014-06-27 10:37:06 +00002353void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00002354 switch (DKind) {
Kelvin Li70a12c52016-07-13 21:51:49 +00002355 case OMPD_parallel:
2356 case OMPD_parallel_for:
2357 case OMPD_parallel_for_simd:
2358 case OMPD_parallel_sections:
Carlo Bertolliba1487b2017-10-04 14:12:09 +00002359 case OMPD_teams:
Alexey Bataev999277a2017-12-06 14:31:09 +00002360 case OMPD_teams_distribute:
2361 case OMPD_teams_distribute_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002362 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Alexey Bataev2377fe92015-09-10 08:12:02 +00002363 QualType KmpInt32PtrTy =
2364 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002365 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002366 std::make_pair(".global_tid.", KmpInt32PtrTy),
2367 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2368 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00002369 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00002370 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2371 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00002372 break;
2373 }
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002374 case OMPD_target_teams:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00002375 case OMPD_target_parallel:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00002376 case OMPD_target_parallel_for:
Alexey Bataevdfa430f2017-12-08 15:03:50 +00002377 case OMPD_target_parallel_for_simd:
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00002378 case OMPD_target_teams_distribute:
2379 case OMPD_target_teams_distribute_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002380 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2381 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2382 QualType KmpInt32PtrTy =
2383 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2384 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00002385 FunctionProtoType::ExtProtoInfo EPI;
2386 EPI.Variadic = true;
2387 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2388 Sema::CapturedParamNameType Params[] = {
2389 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002390 std::make_pair(".part_id.", KmpInt32PtrTy),
2391 std::make_pair(".privates.", VoidPtrTy),
2392 std::make_pair(
2393 ".copy_fn.",
2394 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00002395 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2396 std::make_pair(StringRef(), QualType()) // __context with shared vars
2397 };
2398 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2399 Params);
Alexey Bataev0c869ef2018-01-16 15:57:07 +00002400 // Mark this captured region as inlined, because we don't use outlined
2401 // function directly.
2402 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2403 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002404 Context, AlwaysInlineAttr::Keyword_forceinline));
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002405 Sema::CapturedParamNameType ParamsTarget[] = {
2406 std::make_pair(StringRef(), QualType()) // __context with shared vars
2407 };
2408 // Start a captured region for 'target' with no implicit parameters.
2409 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2410 ParamsTarget);
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002411 Sema::CapturedParamNameType ParamsTeamsOrParallel[] = {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002412 std::make_pair(".global_tid.", KmpInt32PtrTy),
2413 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2414 std::make_pair(StringRef(), QualType()) // __context with shared vars
2415 };
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002416 // Start a captured region for 'teams' or 'parallel'. Both regions have
2417 // the same implicit parameters.
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002418 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002419 ParamsTeamsOrParallel);
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002420 break;
2421 }
Alexey Bataev8451efa2018-01-15 19:06:12 +00002422 case OMPD_target:
2423 case OMPD_target_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002424 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2425 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2426 QualType KmpInt32PtrTy =
2427 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2428 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00002429 FunctionProtoType::ExtProtoInfo EPI;
2430 EPI.Variadic = true;
2431 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2432 Sema::CapturedParamNameType Params[] = {
2433 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002434 std::make_pair(".part_id.", KmpInt32PtrTy),
2435 std::make_pair(".privates.", VoidPtrTy),
2436 std::make_pair(
2437 ".copy_fn.",
2438 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00002439 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2440 std::make_pair(StringRef(), QualType()) // __context with shared vars
2441 };
2442 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2443 Params);
2444 // Mark this captured region as inlined, because we don't use outlined
2445 // function directly.
2446 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2447 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002448 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev8451efa2018-01-15 19:06:12 +00002449 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2450 std::make_pair(StringRef(), QualType()));
2451 break;
2452 }
Kelvin Li70a12c52016-07-13 21:51:49 +00002453 case OMPD_simd:
2454 case OMPD_for:
2455 case OMPD_for_simd:
2456 case OMPD_sections:
2457 case OMPD_section:
2458 case OMPD_single:
2459 case OMPD_master:
2460 case OMPD_critical:
Kelvin Lia579b912016-07-14 02:54:56 +00002461 case OMPD_taskgroup:
2462 case OMPD_distribute:
Alexey Bataev46506272017-12-05 17:41:34 +00002463 case OMPD_distribute_simd:
Kelvin Li70a12c52016-07-13 21:51:49 +00002464 case OMPD_ordered:
2465 case OMPD_atomic:
Alexey Bataev8451efa2018-01-15 19:06:12 +00002466 case OMPD_target_data: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002467 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002468 std::make_pair(StringRef(), QualType()) // __context with shared vars
2469 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00002470 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2471 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002472 break;
2473 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002474 case OMPD_task: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002475 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2476 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2477 QualType KmpInt32PtrTy =
2478 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2479 QualType Args[] = {VoidPtrTy};
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002480 FunctionProtoType::ExtProtoInfo EPI;
2481 EPI.Variadic = true;
2482 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002483 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00002484 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002485 std::make_pair(".part_id.", KmpInt32PtrTy),
2486 std::make_pair(".privates.", VoidPtrTy),
2487 std::make_pair(
2488 ".copy_fn.",
2489 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev48591dd2016-04-20 04:01:36 +00002490 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002491 std::make_pair(StringRef(), QualType()) // __context with shared vars
2492 };
2493 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2494 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00002495 // Mark this captured region as inlined, because we don't use outlined
2496 // function directly.
2497 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2498 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002499 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002500 break;
2501 }
Alexey Bataev1e73ef32016-04-28 12:14:51 +00002502 case OMPD_taskloop:
2503 case OMPD_taskloop_simd: {
Alexey Bataev7292c292016-04-25 12:22:29 +00002504 QualType KmpInt32Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002505 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1)
2506 .withConst();
Alexey Bataev7292c292016-04-25 12:22:29 +00002507 QualType KmpUInt64Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002508 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0)
2509 .withConst();
Alexey Bataev7292c292016-04-25 12:22:29 +00002510 QualType KmpInt64Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002511 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1)
2512 .withConst();
2513 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2514 QualType KmpInt32PtrTy =
2515 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2516 QualType Args[] = {VoidPtrTy};
Alexey Bataev7292c292016-04-25 12:22:29 +00002517 FunctionProtoType::ExtProtoInfo EPI;
2518 EPI.Variadic = true;
2519 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev49f6e782015-12-01 04:18:41 +00002520 Sema::CapturedParamNameType Params[] = {
Alexey Bataev7292c292016-04-25 12:22:29 +00002521 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002522 std::make_pair(".part_id.", KmpInt32PtrTy),
2523 std::make_pair(".privates.", VoidPtrTy),
Alexey Bataev7292c292016-04-25 12:22:29 +00002524 std::make_pair(
2525 ".copy_fn.",
2526 Context.getPointerType(CopyFnType).withConst().withRestrict()),
2527 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2528 std::make_pair(".lb.", KmpUInt64Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002529 std::make_pair(".ub.", KmpUInt64Ty),
2530 std::make_pair(".st.", KmpInt64Ty),
Alexey Bataev7292c292016-04-25 12:22:29 +00002531 std::make_pair(".liter.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002532 std::make_pair(".reductions.", VoidPtrTy),
Alexey Bataev49f6e782015-12-01 04:18:41 +00002533 std::make_pair(StringRef(), QualType()) // __context with shared vars
2534 };
2535 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2536 Params);
Alexey Bataev7292c292016-04-25 12:22:29 +00002537 // Mark this captured region as inlined, because we don't use outlined
2538 // function directly.
2539 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2540 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002541 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev49f6e782015-12-01 04:18:41 +00002542 break;
2543 }
Kelvin Li4a39add2016-07-05 05:00:15 +00002544 case OMPD_distribute_parallel_for_simd:
Alexey Bataev647dd842018-01-15 20:59:40 +00002545 case OMPD_distribute_parallel_for: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002546 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli9925f152016-06-27 14:55:37 +00002547 QualType KmpInt32PtrTy =
2548 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2549 Sema::CapturedParamNameType Params[] = {
2550 std::make_pair(".global_tid.", KmpInt32PtrTy),
2551 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002552 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2553 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli9925f152016-06-27 14:55:37 +00002554 std::make_pair(StringRef(), QualType()) // __context with shared vars
2555 };
2556 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2557 Params);
2558 break;
2559 }
Alexey Bataev647dd842018-01-15 20:59:40 +00002560 case OMPD_target_teams_distribute_parallel_for:
2561 case OMPD_target_teams_distribute_parallel_for_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002562 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli52978c32018-01-03 21:12:44 +00002563 QualType KmpInt32PtrTy =
2564 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002565 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
Carlo Bertolli52978c32018-01-03 21:12:44 +00002566
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002567 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00002568 FunctionProtoType::ExtProtoInfo EPI;
2569 EPI.Variadic = true;
2570 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2571 Sema::CapturedParamNameType Params[] = {
2572 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002573 std::make_pair(".part_id.", KmpInt32PtrTy),
2574 std::make_pair(".privates.", VoidPtrTy),
2575 std::make_pair(
2576 ".copy_fn.",
2577 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00002578 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2579 std::make_pair(StringRef(), QualType()) // __context with shared vars
2580 };
2581 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2582 Params);
Alexey Bataev9f9fb0b2018-01-16 19:02:33 +00002583 // Mark this captured region as inlined, because we don't use outlined
2584 // function directly.
2585 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2586 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002587 Context, AlwaysInlineAttr::Keyword_forceinline));
Carlo Bertolli52978c32018-01-03 21:12:44 +00002588 Sema::CapturedParamNameType ParamsTarget[] = {
2589 std::make_pair(StringRef(), QualType()) // __context with shared vars
2590 };
2591 // Start a captured region for 'target' with no implicit parameters.
2592 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2593 ParamsTarget);
2594
2595 Sema::CapturedParamNameType ParamsTeams[] = {
2596 std::make_pair(".global_tid.", KmpInt32PtrTy),
2597 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2598 std::make_pair(StringRef(), QualType()) // __context with shared vars
2599 };
2600 // Start a captured region for 'target' with no implicit parameters.
2601 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2602 ParamsTeams);
2603
2604 Sema::CapturedParamNameType ParamsParallel[] = {
2605 std::make_pair(".global_tid.", KmpInt32PtrTy),
2606 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002607 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2608 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli52978c32018-01-03 21:12:44 +00002609 std::make_pair(StringRef(), QualType()) // __context with shared vars
2610 };
2611 // Start a captured region for 'teams' or 'parallel'. Both regions have
2612 // the same implicit parameters.
2613 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2614 ParamsParallel);
2615 break;
2616 }
2617
Alexey Bataev46506272017-12-05 17:41:34 +00002618 case OMPD_teams_distribute_parallel_for:
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00002619 case OMPD_teams_distribute_parallel_for_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002620 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli62fae152017-11-20 20:46:39 +00002621 QualType KmpInt32PtrTy =
2622 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2623
2624 Sema::CapturedParamNameType ParamsTeams[] = {
2625 std::make_pair(".global_tid.", KmpInt32PtrTy),
2626 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2627 std::make_pair(StringRef(), QualType()) // __context with shared vars
2628 };
2629 // Start a captured region for 'target' with no implicit parameters.
2630 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2631 ParamsTeams);
2632
2633 Sema::CapturedParamNameType ParamsParallel[] = {
2634 std::make_pair(".global_tid.", KmpInt32PtrTy),
2635 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002636 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2637 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli62fae152017-11-20 20:46:39 +00002638 std::make_pair(StringRef(), QualType()) // __context with shared vars
2639 };
2640 // Start a captured region for 'teams' or 'parallel'. Both regions have
2641 // the same implicit parameters.
2642 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2643 ParamsParallel);
2644 break;
2645 }
Alexey Bataev7828b252017-11-21 17:08:48 +00002646 case OMPD_target_update:
2647 case OMPD_target_enter_data:
2648 case OMPD_target_exit_data: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002649 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2650 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2651 QualType KmpInt32PtrTy =
2652 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2653 QualType Args[] = {VoidPtrTy};
Alexey Bataev7828b252017-11-21 17:08:48 +00002654 FunctionProtoType::ExtProtoInfo EPI;
2655 EPI.Variadic = true;
2656 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2657 Sema::CapturedParamNameType Params[] = {
2658 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002659 std::make_pair(".part_id.", KmpInt32PtrTy),
2660 std::make_pair(".privates.", VoidPtrTy),
2661 std::make_pair(
2662 ".copy_fn.",
2663 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev7828b252017-11-21 17:08:48 +00002664 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2665 std::make_pair(StringRef(), QualType()) // __context with shared vars
2666 };
2667 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2668 Params);
2669 // Mark this captured region as inlined, because we don't use outlined
2670 // function directly.
2671 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2672 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002673 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev7828b252017-11-21 17:08:48 +00002674 break;
2675 }
Alexey Bataev9959db52014-05-06 10:08:46 +00002676 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00002677 case OMPD_taskyield:
2678 case OMPD_barrier:
2679 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002680 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00002681 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00002682 case OMPD_flush:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002683 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00002684 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00002685 case OMPD_declare_target:
2686 case OMPD_end_declare_target:
Kelvin Li1408f912018-09-26 04:28:39 +00002687 case OMPD_requires:
Alexey Bataev9959db52014-05-06 10:08:46 +00002688 llvm_unreachable("OpenMP Directive is not allowed");
2689 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00002690 llvm_unreachable("Unknown OpenMP directive");
2691 }
2692}
2693
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002694int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) {
2695 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2696 getOpenMPCaptureRegions(CaptureRegions, DKind);
2697 return CaptureRegions.size();
2698}
2699
Alexey Bataev3392d762016-02-16 11:18:12 +00002700static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00002701 Expr *CaptureExpr, bool WithInit,
2702 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00002703 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00002704 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00002705 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00002706 QualType Ty = Init->getType();
2707 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002708 if (S.getLangOpts().CPlusPlus) {
Alexey Bataev4244be22016-02-11 05:35:55 +00002709 Ty = C.getLValueReferenceType(Ty);
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002710 } else {
Alexey Bataev4244be22016-02-11 05:35:55 +00002711 Ty = C.getPointerType(Ty);
2712 ExprResult Res =
2713 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
2714 if (!Res.isUsable())
2715 return nullptr;
2716 Init = Res.get();
2717 }
Alexey Bataev61205072016-03-02 04:57:40 +00002718 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00002719 }
Alexey Bataeva7206b92016-12-20 16:51:02 +00002720 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty,
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002721 CaptureExpr->getBeginLoc());
Alexey Bataev2bbf7212016-03-03 03:52:24 +00002722 if (!WithInit)
Alexey Bataeve3727102018-04-18 15:57:46 +00002723 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C));
Alexey Bataev4244be22016-02-11 05:35:55 +00002724 S.CurContext->addHiddenDecl(CED);
Richard Smith3beb7c62017-01-12 02:27:38 +00002725 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00002726 return CED;
2727}
2728
Alexey Bataev61205072016-03-02 04:57:40 +00002729static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
2730 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00002731 OMPCapturedExprDecl *CD;
Alexey Bataeve3727102018-04-18 15:57:46 +00002732 if (VarDecl *VD = S.isOpenMPCapturedDecl(D))
Alexey Bataevb7a34b62016-02-25 03:59:29 +00002733 CD = cast<OMPCapturedExprDecl>(VD);
Alexey Bataeve3727102018-04-18 15:57:46 +00002734 else
Alexey Bataev5a3af132016-03-29 08:58:54 +00002735 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
2736 /*AsExpression=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00002737 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00002738 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00002739}
2740
Alexey Bataev5a3af132016-03-29 08:58:54 +00002741static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002742 CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00002743 if (!Ref) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002744 OMPCapturedExprDecl *CD = buildCaptureDecl(
2745 S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr,
2746 /*WithInit=*/true, /*AsExpression=*/true);
Alexey Bataev5a3af132016-03-29 08:58:54 +00002747 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
2748 CaptureExpr->getExprLoc());
2749 }
2750 ExprResult Res = Ref;
2751 if (!S.getLangOpts().CPlusPlus &&
2752 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002753 Ref->getType()->isPointerType()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00002754 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002755 if (!Res.isUsable())
2756 return ExprError();
2757 }
2758 return S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00002759}
2760
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002761namespace {
2762// OpenMP directives parsed in this section are represented as a
2763// CapturedStatement with an associated statement. If a syntax error
2764// is detected during the parsing of the associated statement, the
2765// compiler must abort processing and close the CapturedStatement.
2766//
2767// Combined directives such as 'target parallel' have more than one
2768// nested CapturedStatements. This RAII ensures that we unwind out
2769// of all the nested CapturedStatements when an error is found.
2770class CaptureRegionUnwinderRAII {
2771private:
2772 Sema &S;
2773 bool &ErrorFound;
Alexey Bataeve3727102018-04-18 15:57:46 +00002774 OpenMPDirectiveKind DKind = OMPD_unknown;
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002775
2776public:
2777 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound,
2778 OpenMPDirectiveKind DKind)
2779 : S(S), ErrorFound(ErrorFound), DKind(DKind) {}
2780 ~CaptureRegionUnwinderRAII() {
2781 if (ErrorFound) {
2782 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind);
2783 while (--ThisCaptureLevel >= 0)
2784 S.ActOnCapturedRegionError();
2785 }
2786 }
2787};
2788} // namespace
2789
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002790StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
2791 ArrayRef<OMPClause *> Clauses) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002792 bool ErrorFound = false;
2793 CaptureRegionUnwinderRAII CaptureRegionUnwinder(
2794 *this, ErrorFound, DSAStack->getCurrentDirective());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002795 if (!S.isUsable()) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002796 ErrorFound = true;
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002797 return StmtError();
2798 }
Alexey Bataev993d2802015-12-28 06:23:08 +00002799
Alexey Bataev2ba67042017-11-28 21:11:44 +00002800 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2801 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective());
Alexey Bataev993d2802015-12-28 06:23:08 +00002802 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00002803 OMPScheduleClause *SC = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +00002804 SmallVector<const OMPLinearClause *, 4> LCs;
2805 SmallVector<const OMPClauseWithPreInit *, 4> PICs;
Alexey Bataev040d5402015-05-12 08:35:28 +00002806 // This is required for proper codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00002807 for (OMPClause *Clause : Clauses) {
Alexey Bataev88202be2017-07-27 13:20:36 +00002808 if (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) &&
2809 Clause->getClauseKind() == OMPC_in_reduction) {
2810 // Capture taskgroup task_reduction descriptors inside the tasking regions
2811 // with the corresponding in_reduction items.
2812 auto *IRC = cast<OMPInReductionClause>(Clause);
Alexey Bataeve3727102018-04-18 15:57:46 +00002813 for (Expr *E : IRC->taskgroup_descriptors())
Alexey Bataev88202be2017-07-27 13:20:36 +00002814 if (E)
2815 MarkDeclarationsReferencedInExpr(E);
2816 }
Alexey Bataev16dc7b62015-05-20 03:46:04 +00002817 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00002818 Clause->getClauseKind() == OMPC_copyprivate ||
2819 (getLangOpts().OpenMPUseTLS &&
2820 getASTContext().getTargetInfo().isTLSSupported() &&
2821 Clause->getClauseKind() == OMPC_copyin)) {
2822 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00002823 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeve3727102018-04-18 15:57:46 +00002824 for (Stmt *VarRef : Clause->children()) {
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002825 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00002826 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002827 }
2828 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00002829 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev2ba67042017-11-28 21:11:44 +00002830 } else if (CaptureRegions.size() > 1 ||
2831 CaptureRegions.back() != OMPD_unknown) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002832 if (auto *C = OMPClauseWithPreInit::get(Clause))
2833 PICs.push_back(C);
Alexey Bataev005248a2016-02-25 05:25:57 +00002834 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002835 if (Expr *E = C->getPostUpdateExpr())
Alexey Bataev005248a2016-02-25 05:25:57 +00002836 MarkDeclarationsReferencedInExpr(E);
2837 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002838 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002839 if (Clause->getClauseKind() == OMPC_schedule)
2840 SC = cast<OMPScheduleClause>(Clause);
2841 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00002842 OC = cast<OMPOrderedClause>(Clause);
2843 else if (Clause->getClauseKind() == OMPC_linear)
2844 LCs.push_back(cast<OMPLinearClause>(Clause));
2845 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002846 // OpenMP, 2.7.1 Loop Construct, Restrictions
2847 // The nonmonotonic modifier cannot be specified if an ordered clause is
2848 // specified.
2849 if (SC &&
2850 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
2851 SC->getSecondScheduleModifier() ==
2852 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
2853 OC) {
2854 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
2855 ? SC->getFirstScheduleModifierLoc()
2856 : SC->getSecondScheduleModifierLoc(),
2857 diag::err_omp_schedule_nonmonotonic_ordered)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00002858 << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
Alexey Bataev6402bca2015-12-28 07:25:51 +00002859 ErrorFound = true;
2860 }
Alexey Bataev993d2802015-12-28 06:23:08 +00002861 if (!LCs.empty() && OC && OC->getNumForLoops()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002862 for (const OMPLinearClause *C : LCs) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002863 Diag(C->getBeginLoc(), diag::err_omp_linear_ordered)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00002864 << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
Alexey Bataev993d2802015-12-28 06:23:08 +00002865 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002866 ErrorFound = true;
2867 }
Alexey Bataev113438c2015-12-30 12:06:23 +00002868 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
2869 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
2870 OC->getNumForLoops()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002871 Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd)
Alexey Bataev113438c2015-12-30 12:06:23 +00002872 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
2873 ErrorFound = true;
2874 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002875 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00002876 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002877 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002878 StmtResult SR = S;
Alexey Bataev2ba67042017-11-28 21:11:44 +00002879 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002880 // Mark all variables in private list clauses as used in inner region.
2881 // Required for proper codegen of combined directives.
2882 // TODO: add processing for other clauses.
Alexey Bataev2ba67042017-11-28 21:11:44 +00002883 if (ThisCaptureRegion != OMPD_unknown) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002884 for (const clang::OMPClauseWithPreInit *C : PICs) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002885 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion();
2886 // Find the particular capture region for the clause if the
2887 // directive is a combined one with multiple capture regions.
2888 // If the directive is not a combined one, the capture region
2889 // associated with the clause is OMPD_unknown and is generated
2890 // only once.
2891 if (CaptureRegion == ThisCaptureRegion ||
2892 CaptureRegion == OMPD_unknown) {
2893 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002894 for (Decl *D : DS->decls())
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002895 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
2896 }
2897 }
2898 }
2899 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002900 SR = ActOnCapturedRegionEnd(SR.get());
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002901 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002902 return SR;
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002903}
2904
Jonas Hahnfeld64a9e3c2017-02-22 06:49:10 +00002905static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion,
2906 OpenMPDirectiveKind CancelRegion,
2907 SourceLocation StartLoc) {
2908 // CancelRegion is only needed for cancel and cancellation_point.
2909 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point)
2910 return false;
2911
2912 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for ||
2913 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup)
2914 return false;
2915
2916 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region)
2917 << getOpenMPDirectiveName(CancelRegion);
2918 return true;
2919}
2920
Alexey Bataeve3727102018-04-18 15:57:46 +00002921static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002922 OpenMPDirectiveKind CurrentRegion,
2923 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002924 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002925 SourceLocation StartLoc) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002926 if (Stack->getCurScope()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002927 OpenMPDirectiveKind ParentRegion = Stack->getParentDirective();
2928 OpenMPDirectiveKind OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002929 bool NestingProhibited = false;
2930 bool CloseNesting = true;
David Majnemer9d168222016-08-05 17:44:54 +00002931 bool OrphanSeen = false;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002932 enum {
2933 NoRecommend,
2934 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002935 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002936 ShouldBeInTargetRegion,
2937 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002938 } Recommend = NoRecommend;
Kelvin Lifd8b5742016-07-01 14:30:25 +00002939 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002940 // OpenMP [2.16, Nesting of Regions]
2941 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002942 // OpenMP [2.8.1,simd Construct, Restrictions]
Kelvin Lifd8b5742016-07-01 14:30:25 +00002943 // An ordered construct with the simd clause is the only OpenMP
2944 // construct that can appear in the simd region.
David Majnemer9d168222016-08-05 17:44:54 +00002945 // Allowing a SIMD construct nested in another SIMD construct is an
Kelvin Lifd8b5742016-07-01 14:30:25 +00002946 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
2947 // message.
2948 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
2949 ? diag::err_omp_prohibited_region_simd
2950 : diag::warn_omp_nesting_simd);
2951 return CurrentRegion != OMPD_simd;
Alexey Bataev549210e2014-06-24 04:39:47 +00002952 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002953 if (ParentRegion == OMPD_atomic) {
2954 // OpenMP [2.16, Nesting of Regions]
2955 // OpenMP constructs may not be nested inside an atomic region.
2956 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2957 return true;
2958 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002959 if (CurrentRegion == OMPD_section) {
2960 // OpenMP [2.7.2, sections Construct, Restrictions]
2961 // Orphaned section directives are prohibited. That is, the section
2962 // directives must appear within the sections construct and must not be
2963 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002964 if (ParentRegion != OMPD_sections &&
2965 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002966 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2967 << (ParentRegion != OMPD_unknown)
2968 << getOpenMPDirectiveName(ParentRegion);
2969 return true;
2970 }
2971 return false;
2972 }
Kelvin Li2b51f722016-07-26 04:32:50 +00002973 // Allow some constructs (except teams) to be orphaned (they could be
David Majnemer9d168222016-08-05 17:44:54 +00002974 // used in functions, called from OpenMP regions with the required
Kelvin Li2b51f722016-07-26 04:32:50 +00002975 // preconditions).
Kelvin Libf594a52016-12-17 05:48:59 +00002976 if (ParentRegion == OMPD_unknown &&
2977 !isOpenMPNestingTeamsDirective(CurrentRegion))
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002978 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002979 if (CurrentRegion == OMPD_cancellation_point ||
2980 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002981 // OpenMP [2.16, Nesting of Regions]
2982 // A cancellation point construct for which construct-type-clause is
2983 // taskgroup must be nested inside a task construct. A cancellation
2984 // point construct for which construct-type-clause is not taskgroup must
2985 // be closely nested inside an OpenMP construct that matches the type
2986 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002987 // A cancel construct for which construct-type-clause is taskgroup must be
2988 // nested inside a task construct. A cancel construct for which
2989 // construct-type-clause is not taskgroup must be closely nested inside an
2990 // OpenMP construct that matches the type specified in
2991 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002992 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002993 !((CancelRegion == OMPD_parallel &&
2994 (ParentRegion == OMPD_parallel ||
2995 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002996 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002997 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00002998 ParentRegion == OMPD_target_parallel_for ||
2999 ParentRegion == OMPD_distribute_parallel_for ||
Alexey Bataev16e79882017-11-22 21:12:03 +00003000 ParentRegion == OMPD_teams_distribute_parallel_for ||
3001 ParentRegion == OMPD_target_teams_distribute_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003002 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
3003 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00003004 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
3005 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003006 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00003007 // OpenMP [2.16, Nesting of Regions]
3008 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003009 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00003010 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003011 isOpenMPTaskingDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003012 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
3013 // OpenMP [2.16, Nesting of Regions]
3014 // A critical region may not be nested (closely or otherwise) inside a
3015 // critical region with the same name. Note that this restriction is not
3016 // sufficient to prevent deadlock.
3017 SourceLocation PreviousCriticalLoc;
David Majnemer9d168222016-08-05 17:44:54 +00003018 bool DeadLock = Stack->hasDirective(
3019 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
3020 const DeclarationNameInfo &DNI,
Alexey Bataeve3727102018-04-18 15:57:46 +00003021 SourceLocation Loc) {
David Majnemer9d168222016-08-05 17:44:54 +00003022 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
3023 PreviousCriticalLoc = Loc;
3024 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003025 }
3026 return false;
David Majnemer9d168222016-08-05 17:44:54 +00003027 },
3028 false /* skip top directive */);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003029 if (DeadLock) {
3030 SemaRef.Diag(StartLoc,
3031 diag::err_omp_prohibited_region_critical_same_name)
3032 << CurrentName.getName();
3033 if (PreviousCriticalLoc.isValid())
3034 SemaRef.Diag(PreviousCriticalLoc,
3035 diag::note_omp_previous_critical_region);
3036 return true;
3037 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003038 } else if (CurrentRegion == OMPD_barrier) {
3039 // OpenMP [2.16, Nesting of Regions]
3040 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003041 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003042 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3043 isOpenMPTaskingDirective(ParentRegion) ||
3044 ParentRegion == OMPD_master ||
3045 ParentRegion == OMPD_critical ||
3046 ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00003047 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Kelvin Li579e41c2016-11-30 23:51:03 +00003048 !isOpenMPParallelDirective(CurrentRegion) &&
3049 !isOpenMPTeamsDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003050 // OpenMP [2.16, Nesting of Regions]
3051 // A worksharing region may not be closely nested inside a worksharing,
3052 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003053 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3054 isOpenMPTaskingDirective(ParentRegion) ||
3055 ParentRegion == OMPD_master ||
3056 ParentRegion == OMPD_critical ||
3057 ParentRegion == OMPD_ordered;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003058 Recommend = ShouldBeInParallelRegion;
3059 } else if (CurrentRegion == OMPD_ordered) {
3060 // OpenMP [2.16, Nesting of Regions]
3061 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00003062 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003063 // An ordered region must be closely nested inside a loop region (or
3064 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003065 // OpenMP [2.8.1,simd Construct, Restrictions]
3066 // An ordered construct with the simd clause is the only OpenMP construct
3067 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003068 NestingProhibited = ParentRegion == OMPD_critical ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003069 isOpenMPTaskingDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003070 !(isOpenMPSimdDirective(ParentRegion) ||
3071 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003072 Recommend = ShouldBeInOrderedRegion;
Kelvin Libf594a52016-12-17 05:48:59 +00003073 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00003074 // OpenMP [2.16, Nesting of Regions]
3075 // If specified, a teams construct must be contained within a target
3076 // construct.
3077 NestingProhibited = ParentRegion != OMPD_target;
Kelvin Li2b51f722016-07-26 04:32:50 +00003078 OrphanSeen = ParentRegion == OMPD_unknown;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003079 Recommend = ShouldBeInTargetRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003080 }
Kelvin Libf594a52016-12-17 05:48:59 +00003081 if (!NestingProhibited &&
3082 !isOpenMPTargetExecutionDirective(CurrentRegion) &&
3083 !isOpenMPTargetDataManagementDirective(CurrentRegion) &&
3084 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00003085 // OpenMP [2.16, Nesting of Regions]
3086 // distribute, parallel, parallel sections, parallel workshare, and the
3087 // parallel loop and parallel loop SIMD constructs are the only OpenMP
3088 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003089 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
3090 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00003091 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003092 }
David Majnemer9d168222016-08-05 17:44:54 +00003093 if (!NestingProhibited &&
Kelvin Li02532872016-08-05 14:37:37 +00003094 isOpenMPNestingDistributeDirective(CurrentRegion)) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003095 // OpenMP 4.5 [2.17 Nesting of Regions]
3096 // The region associated with the distribute construct must be strictly
3097 // nested inside a teams region
Kelvin Libf594a52016-12-17 05:48:59 +00003098 NestingProhibited =
3099 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003100 Recommend = ShouldBeInTeamsRegion;
3101 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003102 if (!NestingProhibited &&
3103 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
3104 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
3105 // OpenMP 4.5 [2.17 Nesting of Regions]
3106 // If a target, target update, target data, target enter data, or
3107 // target exit data construct is encountered during execution of a
3108 // target region, the behavior is unspecified.
3109 NestingProhibited = Stack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +00003110 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
Alexey Bataeve3727102018-04-18 15:57:46 +00003111 SourceLocation) {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003112 if (isOpenMPTargetExecutionDirective(K)) {
3113 OffendingRegion = K;
3114 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003115 }
3116 return false;
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003117 },
3118 false /* don't skip top directive */);
3119 CloseNesting = false;
3120 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003121 if (NestingProhibited) {
Kelvin Li2b51f722016-07-26 04:32:50 +00003122 if (OrphanSeen) {
3123 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
3124 << getOpenMPDirectiveName(CurrentRegion) << Recommend;
3125 } else {
3126 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
3127 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
3128 << Recommend << getOpenMPDirectiveName(CurrentRegion);
3129 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003130 return true;
3131 }
3132 }
3133 return false;
3134}
3135
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003136static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
3137 ArrayRef<OMPClause *> Clauses,
3138 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
3139 bool ErrorFound = false;
3140 unsigned NamedModifiersNumber = 0;
3141 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
3142 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00003143 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00003144 for (const OMPClause *C : Clauses) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003145 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
3146 // At most one if clause without a directive-name-modifier can appear on
3147 // the directive.
3148 OpenMPDirectiveKind CurNM = IC->getNameModifier();
3149 if (FoundNameModifiers[CurNM]) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003150 S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003151 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
3152 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
3153 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003154 } else if (CurNM != OMPD_unknown) {
3155 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003156 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003157 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003158 FoundNameModifiers[CurNM] = IC;
3159 if (CurNM == OMPD_unknown)
3160 continue;
3161 // Check if the specified name modifier is allowed for the current
3162 // directive.
3163 // At most one if clause with the particular directive-name-modifier can
3164 // appear on the directive.
3165 bool MatchFound = false;
3166 for (auto NM : AllowedNameModifiers) {
3167 if (CurNM == NM) {
3168 MatchFound = true;
3169 break;
3170 }
3171 }
3172 if (!MatchFound) {
3173 S.Diag(IC->getNameModifierLoc(),
3174 diag::err_omp_wrong_if_directive_name_modifier)
3175 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
3176 ErrorFound = true;
3177 }
3178 }
3179 }
3180 // If any if clause on the directive includes a directive-name-modifier then
3181 // all if clauses on the directive must include a directive-name-modifier.
3182 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
3183 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003184 S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003185 diag::err_omp_no_more_if_clause);
3186 } else {
3187 std::string Values;
3188 std::string Sep(", ");
3189 unsigned AllowedCnt = 0;
3190 unsigned TotalAllowedNum =
3191 AllowedNameModifiers.size() - NamedModifiersNumber;
3192 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3193 ++Cnt) {
3194 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
3195 if (!FoundNameModifiers[NM]) {
3196 Values += "'";
3197 Values += getOpenMPDirectiveName(NM);
3198 Values += "'";
3199 if (AllowedCnt + 2 == TotalAllowedNum)
3200 Values += " or ";
3201 else if (AllowedCnt + 1 != TotalAllowedNum)
3202 Values += Sep;
3203 ++AllowedCnt;
3204 }
3205 }
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003206 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003207 diag::err_omp_unnamed_if_clause)
3208 << (TotalAllowedNum > 1) << Values;
3209 }
Alexey Bataeve3727102018-04-18 15:57:46 +00003210 for (SourceLocation Loc : NameModifierLoc) {
Alexey Bataevecb156a2015-09-15 17:23:56 +00003211 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3212 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003213 ErrorFound = true;
3214 }
3215 return ErrorFound;
3216}
3217
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003218StmtResult Sema::ActOnOpenMPExecutableDirective(
3219 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
3220 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3221 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003222 StmtResult Res = StmtError();
Jonas Hahnfeld64a9e3c2017-02-22 06:49:10 +00003223 // First check CancelRegion which is then used in checkNestingOfRegions.
3224 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) ||
3225 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003226 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00003227 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003228
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003229 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataeve3727102018-04-18 15:57:46 +00003230 VarsWithInheritedDSAType VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003231 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00003232 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00003233 if (AStmt && !CurContext->isDependentContext()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00003234 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
3235
3236 // Check default data sharing attributes for referenced variables.
3237 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
Arpith Chacko Jacob1f46b702017-01-23 15:38:49 +00003238 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind);
3239 Stmt *S = AStmt;
3240 while (--ThisCaptureLevel >= 0)
3241 S = cast<CapturedStmt>(S)->getCapturedStmt();
3242 DSAChecker.Visit(S);
Alexey Bataev68446b72014-07-18 07:47:19 +00003243 if (DSAChecker.isErrorFound())
3244 return StmtError();
3245 // Generate list of implicitly defined firstprivate variables.
3246 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00003247
Alexey Bataev88202be2017-07-27 13:20:36 +00003248 SmallVector<Expr *, 4> ImplicitFirstprivates(
3249 DSAChecker.getImplicitFirstprivate().begin(),
3250 DSAChecker.getImplicitFirstprivate().end());
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003251 SmallVector<Expr *, 4> ImplicitMaps(DSAChecker.getImplicitMap().begin(),
3252 DSAChecker.getImplicitMap().end());
Alexey Bataev88202be2017-07-27 13:20:36 +00003253 // Mark taskgroup task_reduction descriptors as implicitly firstprivate.
Alexey Bataeve3727102018-04-18 15:57:46 +00003254 for (OMPClause *C : Clauses) {
Alexey Bataev88202be2017-07-27 13:20:36 +00003255 if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003256 for (Expr *E : IRC->taskgroup_descriptors())
Alexey Bataev88202be2017-07-27 13:20:36 +00003257 if (E)
3258 ImplicitFirstprivates.emplace_back(E);
3259 }
3260 }
3261 if (!ImplicitFirstprivates.empty()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00003262 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
Alexey Bataev88202be2017-07-27 13:20:36 +00003263 ImplicitFirstprivates, SourceLocation(), SourceLocation(),
3264 SourceLocation())) {
Alexey Bataev68446b72014-07-18 07:47:19 +00003265 ClausesWithImplicit.push_back(Implicit);
3266 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
Alexey Bataev88202be2017-07-27 13:20:36 +00003267 ImplicitFirstprivates.size();
Alexey Bataeve3727102018-04-18 15:57:46 +00003268 } else {
Alexey Bataev68446b72014-07-18 07:47:19 +00003269 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003270 }
Alexey Bataev68446b72014-07-18 07:47:19 +00003271 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003272 if (!ImplicitMaps.empty()) {
3273 if (OMPClause *Implicit = ActOnOpenMPMapClause(
3274 OMPC_MAP_unknown, OMPC_MAP_tofrom, /*IsMapTypeImplicit=*/true,
3275 SourceLocation(), SourceLocation(), ImplicitMaps,
3276 SourceLocation(), SourceLocation(), SourceLocation())) {
3277 ClausesWithImplicit.emplace_back(Implicit);
3278 ErrorFound |=
3279 cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMaps.size();
Alexey Bataeve3727102018-04-18 15:57:46 +00003280 } else {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003281 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003282 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003283 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003284 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003285
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003286 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003287 switch (Kind) {
3288 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00003289 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3290 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003291 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003292 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003293 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003294 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3295 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003296 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003297 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003298 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3299 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003300 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003301 case OMPD_for_simd:
3302 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3303 EndLoc, VarsWithInheritedDSA);
3304 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003305 case OMPD_sections:
3306 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3307 EndLoc);
3308 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003309 case OMPD_section:
3310 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003311 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003312 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3313 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003314 case OMPD_single:
3315 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3316 EndLoc);
3317 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003318 case OMPD_master:
3319 assert(ClausesWithImplicit.empty() &&
3320 "No clauses are allowed for 'omp master' directive");
3321 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3322 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003323 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003324 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3325 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003326 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003327 case OMPD_parallel_for:
3328 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3329 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003330 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003331 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003332 case OMPD_parallel_for_simd:
3333 Res = ActOnOpenMPParallelForSimdDirective(
3334 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003335 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003336 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003337 case OMPD_parallel_sections:
3338 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3339 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003340 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003341 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003342 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003343 Res =
3344 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003345 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003346 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003347 case OMPD_taskyield:
3348 assert(ClausesWithImplicit.empty() &&
3349 "No clauses are allowed for 'omp taskyield' directive");
3350 assert(AStmt == nullptr &&
3351 "No associated statement allowed for 'omp taskyield' directive");
3352 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3353 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003354 case OMPD_barrier:
3355 assert(ClausesWithImplicit.empty() &&
3356 "No clauses are allowed for 'omp barrier' directive");
3357 assert(AStmt == nullptr &&
3358 "No associated statement allowed for 'omp barrier' directive");
3359 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3360 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003361 case OMPD_taskwait:
3362 assert(ClausesWithImplicit.empty() &&
3363 "No clauses are allowed for 'omp taskwait' directive");
3364 assert(AStmt == nullptr &&
3365 "No associated statement allowed for 'omp taskwait' directive");
3366 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3367 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003368 case OMPD_taskgroup:
Alexey Bataev169d96a2017-07-18 20:17:46 +00003369 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc,
3370 EndLoc);
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003371 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003372 case OMPD_flush:
3373 assert(AStmt == nullptr &&
3374 "No associated statement allowed for 'omp flush' directive");
3375 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3376 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003377 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003378 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3379 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003380 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003381 case OMPD_atomic:
3382 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3383 EndLoc);
3384 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003385 case OMPD_teams:
3386 Res =
3387 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3388 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003389 case OMPD_target:
3390 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3391 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003392 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003393 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003394 case OMPD_target_parallel:
3395 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3396 StartLoc, EndLoc);
3397 AllowedNameModifiers.push_back(OMPD_target);
3398 AllowedNameModifiers.push_back(OMPD_parallel);
3399 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003400 case OMPD_target_parallel_for:
3401 Res = ActOnOpenMPTargetParallelForDirective(
3402 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3403 AllowedNameModifiers.push_back(OMPD_target);
3404 AllowedNameModifiers.push_back(OMPD_parallel);
3405 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003406 case OMPD_cancellation_point:
3407 assert(ClausesWithImplicit.empty() &&
3408 "No clauses are allowed for 'omp cancellation point' directive");
3409 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3410 "cancellation point' directive");
3411 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3412 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003413 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003414 assert(AStmt == nullptr &&
3415 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003416 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3417 CancelRegion);
3418 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003419 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003420 case OMPD_target_data:
3421 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3422 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003423 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003424 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003425 case OMPD_target_enter_data:
3426 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00003427 EndLoc, AStmt);
Samuel Antaodf67fc42016-01-19 19:15:56 +00003428 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3429 break;
Samuel Antao72590762016-01-19 20:04:50 +00003430 case OMPD_target_exit_data:
3431 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00003432 EndLoc, AStmt);
Samuel Antao72590762016-01-19 20:04:50 +00003433 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3434 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003435 case OMPD_taskloop:
3436 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3437 EndLoc, VarsWithInheritedDSA);
3438 AllowedNameModifiers.push_back(OMPD_taskloop);
3439 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003440 case OMPD_taskloop_simd:
3441 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3442 EndLoc, VarsWithInheritedDSA);
3443 AllowedNameModifiers.push_back(OMPD_taskloop);
3444 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003445 case OMPD_distribute:
3446 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3447 EndLoc, VarsWithInheritedDSA);
3448 break;
Samuel Antao686c70c2016-05-26 17:30:50 +00003449 case OMPD_target_update:
Alexey Bataev7828b252017-11-21 17:08:48 +00003450 Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc,
3451 EndLoc, AStmt);
Samuel Antao686c70c2016-05-26 17:30:50 +00003452 AllowedNameModifiers.push_back(OMPD_target_update);
3453 break;
Carlo Bertolli9925f152016-06-27 14:55:37 +00003454 case OMPD_distribute_parallel_for:
3455 Res = ActOnOpenMPDistributeParallelForDirective(
3456 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3457 AllowedNameModifiers.push_back(OMPD_parallel);
3458 break;
Kelvin Li4a39add2016-07-05 05:00:15 +00003459 case OMPD_distribute_parallel_for_simd:
3460 Res = ActOnOpenMPDistributeParallelForSimdDirective(
3461 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3462 AllowedNameModifiers.push_back(OMPD_parallel);
3463 break;
Kelvin Li787f3fc2016-07-06 04:45:38 +00003464 case OMPD_distribute_simd:
3465 Res = ActOnOpenMPDistributeSimdDirective(
3466 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3467 break;
Kelvin Lia579b912016-07-14 02:54:56 +00003468 case OMPD_target_parallel_for_simd:
3469 Res = ActOnOpenMPTargetParallelForSimdDirective(
3470 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3471 AllowedNameModifiers.push_back(OMPD_target);
3472 AllowedNameModifiers.push_back(OMPD_parallel);
3473 break;
Kelvin Li986330c2016-07-20 22:57:10 +00003474 case OMPD_target_simd:
3475 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3476 EndLoc, VarsWithInheritedDSA);
3477 AllowedNameModifiers.push_back(OMPD_target);
3478 break;
Kelvin Li02532872016-08-05 14:37:37 +00003479 case OMPD_teams_distribute:
David Majnemer9d168222016-08-05 17:44:54 +00003480 Res = ActOnOpenMPTeamsDistributeDirective(
3481 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Kelvin Li02532872016-08-05 14:37:37 +00003482 break;
Kelvin Li4e325f72016-10-25 12:50:55 +00003483 case OMPD_teams_distribute_simd:
3484 Res = ActOnOpenMPTeamsDistributeSimdDirective(
3485 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3486 break;
Kelvin Li579e41c2016-11-30 23:51:03 +00003487 case OMPD_teams_distribute_parallel_for_simd:
3488 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective(
3489 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3490 AllowedNameModifiers.push_back(OMPD_parallel);
3491 break;
Kelvin Li7ade93f2016-12-09 03:24:30 +00003492 case OMPD_teams_distribute_parallel_for:
3493 Res = ActOnOpenMPTeamsDistributeParallelForDirective(
3494 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3495 AllowedNameModifiers.push_back(OMPD_parallel);
3496 break;
Kelvin Libf594a52016-12-17 05:48:59 +00003497 case OMPD_target_teams:
3498 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc,
3499 EndLoc);
3500 AllowedNameModifiers.push_back(OMPD_target);
3501 break;
Kelvin Li83c451e2016-12-25 04:52:54 +00003502 case OMPD_target_teams_distribute:
3503 Res = ActOnOpenMPTargetTeamsDistributeDirective(
3504 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3505 AllowedNameModifiers.push_back(OMPD_target);
3506 break;
Kelvin Li80e8f562016-12-29 22:16:30 +00003507 case OMPD_target_teams_distribute_parallel_for:
3508 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective(
3509 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3510 AllowedNameModifiers.push_back(OMPD_target);
3511 AllowedNameModifiers.push_back(OMPD_parallel);
3512 break;
Kelvin Li1851df52017-01-03 05:23:48 +00003513 case OMPD_target_teams_distribute_parallel_for_simd:
3514 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
3515 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3516 AllowedNameModifiers.push_back(OMPD_target);
3517 AllowedNameModifiers.push_back(OMPD_parallel);
3518 break;
Kelvin Lida681182017-01-10 18:08:18 +00003519 case OMPD_target_teams_distribute_simd:
3520 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective(
3521 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3522 AllowedNameModifiers.push_back(OMPD_target);
3523 break;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00003524 case OMPD_declare_target:
3525 case OMPD_end_declare_target:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003526 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003527 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003528 case OMPD_declare_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00003529 case OMPD_requires:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003530 llvm_unreachable("OpenMP Directive is not allowed");
3531 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003532 llvm_unreachable("Unknown OpenMP directive");
3533 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003534
Alexey Bataeve3727102018-04-18 15:57:46 +00003535 for (const auto &P : VarsWithInheritedDSA) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003536 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3537 << P.first << P.second->getSourceRange();
3538 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003539 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3540
3541 if (!AllowedNameModifiers.empty())
3542 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3543 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003544
Alexey Bataeved09d242014-05-28 05:53:51 +00003545 if (ErrorFound)
3546 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003547 return Res;
3548}
3549
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003550Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3551 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
Alexey Bataevd93d3762016-04-12 09:35:56 +00003552 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
Alexey Bataevecba70f2016-04-12 11:02:11 +00003553 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3554 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003555 assert(Aligneds.size() == Alignments.size());
Alexey Bataevecba70f2016-04-12 11:02:11 +00003556 assert(Linears.size() == LinModifiers.size());
3557 assert(Linears.size() == Steps.size());
Alexey Bataev587e1de2016-03-30 10:43:55 +00003558 if (!DG || DG.get().isNull())
3559 return DeclGroupPtrTy();
3560
3561 if (!DG.get().isSingleDecl()) {
Alexey Bataev20dfd772016-04-04 10:12:15 +00003562 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003563 return DG;
3564 }
Alexey Bataeve3727102018-04-18 15:57:46 +00003565 Decl *ADecl = DG.get().getSingleDecl();
Alexey Bataev587e1de2016-03-30 10:43:55 +00003566 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3567 ADecl = FTD->getTemplatedDecl();
3568
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003569 auto *FD = dyn_cast<FunctionDecl>(ADecl);
3570 if (!FD) {
3571 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003572 return DeclGroupPtrTy();
3573 }
3574
Alexey Bataev2af33e32016-04-07 12:45:37 +00003575 // OpenMP [2.8.2, declare simd construct, Description]
3576 // The parameter of the simdlen clause must be a constant positive integer
3577 // expression.
3578 ExprResult SL;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003579 if (Simdlen)
Alexey Bataev2af33e32016-04-07 12:45:37 +00003580 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003581 // OpenMP [2.8.2, declare simd construct, Description]
3582 // The special this pointer can be used as if was one of the arguments to the
3583 // function in any of the linear, aligned, or uniform clauses.
3584 // The uniform clause declares one or more arguments to have an invariant
3585 // value for all concurrent invocations of the function in the execution of a
3586 // single SIMD loop.
Alexey Bataeve3727102018-04-18 15:57:46 +00003587 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs;
3588 const Expr *UniformedLinearThis = nullptr;
3589 for (const Expr *E : Uniforms) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003590 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00003591 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3592 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003593 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3594 FD->getParamDecl(PVD->getFunctionScopeIndex())
Alexey Bataevecba70f2016-04-12 11:02:11 +00003595 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
Alexey Bataev43a919f2018-04-13 17:48:43 +00003596 UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003597 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003598 }
3599 if (isa<CXXThisExpr>(E)) {
3600 UniformedLinearThis = E;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003601 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003602 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003603 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3604 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
Alexey Bataev2af33e32016-04-07 12:45:37 +00003605 }
Alexey Bataevd93d3762016-04-12 09:35:56 +00003606 // OpenMP [2.8.2, declare simd construct, Description]
3607 // The aligned clause declares that the object to which each list item points
3608 // is aligned to the number of bytes expressed in the optional parameter of
3609 // the aligned clause.
3610 // The special this pointer can be used as if was one of the arguments to the
3611 // function in any of the linear, aligned, or uniform clauses.
3612 // The type of list items appearing in the aligned clause must be array,
3613 // pointer, reference to array, or reference to pointer.
Alexey Bataeve3727102018-04-18 15:57:46 +00003614 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs;
3615 const Expr *AlignedThis = nullptr;
3616 for (const Expr *E : Aligneds) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003617 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00003618 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3619 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3620 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevd93d3762016-04-12 09:35:56 +00003621 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3622 FD->getParamDecl(PVD->getFunctionScopeIndex())
3623 ->getCanonicalDecl() == CanonPVD) {
3624 // OpenMP [2.8.1, simd construct, Restrictions]
3625 // A list-item cannot appear in more than one aligned clause.
3626 if (AlignedArgs.count(CanonPVD) > 0) {
3627 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3628 << 1 << E->getSourceRange();
3629 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3630 diag::note_omp_explicit_dsa)
3631 << getOpenMPClauseName(OMPC_aligned);
3632 continue;
3633 }
3634 AlignedArgs[CanonPVD] = E;
3635 QualType QTy = PVD->getType()
3636 .getNonReferenceType()
3637 .getUnqualifiedType()
3638 .getCanonicalType();
3639 const Type *Ty = QTy.getTypePtrOrNull();
3640 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3641 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3642 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3643 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3644 }
3645 continue;
3646 }
3647 }
3648 if (isa<CXXThisExpr>(E)) {
3649 if (AlignedThis) {
3650 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3651 << 2 << E->getSourceRange();
3652 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3653 << getOpenMPClauseName(OMPC_aligned);
3654 }
3655 AlignedThis = E;
3656 continue;
3657 }
3658 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3659 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3660 }
3661 // The optional parameter of the aligned clause, alignment, must be a constant
3662 // positive integer expression. If no optional parameter is specified,
3663 // implementation-defined default alignments for SIMD instructions on the
3664 // target platforms are assumed.
Alexey Bataeve3727102018-04-18 15:57:46 +00003665 SmallVector<const Expr *, 4> NewAligns;
3666 for (Expr *E : Alignments) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003667 ExprResult Align;
3668 if (E)
3669 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3670 NewAligns.push_back(Align.get());
3671 }
Alexey Bataevecba70f2016-04-12 11:02:11 +00003672 // OpenMP [2.8.2, declare simd construct, Description]
3673 // The linear clause declares one or more list items to be private to a SIMD
3674 // lane and to have a linear relationship with respect to the iteration space
3675 // of a loop.
3676 // The special this pointer can be used as if was one of the arguments to the
3677 // function in any of the linear, aligned, or uniform clauses.
3678 // When a linear-step expression is specified in a linear clause it must be
3679 // either a constant integer expression or an integer-typed parameter that is
3680 // specified in a uniform clause on the directive.
Alexey Bataeve3727102018-04-18 15:57:46 +00003681 llvm::DenseMap<const Decl *, const Expr *> LinearArgs;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003682 const bool IsUniformedThis = UniformedLinearThis != nullptr;
3683 auto MI = LinModifiers.begin();
Alexey Bataeve3727102018-04-18 15:57:46 +00003684 for (const Expr *E : Linears) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003685 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3686 ++MI;
3687 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00003688 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3689 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3690 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevecba70f2016-04-12 11:02:11 +00003691 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3692 FD->getParamDecl(PVD->getFunctionScopeIndex())
3693 ->getCanonicalDecl() == CanonPVD) {
3694 // OpenMP [2.15.3.7, linear Clause, Restrictions]
3695 // A list-item cannot appear in more than one linear clause.
3696 if (LinearArgs.count(CanonPVD) > 0) {
3697 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3698 << getOpenMPClauseName(OMPC_linear)
3699 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3700 Diag(LinearArgs[CanonPVD]->getExprLoc(),
3701 diag::note_omp_explicit_dsa)
3702 << getOpenMPClauseName(OMPC_linear);
3703 continue;
3704 }
3705 // Each argument can appear in at most one uniform or linear clause.
3706 if (UniformedArgs.count(CanonPVD) > 0) {
3707 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3708 << getOpenMPClauseName(OMPC_linear)
3709 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3710 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3711 diag::note_omp_explicit_dsa)
3712 << getOpenMPClauseName(OMPC_uniform);
3713 continue;
3714 }
3715 LinearArgs[CanonPVD] = E;
3716 if (E->isValueDependent() || E->isTypeDependent() ||
3717 E->isInstantiationDependent() ||
3718 E->containsUnexpandedParameterPack())
3719 continue;
3720 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3721 PVD->getOriginalType());
3722 continue;
3723 }
3724 }
3725 if (isa<CXXThisExpr>(E)) {
3726 if (UniformedLinearThis) {
3727 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3728 << getOpenMPClauseName(OMPC_linear)
3729 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3730 << E->getSourceRange();
3731 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3732 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3733 : OMPC_linear);
3734 continue;
3735 }
3736 UniformedLinearThis = E;
3737 if (E->isValueDependent() || E->isTypeDependent() ||
3738 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3739 continue;
3740 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3741 E->getType());
3742 continue;
3743 }
3744 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3745 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3746 }
3747 Expr *Step = nullptr;
3748 Expr *NewStep = nullptr;
3749 SmallVector<Expr *, 4> NewSteps;
Alexey Bataeve3727102018-04-18 15:57:46 +00003750 for (Expr *E : Steps) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003751 // Skip the same step expression, it was checked already.
3752 if (Step == E || !E) {
3753 NewSteps.push_back(E ? NewStep : nullptr);
3754 continue;
3755 }
3756 Step = E;
Alexey Bataeve3727102018-04-18 15:57:46 +00003757 if (const auto *DRE = dyn_cast<DeclRefExpr>(Step))
3758 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3759 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevecba70f2016-04-12 11:02:11 +00003760 if (UniformedArgs.count(CanonPVD) == 0) {
3761 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
3762 << Step->getSourceRange();
3763 } else if (E->isValueDependent() || E->isTypeDependent() ||
3764 E->isInstantiationDependent() ||
3765 E->containsUnexpandedParameterPack() ||
Alexey Bataeve3727102018-04-18 15:57:46 +00003766 CanonPVD->getType()->hasIntegerRepresentation()) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003767 NewSteps.push_back(Step);
Alexey Bataeve3727102018-04-18 15:57:46 +00003768 } else {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003769 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
3770 << Step->getSourceRange();
3771 }
3772 continue;
3773 }
3774 NewStep = Step;
3775 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3776 !Step->isInstantiationDependent() &&
3777 !Step->containsUnexpandedParameterPack()) {
3778 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
3779 .get();
3780 if (NewStep)
3781 NewStep = VerifyIntegerConstantExpression(NewStep).get();
3782 }
3783 NewSteps.push_back(NewStep);
3784 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003785 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3786 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
Alexey Bataevd93d3762016-04-12 09:35:56 +00003787 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
Alexey Bataevecba70f2016-04-12 11:02:11 +00003788 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
3789 const_cast<Expr **>(Linears.data()), Linears.size(),
3790 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
3791 NewSteps.data(), NewSteps.size(), SR);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003792 ADecl->addAttr(NewAttr);
3793 return ConvertDeclToDeclGroup(ADecl);
3794}
3795
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003796StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3797 Stmt *AStmt,
3798 SourceLocation StartLoc,
3799 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003800 if (!AStmt)
3801 return StmtError();
3802
Alexey Bataeve3727102018-04-18 15:57:46 +00003803 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev9959db52014-05-06 10:08:46 +00003804 // 1.2.2 OpenMP Language Terminology
3805 // Structured block - An executable statement with a single entry at the
3806 // top and a single exit at the bottom.
3807 // The point of exit cannot be a branch out of the structured block.
3808 // longjmp() and throw() must not violate the entry/exit criteria.
3809 CS->getCapturedDecl()->setNothrow();
3810
Reid Kleckner87a31802018-03-12 21:43:02 +00003811 setFunctionHasBranchProtectedScope();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003812
Alexey Bataev25e5b442015-09-15 12:52:43 +00003813 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3814 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003815}
3816
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003817namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003818/// Helper class for checking canonical form of the OpenMP loops and
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003819/// extracting iteration space of each loop in the loop nest, that will be used
3820/// for IR generation.
3821class OpenMPIterationSpaceChecker {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003822 /// Reference to Sema.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003823 Sema &SemaRef;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003824 /// A location for diagnostics (when there is no some better location).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003825 SourceLocation DefaultLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003826 /// A location for diagnostics (when increment is not compatible).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003827 SourceLocation ConditionLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003828 /// A source location for referring to loop init later.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003829 SourceRange InitSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003830 /// A source location for referring to condition later.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003831 SourceRange ConditionSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003832 /// A source location for referring to increment later.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003833 SourceRange IncrementSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003834 /// Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003835 ValueDecl *LCDecl = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003836 /// Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003837 Expr *LCRef = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003838 /// Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003839 Expr *LB = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003840 /// Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003841 Expr *UB = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003842 /// Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003843 Expr *Step = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003844 /// This flag is true when condition is one of:
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003845 /// Var < UB
3846 /// Var <= UB
3847 /// UB > Var
3848 /// UB >= Var
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003849 bool TestIsLessOp = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003850 /// This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003851 bool TestIsStrictOp = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003852 /// This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003853 bool SubtractStep = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003854
3855public:
3856 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003857 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003858 /// Check init-expr for canonical loop form and save loop counter
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003859 /// variable - #Var and its initialization value - #LB.
Alexey Bataeve3727102018-04-18 15:57:46 +00003860 bool checkAndSetInit(Stmt *S, bool EmitDiags = true);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003861 /// Check test-expr for canonical form, save upper-bound (#UB), flags
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003862 /// for less/greater and for strict/non-strict comparison.
Alexey Bataeve3727102018-04-18 15:57:46 +00003863 bool checkAndSetCond(Expr *S);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003864 /// Check incr-expr for canonical loop form and return true if it
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003865 /// does not conform, otherwise save loop step (#Step).
Alexey Bataeve3727102018-04-18 15:57:46 +00003866 bool checkAndSetInc(Expr *S);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003867 /// Return the loop counter variable.
Alexey Bataeve3727102018-04-18 15:57:46 +00003868 ValueDecl *getLoopDecl() const { return LCDecl; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003869 /// Return the reference expression to loop counter variable.
Alexey Bataeve3727102018-04-18 15:57:46 +00003870 Expr *getLoopDeclRefExpr() const { return LCRef; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003871 /// Source range of the loop init.
Alexey Bataeve3727102018-04-18 15:57:46 +00003872 SourceRange getInitSrcRange() const { return InitSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003873 /// Source range of the loop condition.
Alexey Bataeve3727102018-04-18 15:57:46 +00003874 SourceRange getConditionSrcRange() const { return ConditionSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003875 /// Source range of the loop increment.
Alexey Bataeve3727102018-04-18 15:57:46 +00003876 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003877 /// True if the step should be subtracted.
Alexey Bataeve3727102018-04-18 15:57:46 +00003878 bool shouldSubtractStep() const { return SubtractStep; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003879 /// Build the expression to calculate the number of iterations.
Alexey Bataeve3727102018-04-18 15:57:46 +00003880 Expr *buildNumIterations(
3881 Scope *S, const bool LimitedType,
3882 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003883 /// Build the precondition expression for the loops.
Alexey Bataeve3727102018-04-18 15:57:46 +00003884 Expr *
3885 buildPreCond(Scope *S, Expr *Cond,
3886 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003887 /// Build reference expression to the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00003888 DeclRefExpr *
3889 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
3890 DSAStackTy &DSA) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003891 /// Build reference expression to the private counter be used for
Alexey Bataeva8899172015-08-06 12:30:57 +00003892 /// codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00003893 Expr *buildPrivateCounterVar() const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003894 /// Build initialization of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00003895 Expr *buildCounterInit() const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003896 /// Build step of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00003897 Expr *buildCounterStep() const;
Alexey Bataevf138fda2018-08-13 19:04:24 +00003898 /// Build loop data with counter value for depend clauses in ordered
3899 /// directives.
3900 Expr *
3901 buildOrderedLoopData(Scope *S, Expr *Counter,
3902 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
3903 SourceLocation Loc, Expr *Inc = nullptr,
3904 OverloadedOperatorKind OOK = OO_Amp);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003905 /// Return true if any expression is dependent.
Alexey Bataeve3727102018-04-18 15:57:46 +00003906 bool dependent() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003907
3908private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003909 /// Check the right-hand side of an assignment in the increment
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003910 /// expression.
Alexey Bataeve3727102018-04-18 15:57:46 +00003911 bool checkAndSetIncRHS(Expr *RHS);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003912 /// Helper to set loop counter variable and its initializer.
Alexey Bataeve3727102018-04-18 15:57:46 +00003913 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003914 /// Helper to set upper bound.
Alexey Bataeve3727102018-04-18 15:57:46 +00003915 bool setUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003916 SourceLocation SL);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003917 /// Helper to set loop increment.
Alexey Bataeve3727102018-04-18 15:57:46 +00003918 bool setStep(Expr *NewStep, bool Subtract);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003919};
3920
Alexey Bataeve3727102018-04-18 15:57:46 +00003921bool OpenMPIterationSpaceChecker::dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003922 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003923 assert(!LB && !UB && !Step);
3924 return false;
3925 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003926 return LCDecl->getType()->isDependentType() ||
3927 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
3928 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003929}
3930
Alexey Bataeve3727102018-04-18 15:57:46 +00003931bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl,
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003932 Expr *NewLCRefExpr,
3933 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003934 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003935 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003936 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003937 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003938 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003939 LCDecl = getCanonicalDecl(NewLCDecl);
3940 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003941 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3942 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003943 if ((Ctor->isCopyOrMoveConstructor() ||
3944 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3945 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003946 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003947 LB = NewLB;
3948 return false;
3949}
3950
Alexey Bataeve3727102018-04-18 15:57:46 +00003951bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00003952 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003953 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003954 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
3955 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003956 if (!NewUB)
3957 return true;
3958 UB = NewUB;
3959 TestIsLessOp = LessOp;
3960 TestIsStrictOp = StrictOp;
3961 ConditionSrcRange = SR;
3962 ConditionLoc = SL;
3963 return false;
3964}
3965
Alexey Bataeve3727102018-04-18 15:57:46 +00003966bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003967 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003968 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003969 if (!NewStep)
3970 return true;
3971 if (!NewStep->isValueDependent()) {
3972 // Check that the step is integer expression.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003973 SourceLocation StepLoc = NewStep->getBeginLoc();
Alexey Bataev5372fb82017-08-31 23:06:52 +00003974 ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion(
3975 StepLoc, getExprAsWritten(NewStep));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003976 if (Val.isInvalid())
3977 return true;
3978 NewStep = Val.get();
3979
3980 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3981 // If test-expr is of form var relational-op b and relational-op is < or
3982 // <= then incr-expr must cause var to increase on each iteration of the
3983 // loop. If test-expr is of form var relational-op b and relational-op is
3984 // > or >= then incr-expr must cause var to decrease on each iteration of
3985 // the loop.
3986 // If test-expr is of form b relational-op var and relational-op is < or
3987 // <= then incr-expr must cause var to decrease on each iteration of the
3988 // loop. If test-expr is of form b relational-op var and relational-op is
3989 // > or >= then incr-expr must cause var to increase on each iteration of
3990 // the loop.
3991 llvm::APSInt Result;
3992 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3993 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3994 bool IsConstNeg =
3995 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003996 bool IsConstPos =
3997 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003998 bool IsConstZero = IsConstant && !Result.getBoolValue();
3999 if (UB && (IsConstZero ||
4000 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00004001 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004002 SemaRef.Diag(NewStep->getExprLoc(),
4003 diag::err_omp_loop_incr_not_compatible)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004004 << LCDecl << TestIsLessOp << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004005 SemaRef.Diag(ConditionLoc,
4006 diag::note_omp_loop_cond_requres_compatible_incr)
4007 << TestIsLessOp << ConditionSrcRange;
4008 return true;
4009 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004010 if (TestIsLessOp == Subtract) {
David Majnemer9d168222016-08-05 17:44:54 +00004011 NewStep =
4012 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep)
4013 .get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004014 Subtract = !Subtract;
4015 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004016 }
4017
4018 Step = NewStep;
4019 SubtractStep = Subtract;
4020 return false;
4021}
4022
Alexey Bataeve3727102018-04-18 15:57:46 +00004023bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004024 // Check init-expr for canonical loop form and save loop counter
4025 // variable - #Var and its initialization value - #LB.
4026 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
4027 // var = lb
4028 // integer-type var = lb
4029 // random-access-iterator-type var = lb
4030 // pointer-type var = lb
4031 //
4032 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00004033 if (EmitDiags) {
4034 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
4035 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004036 return true;
4037 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004038 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4039 if (!ExprTemp->cleanupsHaveSideEffects())
4040 S = ExprTemp->getSubExpr();
4041
Alexander Musmana5f070a2014-10-01 06:03:56 +00004042 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004043 if (Expr *E = dyn_cast<Expr>(S))
4044 S = E->IgnoreParens();
David Majnemer9d168222016-08-05 17:44:54 +00004045 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004046 if (BO->getOpcode() == BO_Assign) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004047 Expr *LHS = BO->getLHS()->IgnoreParens();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004048 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
4049 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4050 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataeve3727102018-04-18 15:57:46 +00004051 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4052 return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004053 }
4054 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4055 if (ME->isArrow() &&
4056 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
Alexey Bataeve3727102018-04-18 15:57:46 +00004057 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004058 }
4059 }
David Majnemer9d168222016-08-05 17:44:54 +00004060 } else if (auto *DS = dyn_cast<DeclStmt>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004061 if (DS->isSingleDecl()) {
David Majnemer9d168222016-08-05 17:44:54 +00004062 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00004063 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004064 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00004065 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004066 SemaRef.Diag(S->getBeginLoc(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004067 diag::ext_omp_loop_not_canonical_init)
4068 << S->getSourceRange();
Alexey Bataevf138fda2018-08-13 19:04:24 +00004069 return setLCDeclAndLB(
4070 Var,
4071 buildDeclRefExpr(SemaRef, Var,
4072 Var->getType().getNonReferenceType(),
4073 DS->getBeginLoc()),
4074 Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004075 }
4076 }
4077 }
David Majnemer9d168222016-08-05 17:44:54 +00004078 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004079 if (CE->getOperator() == OO_Equal) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004080 Expr *LHS = CE->getArg(0);
David Majnemer9d168222016-08-05 17:44:54 +00004081 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004082 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4083 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataeve3727102018-04-18 15:57:46 +00004084 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4085 return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004086 }
4087 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4088 if (ME->isArrow() &&
4089 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
Alexey Bataeve3727102018-04-18 15:57:46 +00004090 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004091 }
4092 }
4093 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004094
Alexey Bataeve3727102018-04-18 15:57:46 +00004095 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004096 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00004097 if (EmitDiags) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004098 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init)
Alexey Bataev9c821032015-04-30 04:23:23 +00004099 << S->getSourceRange();
4100 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004101 return true;
4102}
4103
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004104/// Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004105/// variable (which may be the loop variable) if possible.
Alexey Bataeve3727102018-04-18 15:57:46 +00004106static const ValueDecl *getInitLCDecl(const Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004107 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00004108 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004109 E = getExprAsWritten(E);
Alexey Bataeve3727102018-04-18 15:57:46 +00004110 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004111 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00004112 if ((Ctor->isCopyOrMoveConstructor() ||
4113 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4114 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004115 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00004116 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
4117 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004118 return getCanonicalDecl(VD);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004119 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004120 if (const auto *ME = dyn_cast_or_null<MemberExpr>(E))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004121 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4122 return getCanonicalDecl(ME->getMemberDecl());
4123 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004124}
4125
Alexey Bataeve3727102018-04-18 15:57:46 +00004126bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004127 // Check test-expr for canonical form, save upper-bound UB, flags for
4128 // less/greater and for strict/non-strict comparison.
4129 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4130 // var relational-op b
4131 // b relational-op var
4132 //
4133 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004134 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004135 return true;
4136 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004137 S = getExprAsWritten(S);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004138 SourceLocation CondLoc = S->getBeginLoc();
David Majnemer9d168222016-08-05 17:44:54 +00004139 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004140 if (BO->isRelationalOp()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004141 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4142 return setUB(BO->getRHS(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004143 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
4144 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4145 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +00004146 if (getInitLCDecl(BO->getRHS()) == LCDecl)
4147 return setUB(BO->getLHS(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004148 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
4149 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4150 BO->getSourceRange(), BO->getOperatorLoc());
4151 }
David Majnemer9d168222016-08-05 17:44:54 +00004152 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004153 if (CE->getNumArgs() == 2) {
4154 auto Op = CE->getOperator();
4155 switch (Op) {
4156 case OO_Greater:
4157 case OO_GreaterEqual:
4158 case OO_Less:
4159 case OO_LessEqual:
Alexey Bataeve3727102018-04-18 15:57:46 +00004160 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4161 return setUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004162 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4163 CE->getOperatorLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +00004164 if (getInitLCDecl(CE->getArg(1)) == LCDecl)
4165 return setUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004166 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4167 CE->getOperatorLoc());
4168 break;
4169 default:
4170 break;
4171 }
4172 }
4173 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004174 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004175 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004176 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004177 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004178 return true;
4179}
4180
Alexey Bataeve3727102018-04-18 15:57:46 +00004181bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004182 // RHS of canonical loop form increment can be:
4183 // var + incr
4184 // incr + var
4185 // var - incr
4186 //
4187 RHS = RHS->IgnoreParenImpCasts();
David Majnemer9d168222016-08-05 17:44:54 +00004188 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004189 if (BO->isAdditiveOp()) {
4190 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataeve3727102018-04-18 15:57:46 +00004191 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4192 return setStep(BO->getRHS(), !IsAdd);
4193 if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl)
4194 return setStep(BO->getLHS(), /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004195 }
David Majnemer9d168222016-08-05 17:44:54 +00004196 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004197 bool IsAdd = CE->getOperator() == OO_Plus;
4198 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004199 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4200 return setStep(CE->getArg(1), !IsAdd);
4201 if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl)
4202 return setStep(CE->getArg(0), /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004203 }
4204 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004205 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004206 return false;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004207 SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004208 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004209 return true;
4210}
4211
Alexey Bataeve3727102018-04-18 15:57:46 +00004212bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004213 // Check incr-expr for canonical loop form and return true if it
4214 // does not conform.
4215 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4216 // ++var
4217 // var++
4218 // --var
4219 // var--
4220 // var += incr
4221 // var -= incr
4222 // var = var + incr
4223 // var = incr + var
4224 // var = var - incr
4225 //
4226 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004227 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004228 return true;
4229 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004230 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4231 if (!ExprTemp->cleanupsHaveSideEffects())
4232 S = ExprTemp->getSubExpr();
4233
Alexander Musmana5f070a2014-10-01 06:03:56 +00004234 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004235 S = S->IgnoreParens();
David Majnemer9d168222016-08-05 17:44:54 +00004236 if (auto *UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004237 if (UO->isIncrementDecrementOp() &&
Alexey Bataeve3727102018-04-18 15:57:46 +00004238 getInitLCDecl(UO->getSubExpr()) == LCDecl)
4239 return setStep(SemaRef
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004240 .ActOnIntegerConstant(UO->getBeginLoc(),
David Majnemer9d168222016-08-05 17:44:54 +00004241 (UO->isDecrementOp() ? -1 : 1))
4242 .get(),
Alexey Bataeve3727102018-04-18 15:57:46 +00004243 /*Subtract=*/false);
David Majnemer9d168222016-08-05 17:44:54 +00004244 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004245 switch (BO->getOpcode()) {
4246 case BO_AddAssign:
4247 case BO_SubAssign:
Alexey Bataeve3727102018-04-18 15:57:46 +00004248 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4249 return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004250 break;
4251 case BO_Assign:
Alexey Bataeve3727102018-04-18 15:57:46 +00004252 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4253 return checkAndSetIncRHS(BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004254 break;
4255 default:
4256 break;
4257 }
David Majnemer9d168222016-08-05 17:44:54 +00004258 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004259 switch (CE->getOperator()) {
4260 case OO_PlusPlus:
4261 case OO_MinusMinus:
Alexey Bataeve3727102018-04-18 15:57:46 +00004262 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4263 return setStep(SemaRef
David Majnemer9d168222016-08-05 17:44:54 +00004264 .ActOnIntegerConstant(
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004265 CE->getBeginLoc(),
David Majnemer9d168222016-08-05 17:44:54 +00004266 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
4267 .get(),
Alexey Bataeve3727102018-04-18 15:57:46 +00004268 /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004269 break;
4270 case OO_PlusEqual:
4271 case OO_MinusEqual:
Alexey Bataeve3727102018-04-18 15:57:46 +00004272 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4273 return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004274 break;
4275 case OO_Equal:
Alexey Bataeve3727102018-04-18 15:57:46 +00004276 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4277 return checkAndSetIncRHS(CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004278 break;
4279 default:
4280 break;
4281 }
4282 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004283 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004284 return false;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004285 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004286 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004287 return true;
4288}
Alexander Musmana5f070a2014-10-01 06:03:56 +00004289
Alexey Bataev5a3af132016-03-29 08:58:54 +00004290static ExprResult
4291tryBuildCapture(Sema &SemaRef, Expr *Capture,
Alexey Bataeve3727102018-04-18 15:57:46 +00004292 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00004293 if (SemaRef.CurContext->isDependentContext())
4294 return ExprResult(Capture);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004295 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
4296 return SemaRef.PerformImplicitConversion(
4297 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
4298 /*AllowExplicit=*/true);
4299 auto I = Captures.find(Capture);
4300 if (I != Captures.end())
4301 return buildCapture(SemaRef, Capture, I->second);
4302 DeclRefExpr *Ref = nullptr;
4303 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
4304 Captures[Capture] = Ref;
4305 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004306}
4307
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004308/// Build the expression to calculate the number of iterations.
Alexey Bataeve3727102018-04-18 15:57:46 +00004309Expr *OpenMPIterationSpaceChecker::buildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004310 Scope *S, const bool LimitedType,
Alexey Bataeve3727102018-04-18 15:57:46 +00004311 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004312 ExprResult Diff;
Alexey Bataeve3727102018-04-18 15:57:46 +00004313 QualType VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004314 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004315 SemaRef.getLangOpts().CPlusPlus) {
4316 // Upper - Lower
Alexey Bataeve3727102018-04-18 15:57:46 +00004317 Expr *UBExpr = TestIsLessOp ? UB : LB;
4318 Expr *LBExpr = TestIsLessOp ? LB : UB;
Alexey Bataev5a3af132016-03-29 08:58:54 +00004319 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
4320 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004321 if (!Upper || !Lower)
4322 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004323
4324 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4325
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004326 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004327 // BuildBinOp already emitted error, this one is to point user to upper
4328 // and lower bound, and to tell what is passed to 'operator-'.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004329 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004330 << Upper->getSourceRange() << Lower->getSourceRange();
4331 return nullptr;
4332 }
4333 }
4334
4335 if (!Diff.isUsable())
4336 return nullptr;
4337
4338 // Upper - Lower [- 1]
4339 if (TestIsStrictOp)
4340 Diff = SemaRef.BuildBinOp(
4341 S, DefaultLoc, BO_Sub, Diff.get(),
4342 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4343 if (!Diff.isUsable())
4344 return nullptr;
4345
4346 // Upper - Lower [- 1] + Step
Alexey Bataeve3727102018-04-18 15:57:46 +00004347 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004348 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004349 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004350 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004351 if (!Diff.isUsable())
4352 return nullptr;
4353
4354 // Parentheses (for dumping/debugging purposes only).
4355 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4356 if (!Diff.isUsable())
4357 return nullptr;
4358
4359 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004360 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004361 if (!Diff.isUsable())
4362 return nullptr;
4363
Alexander Musman174b3ca2014-10-06 11:16:29 +00004364 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004365 QualType Type = Diff.get()->getType();
Alexey Bataeve3727102018-04-18 15:57:46 +00004366 ASTContext &C = SemaRef.Context;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004367 bool UseVarType = VarType->hasIntegerRepresentation() &&
4368 C.getTypeSize(Type) > C.getTypeSize(VarType);
4369 if (!Type->isIntegerType() || UseVarType) {
4370 unsigned NewSize =
4371 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4372 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4373 : Type->hasSignedIntegerRepresentation();
4374 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00004375 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4376 Diff = SemaRef.PerformImplicitConversion(
4377 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4378 if (!Diff.isUsable())
4379 return nullptr;
4380 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004381 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004382 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00004383 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4384 if (NewSize != C.getTypeSize(Type)) {
4385 if (NewSize < C.getTypeSize(Type)) {
4386 assert(NewSize == 64 && "incorrect loop var size");
4387 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4388 << InitSrcRange << ConditionSrcRange;
4389 }
4390 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004391 NewSize, Type->hasSignedIntegerRepresentation() ||
4392 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00004393 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4394 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4395 Sema::AA_Converting, true);
4396 if (!Diff.isUsable())
4397 return nullptr;
4398 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004399 }
4400 }
4401
Alexander Musmana5f070a2014-10-01 06:03:56 +00004402 return Diff.get();
4403}
4404
Alexey Bataeve3727102018-04-18 15:57:46 +00004405Expr *OpenMPIterationSpaceChecker::buildPreCond(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004406 Scope *S, Expr *Cond,
Alexey Bataeve3727102018-04-18 15:57:46 +00004407 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004408 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4409 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4410 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004411
Alexey Bataeve3727102018-04-18 15:57:46 +00004412 ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures);
4413 ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004414 if (!NewLB.isUsable() || !NewUB.isUsable())
4415 return nullptr;
4416
Alexey Bataeve3727102018-04-18 15:57:46 +00004417 ExprResult CondExpr =
4418 SemaRef.BuildBinOp(S, DefaultLoc,
4419 TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
4420 : (TestIsStrictOp ? BO_GT : BO_GE),
4421 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004422 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004423 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4424 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00004425 CondExpr = SemaRef.PerformImplicitConversion(
4426 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4427 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004428 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00004429 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4430 // Otherwise use original loop conditon and evaluate it in runtime.
4431 return CondExpr.isUsable() ? CondExpr.get() : Cond;
4432}
4433
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004434/// Build reference expression to the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004435DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar(
Alexey Bataevf138fda2018-08-13 19:04:24 +00004436 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
4437 DSAStackTy &DSA) const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004438 auto *VD = dyn_cast<VarDecl>(LCDecl);
4439 if (!VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004440 VD = SemaRef.isOpenMPCapturedDecl(LCDecl);
4441 DeclRefExpr *Ref = buildDeclRefExpr(
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004442 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
Alexey Bataeve3727102018-04-18 15:57:46 +00004443 const DSAStackTy::DSAVarData Data =
4444 DSA.getTopDSA(LCDecl, /*FromParent=*/false);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004445 // If the loop control decl is explicitly marked as private, do not mark it
4446 // as captured again.
4447 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4448 Captures.insert(std::make_pair(LCRef, Ref));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004449 return Ref;
4450 }
4451 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
Alexey Bataeva8899172015-08-06 12:30:57 +00004452 DefaultLoc);
4453}
4454
Alexey Bataeve3727102018-04-18 15:57:46 +00004455Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004456 if (LCDecl && !LCDecl->isInvalidDecl()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004457 QualType Type = LCDecl->getType().getNonReferenceType();
4458 VarDecl *PrivateVar = buildVarDecl(
Alexey Bataev63cc8e92018-03-20 14:45:59 +00004459 SemaRef, DefaultLoc, Type, LCDecl->getName(),
4460 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr,
4461 isa<VarDecl>(LCDecl)
4462 ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc)
4463 : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00004464 if (PrivateVar->isInvalidDecl())
4465 return nullptr;
4466 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4467 }
4468 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004469}
4470
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004471/// Build initialization of the counter to be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004472Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004473
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004474/// Build step of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004475Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004476
Alexey Bataevf138fda2018-08-13 19:04:24 +00004477Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData(
4478 Scope *S, Expr *Counter,
4479 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc,
4480 Expr *Inc, OverloadedOperatorKind OOK) {
4481 Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get();
4482 if (!Cnt)
4483 return nullptr;
4484 if (Inc) {
4485 assert((OOK == OO_Plus || OOK == OO_Minus) &&
4486 "Expected only + or - operations for depend clauses.");
4487 BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub;
4488 Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get();
4489 if (!Cnt)
4490 return nullptr;
4491 }
4492 ExprResult Diff;
4493 QualType VarType = LCDecl->getType().getNonReferenceType();
4494 if (VarType->isIntegerType() || VarType->isPointerType() ||
4495 SemaRef.getLangOpts().CPlusPlus) {
4496 // Upper - Lower
4497 Expr *Upper =
4498 TestIsLessOp ? Cnt : tryBuildCapture(SemaRef, UB, Captures).get();
4499 Expr *Lower =
4500 TestIsLessOp ? tryBuildCapture(SemaRef, LB, Captures).get() : Cnt;
4501 if (!Upper || !Lower)
4502 return nullptr;
4503
4504 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4505
4506 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
4507 // BuildBinOp already emitted error, this one is to point user to upper
4508 // and lower bound, and to tell what is passed to 'operator-'.
4509 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
4510 << Upper->getSourceRange() << Lower->getSourceRange();
4511 return nullptr;
4512 }
4513 }
4514
4515 if (!Diff.isUsable())
4516 return nullptr;
4517
4518 // Parentheses (for dumping/debugging purposes only).
4519 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4520 if (!Diff.isUsable())
4521 return nullptr;
4522
4523 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
4524 if (!NewStep.isUsable())
4525 return nullptr;
4526 // (Upper - Lower) / Step
4527 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
4528 if (!Diff.isUsable())
4529 return nullptr;
4530
4531 return Diff.get();
4532}
4533
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004534/// Iteration space of a single for loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004535struct LoopIterationSpace final {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004536 /// Condition of the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004537 Expr *PreCond = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004538 /// This expression calculates the number of iterations in the loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004539 /// It is always possible to calculate it before starting the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004540 Expr *NumIterations = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004541 /// The loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004542 Expr *CounterVar = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004543 /// Private loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004544 Expr *PrivateCounterVar = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004545 /// This is initializer for the initial value of #CounterVar.
Alexey Bataev8b427062016-05-25 12:36:08 +00004546 Expr *CounterInit = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004547 /// This is step for the #CounterVar used to generate its update:
Alexander Musmana5f070a2014-10-01 06:03:56 +00004548 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
Alexey Bataev8b427062016-05-25 12:36:08 +00004549 Expr *CounterStep = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004550 /// Should step be subtracted?
Alexey Bataev8b427062016-05-25 12:36:08 +00004551 bool Subtract = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004552 /// Source range of the loop init.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004553 SourceRange InitSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004554 /// Source range of the loop condition.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004555 SourceRange CondSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004556 /// Source range of the loop increment.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004557 SourceRange IncSrcRange;
4558};
4559
Alexey Bataev23b69422014-06-18 07:08:49 +00004560} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004561
Alexey Bataev9c821032015-04-30 04:23:23 +00004562void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4563 assert(getLangOpts().OpenMP && "OpenMP is not active.");
4564 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004565 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4566 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00004567 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4568 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataeve3727102018-04-18 15:57:46 +00004569 if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) {
4570 if (ValueDecl *D = ISC.getLoopDecl()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004571 auto *VD = dyn_cast<VarDecl>(D);
4572 if (!VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004573 if (VarDecl *Private = isOpenMPCapturedDecl(D)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004574 VD = Private;
Alexey Bataeve3727102018-04-18 15:57:46 +00004575 } else {
4576 DeclRefExpr *Ref = buildCapture(*this, D, ISC.getLoopDeclRefExpr(),
4577 /*WithInit=*/false);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004578 VD = cast<VarDecl>(Ref->getDecl());
4579 }
4580 }
4581 DSAStack->addLoopControlVariable(D, VD);
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00004582 const Decl *LD = DSAStack->getPossiblyLoopCunter();
4583 if (LD != D->getCanonicalDecl()) {
4584 DSAStack->resetPossibleLoopCounter();
4585 if (auto *Var = dyn_cast_or_null<VarDecl>(LD))
4586 MarkDeclarationsReferencedInExpr(
4587 buildDeclRefExpr(*this, const_cast<VarDecl *>(Var),
4588 Var->getType().getNonLValueExprType(Context),
4589 ForLoc, /*RefersToCapture=*/true));
4590 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004591 }
4592 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004593 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00004594 }
4595}
4596
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004597/// Called on a for stmt to check and extract its iteration space
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004598/// for further processing (such as collapsing).
Alexey Bataeve3727102018-04-18 15:57:46 +00004599static bool checkOpenMPIterationSpace(
Alexey Bataev4acb8592014-07-07 13:01:15 +00004600 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4601 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataevf138fda2018-08-13 19:04:24 +00004602 unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr,
4603 Expr *OrderedLoopCountExpr,
Alexey Bataeve3727102018-04-18 15:57:46 +00004604 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004605 LoopIterationSpace &ResultIterSpace,
Alexey Bataeve3727102018-04-18 15:57:46 +00004606 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004607 // OpenMP [2.6, Canonical Loop Form]
4608 // for (init-expr; test-expr; incr-expr) structured-block
David Majnemer9d168222016-08-05 17:44:54 +00004609 auto *For = dyn_cast_or_null<ForStmt>(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004610 if (!For) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004611 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004612 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
Alexey Bataevf138fda2018-08-13 19:04:24 +00004613 << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount
Alexey Bataev10e775f2015-07-30 11:36:16 +00004614 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
Alexey Bataevf138fda2018-08-13 19:04:24 +00004615 if (TotalNestedLoopCount > 1) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00004616 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4617 SemaRef.Diag(DSA.getConstructLoc(),
4618 diag::note_omp_collapse_ordered_expr)
4619 << 2 << CollapseLoopCountExpr->getSourceRange()
4620 << OrderedLoopCountExpr->getSourceRange();
4621 else if (CollapseLoopCountExpr)
4622 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4623 diag::note_omp_collapse_ordered_expr)
4624 << 0 << CollapseLoopCountExpr->getSourceRange();
4625 else
4626 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4627 diag::note_omp_collapse_ordered_expr)
4628 << 1 << OrderedLoopCountExpr->getSourceRange();
4629 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004630 return true;
4631 }
4632 assert(For->getBody());
4633
4634 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4635
4636 // Check init.
Alexey Bataeve3727102018-04-18 15:57:46 +00004637 Stmt *Init = For->getInit();
4638 if (ISC.checkAndSetInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004639 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004640
4641 bool HasErrors = false;
4642
4643 // Check loop variable's type.
Alexey Bataeve3727102018-04-18 15:57:46 +00004644 if (ValueDecl *LCDecl = ISC.getLoopDecl()) {
4645 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004646
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004647 // OpenMP [2.6, Canonical Loop Form]
4648 // Var is one of the following:
4649 // A variable of signed or unsigned integer type.
4650 // For C++, a variable of a random access iterator type.
4651 // For C, a variable of a pointer type.
Alexey Bataeve3727102018-04-18 15:57:46 +00004652 QualType VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004653 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4654 !VarType->isPointerType() &&
4655 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004656 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004657 << SemaRef.getLangOpts().CPlusPlus;
4658 HasErrors = true;
4659 }
4660
4661 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4662 // a Construct
4663 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4664 // parallel for construct is (are) private.
4665 // The loop iteration variable in the associated for-loop of a simd
4666 // construct with just one associated for-loop is linear with a
4667 // constant-linear-step that is the increment of the associated for-loop.
4668 // Exclude loop var from the list of variables with implicitly defined data
4669 // sharing attributes.
4670 VarsWithImplicitDSA.erase(LCDecl);
4671
4672 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4673 // in a Construct, C/C++].
4674 // The loop iteration variable in the associated for-loop of a simd
4675 // construct with just one associated for-loop may be listed in a linear
4676 // clause with a constant-linear-step that is the increment of the
4677 // associated for-loop.
4678 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4679 // parallel for construct may be listed in a private or lastprivate clause.
4680 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4681 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4682 // declared in the loop and it is predetermined as a private.
Alexey Bataeve3727102018-04-18 15:57:46 +00004683 OpenMPClauseKind PredeterminedCKind =
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004684 isOpenMPSimdDirective(DKind)
4685 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4686 : OMPC_private;
4687 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4688 DVar.CKind != PredeterminedCKind) ||
4689 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4690 isOpenMPDistributeDirective(DKind)) &&
4691 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4692 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4693 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004694 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004695 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4696 << getOpenMPClauseName(PredeterminedCKind);
4697 if (DVar.RefExpr == nullptr)
4698 DVar.CKind = PredeterminedCKind;
Alexey Bataeve3727102018-04-18 15:57:46 +00004699 reportOriginalDsa(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004700 HasErrors = true;
4701 } else if (LoopDeclRefExpr != nullptr) {
4702 // Make the loop iteration variable private (for worksharing constructs),
4703 // linear (for simd directives with the only one associated loop) or
4704 // lastprivate (for simd directives with several collapsed or ordered
4705 // loops).
4706 if (DVar.CKind == OMPC_unknown)
Alexey Bataev7ace49d2016-05-17 08:55:33 +00004707 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate,
4708 [](OpenMPDirectiveKind) -> bool { return true; },
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004709 /*FromParent=*/false);
4710 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4711 }
4712
4713 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4714
4715 // Check test-expr.
Alexey Bataeve3727102018-04-18 15:57:46 +00004716 HasErrors |= ISC.checkAndSetCond(For->getCond());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004717
4718 // Check incr-expr.
Alexey Bataeve3727102018-04-18 15:57:46 +00004719 HasErrors |= ISC.checkAndSetInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004720 }
4721
Alexey Bataeve3727102018-04-18 15:57:46 +00004722 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004723 return HasErrors;
4724
Alexander Musmana5f070a2014-10-01 06:03:56 +00004725 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004726 ResultIterSpace.PreCond =
Alexey Bataeve3727102018-04-18 15:57:46 +00004727 ISC.buildPreCond(DSA.getCurScope(), For->getCond(), Captures);
4728 ResultIterSpace.NumIterations = ISC.buildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004729 DSA.getCurScope(),
4730 (isOpenMPWorksharingDirective(DKind) ||
4731 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4732 Captures);
Alexey Bataeve3727102018-04-18 15:57:46 +00004733 ResultIterSpace.CounterVar = ISC.buildCounterVar(Captures, DSA);
4734 ResultIterSpace.PrivateCounterVar = ISC.buildPrivateCounterVar();
4735 ResultIterSpace.CounterInit = ISC.buildCounterInit();
4736 ResultIterSpace.CounterStep = ISC.buildCounterStep();
4737 ResultIterSpace.InitSrcRange = ISC.getInitSrcRange();
4738 ResultIterSpace.CondSrcRange = ISC.getConditionSrcRange();
4739 ResultIterSpace.IncSrcRange = ISC.getIncrementSrcRange();
4740 ResultIterSpace.Subtract = ISC.shouldSubtractStep();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004741
Alexey Bataev62dbb972015-04-22 11:59:37 +00004742 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4743 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004744 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004745 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004746 ResultIterSpace.CounterInit == nullptr ||
4747 ResultIterSpace.CounterStep == nullptr);
Alexey Bataevf138fda2018-08-13 19:04:24 +00004748 if (!HasErrors && DSA.isOrderedRegion()) {
4749 if (DSA.getOrderedRegionParam().second->getNumForLoops()) {
4750 if (CurrentNestedLoopCount <
4751 DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) {
4752 DSA.getOrderedRegionParam().second->setLoopNumIterations(
4753 CurrentNestedLoopCount, ResultIterSpace.NumIterations);
4754 DSA.getOrderedRegionParam().second->setLoopCounter(
4755 CurrentNestedLoopCount, ResultIterSpace.CounterVar);
4756 }
4757 }
4758 for (auto &Pair : DSA.getDoacrossDependClauses()) {
4759 if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) {
4760 // Erroneous case - clause has some problems.
4761 continue;
4762 }
4763 if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink &&
4764 Pair.second.size() <= CurrentNestedLoopCount) {
4765 // Erroneous case - clause has some problems.
4766 Pair.first->setLoopData(CurrentNestedLoopCount, nullptr);
4767 continue;
4768 }
4769 Expr *CntValue;
4770 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
4771 CntValue = ISC.buildOrderedLoopData(
4772 DSA.getCurScope(), ResultIterSpace.CounterVar, Captures,
4773 Pair.first->getDependencyLoc());
4774 else
4775 CntValue = ISC.buildOrderedLoopData(
4776 DSA.getCurScope(), ResultIterSpace.CounterVar, Captures,
4777 Pair.first->getDependencyLoc(),
4778 Pair.second[CurrentNestedLoopCount].first,
4779 Pair.second[CurrentNestedLoopCount].second);
4780 Pair.first->setLoopData(CurrentNestedLoopCount, CntValue);
4781 }
4782 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004783
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004784 return HasErrors;
4785}
4786
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004787/// Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004788static ExprResult
Alexey Bataeve3727102018-04-18 15:57:46 +00004789buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004790 ExprResult Start,
Alexey Bataeve3727102018-04-18 15:57:46 +00004791 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004792 // Build 'VarRef = Start.
Alexey Bataeve3727102018-04-18 15:57:46 +00004793 ExprResult NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004794 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004795 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004796 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00004797 VarRef.get()->getType())) {
4798 NewStart = SemaRef.PerformImplicitConversion(
4799 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4800 /*AllowExplicit=*/true);
4801 if (!NewStart.isUsable())
4802 return ExprError();
4803 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004804
Alexey Bataeve3727102018-04-18 15:57:46 +00004805 ExprResult Init =
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004806 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4807 return Init;
4808}
4809
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004810/// Build 'VarRef = Start + Iter * Step'.
Alexey Bataeve3727102018-04-18 15:57:46 +00004811static ExprResult buildCounterUpdate(
4812 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4813 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract,
4814 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004815 // Add parentheses (for debugging purposes only).
4816 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4817 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4818 !Step.isUsable())
4819 return ExprError();
4820
Alexey Bataev5a3af132016-03-29 08:58:54 +00004821 ExprResult NewStep = Step;
4822 if (Captures)
4823 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004824 if (NewStep.isInvalid())
4825 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004826 ExprResult Update =
4827 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004828 if (!Update.isUsable())
4829 return ExprError();
4830
Alexey Bataevc0214e02016-02-16 12:13:49 +00004831 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4832 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004833 ExprResult NewStart = Start;
4834 if (Captures)
4835 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004836 if (NewStart.isInvalid())
4837 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004838
Alexey Bataevc0214e02016-02-16 12:13:49 +00004839 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4840 ExprResult SavedUpdate = Update;
4841 ExprResult UpdateVal;
4842 if (VarRef.get()->getType()->isOverloadableType() ||
4843 NewStart.get()->getType()->isOverloadableType() ||
4844 Update.get()->getType()->isOverloadableType()) {
4845 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4846 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4847 Update =
4848 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4849 if (Update.isUsable()) {
4850 UpdateVal =
4851 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4852 VarRef.get(), SavedUpdate.get());
4853 if (UpdateVal.isUsable()) {
4854 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4855 UpdateVal.get());
4856 }
4857 }
4858 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4859 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004860
Alexey Bataevc0214e02016-02-16 12:13:49 +00004861 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4862 if (!Update.isUsable() || !UpdateVal.isUsable()) {
4863 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4864 NewStart.get(), SavedUpdate.get());
4865 if (!Update.isUsable())
4866 return ExprError();
4867
Alexey Bataev11481f52016-02-17 10:29:05 +00004868 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4869 VarRef.get()->getType())) {
4870 Update = SemaRef.PerformImplicitConversion(
4871 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4872 if (!Update.isUsable())
4873 return ExprError();
4874 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00004875
4876 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4877 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004878 return Update;
4879}
4880
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004881/// Convert integer expression \a E to make it have at least \a Bits
Alexander Musmana5f070a2014-10-01 06:03:56 +00004882/// bits.
Alexey Bataeve3727102018-04-18 15:57:46 +00004883static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004884 if (E == nullptr)
4885 return ExprError();
Alexey Bataeve3727102018-04-18 15:57:46 +00004886 ASTContext &C = SemaRef.Context;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004887 QualType OldType = E->getType();
4888 unsigned HasBits = C.getTypeSize(OldType);
4889 if (HasBits >= Bits)
4890 return ExprResult(E);
4891 // OK to convert to signed, because new type has more bits than old.
4892 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4893 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4894 true);
4895}
4896
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004897/// Check if the given expression \a E is a constant integer that fits
Alexander Musmana5f070a2014-10-01 06:03:56 +00004898/// into \a Bits bits.
Alexey Bataeve3727102018-04-18 15:57:46 +00004899static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004900 if (E == nullptr)
4901 return false;
4902 llvm::APSInt Result;
4903 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4904 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4905 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004906}
4907
Alexey Bataev5a3af132016-03-29 08:58:54 +00004908/// Build preinits statement for the given declarations.
4909static Stmt *buildPreInits(ASTContext &Context,
Alexey Bataevc5514062017-10-25 15:44:52 +00004910 MutableArrayRef<Decl *> PreInits) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004911 if (!PreInits.empty()) {
4912 return new (Context) DeclStmt(
4913 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
4914 SourceLocation(), SourceLocation());
4915 }
4916 return nullptr;
4917}
4918
4919/// Build preinits statement for the given declarations.
Alexey Bataevc5514062017-10-25 15:44:52 +00004920static Stmt *
4921buildPreInits(ASTContext &Context,
Alexey Bataeve3727102018-04-18 15:57:46 +00004922 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004923 if (!Captures.empty()) {
4924 SmallVector<Decl *, 16> PreInits;
Alexey Bataeve3727102018-04-18 15:57:46 +00004925 for (const auto &Pair : Captures)
Alexey Bataev5a3af132016-03-29 08:58:54 +00004926 PreInits.push_back(Pair.second->getDecl());
4927 return buildPreInits(Context, PreInits);
4928 }
4929 return nullptr;
4930}
4931
4932/// Build postupdate expression for the given list of postupdates expressions.
4933static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
4934 Expr *PostUpdate = nullptr;
4935 if (!PostUpdates.empty()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004936 for (Expr *E : PostUpdates) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004937 Expr *ConvE = S.BuildCStyleCastExpr(
4938 E->getExprLoc(),
4939 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
4940 E->getExprLoc(), E)
4941 .get();
4942 PostUpdate = PostUpdate
4943 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
4944 PostUpdate, ConvE)
4945 .get()
4946 : ConvE;
4947 }
4948 }
4949 return PostUpdate;
4950}
4951
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004952/// Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00004953/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4954/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004955static unsigned
Alexey Bataeve3727102018-04-18 15:57:46 +00004956checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004957 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4958 DSAStackTy &DSA,
Alexey Bataeve3727102018-04-18 15:57:46 +00004959 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00004960 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004961 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004962 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004963 // Found 'collapse' clause - calculate collapse number.
4964 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004965 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004966 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004967 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00004968 unsigned OrderedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004969 if (OrderedLoopCountExpr) {
4970 // Found 'ordered' clause - calculate collapse number.
4971 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004972 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4973 if (Result.getLimitedValue() < NestedLoopCount) {
4974 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4975 diag::err_omp_wrong_ordered_loop_count)
4976 << OrderedLoopCountExpr->getSourceRange();
4977 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4978 diag::note_collapse_loop_count)
4979 << CollapseLoopCountExpr->getSourceRange();
4980 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00004981 OrderedLoopCount = Result.getLimitedValue();
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004982 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004983 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004984 // This is helper routine for loop directives (e.g., 'for', 'simd',
4985 // 'for simd', etc.).
Alexey Bataeve3727102018-04-18 15:57:46 +00004986 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004987 SmallVector<LoopIterationSpace, 4> IterSpaces;
Alexey Bataevf138fda2018-08-13 19:04:24 +00004988 IterSpaces.resize(std::max(OrderedLoopCount, NestedLoopCount));
Alexander Musmana5f070a2014-10-01 06:03:56 +00004989 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004990 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataevf138fda2018-08-13 19:04:24 +00004991 if (checkOpenMPIterationSpace(
4992 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
4993 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
4994 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt],
4995 Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00004996 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004997 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004998 // OpenMP [2.8.1, simd construct, Restrictions]
4999 // All loops associated with the construct must be perfectly nested; that
5000 // is, there must be no intervening code nor any OpenMP directive between
5001 // any two loops.
5002 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005003 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00005004 for (unsigned Cnt = NestedLoopCount; Cnt < OrderedLoopCount; ++Cnt) {
5005 if (checkOpenMPIterationSpace(
5006 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
5007 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
5008 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt],
5009 Captures))
5010 return 0;
5011 if (Cnt > 0 && IterSpaces[Cnt].CounterVar) {
5012 // Handle initialization of captured loop iterator variables.
5013 auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar);
5014 if (isa<OMPCapturedExprDecl>(DRE->getDecl())) {
5015 Captures[DRE] = DRE;
5016 }
5017 }
5018 // Move on to the next nested for loop, or to the loop body.
5019 // OpenMP [2.8.1, simd construct, Restrictions]
5020 // All loops associated with the construct must be perfectly nested; that
5021 // is, there must be no intervening code nor any OpenMP directive between
5022 // any two loops.
5023 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
5024 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005025
Alexander Musmana5f070a2014-10-01 06:03:56 +00005026 Built.clear(/* size */ NestedLoopCount);
5027
5028 if (SemaRef.CurContext->isDependentContext())
5029 return NestedLoopCount;
5030
5031 // An example of what is generated for the following code:
5032 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00005033 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00005034 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00005035 // for (k = 0; k < NK; ++k)
5036 // for (j = J0; j < NJ; j+=2) {
5037 // <loop body>
5038 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005039 //
5040 // We generate the code below.
5041 // Note: the loop body may be outlined in CodeGen.
5042 // Note: some counters may be C++ classes, operator- is used to find number of
5043 // iterations and operator+= to calculate counter value.
5044 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
5045 // or i64 is currently supported).
5046 //
5047 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
5048 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
5049 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
5050 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
5051 // // similar updates for vars in clauses (e.g. 'linear')
5052 // <loop body (using local i and j)>
5053 // }
5054 // i = NI; // assign final values of counters
5055 // j = NJ;
5056 //
5057
5058 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
5059 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00005060 // Precondition tests if there is at least one iteration (all conditions are
5061 // true).
5062 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexey Bataeve3727102018-04-18 15:57:46 +00005063 Expr *N0 = IterSpaces[0].NumIterations;
5064 ExprResult LastIteration32 =
5065 widenIterationCount(/*Bits=*/32,
5066 SemaRef
5067 .PerformImplicitConversion(
5068 N0->IgnoreImpCasts(), N0->getType(),
5069 Sema::AA_Converting, /*AllowExplicit=*/true)
5070 .get(),
5071 SemaRef);
5072 ExprResult LastIteration64 = widenIterationCount(
5073 /*Bits=*/64,
5074 SemaRef
5075 .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(),
5076 Sema::AA_Converting,
5077 /*AllowExplicit=*/true)
5078 .get(),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005079 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005080
5081 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
5082 return NestedLoopCount;
5083
Alexey Bataeve3727102018-04-18 15:57:46 +00005084 ASTContext &C = SemaRef.Context;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005085 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
5086
5087 Scope *CurScope = DSA.getCurScope();
5088 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00005089 if (PreCond.isUsable()) {
Alexey Bataeva7206b92016-12-20 16:51:02 +00005090 PreCond =
5091 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd,
5092 PreCond.get(), IterSpaces[Cnt].PreCond);
Alexey Bataev62dbb972015-04-22 11:59:37 +00005093 }
Alexey Bataeve3727102018-04-18 15:57:46 +00005094 Expr *N = IterSpaces[Cnt].NumIterations;
Alexey Bataeva7206b92016-12-20 16:51:02 +00005095 SourceLocation Loc = N->getExprLoc();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005096 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
5097 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005098 LastIteration32 = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005099 CurScope, Loc, BO_Mul, LastIteration32.get(),
David Majnemer9d168222016-08-05 17:44:54 +00005100 SemaRef
5101 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5102 Sema::AA_Converting,
5103 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005104 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005105 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005106 LastIteration64 = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005107 CurScope, Loc, BO_Mul, LastIteration64.get(),
David Majnemer9d168222016-08-05 17:44:54 +00005108 SemaRef
5109 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5110 Sema::AA_Converting,
5111 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005112 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005113 }
5114
5115 // Choose either the 32-bit or 64-bit version.
5116 ExprResult LastIteration = LastIteration64;
5117 if (LastIteration32.isUsable() &&
5118 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
5119 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
Alexey Bataeve3727102018-04-18 15:57:46 +00005120 fitsInto(
5121 /*Bits=*/32,
Alexander Musmana5f070a2014-10-01 06:03:56 +00005122 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
5123 LastIteration64.get(), SemaRef)))
5124 LastIteration = LastIteration32;
Alexey Bataev7292c292016-04-25 12:22:29 +00005125 QualType VType = LastIteration.get()->getType();
5126 QualType RealVType = VType;
5127 QualType StrideVType = VType;
5128 if (isOpenMPTaskLoopDirective(DKind)) {
5129 VType =
5130 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
5131 StrideVType =
5132 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
5133 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005134
5135 if (!LastIteration.isUsable())
5136 return 0;
5137
5138 // Save the number of iterations.
5139 ExprResult NumIterations = LastIteration;
5140 {
5141 LastIteration = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005142 CurScope, LastIteration.get()->getExprLoc(), BO_Sub,
5143 LastIteration.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00005144 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5145 if (!LastIteration.isUsable())
5146 return 0;
5147 }
5148
5149 // Calculate the last iteration number beforehand instead of doing this on
5150 // each iteration. Do not do this if the number of iterations may be kfold-ed.
5151 llvm::APSInt Result;
5152 bool IsConstant =
5153 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
5154 ExprResult CalcLastIteration;
5155 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005156 ExprResult SaveRef =
5157 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005158 LastIteration = SaveRef;
5159
5160 // Prepare SaveRef + 1.
5161 NumIterations = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005162 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00005163 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5164 if (!NumIterations.isUsable())
5165 return 0;
5166 }
5167
5168 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
5169
David Majnemer9d168222016-08-05 17:44:54 +00005170 // Build variables passed into runtime, necessary for worksharing directives.
Carlo Bertolliffafe102017-04-20 00:39:39 +00005171 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005172 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5173 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005174 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005175 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
5176 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005177 SemaRef.AddInitializerToDecl(LBDecl,
5178 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5179 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005180
5181 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005182 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
5183 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005184 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
Richard Smith3beb7c62017-01-12 02:27:38 +00005185 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005186
5187 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
5188 // This will be used to implement clause 'lastprivate'.
5189 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005190 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
5191 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005192 SemaRef.AddInitializerToDecl(ILDecl,
5193 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5194 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005195
5196 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev7292c292016-04-25 12:22:29 +00005197 VarDecl *STDecl =
5198 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
5199 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005200 SemaRef.AddInitializerToDecl(STDecl,
5201 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
5202 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005203
5204 // Build expression: UB = min(UB, LastIteration)
David Majnemer9d168222016-08-05 17:44:54 +00005205 // It is necessary for CodeGen of directives with static scheduling.
Alexander Musmanc6388682014-12-15 07:07:06 +00005206 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
5207 UB.get(), LastIteration.get());
5208 ExprResult CondOp = SemaRef.ActOnConditionalOp(
Alexey Bataev86ec3fe2018-07-25 14:40:26 +00005209 LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(),
5210 LastIteration.get(), UB.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00005211 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
5212 CondOp.get());
5213 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
Carlo Bertolli9925f152016-06-27 14:55:37 +00005214
5215 // If we have a combined directive that combines 'distribute', 'for' or
5216 // 'simd' we need to be able to access the bounds of the schedule of the
5217 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
5218 // by scheduling 'distribute' have to be passed to the schedule of 'for'.
5219 if (isOpenMPLoopBoundSharingDirective(DKind)) {
Carlo Bertolliffafe102017-04-20 00:39:39 +00005220 // Lower bound variable, initialized with zero.
5221 VarDecl *CombLBDecl =
5222 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb");
5223 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc);
5224 SemaRef.AddInitializerToDecl(
5225 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5226 /*DirectInit*/ false);
5227
5228 // Upper bound variable, initialized with last iteration number.
5229 VarDecl *CombUBDecl =
5230 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub");
5231 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc);
5232 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(),
5233 /*DirectInit*/ false);
5234
5235 ExprResult CombIsUBGreater = SemaRef.BuildBinOp(
5236 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get());
5237 ExprResult CombCondOp =
5238 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(),
5239 LastIteration.get(), CombUB.get());
5240 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(),
5241 CombCondOp.get());
5242 CombEUB = SemaRef.ActOnFinishFullExpr(CombEUB.get());
5243
Alexey Bataeve3727102018-04-18 15:57:46 +00005244 const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005245 // We expect to have at least 2 more parameters than the 'parallel'
5246 // directive does - the lower and upper bounds of the previous schedule.
5247 assert(CD->getNumParams() >= 4 &&
5248 "Unexpected number of parameters in loop combined directive");
5249
5250 // Set the proper type for the bounds given what we learned from the
5251 // enclosed loops.
Alexey Bataeve3727102018-04-18 15:57:46 +00005252 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
5253 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
Carlo Bertolli9925f152016-06-27 14:55:37 +00005254
5255 // Previous lower and upper bounds are obtained from the region
5256 // parameters.
5257 PrevLB =
5258 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
5259 PrevUB =
5260 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
5261 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005262 }
5263
5264 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005265 ExprResult IV;
Carlo Bertolliffafe102017-04-20 00:39:39 +00005266 ExprResult Init, CombInit;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005267 {
Alexey Bataev7292c292016-04-25 12:22:29 +00005268 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
5269 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
David Majnemer9d168222016-08-05 17:44:54 +00005270 Expr *RHS =
5271 (isOpenMPWorksharingDirective(DKind) ||
5272 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
5273 ? LB.get()
5274 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005275 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
5276 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Carlo Bertolliffafe102017-04-20 00:39:39 +00005277
5278 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5279 Expr *CombRHS =
5280 (isOpenMPWorksharingDirective(DKind) ||
5281 isOpenMPTaskLoopDirective(DKind) ||
5282 isOpenMPDistributeDirective(DKind))
5283 ? CombLB.get()
5284 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
5285 CombInit =
5286 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS);
5287 CombInit = SemaRef.ActOnFinishFullExpr(CombInit.get());
5288 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005289 }
5290
Alexander Musmanc6388682014-12-15 07:07:06 +00005291 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005292 SourceLocation CondLoc = AStmt->getBeginLoc();
Alexander Musmanc6388682014-12-15 07:07:06 +00005293 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005294 (isOpenMPWorksharingDirective(DKind) ||
5295 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00005296 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
5297 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
5298 NumIterations.get());
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005299 ExprResult CombDistCond;
5300 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5301 CombDistCond =
5302 SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), NumIterations.get());
5303 }
5304
Carlo Bertolliffafe102017-04-20 00:39:39 +00005305 ExprResult CombCond;
5306 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5307 CombCond =
5308 SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), CombUB.get());
5309 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005310 // Loop increment (IV = IV + 1)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005311 SourceLocation IncLoc = AStmt->getBeginLoc();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005312 ExprResult Inc =
5313 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
5314 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
5315 if (!Inc.isUsable())
5316 return 0;
5317 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00005318 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
5319 if (!Inc.isUsable())
5320 return 0;
5321
5322 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
5323 // Used for directives with static scheduling.
Carlo Bertolliffafe102017-04-20 00:39:39 +00005324 // In combined construct, add combined version that use CombLB and CombUB
5325 // base variables for the update
5326 ExprResult NextLB, NextUB, CombNextLB, CombNextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005327 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5328 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005329 // LB + ST
5330 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
5331 if (!NextLB.isUsable())
5332 return 0;
5333 // LB = LB + ST
5334 NextLB =
5335 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
5336 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
5337 if (!NextLB.isUsable())
5338 return 0;
5339 // UB + ST
5340 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
5341 if (!NextUB.isUsable())
5342 return 0;
5343 // UB = UB + ST
5344 NextUB =
5345 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
5346 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
5347 if (!NextUB.isUsable())
5348 return 0;
Carlo Bertolliffafe102017-04-20 00:39:39 +00005349 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5350 CombNextLB =
5351 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get());
5352 if (!NextLB.isUsable())
5353 return 0;
5354 // LB = LB + ST
5355 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(),
5356 CombNextLB.get());
5357 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get());
5358 if (!CombNextLB.isUsable())
5359 return 0;
5360 // UB + ST
5361 CombNextUB =
5362 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get());
5363 if (!CombNextUB.isUsable())
5364 return 0;
5365 // UB = UB + ST
5366 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(),
5367 CombNextUB.get());
5368 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get());
5369 if (!CombNextUB.isUsable())
5370 return 0;
5371 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005372 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005373
Carlo Bertolliffafe102017-04-20 00:39:39 +00005374 // Create increment expression for distribute loop when combined in a same
Carlo Bertolli8429d812017-02-17 21:29:13 +00005375 // directive with for as IV = IV + ST; ensure upper bound expression based
5376 // on PrevUB instead of NumIterations - used to implement 'for' when found
5377 // in combination with 'distribute', like in 'distribute parallel for'
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005378 SourceLocation DistIncLoc = AStmt->getBeginLoc();
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005379 ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond;
Carlo Bertolli8429d812017-02-17 21:29:13 +00005380 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5381 DistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get());
5382 assert(DistCond.isUsable() && "distribute cond expr was not built");
5383
5384 DistInc =
5385 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get());
5386 assert(DistInc.isUsable() && "distribute inc expr was not built");
5387 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(),
5388 DistInc.get());
5389 DistInc = SemaRef.ActOnFinishFullExpr(DistInc.get());
5390 assert(DistInc.isUsable() && "distribute inc expr was not built");
5391
5392 // Build expression: UB = min(UB, prevUB) for #for in composite or combined
5393 // construct
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005394 SourceLocation DistEUBLoc = AStmt->getBeginLoc();
Carlo Bertolli8429d812017-02-17 21:29:13 +00005395 ExprResult IsUBGreater =
5396 SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get());
5397 ExprResult CondOp = SemaRef.ActOnConditionalOp(
5398 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get());
5399 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(),
5400 CondOp.get());
5401 PrevEUB = SemaRef.ActOnFinishFullExpr(PrevEUB.get());
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005402
5403 // Build IV <= PrevUB to be used in parallel for is in combination with
5404 // a distribute directive with schedule(static, 1)
5405 ParForInDistCond =
5406 SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), PrevUB.get());
Carlo Bertolli8429d812017-02-17 21:29:13 +00005407 }
5408
Alexander Musmana5f070a2014-10-01 06:03:56 +00005409 // Build updates and final values of the loop counters.
5410 bool HasErrors = false;
5411 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005412 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005413 Built.Updates.resize(NestedLoopCount);
5414 Built.Finals.resize(NestedLoopCount);
5415 {
5416 ExprResult Div;
5417 // Go from inner nested loop to outer.
5418 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5419 LoopIterationSpace &IS = IterSpaces[Cnt];
5420 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
5421 // Build: Iter = (IV / Div) % IS.NumIters
5422 // where Div is product of previous iterations' IS.NumIters.
5423 ExprResult Iter;
5424 if (Div.isUsable()) {
5425 Iter =
5426 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
5427 } else {
5428 Iter = IV;
5429 assert((Cnt == (int)NestedLoopCount - 1) &&
5430 "unusable div expected on first iteration only");
5431 }
5432
5433 if (Cnt != 0 && Iter.isUsable())
5434 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
5435 IS.NumIterations);
5436 if (!Iter.isUsable()) {
5437 HasErrors = true;
5438 break;
5439 }
5440
Alexey Bataev39f915b82015-05-08 10:41:21 +00005441 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005442 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00005443 DeclRefExpr *CounterVar = buildDeclRefExpr(
5444 SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
5445 /*RefersToCapture=*/true);
5446 ExprResult Init = buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005447 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005448 if (!Init.isUsable()) {
5449 HasErrors = true;
5450 break;
5451 }
Alexey Bataeve3727102018-04-18 15:57:46 +00005452 ExprResult Update = buildCounterUpdate(
Alexey Bataev5a3af132016-03-29 08:58:54 +00005453 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
5454 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005455 if (!Update.isUsable()) {
5456 HasErrors = true;
5457 break;
5458 }
5459
5460 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
Alexey Bataeve3727102018-04-18 15:57:46 +00005461 ExprResult Final = buildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00005462 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005463 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005464 if (!Final.isUsable()) {
5465 HasErrors = true;
5466 break;
5467 }
5468
5469 // Build Div for the next iteration: Div <- Div * IS.NumIters
5470 if (Cnt != 0) {
5471 if (Div.isUnset())
5472 Div = IS.NumIterations;
5473 else
5474 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
5475 IS.NumIterations);
5476
5477 // Add parentheses (for debugging purposes only).
5478 if (Div.isUsable())
Alexey Bataev8b427062016-05-25 12:36:08 +00005479 Div = tryBuildCapture(SemaRef, Div.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005480 if (!Div.isUsable()) {
5481 HasErrors = true;
5482 break;
5483 }
5484 }
5485 if (!Update.isUsable() || !Final.isUsable()) {
5486 HasErrors = true;
5487 break;
5488 }
5489 // Save results
5490 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00005491 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005492 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005493 Built.Updates[Cnt] = Update.get();
5494 Built.Finals[Cnt] = Final.get();
5495 }
5496 }
5497
5498 if (HasErrors)
5499 return 0;
5500
5501 // Save results
5502 Built.IterationVarRef = IV.get();
5503 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00005504 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00005505 Built.CalcLastIteration =
5506 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005507 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00005508 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005509 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005510 Built.Init = Init.get();
5511 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005512 Built.LB = LB.get();
5513 Built.UB = UB.get();
5514 Built.IL = IL.get();
5515 Built.ST = ST.get();
5516 Built.EUB = EUB.get();
5517 Built.NLB = NextLB.get();
5518 Built.NUB = NextUB.get();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005519 Built.PrevLB = PrevLB.get();
5520 Built.PrevUB = PrevUB.get();
Carlo Bertolli8429d812017-02-17 21:29:13 +00005521 Built.DistInc = DistInc.get();
5522 Built.PrevEUB = PrevEUB.get();
Carlo Bertolliffafe102017-04-20 00:39:39 +00005523 Built.DistCombinedFields.LB = CombLB.get();
5524 Built.DistCombinedFields.UB = CombUB.get();
5525 Built.DistCombinedFields.EUB = CombEUB.get();
5526 Built.DistCombinedFields.Init = CombInit.get();
5527 Built.DistCombinedFields.Cond = CombCond.get();
5528 Built.DistCombinedFields.NLB = CombNextLB.get();
5529 Built.DistCombinedFields.NUB = CombNextUB.get();
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005530 Built.DistCombinedFields.DistCond = CombDistCond.get();
5531 Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005532
Alexey Bataevabfc0692014-06-25 06:52:00 +00005533 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005534}
5535
Alexey Bataev10e775f2015-07-30 11:36:16 +00005536static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005537 auto CollapseClauses =
5538 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
5539 if (CollapseClauses.begin() != CollapseClauses.end())
5540 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005541 return nullptr;
5542}
5543
Alexey Bataev10e775f2015-07-30 11:36:16 +00005544static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005545 auto OrderedClauses =
5546 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
5547 if (OrderedClauses.begin() != OrderedClauses.end())
5548 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005549 return nullptr;
5550}
5551
Kelvin Lic5609492016-07-15 04:39:07 +00005552static bool checkSimdlenSafelenSpecified(Sema &S,
5553 const ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00005554 const OMPSafelenClause *Safelen = nullptr;
5555 const OMPSimdlenClause *Simdlen = nullptr;
Kelvin Lic5609492016-07-15 04:39:07 +00005556
Alexey Bataeve3727102018-04-18 15:57:46 +00005557 for (const OMPClause *Clause : Clauses) {
Kelvin Lic5609492016-07-15 04:39:07 +00005558 if (Clause->getClauseKind() == OMPC_safelen)
5559 Safelen = cast<OMPSafelenClause>(Clause);
5560 else if (Clause->getClauseKind() == OMPC_simdlen)
5561 Simdlen = cast<OMPSimdlenClause>(Clause);
5562 if (Safelen && Simdlen)
5563 break;
5564 }
5565
5566 if (Simdlen && Safelen) {
5567 llvm::APSInt SimdlenRes, SafelenRes;
Alexey Bataeve3727102018-04-18 15:57:46 +00005568 const Expr *SimdlenLength = Simdlen->getSimdlen();
5569 const Expr *SafelenLength = Safelen->getSafelen();
Kelvin Lic5609492016-07-15 04:39:07 +00005570 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
5571 SimdlenLength->isInstantiationDependent() ||
5572 SimdlenLength->containsUnexpandedParameterPack())
5573 return false;
5574 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
5575 SafelenLength->isInstantiationDependent() ||
5576 SafelenLength->containsUnexpandedParameterPack())
5577 return false;
5578 SimdlenLength->EvaluateAsInt(SimdlenRes, S.Context);
5579 SafelenLength->EvaluateAsInt(SafelenRes, S.Context);
5580 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
5581 // If both simdlen and safelen clauses are specified, the value of the
5582 // simdlen parameter must be less than or equal to the value of the safelen
5583 // parameter.
5584 if (SimdlenRes > SafelenRes) {
5585 S.Diag(SimdlenLength->getExprLoc(),
5586 diag::err_omp_wrong_simdlen_safelen_values)
5587 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
5588 return true;
5589 }
Alexey Bataev66b15b52015-08-21 11:14:16 +00005590 }
5591 return false;
5592}
5593
Alexey Bataeve3727102018-04-18 15:57:46 +00005594StmtResult
5595Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
5596 SourceLocation StartLoc, SourceLocation EndLoc,
5597 VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005598 if (!AStmt)
5599 return StmtError();
5600
5601 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005602 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005603 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5604 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00005605 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataev10e775f2015-07-30 11:36:16 +00005606 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5607 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005608 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005609 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005610
Alexander Musmana5f070a2014-10-01 06:03:56 +00005611 assert((CurContext->isDependentContext() || B.builtAll()) &&
5612 "omp simd loop exprs were not built");
5613
Alexander Musman3276a272015-03-21 10:12:56 +00005614 if (!CurContext->isDependentContext()) {
5615 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005616 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005617 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexander Musman3276a272015-03-21 10:12:56 +00005618 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005619 B.NumIterations, *this, CurScope,
5620 DSAStack))
Alexander Musman3276a272015-03-21 10:12:56 +00005621 return StmtError();
5622 }
5623 }
5624
Kelvin Lic5609492016-07-15 04:39:07 +00005625 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005626 return StmtError();
5627
Reid Kleckner87a31802018-03-12 21:43:02 +00005628 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005629 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5630 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005631}
5632
Alexey Bataeve3727102018-04-18 15:57:46 +00005633StmtResult
5634Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
5635 SourceLocation StartLoc, SourceLocation EndLoc,
5636 VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005637 if (!AStmt)
5638 return StmtError();
5639
5640 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005641 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005642 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5643 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00005644 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataev10e775f2015-07-30 11:36:16 +00005645 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5646 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005647 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005648 return StmtError();
5649
Alexander Musmana5f070a2014-10-01 06:03:56 +00005650 assert((CurContext->isDependentContext() || B.builtAll()) &&
5651 "omp for loop exprs were not built");
5652
Alexey Bataev54acd402015-08-04 11:18:19 +00005653 if (!CurContext->isDependentContext()) {
5654 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005655 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005656 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00005657 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005658 B.NumIterations, *this, CurScope,
5659 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005660 return StmtError();
5661 }
5662 }
5663
Reid Kleckner87a31802018-03-12 21:43:02 +00005664 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005665 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005666 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005667}
5668
Alexander Musmanf82886e2014-09-18 05:12:34 +00005669StmtResult Sema::ActOnOpenMPForSimdDirective(
5670 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00005671 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005672 if (!AStmt)
5673 return StmtError();
5674
5675 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005676 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005677 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5678 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00005679 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00005680 checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00005681 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5682 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005683 if (NestedLoopCount == 0)
5684 return StmtError();
5685
Alexander Musmanc6388682014-12-15 07:07:06 +00005686 assert((CurContext->isDependentContext() || B.builtAll()) &&
5687 "omp for simd loop exprs were not built");
5688
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005689 if (!CurContext->isDependentContext()) {
5690 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005691 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005692 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005693 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005694 B.NumIterations, *this, CurScope,
5695 DSAStack))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005696 return StmtError();
5697 }
5698 }
5699
Kelvin Lic5609492016-07-15 04:39:07 +00005700 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005701 return StmtError();
5702
Reid Kleckner87a31802018-03-12 21:43:02 +00005703 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005704 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5705 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005706}
5707
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005708StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5709 Stmt *AStmt,
5710 SourceLocation StartLoc,
5711 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005712 if (!AStmt)
5713 return StmtError();
5714
5715 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005716 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00005717 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005718 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00005719 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005720 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005721 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005722 return StmtError();
5723 // All associated statements must be '#pragma omp section' except for
5724 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005725 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005726 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5727 if (SectionStmt)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005728 Diag(SectionStmt->getBeginLoc(),
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005729 diag::err_omp_sections_substmt_not_section);
5730 return StmtError();
5731 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005732 cast<OMPSectionDirective>(SectionStmt)
5733 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005734 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005735 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005736 Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005737 return StmtError();
5738 }
5739
Reid Kleckner87a31802018-03-12 21:43:02 +00005740 setFunctionHasBranchProtectedScope();
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005741
Alexey Bataev25e5b442015-09-15 12:52:43 +00005742 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5743 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005744}
5745
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005746StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5747 SourceLocation StartLoc,
5748 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005749 if (!AStmt)
5750 return StmtError();
5751
5752 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005753
Reid Kleckner87a31802018-03-12 21:43:02 +00005754 setFunctionHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00005755 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005756
Alexey Bataev25e5b442015-09-15 12:52:43 +00005757 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5758 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005759}
5760
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005761StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5762 Stmt *AStmt,
5763 SourceLocation StartLoc,
5764 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005765 if (!AStmt)
5766 return StmtError();
5767
5768 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00005769
Reid Kleckner87a31802018-03-12 21:43:02 +00005770 setFunctionHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00005771
Alexey Bataev3255bf32015-01-19 05:20:46 +00005772 // OpenMP [2.7.3, single Construct, Restrictions]
5773 // The copyprivate clause must not be used with the nowait clause.
Alexey Bataeve3727102018-04-18 15:57:46 +00005774 const OMPClause *Nowait = nullptr;
5775 const OMPClause *Copyprivate = nullptr;
5776 for (const OMPClause *Clause : Clauses) {
Alexey Bataev3255bf32015-01-19 05:20:46 +00005777 if (Clause->getClauseKind() == OMPC_nowait)
5778 Nowait = Clause;
5779 else if (Clause->getClauseKind() == OMPC_copyprivate)
5780 Copyprivate = Clause;
5781 if (Copyprivate && Nowait) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005782 Diag(Copyprivate->getBeginLoc(),
Alexey Bataev3255bf32015-01-19 05:20:46 +00005783 diag::err_omp_single_copyprivate_with_nowait);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005784 Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here);
Alexey Bataev3255bf32015-01-19 05:20:46 +00005785 return StmtError();
5786 }
5787 }
5788
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005789 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5790}
5791
Alexander Musman80c22892014-07-17 08:54:58 +00005792StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5793 SourceLocation StartLoc,
5794 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005795 if (!AStmt)
5796 return StmtError();
5797
5798 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00005799
Reid Kleckner87a31802018-03-12 21:43:02 +00005800 setFunctionHasBranchProtectedScope();
Alexander Musman80c22892014-07-17 08:54:58 +00005801
5802 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5803}
5804
Alexey Bataev28c75412015-12-15 08:19:24 +00005805StmtResult Sema::ActOnOpenMPCriticalDirective(
5806 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5807 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005808 if (!AStmt)
5809 return StmtError();
5810
5811 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005812
Alexey Bataev28c75412015-12-15 08:19:24 +00005813 bool ErrorFound = false;
5814 llvm::APSInt Hint;
5815 SourceLocation HintLoc;
5816 bool DependentHint = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00005817 for (const OMPClause *C : Clauses) {
Alexey Bataev28c75412015-12-15 08:19:24 +00005818 if (C->getClauseKind() == OMPC_hint) {
5819 if (!DirName.getName()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005820 Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name);
Alexey Bataev28c75412015-12-15 08:19:24 +00005821 ErrorFound = true;
5822 }
5823 Expr *E = cast<OMPHintClause>(C)->getHint();
5824 if (E->isTypeDependent() || E->isValueDependent() ||
Alexey Bataeve3727102018-04-18 15:57:46 +00005825 E->isInstantiationDependent()) {
Alexey Bataev28c75412015-12-15 08:19:24 +00005826 DependentHint = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00005827 } else {
Alexey Bataev28c75412015-12-15 08:19:24 +00005828 Hint = E->EvaluateKnownConstInt(Context);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005829 HintLoc = C->getBeginLoc();
Alexey Bataev28c75412015-12-15 08:19:24 +00005830 }
5831 }
5832 }
5833 if (ErrorFound)
5834 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00005835 const auto Pair = DSAStack->getCriticalWithHint(DirName);
Alexey Bataev28c75412015-12-15 08:19:24 +00005836 if (Pair.first && DirName.getName() && !DependentHint) {
5837 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5838 Diag(StartLoc, diag::err_omp_critical_with_hint);
Alexey Bataeve3727102018-04-18 15:57:46 +00005839 if (HintLoc.isValid())
Alexey Bataev28c75412015-12-15 08:19:24 +00005840 Diag(HintLoc, diag::note_omp_critical_hint_here)
5841 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +00005842 else
Alexey Bataev28c75412015-12-15 08:19:24 +00005843 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
Alexey Bataeve3727102018-04-18 15:57:46 +00005844 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005845 Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here)
Alexey Bataev28c75412015-12-15 08:19:24 +00005846 << 1
5847 << C->getHint()->EvaluateKnownConstInt(Context).toString(
5848 /*Radix=*/10, /*Signed=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +00005849 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005850 Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1;
Alexey Bataeve3727102018-04-18 15:57:46 +00005851 }
Alexey Bataev28c75412015-12-15 08:19:24 +00005852 }
5853 }
5854
Reid Kleckner87a31802018-03-12 21:43:02 +00005855 setFunctionHasBranchProtectedScope();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005856
Alexey Bataev28c75412015-12-15 08:19:24 +00005857 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5858 Clauses, AStmt);
5859 if (!Pair.first && DirName.getName() && !DependentHint)
5860 DSAStack->addCriticalWithHint(Dir, Hint);
5861 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005862}
5863
Alexey Bataev4acb8592014-07-07 13:01:15 +00005864StmtResult Sema::ActOnOpenMPParallelForDirective(
5865 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00005866 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005867 if (!AStmt)
5868 return StmtError();
5869
Alexey Bataeve3727102018-04-18 15:57:46 +00005870 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005871 // 1.2.2 OpenMP Language Terminology
5872 // Structured block - An executable statement with a single entry at the
5873 // top and a single exit at the bottom.
5874 // The point of exit cannot be a branch out of the structured block.
5875 // longjmp() and throw() must not violate the entry/exit criteria.
5876 CS->getCapturedDecl()->setNothrow();
5877
Alexander Musmanc6388682014-12-15 07:07:06 +00005878 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005879 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5880 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005881 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00005882 checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00005883 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5884 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005885 if (NestedLoopCount == 0)
5886 return StmtError();
5887
Alexander Musmana5f070a2014-10-01 06:03:56 +00005888 assert((CurContext->isDependentContext() || B.builtAll()) &&
5889 "omp parallel for loop exprs were not built");
5890
Alexey Bataev54acd402015-08-04 11:18:19 +00005891 if (!CurContext->isDependentContext()) {
5892 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005893 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005894 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00005895 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005896 B.NumIterations, *this, CurScope,
5897 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005898 return StmtError();
5899 }
5900 }
5901
Reid Kleckner87a31802018-03-12 21:43:02 +00005902 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005903 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005904 NestedLoopCount, Clauses, AStmt, B,
5905 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00005906}
5907
Alexander Musmane4e893b2014-09-23 09:33:00 +00005908StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
5909 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00005910 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005911 if (!AStmt)
5912 return StmtError();
5913
Alexey Bataeve3727102018-04-18 15:57:46 +00005914 auto *CS = cast<CapturedStmt>(AStmt);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005915 // 1.2.2 OpenMP Language Terminology
5916 // Structured block - An executable statement with a single entry at the
5917 // top and a single exit at the bottom.
5918 // The point of exit cannot be a branch out of the structured block.
5919 // longjmp() and throw() must not violate the entry/exit criteria.
5920 CS->getCapturedDecl()->setNothrow();
5921
Alexander Musmanc6388682014-12-15 07:07:06 +00005922 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005923 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5924 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00005925 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00005926 checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00005927 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5928 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005929 if (NestedLoopCount == 0)
5930 return StmtError();
5931
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005932 if (!CurContext->isDependentContext()) {
5933 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005934 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005935 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005936 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005937 B.NumIterations, *this, CurScope,
5938 DSAStack))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005939 return StmtError();
5940 }
5941 }
5942
Kelvin Lic5609492016-07-15 04:39:07 +00005943 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005944 return StmtError();
5945
Reid Kleckner87a31802018-03-12 21:43:02 +00005946 setFunctionHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005947 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00005948 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005949}
5950
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005951StmtResult
5952Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
5953 Stmt *AStmt, SourceLocation StartLoc,
5954 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005955 if (!AStmt)
5956 return StmtError();
5957
5958 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005959 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00005960 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005961 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00005962 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005963 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005964 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005965 return StmtError();
5966 // All associated statements must be '#pragma omp section' except for
5967 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005968 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005969 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5970 if (SectionStmt)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005971 Diag(SectionStmt->getBeginLoc(),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005972 diag::err_omp_parallel_sections_substmt_not_section);
5973 return StmtError();
5974 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005975 cast<OMPSectionDirective>(SectionStmt)
5976 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005977 }
5978 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005979 Diag(AStmt->getBeginLoc(),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005980 diag::err_omp_parallel_sections_not_compound_stmt);
5981 return StmtError();
5982 }
5983
Reid Kleckner87a31802018-03-12 21:43:02 +00005984 setFunctionHasBranchProtectedScope();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005985
Alexey Bataev25e5b442015-09-15 12:52:43 +00005986 return OMPParallelSectionsDirective::Create(
5987 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005988}
5989
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005990StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5991 Stmt *AStmt, SourceLocation StartLoc,
5992 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005993 if (!AStmt)
5994 return StmtError();
5995
David Majnemer9d168222016-08-05 17:44:54 +00005996 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005997 // 1.2.2 OpenMP Language Terminology
5998 // Structured block - An executable statement with a single entry at the
5999 // top and a single exit at the bottom.
6000 // The point of exit cannot be a branch out of the structured block.
6001 // longjmp() and throw() must not violate the entry/exit criteria.
6002 CS->getCapturedDecl()->setNothrow();
6003
Reid Kleckner87a31802018-03-12 21:43:02 +00006004 setFunctionHasBranchProtectedScope();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006005
Alexey Bataev25e5b442015-09-15 12:52:43 +00006006 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
6007 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006008}
6009
Alexey Bataev68446b72014-07-18 07:47:19 +00006010StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
6011 SourceLocation EndLoc) {
6012 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
6013}
6014
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00006015StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
6016 SourceLocation EndLoc) {
6017 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
6018}
6019
Alexey Bataev2df347a2014-07-18 10:17:07 +00006020StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
6021 SourceLocation EndLoc) {
6022 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
6023}
6024
Alexey Bataev169d96a2017-07-18 20:17:46 +00006025StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses,
6026 Stmt *AStmt,
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006027 SourceLocation StartLoc,
6028 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006029 if (!AStmt)
6030 return StmtError();
6031
6032 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006033
Reid Kleckner87a31802018-03-12 21:43:02 +00006034 setFunctionHasBranchProtectedScope();
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006035
Alexey Bataev169d96a2017-07-18 20:17:46 +00006036 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses,
Alexey Bataev3b1b8952017-07-25 15:53:26 +00006037 AStmt,
6038 DSAStack->getTaskgroupReductionRef());
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006039}
6040
Alexey Bataev6125da92014-07-21 11:26:11 +00006041StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
6042 SourceLocation StartLoc,
6043 SourceLocation EndLoc) {
6044 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
6045 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
6046}
6047
Alexey Bataev346265e2015-09-25 10:37:12 +00006048StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
6049 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00006050 SourceLocation StartLoc,
6051 SourceLocation EndLoc) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006052 const OMPClause *DependFound = nullptr;
6053 const OMPClause *DependSourceClause = nullptr;
6054 const OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00006055 bool ErrorFound = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00006056 const OMPThreadsClause *TC = nullptr;
6057 const OMPSIMDClause *SC = nullptr;
6058 for (const OMPClause *C : Clauses) {
Alexey Bataeveb482352015-12-18 05:05:56 +00006059 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
6060 DependFound = C;
6061 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
6062 if (DependSourceClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006063 Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
Alexey Bataeveb482352015-12-18 05:05:56 +00006064 << getOpenMPDirectiveName(OMPD_ordered)
6065 << getOpenMPClauseName(OMPC_depend) << 2;
6066 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006067 } else {
Alexey Bataeveb482352015-12-18 05:05:56 +00006068 DependSourceClause = C;
Alexey Bataeve3727102018-04-18 15:57:46 +00006069 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006070 if (DependSinkClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006071 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006072 << 0;
6073 ErrorFound = true;
6074 }
6075 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
6076 if (DependSourceClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006077 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006078 << 1;
6079 ErrorFound = true;
6080 }
6081 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00006082 }
Alexey Bataeve3727102018-04-18 15:57:46 +00006083 } else if (C->getClauseKind() == OMPC_threads) {
Alexey Bataev346265e2015-09-25 10:37:12 +00006084 TC = cast<OMPThreadsClause>(C);
Alexey Bataeve3727102018-04-18 15:57:46 +00006085 } else if (C->getClauseKind() == OMPC_simd) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006086 SC = cast<OMPSIMDClause>(C);
Alexey Bataeve3727102018-04-18 15:57:46 +00006087 }
Alexey Bataev346265e2015-09-25 10:37:12 +00006088 }
Alexey Bataeveb482352015-12-18 05:05:56 +00006089 if (!ErrorFound && !SC &&
6090 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006091 // OpenMP [2.8.1,simd Construct, Restrictions]
6092 // An ordered construct with the simd clause is the only OpenMP construct
6093 // that can appear in the simd region.
6094 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00006095 ErrorFound = true;
6096 } else if (DependFound && (TC || SC)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006097 Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd)
Alexey Bataeveb482352015-12-18 05:05:56 +00006098 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
6099 ErrorFound = true;
Alexey Bataevf138fda2018-08-13 19:04:24 +00006100 } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006101 Diag(DependFound->getBeginLoc(),
Alexey Bataeveb482352015-12-18 05:05:56 +00006102 diag::err_omp_ordered_directive_without_param);
6103 ErrorFound = true;
6104 } else if (TC || Clauses.empty()) {
Alexey Bataevf138fda2018-08-13 19:04:24 +00006105 if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006106 SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc;
Alexey Bataeveb482352015-12-18 05:05:56 +00006107 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
6108 << (TC != nullptr);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006109 Diag(Param->getBeginLoc(), diag::note_omp_ordered_param);
Alexey Bataeveb482352015-12-18 05:05:56 +00006110 ErrorFound = true;
6111 }
6112 }
6113 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006114 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00006115
6116 if (AStmt) {
6117 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6118
Reid Kleckner87a31802018-03-12 21:43:02 +00006119 setFunctionHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006120 }
Alexey Bataev346265e2015-09-25 10:37:12 +00006121
6122 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00006123}
6124
Alexey Bataev1d160b12015-03-13 12:27:31 +00006125namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006126/// Helper class for checking expression in 'omp atomic [update]'
Alexey Bataev1d160b12015-03-13 12:27:31 +00006127/// construct.
6128class OpenMPAtomicUpdateChecker {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006129 /// Error results for atomic update expressions.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006130 enum ExprAnalysisErrorCode {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006131 /// A statement is not an expression statement.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006132 NotAnExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006133 /// Expression is not builtin binary or unary operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006134 NotABinaryOrUnaryExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006135 /// Unary operation is not post-/pre- increment/decrement operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006136 NotAnUnaryIncDecExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006137 /// An expression is not of scalar type.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006138 NotAScalarType,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006139 /// A binary operation is not an assignment operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006140 NotAnAssignmentOp,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006141 /// RHS part of the binary operation is not a binary expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006142 NotABinaryExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006143 /// RHS part is not additive/multiplicative/shift/biwise binary
Alexey Bataev1d160b12015-03-13 12:27:31 +00006144 /// expression.
6145 NotABinaryOperator,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006146 /// RHS binary operation does not have reference to the updated LHS
Alexey Bataev1d160b12015-03-13 12:27:31 +00006147 /// part.
6148 NotAnUpdateExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006149 /// No errors is found.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006150 NoError
6151 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006152 /// Reference to Sema.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006153 Sema &SemaRef;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006154 /// A location for note diagnostics (when error is found).
Alexey Bataev1d160b12015-03-13 12:27:31 +00006155 SourceLocation NoteLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006156 /// 'x' lvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006157 Expr *X;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006158 /// 'expr' rvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006159 Expr *E;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006160 /// Helper expression of the form
Alexey Bataevb4505a72015-03-30 05:20:59 +00006161 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6162 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6163 Expr *UpdateExpr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006164 /// Is 'x' a LHS in a RHS part of full update expression. It is
Alexey Bataevb4505a72015-03-30 05:20:59 +00006165 /// important for non-associative operations.
6166 bool IsXLHSInRHSPart;
6167 BinaryOperatorKind Op;
6168 SourceLocation OpLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006169 /// true if the source expression is a postfix unary operation, false
Alexey Bataevb78ca832015-04-01 03:33:17 +00006170 /// if it is a prefix unary operation.
6171 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006172
6173public:
6174 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00006175 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00006176 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006177 /// Check specified statement that it is suitable for 'atomic update'
Alexey Bataev1d160b12015-03-13 12:27:31 +00006178 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00006179 /// expression. If DiagId and NoteId == 0, then only check is performed
6180 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006181 /// \param DiagId Diagnostic which should be emitted if error is found.
6182 /// \param NoteId Diagnostic note for the main error message.
6183 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00006184 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006185 /// Return the 'x' lvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006186 Expr *getX() const { return X; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006187 /// Return the 'expr' rvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006188 Expr *getExpr() const { return E; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006189 /// Return the update expression used in calculation of the updated
Alexey Bataevb4505a72015-03-30 05:20:59 +00006190 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6191 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6192 Expr *getUpdateExpr() const { return UpdateExpr; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006193 /// Return true if 'x' is LHS in RHS part of full update expression,
Alexey Bataevb4505a72015-03-30 05:20:59 +00006194 /// false otherwise.
6195 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
6196
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006197 /// true if the source expression is a postfix unary operation, false
Alexey Bataevb78ca832015-04-01 03:33:17 +00006198 /// if it is a prefix unary operation.
6199 bool isPostfixUpdate() const { return IsPostfixUpdate; }
6200
Alexey Bataev1d160b12015-03-13 12:27:31 +00006201private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00006202 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
6203 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00006204};
6205} // namespace
6206
6207bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
6208 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
6209 ExprAnalysisErrorCode ErrorFound = NoError;
6210 SourceLocation ErrorLoc, NoteLoc;
6211 SourceRange ErrorRange, NoteRange;
6212 // Allowed constructs are:
6213 // x = x binop expr;
6214 // x = expr binop x;
6215 if (AtomicBinOp->getOpcode() == BO_Assign) {
6216 X = AtomicBinOp->getLHS();
Alexey Bataeve3727102018-04-18 15:57:46 +00006217 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006218 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
6219 if (AtomicInnerBinOp->isMultiplicativeOp() ||
6220 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
6221 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006222 Op = AtomicInnerBinOp->getOpcode();
6223 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataeve3727102018-04-18 15:57:46 +00006224 Expr *LHS = AtomicInnerBinOp->getLHS();
6225 Expr *RHS = AtomicInnerBinOp->getRHS();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006226 llvm::FoldingSetNodeID XId, LHSId, RHSId;
6227 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
6228 /*Canonical=*/true);
6229 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
6230 /*Canonical=*/true);
6231 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
6232 /*Canonical=*/true);
6233 if (XId == LHSId) {
6234 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006235 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006236 } else if (XId == RHSId) {
6237 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006238 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006239 } else {
6240 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6241 ErrorRange = AtomicInnerBinOp->getSourceRange();
6242 NoteLoc = X->getExprLoc();
6243 NoteRange = X->getSourceRange();
6244 ErrorFound = NotAnUpdateExpression;
6245 }
6246 } else {
6247 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6248 ErrorRange = AtomicInnerBinOp->getSourceRange();
6249 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
6250 NoteRange = SourceRange(NoteLoc, NoteLoc);
6251 ErrorFound = NotABinaryOperator;
6252 }
6253 } else {
6254 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
6255 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
6256 ErrorFound = NotABinaryExpression;
6257 }
6258 } else {
6259 ErrorLoc = AtomicBinOp->getExprLoc();
6260 ErrorRange = AtomicBinOp->getSourceRange();
6261 NoteLoc = AtomicBinOp->getOperatorLoc();
6262 NoteRange = SourceRange(NoteLoc, NoteLoc);
6263 ErrorFound = NotAnAssignmentOp;
6264 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006265 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006266 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6267 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6268 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006269 }
6270 if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006271 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006272 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006273}
6274
6275bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
6276 unsigned NoteId) {
6277 ExprAnalysisErrorCode ErrorFound = NoError;
6278 SourceLocation ErrorLoc, NoteLoc;
6279 SourceRange ErrorRange, NoteRange;
6280 // Allowed constructs are:
6281 // x++;
6282 // x--;
6283 // ++x;
6284 // --x;
6285 // x binop= expr;
6286 // x = x binop expr;
6287 // x = expr binop x;
6288 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
6289 AtomicBody = AtomicBody->IgnoreParenImpCasts();
6290 if (AtomicBody->getType()->isScalarType() ||
6291 AtomicBody->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006292 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006293 AtomicBody->IgnoreParenImpCasts())) {
6294 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00006295 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006296 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00006297 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006298 E = AtomicCompAssignOp->getRHS();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006299 X = AtomicCompAssignOp->getLHS()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006300 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006301 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
6302 AtomicBody->IgnoreParenImpCasts())) {
6303 // Check for Binary Operation
David Majnemer9d168222016-08-05 17:44:54 +00006304 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
Alexey Bataevb4505a72015-03-30 05:20:59 +00006305 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006306 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
David Majnemer9d168222016-08-05 17:44:54 +00006307 AtomicBody->IgnoreParenImpCasts())) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006308 // Check for Unary Operation
6309 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006310 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006311 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
6312 OpLoc = AtomicUnaryOp->getOperatorLoc();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006313 X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006314 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
6315 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006316 } else {
6317 ErrorFound = NotAnUnaryIncDecExpression;
6318 ErrorLoc = AtomicUnaryOp->getExprLoc();
6319 ErrorRange = AtomicUnaryOp->getSourceRange();
6320 NoteLoc = AtomicUnaryOp->getOperatorLoc();
6321 NoteRange = SourceRange(NoteLoc, NoteLoc);
6322 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006323 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006324 ErrorFound = NotABinaryOrUnaryExpression;
6325 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
6326 NoteRange = ErrorRange = AtomicBody->getSourceRange();
6327 }
6328 } else {
6329 ErrorFound = NotAScalarType;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006330 NoteLoc = ErrorLoc = AtomicBody->getBeginLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006331 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6332 }
6333 } else {
6334 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006335 NoteLoc = ErrorLoc = S->getBeginLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006336 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6337 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006338 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006339 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6340 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6341 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006342 }
6343 if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006344 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006345 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006346 // Build an update expression of form 'OpaqueValueExpr(x) binop
6347 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
6348 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
6349 auto *OVEX = new (SemaRef.getASTContext())
6350 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
6351 auto *OVEExpr = new (SemaRef.getASTContext())
6352 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
Alexey Bataeve3727102018-04-18 15:57:46 +00006353 ExprResult Update =
Alexey Bataevb4505a72015-03-30 05:20:59 +00006354 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
6355 IsXLHSInRHSPart ? OVEExpr : OVEX);
6356 if (Update.isInvalid())
6357 return true;
6358 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
6359 Sema::AA_Casting);
6360 if (Update.isInvalid())
6361 return true;
6362 UpdateExpr = Update.get();
6363 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00006364 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006365}
6366
Alexey Bataev0162e452014-07-22 10:10:35 +00006367StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
6368 Stmt *AStmt,
6369 SourceLocation StartLoc,
6370 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006371 if (!AStmt)
6372 return StmtError();
6373
David Majnemer9d168222016-08-05 17:44:54 +00006374 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00006375 // 1.2.2 OpenMP Language Terminology
6376 // Structured block - An executable statement with a single entry at the
6377 // top and a single exit at the bottom.
6378 // The point of exit cannot be a branch out of the structured block.
6379 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00006380 OpenMPClauseKind AtomicKind = OMPC_unknown;
6381 SourceLocation AtomicKindLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00006382 for (const OMPClause *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00006383 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00006384 C->getClauseKind() == OMPC_update ||
6385 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00006386 if (AtomicKind != OMPC_unknown) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006387 Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00006388 << SourceRange(C->getBeginLoc(), C->getEndLoc());
Alexey Bataevdea47612014-07-23 07:46:59 +00006389 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
6390 << getOpenMPClauseName(AtomicKind);
6391 } else {
6392 AtomicKind = C->getClauseKind();
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006393 AtomicKindLoc = C->getBeginLoc();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006394 }
6395 }
6396 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006397
Alexey Bataeve3727102018-04-18 15:57:46 +00006398 Stmt *Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00006399 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
6400 Body = EWC->getSubExpr();
6401
Alexey Bataev62cec442014-11-18 10:14:22 +00006402 Expr *X = nullptr;
6403 Expr *V = nullptr;
6404 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006405 Expr *UE = nullptr;
6406 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006407 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00006408 // OpenMP [2.12.6, atomic Construct]
6409 // In the next expressions:
6410 // * x and v (as applicable) are both l-value expressions with scalar type.
6411 // * During the execution of an atomic region, multiple syntactic
6412 // occurrences of x must designate the same storage location.
6413 // * Neither of v and expr (as applicable) may access the storage location
6414 // designated by x.
6415 // * Neither of x and expr (as applicable) may access the storage location
6416 // designated by v.
6417 // * expr is an expression with scalar type.
6418 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
6419 // * binop, binop=, ++, and -- are not overloaded operators.
6420 // * The expression x binop expr must be numerically equivalent to x binop
6421 // (expr). This requirement is satisfied if the operators in expr have
6422 // precedence greater than binop, or by using parentheses around expr or
6423 // subexpressions of expr.
6424 // * The expression expr binop x must be numerically equivalent to (expr)
6425 // binop x. This requirement is satisfied if the operators in expr have
6426 // precedence equal to or greater than binop, or by using parentheses around
6427 // expr or subexpressions of expr.
6428 // * For forms that allow multiple occurrences of x, the number of times
6429 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00006430 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006431 enum {
6432 NotAnExpression,
6433 NotAnAssignmentOp,
6434 NotAScalarType,
6435 NotAnLValue,
6436 NoError
6437 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00006438 SourceLocation ErrorLoc, NoteLoc;
6439 SourceRange ErrorRange, NoteRange;
6440 // If clause is read:
6441 // v = x;
Alexey Bataeve3727102018-04-18 15:57:46 +00006442 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
6443 const auto *AtomicBinOp =
Alexey Bataev62cec442014-11-18 10:14:22 +00006444 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6445 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6446 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6447 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
6448 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6449 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
6450 if (!X->isLValue() || !V->isLValue()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006451 const Expr *NotLValueExpr = X->isLValue() ? V : X;
Alexey Bataev62cec442014-11-18 10:14:22 +00006452 ErrorFound = NotAnLValue;
6453 ErrorLoc = AtomicBinOp->getExprLoc();
6454 ErrorRange = AtomicBinOp->getSourceRange();
6455 NoteLoc = NotLValueExpr->getExprLoc();
6456 NoteRange = NotLValueExpr->getSourceRange();
6457 }
6458 } else if (!X->isInstantiationDependent() ||
6459 !V->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006460 const Expr *NotScalarExpr =
Alexey Bataev62cec442014-11-18 10:14:22 +00006461 (X->isInstantiationDependent() || X->getType()->isScalarType())
6462 ? V
6463 : X;
6464 ErrorFound = NotAScalarType;
6465 ErrorLoc = AtomicBinOp->getExprLoc();
6466 ErrorRange = AtomicBinOp->getSourceRange();
6467 NoteLoc = NotScalarExpr->getExprLoc();
6468 NoteRange = NotScalarExpr->getSourceRange();
6469 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006470 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00006471 ErrorFound = NotAnAssignmentOp;
6472 ErrorLoc = AtomicBody->getExprLoc();
6473 ErrorRange = AtomicBody->getSourceRange();
6474 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6475 : AtomicBody->getExprLoc();
6476 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6477 : AtomicBody->getSourceRange();
6478 }
6479 } else {
6480 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006481 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataev62cec442014-11-18 10:14:22 +00006482 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006483 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006484 if (ErrorFound != NoError) {
6485 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
6486 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006487 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6488 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00006489 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00006490 }
6491 if (CurContext->isDependentContext())
Alexey Bataev62cec442014-11-18 10:14:22 +00006492 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00006493 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006494 enum {
6495 NotAnExpression,
6496 NotAnAssignmentOp,
6497 NotAScalarType,
6498 NotAnLValue,
6499 NoError
6500 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006501 SourceLocation ErrorLoc, NoteLoc;
6502 SourceRange ErrorRange, NoteRange;
6503 // If clause is write:
6504 // x = expr;
Alexey Bataeve3727102018-04-18 15:57:46 +00006505 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
6506 const auto *AtomicBinOp =
Alexey Bataevf33eba62014-11-28 07:21:40 +00006507 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6508 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00006509 X = AtomicBinOp->getLHS();
6510 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006511 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6512 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
6513 if (!X->isLValue()) {
6514 ErrorFound = NotAnLValue;
6515 ErrorLoc = AtomicBinOp->getExprLoc();
6516 ErrorRange = AtomicBinOp->getSourceRange();
6517 NoteLoc = X->getExprLoc();
6518 NoteRange = X->getSourceRange();
6519 }
6520 } else if (!X->isInstantiationDependent() ||
6521 !E->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006522 const Expr *NotScalarExpr =
Alexey Bataevf33eba62014-11-28 07:21:40 +00006523 (X->isInstantiationDependent() || X->getType()->isScalarType())
6524 ? E
6525 : X;
6526 ErrorFound = NotAScalarType;
6527 ErrorLoc = AtomicBinOp->getExprLoc();
6528 ErrorRange = AtomicBinOp->getSourceRange();
6529 NoteLoc = NotScalarExpr->getExprLoc();
6530 NoteRange = NotScalarExpr->getSourceRange();
6531 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006532 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00006533 ErrorFound = NotAnAssignmentOp;
6534 ErrorLoc = AtomicBody->getExprLoc();
6535 ErrorRange = AtomicBody->getSourceRange();
6536 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6537 : AtomicBody->getExprLoc();
6538 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6539 : AtomicBody->getSourceRange();
6540 }
6541 } else {
6542 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006543 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006544 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006545 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00006546 if (ErrorFound != NoError) {
6547 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
6548 << ErrorRange;
6549 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6550 << NoteRange;
6551 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00006552 }
6553 if (CurContext->isDependentContext())
Alexey Bataevf33eba62014-11-28 07:21:40 +00006554 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006555 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006556 // If clause is update:
6557 // x++;
6558 // x--;
6559 // ++x;
6560 // --x;
6561 // x binop= expr;
6562 // x = x binop expr;
6563 // x = expr binop x;
6564 OpenMPAtomicUpdateChecker Checker(*this);
6565 if (Checker.checkStatement(
6566 Body, (AtomicKind == OMPC_update)
6567 ? diag::err_omp_atomic_update_not_expression_statement
6568 : diag::err_omp_atomic_not_expression_statement,
6569 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00006570 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006571 if (!CurContext->isDependentContext()) {
6572 E = Checker.getExpr();
6573 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006574 UE = Checker.getUpdateExpr();
6575 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00006576 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006577 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006578 enum {
6579 NotAnAssignmentOp,
6580 NotACompoundStatement,
6581 NotTwoSubstatements,
6582 NotASpecificExpression,
6583 NoError
6584 } ErrorFound = NoError;
6585 SourceLocation ErrorLoc, NoteLoc;
6586 SourceRange ErrorRange, NoteRange;
Alexey Bataeve3727102018-04-18 15:57:46 +00006587 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006588 // If clause is a capture:
6589 // v = x++;
6590 // v = x--;
6591 // v = ++x;
6592 // v = --x;
6593 // v = x binop= expr;
6594 // v = x = x binop expr;
6595 // v = x = expr binop x;
Alexey Bataeve3727102018-04-18 15:57:46 +00006596 const auto *AtomicBinOp =
Alexey Bataevb78ca832015-04-01 03:33:17 +00006597 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6598 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6599 V = AtomicBinOp->getLHS();
6600 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6601 OpenMPAtomicUpdateChecker Checker(*this);
6602 if (Checker.checkStatement(
6603 Body, diag::err_omp_atomic_capture_not_expression_statement,
6604 diag::note_omp_atomic_update))
6605 return StmtError();
6606 E = Checker.getExpr();
6607 X = Checker.getX();
6608 UE = Checker.getUpdateExpr();
6609 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6610 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00006611 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006612 ErrorLoc = AtomicBody->getExprLoc();
6613 ErrorRange = AtomicBody->getSourceRange();
6614 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6615 : AtomicBody->getExprLoc();
6616 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6617 : AtomicBody->getSourceRange();
6618 ErrorFound = NotAnAssignmentOp;
6619 }
6620 if (ErrorFound != NoError) {
6621 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6622 << ErrorRange;
6623 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6624 return StmtError();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006625 }
Alexey Bataeve3727102018-04-18 15:57:46 +00006626 if (CurContext->isDependentContext())
6627 UE = V = E = X = nullptr;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006628 } else {
6629 // If clause is a capture:
6630 // { v = x; x = expr; }
6631 // { v = x; x++; }
6632 // { v = x; x--; }
6633 // { v = x; ++x; }
6634 // { v = x; --x; }
6635 // { v = x; x binop= expr; }
6636 // { v = x; x = x binop expr; }
6637 // { v = x; x = expr binop x; }
6638 // { x++; v = x; }
6639 // { x--; v = x; }
6640 // { ++x; v = x; }
6641 // { --x; v = x; }
6642 // { x binop= expr; v = x; }
6643 // { x = x binop expr; v = x; }
6644 // { x = expr binop x; v = x; }
6645 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6646 // Check that this is { expr1; expr2; }
6647 if (CS->size() == 2) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006648 Stmt *First = CS->body_front();
6649 Stmt *Second = CS->body_back();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006650 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6651 First = EWC->getSubExpr()->IgnoreParenImpCasts();
6652 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6653 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6654 // Need to find what subexpression is 'v' and what is 'x'.
6655 OpenMPAtomicUpdateChecker Checker(*this);
6656 bool IsUpdateExprFound = !Checker.checkStatement(Second);
6657 BinaryOperator *BinOp = nullptr;
6658 if (IsUpdateExprFound) {
6659 BinOp = dyn_cast<BinaryOperator>(First);
6660 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6661 }
6662 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6663 // { v = x; x++; }
6664 // { v = x; x--; }
6665 // { v = x; ++x; }
6666 // { v = x; --x; }
6667 // { v = x; x binop= expr; }
6668 // { v = x; x = x binop expr; }
6669 // { v = x; x = expr binop x; }
6670 // Check that the first expression has form v = x.
Alexey Bataeve3727102018-04-18 15:57:46 +00006671 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006672 llvm::FoldingSetNodeID XId, PossibleXId;
6673 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6674 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6675 IsUpdateExprFound = XId == PossibleXId;
6676 if (IsUpdateExprFound) {
6677 V = BinOp->getLHS();
6678 X = Checker.getX();
6679 E = Checker.getExpr();
6680 UE = Checker.getUpdateExpr();
6681 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006682 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006683 }
6684 }
6685 if (!IsUpdateExprFound) {
6686 IsUpdateExprFound = !Checker.checkStatement(First);
6687 BinOp = nullptr;
6688 if (IsUpdateExprFound) {
6689 BinOp = dyn_cast<BinaryOperator>(Second);
6690 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6691 }
6692 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6693 // { x++; v = x; }
6694 // { x--; v = x; }
6695 // { ++x; v = x; }
6696 // { --x; v = x; }
6697 // { x binop= expr; v = x; }
6698 // { x = x binop expr; v = x; }
6699 // { x = expr binop x; v = x; }
6700 // Check that the second expression has form v = x.
Alexey Bataeve3727102018-04-18 15:57:46 +00006701 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006702 llvm::FoldingSetNodeID XId, PossibleXId;
6703 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6704 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6705 IsUpdateExprFound = XId == PossibleXId;
6706 if (IsUpdateExprFound) {
6707 V = BinOp->getLHS();
6708 X = Checker.getX();
6709 E = Checker.getExpr();
6710 UE = Checker.getUpdateExpr();
6711 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006712 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006713 }
6714 }
6715 }
6716 if (!IsUpdateExprFound) {
6717 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00006718 auto *FirstExpr = dyn_cast<Expr>(First);
6719 auto *SecondExpr = dyn_cast<Expr>(Second);
6720 if (!FirstExpr || !SecondExpr ||
6721 !(FirstExpr->isInstantiationDependent() ||
6722 SecondExpr->isInstantiationDependent())) {
6723 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6724 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006725 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00006726 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006727 : First->getBeginLoc();
Alexey Bataev5a195472015-09-04 12:55:50 +00006728 NoteRange = ErrorRange = FirstBinOp
6729 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00006730 : SourceRange(ErrorLoc, ErrorLoc);
6731 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006732 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6733 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6734 ErrorFound = NotAnAssignmentOp;
6735 NoteLoc = ErrorLoc = SecondBinOp
6736 ? SecondBinOp->getOperatorLoc()
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006737 : Second->getBeginLoc();
Alexey Bataev5a195472015-09-04 12:55:50 +00006738 NoteRange = ErrorRange =
6739 SecondBinOp ? SecondBinOp->getSourceRange()
6740 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00006741 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +00006742 Expr *PossibleXRHSInFirst =
Alexey Bataev5a195472015-09-04 12:55:50 +00006743 FirstBinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00006744 Expr *PossibleXLHSInSecond =
Alexey Bataev5a195472015-09-04 12:55:50 +00006745 SecondBinOp->getLHS()->IgnoreParenImpCasts();
6746 llvm::FoldingSetNodeID X1Id, X2Id;
6747 PossibleXRHSInFirst->Profile(X1Id, Context,
6748 /*Canonical=*/true);
6749 PossibleXLHSInSecond->Profile(X2Id, Context,
6750 /*Canonical=*/true);
6751 IsUpdateExprFound = X1Id == X2Id;
6752 if (IsUpdateExprFound) {
6753 V = FirstBinOp->getLHS();
6754 X = SecondBinOp->getLHS();
6755 E = SecondBinOp->getRHS();
6756 UE = nullptr;
6757 IsXLHSInRHSPart = false;
6758 IsPostfixUpdate = true;
6759 } else {
6760 ErrorFound = NotASpecificExpression;
6761 ErrorLoc = FirstBinOp->getExprLoc();
6762 ErrorRange = FirstBinOp->getSourceRange();
6763 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6764 NoteRange = SecondBinOp->getRHS()->getSourceRange();
6765 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006766 }
6767 }
6768 }
6769 }
6770 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006771 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006772 NoteRange = ErrorRange =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006773 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
Alexey Bataevb78ca832015-04-01 03:33:17 +00006774 ErrorFound = NotTwoSubstatements;
6775 }
6776 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006777 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006778 NoteRange = ErrorRange =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006779 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
Alexey Bataevb78ca832015-04-01 03:33:17 +00006780 ErrorFound = NotACompoundStatement;
6781 }
6782 if (ErrorFound != NoError) {
6783 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6784 << ErrorRange;
6785 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6786 return StmtError();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006787 }
Alexey Bataeve3727102018-04-18 15:57:46 +00006788 if (CurContext->isDependentContext())
6789 UE = V = E = X = nullptr;
Alexey Bataev459dec02014-07-24 06:46:57 +00006790 }
Alexey Bataevdea47612014-07-23 07:46:59 +00006791 }
Alexey Bataev0162e452014-07-22 10:10:35 +00006792
Reid Kleckner87a31802018-03-12 21:43:02 +00006793 setFunctionHasBranchProtectedScope();
Alexey Bataev0162e452014-07-22 10:10:35 +00006794
Alexey Bataev62cec442014-11-18 10:14:22 +00006795 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00006796 X, V, E, UE, IsXLHSInRHSPart,
6797 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00006798}
6799
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006800StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6801 Stmt *AStmt,
6802 SourceLocation StartLoc,
6803 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006804 if (!AStmt)
6805 return StmtError();
6806
Alexey Bataeve3727102018-04-18 15:57:46 +00006807 auto *CS = cast<CapturedStmt>(AStmt);
Samuel Antao4af1b7b2015-12-02 17:44:43 +00006808 // 1.2.2 OpenMP Language Terminology
6809 // Structured block - An executable statement with a single entry at the
6810 // top and a single exit at the bottom.
6811 // The point of exit cannot be a branch out of the structured block.
6812 // longjmp() and throw() must not violate the entry/exit criteria.
6813 CS->getCapturedDecl()->setNothrow();
Alexey Bataev8451efa2018-01-15 19:06:12 +00006814 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target);
6815 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6816 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6817 // 1.2.2 OpenMP Language Terminology
6818 // Structured block - An executable statement with a single entry at the
6819 // top and a single exit at the bottom.
6820 // The point of exit cannot be a branch out of the structured block.
6821 // longjmp() and throw() must not violate the entry/exit criteria.
6822 CS->getCapturedDecl()->setNothrow();
6823 }
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006824
Alexey Bataev13314bf2014-10-09 04:18:56 +00006825 // OpenMP [2.16, Nesting of Regions]
6826 // If specified, a teams construct must be contained within a target
6827 // construct. That target construct must contain no statements or directives
6828 // outside of the teams construct.
6829 if (DSAStack->hasInnerTeamsRegion()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006830 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true);
Alexey Bataev13314bf2014-10-09 04:18:56 +00006831 bool OMPTeamsFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006832 if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00006833 auto I = CS->body_begin();
6834 while (I != CS->body_end()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006835 const auto *OED = dyn_cast<OMPExecutableDirective>(*I);
Alexey Bataev13314bf2014-10-09 04:18:56 +00006836 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6837 OMPTeamsFound = false;
6838 break;
6839 }
6840 ++I;
6841 }
6842 assert(I != CS->body_end() && "Not found statement");
6843 S = *I;
Kelvin Li3834dce2016-06-27 19:15:43 +00006844 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +00006845 const auto *OED = dyn_cast<OMPExecutableDirective>(S);
Kelvin Li3834dce2016-06-27 19:15:43 +00006846 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
Alexey Bataev13314bf2014-10-09 04:18:56 +00006847 }
6848 if (!OMPTeamsFound) {
6849 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
6850 Diag(DSAStack->getInnerTeamsRegionLoc(),
6851 diag::note_omp_nested_teams_construct_here);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006852 Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here)
Alexey Bataev13314bf2014-10-09 04:18:56 +00006853 << isa<OMPExecutableDirective>(S);
6854 return StmtError();
6855 }
6856 }
6857
Reid Kleckner87a31802018-03-12 21:43:02 +00006858 setFunctionHasBranchProtectedScope();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006859
6860 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6861}
6862
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006863StmtResult
6864Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
6865 Stmt *AStmt, SourceLocation StartLoc,
6866 SourceLocation EndLoc) {
6867 if (!AStmt)
6868 return StmtError();
6869
Alexey Bataeve3727102018-04-18 15:57:46 +00006870 auto *CS = cast<CapturedStmt>(AStmt);
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006871 // 1.2.2 OpenMP Language Terminology
6872 // Structured block - An executable statement with a single entry at the
6873 // top and a single exit at the bottom.
6874 // The point of exit cannot be a branch out of the structured block.
6875 // longjmp() and throw() must not violate the entry/exit criteria.
6876 CS->getCapturedDecl()->setNothrow();
Alexey Bataev8451efa2018-01-15 19:06:12 +00006877 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel);
6878 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6879 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6880 // 1.2.2 OpenMP Language Terminology
6881 // Structured block - An executable statement with a single entry at the
6882 // top and a single exit at the bottom.
6883 // The point of exit cannot be a branch out of the structured block.
6884 // longjmp() and throw() must not violate the entry/exit criteria.
6885 CS->getCapturedDecl()->setNothrow();
6886 }
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006887
Reid Kleckner87a31802018-03-12 21:43:02 +00006888 setFunctionHasBranchProtectedScope();
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006889
6890 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6891 AStmt);
6892}
6893
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006894StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
6895 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00006896 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006897 if (!AStmt)
6898 return StmtError();
6899
Alexey Bataeve3727102018-04-18 15:57:46 +00006900 auto *CS = cast<CapturedStmt>(AStmt);
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006901 // 1.2.2 OpenMP Language Terminology
6902 // Structured block - An executable statement with a single entry at the
6903 // top and a single exit at the bottom.
6904 // The point of exit cannot be a branch out of the structured block.
6905 // longjmp() and throw() must not violate the entry/exit criteria.
6906 CS->getCapturedDecl()->setNothrow();
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00006907 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
6908 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6909 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6910 // 1.2.2 OpenMP Language Terminology
6911 // Structured block - An executable statement with a single entry at the
6912 // top and a single exit at the bottom.
6913 // The point of exit cannot be a branch out of the structured block.
6914 // longjmp() and throw() must not violate the entry/exit criteria.
6915 CS->getCapturedDecl()->setNothrow();
6916 }
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006917
6918 OMPLoopDirective::HelperExprs B;
6919 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6920 // define the nested loops number.
6921 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00006922 checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00006923 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006924 VarsWithImplicitDSA, B);
6925 if (NestedLoopCount == 0)
6926 return StmtError();
6927
6928 assert((CurContext->isDependentContext() || B.builtAll()) &&
6929 "omp target parallel for loop exprs were not built");
6930
6931 if (!CurContext->isDependentContext()) {
6932 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00006933 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00006934 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006935 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006936 B.NumIterations, *this, CurScope,
6937 DSAStack))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006938 return StmtError();
6939 }
6940 }
6941
Reid Kleckner87a31802018-03-12 21:43:02 +00006942 setFunctionHasBranchProtectedScope();
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006943 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
6944 NestedLoopCount, Clauses, AStmt,
6945 B, DSAStack->isCancelRegion());
6946}
6947
Alexey Bataev95b64a92017-05-30 16:00:04 +00006948/// Check for existence of a map clause in the list of clauses.
6949static bool hasClauses(ArrayRef<OMPClause *> Clauses,
6950 const OpenMPClauseKind K) {
6951 return llvm::any_of(
6952 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; });
6953}
Samuel Antaodf67fc42016-01-19 19:15:56 +00006954
Alexey Bataev95b64a92017-05-30 16:00:04 +00006955template <typename... Params>
6956static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K,
6957 const Params... ClauseTypes) {
6958 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...);
Samuel Antaodf67fc42016-01-19 19:15:56 +00006959}
6960
Michael Wong65f367f2015-07-21 13:44:28 +00006961StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6962 Stmt *AStmt,
6963 SourceLocation StartLoc,
6964 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006965 if (!AStmt)
6966 return StmtError();
6967
6968 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6969
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006970 // OpenMP [2.10.1, Restrictions, p. 97]
6971 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00006972 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr)) {
6973 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
6974 << "'map' or 'use_device_ptr'"
David Majnemer9d168222016-08-05 17:44:54 +00006975 << getOpenMPDirectiveName(OMPD_target_data);
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006976 return StmtError();
6977 }
6978
Reid Kleckner87a31802018-03-12 21:43:02 +00006979 setFunctionHasBranchProtectedScope();
Michael Wong65f367f2015-07-21 13:44:28 +00006980
6981 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6982 AStmt);
6983}
6984
Samuel Antaodf67fc42016-01-19 19:15:56 +00006985StmtResult
6986Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
6987 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00006988 SourceLocation EndLoc, Stmt *AStmt) {
6989 if (!AStmt)
6990 return StmtError();
6991
Alexey Bataeve3727102018-04-18 15:57:46 +00006992 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00006993 // 1.2.2 OpenMP Language Terminology
6994 // Structured block - An executable statement with a single entry at the
6995 // top and a single exit at the bottom.
6996 // The point of exit cannot be a branch out of the structured block.
6997 // longjmp() and throw() must not violate the entry/exit criteria.
6998 CS->getCapturedDecl()->setNothrow();
6999 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data);
7000 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7001 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7002 // 1.2.2 OpenMP Language Terminology
7003 // Structured block - An executable statement with a single entry at the
7004 // top and a single exit at the bottom.
7005 // The point of exit cannot be a branch out of the structured block.
7006 // longjmp() and throw() must not violate the entry/exit criteria.
7007 CS->getCapturedDecl()->setNothrow();
7008 }
7009
Samuel Antaodf67fc42016-01-19 19:15:56 +00007010 // OpenMP [2.10.2, Restrictions, p. 99]
7011 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00007012 if (!hasClauses(Clauses, OMPC_map)) {
7013 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
7014 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data);
Samuel Antaodf67fc42016-01-19 19:15:56 +00007015 return StmtError();
7016 }
7017
Alexey Bataev7828b252017-11-21 17:08:48 +00007018 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
7019 AStmt);
Samuel Antaodf67fc42016-01-19 19:15:56 +00007020}
7021
Samuel Antao72590762016-01-19 20:04:50 +00007022StmtResult
7023Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
7024 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00007025 SourceLocation EndLoc, Stmt *AStmt) {
7026 if (!AStmt)
7027 return StmtError();
7028
Alexey Bataeve3727102018-04-18 15:57:46 +00007029 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00007030 // 1.2.2 OpenMP Language Terminology
7031 // Structured block - An executable statement with a single entry at the
7032 // top and a single exit at the bottom.
7033 // The point of exit cannot be a branch out of the structured block.
7034 // longjmp() and throw() must not violate the entry/exit criteria.
7035 CS->getCapturedDecl()->setNothrow();
7036 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data);
7037 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7038 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7039 // 1.2.2 OpenMP Language Terminology
7040 // Structured block - An executable statement with a single entry at the
7041 // top and a single exit at the bottom.
7042 // The point of exit cannot be a branch out of the structured block.
7043 // longjmp() and throw() must not violate the entry/exit criteria.
7044 CS->getCapturedDecl()->setNothrow();
7045 }
7046
Samuel Antao72590762016-01-19 20:04:50 +00007047 // OpenMP [2.10.3, Restrictions, p. 102]
7048 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00007049 if (!hasClauses(Clauses, OMPC_map)) {
7050 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
7051 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data);
Samuel Antao72590762016-01-19 20:04:50 +00007052 return StmtError();
7053 }
7054
Alexey Bataev7828b252017-11-21 17:08:48 +00007055 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
7056 AStmt);
Samuel Antao72590762016-01-19 20:04:50 +00007057}
7058
Samuel Antao686c70c2016-05-26 17:30:50 +00007059StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
7060 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00007061 SourceLocation EndLoc,
7062 Stmt *AStmt) {
7063 if (!AStmt)
7064 return StmtError();
7065
Alexey Bataeve3727102018-04-18 15:57:46 +00007066 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00007067 // 1.2.2 OpenMP Language Terminology
7068 // Structured block - An executable statement with a single entry at the
7069 // top and a single exit at the bottom.
7070 // The point of exit cannot be a branch out of the structured block.
7071 // longjmp() and throw() must not violate the entry/exit criteria.
7072 CS->getCapturedDecl()->setNothrow();
7073 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update);
7074 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7075 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7076 // 1.2.2 OpenMP Language Terminology
7077 // Structured block - An executable statement with a single entry at the
7078 // top and a single exit at the bottom.
7079 // The point of exit cannot be a branch out of the structured block.
7080 // longjmp() and throw() must not violate the entry/exit criteria.
7081 CS->getCapturedDecl()->setNothrow();
7082 }
7083
Alexey Bataev95b64a92017-05-30 16:00:04 +00007084 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) {
Samuel Antao686c70c2016-05-26 17:30:50 +00007085 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
7086 return StmtError();
7087 }
Alexey Bataev7828b252017-11-21 17:08:48 +00007088 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses,
7089 AStmt);
Samuel Antao686c70c2016-05-26 17:30:50 +00007090}
7091
Alexey Bataev13314bf2014-10-09 04:18:56 +00007092StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
7093 Stmt *AStmt, SourceLocation StartLoc,
7094 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007095 if (!AStmt)
7096 return StmtError();
7097
Alexey Bataeve3727102018-04-18 15:57:46 +00007098 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev13314bf2014-10-09 04:18:56 +00007099 // 1.2.2 OpenMP Language Terminology
7100 // Structured block - An executable statement with a single entry at the
7101 // top and a single exit at the bottom.
7102 // The point of exit cannot be a branch out of the structured block.
7103 // longjmp() and throw() must not violate the entry/exit criteria.
7104 CS->getCapturedDecl()->setNothrow();
7105
Reid Kleckner87a31802018-03-12 21:43:02 +00007106 setFunctionHasBranchProtectedScope();
Alexey Bataev13314bf2014-10-09 04:18:56 +00007107
Alexey Bataevceabd412017-11-30 18:01:54 +00007108 DSAStack->setParentTeamsRegionLoc(StartLoc);
7109
Alexey Bataev13314bf2014-10-09 04:18:56 +00007110 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
7111}
7112
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007113StmtResult
7114Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
7115 SourceLocation EndLoc,
7116 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007117 if (DSAStack->isParentNowaitRegion()) {
7118 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
7119 return StmtError();
7120 }
7121 if (DSAStack->isParentOrderedRegion()) {
7122 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
7123 return StmtError();
7124 }
7125 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
7126 CancelRegion);
7127}
7128
Alexey Bataev87933c72015-09-18 08:07:34 +00007129StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
7130 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00007131 SourceLocation EndLoc,
7132 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev80909872015-07-02 11:25:17 +00007133 if (DSAStack->isParentNowaitRegion()) {
7134 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
7135 return StmtError();
7136 }
7137 if (DSAStack->isParentOrderedRegion()) {
7138 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
7139 return StmtError();
7140 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00007141 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00007142 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
7143 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00007144}
7145
Alexey Bataev382967a2015-12-08 12:06:20 +00007146static bool checkGrainsizeNumTasksClauses(Sema &S,
7147 ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007148 const OMPClause *PrevClause = nullptr;
Alexey Bataev382967a2015-12-08 12:06:20 +00007149 bool ErrorFound = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00007150 for (const OMPClause *C : Clauses) {
Alexey Bataev382967a2015-12-08 12:06:20 +00007151 if (C->getClauseKind() == OMPC_grainsize ||
7152 C->getClauseKind() == OMPC_num_tasks) {
7153 if (!PrevClause)
7154 PrevClause = C;
7155 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007156 S.Diag(C->getBeginLoc(),
Alexey Bataev382967a2015-12-08 12:06:20 +00007157 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
7158 << getOpenMPClauseName(C->getClauseKind())
7159 << getOpenMPClauseName(PrevClause->getClauseKind());
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007160 S.Diag(PrevClause->getBeginLoc(),
Alexey Bataev382967a2015-12-08 12:06:20 +00007161 diag::note_omp_previous_grainsize_num_tasks)
7162 << getOpenMPClauseName(PrevClause->getClauseKind());
7163 ErrorFound = true;
7164 }
7165 }
7166 }
7167 return ErrorFound;
7168}
7169
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007170static bool checkReductionClauseWithNogroup(Sema &S,
7171 ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007172 const OMPClause *ReductionClause = nullptr;
7173 const OMPClause *NogroupClause = nullptr;
7174 for (const OMPClause *C : Clauses) {
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007175 if (C->getClauseKind() == OMPC_reduction) {
7176 ReductionClause = C;
7177 if (NogroupClause)
7178 break;
7179 continue;
7180 }
7181 if (C->getClauseKind() == OMPC_nogroup) {
7182 NogroupClause = C;
7183 if (ReductionClause)
7184 break;
7185 continue;
7186 }
7187 }
7188 if (ReductionClause && NogroupClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007189 S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup)
7190 << SourceRange(NogroupClause->getBeginLoc(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007191 NogroupClause->getEndLoc());
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007192 return true;
7193 }
7194 return false;
7195}
7196
Alexey Bataev49f6e782015-12-01 04:18:41 +00007197StmtResult Sema::ActOnOpenMPTaskLoopDirective(
7198 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007199 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00007200 if (!AStmt)
7201 return StmtError();
7202
7203 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7204 OMPLoopDirective::HelperExprs B;
7205 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7206 // define the nested loops number.
7207 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007208 checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007209 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00007210 VarsWithImplicitDSA, B);
7211 if (NestedLoopCount == 0)
7212 return StmtError();
7213
7214 assert((CurContext->isDependentContext() || B.builtAll()) &&
7215 "omp for loop exprs were not built");
7216
Alexey Bataev382967a2015-12-08 12:06:20 +00007217 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7218 // The grainsize clause and num_tasks clause are mutually exclusive and may
7219 // not appear on the same taskloop directive.
7220 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7221 return StmtError();
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007222 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7223 // If a reduction clause is present on the taskloop directive, the nogroup
7224 // clause must not be specified.
7225 if (checkReductionClauseWithNogroup(*this, Clauses))
7226 return StmtError();
Alexey Bataev382967a2015-12-08 12:06:20 +00007227
Reid Kleckner87a31802018-03-12 21:43:02 +00007228 setFunctionHasBranchProtectedScope();
Alexey Bataev49f6e782015-12-01 04:18:41 +00007229 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
7230 NestedLoopCount, Clauses, AStmt, B);
7231}
7232
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007233StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
7234 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007235 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007236 if (!AStmt)
7237 return StmtError();
7238
7239 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7240 OMPLoopDirective::HelperExprs B;
7241 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7242 // define the nested loops number.
7243 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007244 checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007245 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
7246 VarsWithImplicitDSA, B);
7247 if (NestedLoopCount == 0)
7248 return StmtError();
7249
7250 assert((CurContext->isDependentContext() || B.builtAll()) &&
7251 "omp for loop exprs were not built");
7252
Alexey Bataev5a3af132016-03-29 08:58:54 +00007253 if (!CurContext->isDependentContext()) {
7254 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007255 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007256 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007257 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007258 B.NumIterations, *this, CurScope,
7259 DSAStack))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007260 return StmtError();
7261 }
7262 }
7263
Alexey Bataev382967a2015-12-08 12:06:20 +00007264 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7265 // The grainsize clause and num_tasks clause are mutually exclusive and may
7266 // not appear on the same taskloop directive.
7267 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7268 return StmtError();
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007269 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7270 // If a reduction clause is present on the taskloop directive, the nogroup
7271 // clause must not be specified.
7272 if (checkReductionClauseWithNogroup(*this, Clauses))
7273 return StmtError();
Alexey Bataev438388c2017-11-22 18:34:02 +00007274 if (checkSimdlenSafelenSpecified(*this, Clauses))
7275 return StmtError();
Alexey Bataev382967a2015-12-08 12:06:20 +00007276
Reid Kleckner87a31802018-03-12 21:43:02 +00007277 setFunctionHasBranchProtectedScope();
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007278 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
7279 NestedLoopCount, Clauses, AStmt, B);
7280}
7281
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007282StmtResult Sema::ActOnOpenMPDistributeDirective(
7283 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007284 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007285 if (!AStmt)
7286 return StmtError();
7287
7288 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7289 OMPLoopDirective::HelperExprs B;
7290 // In presence of clause 'collapse' with number of loops, it will
7291 // define the nested loops number.
7292 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007293 checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007294 nullptr /*ordered not a clause on distribute*/, AStmt,
7295 *this, *DSAStack, VarsWithImplicitDSA, B);
7296 if (NestedLoopCount == 0)
7297 return StmtError();
7298
7299 assert((CurContext->isDependentContext() || B.builtAll()) &&
7300 "omp for loop exprs were not built");
7301
Reid Kleckner87a31802018-03-12 21:43:02 +00007302 setFunctionHasBranchProtectedScope();
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007303 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
7304 NestedLoopCount, Clauses, AStmt, B);
7305}
7306
Carlo Bertolli9925f152016-06-27 14:55:37 +00007307StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
7308 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007309 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Carlo Bertolli9925f152016-06-27 14:55:37 +00007310 if (!AStmt)
7311 return StmtError();
7312
Alexey Bataeve3727102018-04-18 15:57:46 +00007313 auto *CS = cast<CapturedStmt>(AStmt);
Carlo Bertolli9925f152016-06-27 14:55:37 +00007314 // 1.2.2 OpenMP Language Terminology
7315 // Structured block - An executable statement with a single entry at the
7316 // top and a single exit at the bottom.
7317 // The point of exit cannot be a branch out of the structured block.
7318 // longjmp() and throw() must not violate the entry/exit criteria.
7319 CS->getCapturedDecl()->setNothrow();
Alexey Bataev7f96c372017-11-22 17:19:31 +00007320 for (int ThisCaptureLevel =
7321 getOpenMPCaptureLevels(OMPD_distribute_parallel_for);
7322 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7323 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7324 // 1.2.2 OpenMP Language Terminology
7325 // Structured block - An executable statement with a single entry at the
7326 // top and a single exit at the bottom.
7327 // The point of exit cannot be a branch out of the structured block.
7328 // longjmp() and throw() must not violate the entry/exit criteria.
7329 CS->getCapturedDecl()->setNothrow();
7330 }
Carlo Bertolli9925f152016-06-27 14:55:37 +00007331
7332 OMPLoopDirective::HelperExprs B;
7333 // In presence of clause 'collapse' with number of loops, it will
7334 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007335 unsigned NestedLoopCount = checkOpenMPLoop(
Carlo Bertolli9925f152016-06-27 14:55:37 +00007336 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataev7f96c372017-11-22 17:19:31 +00007337 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Carlo Bertolli9925f152016-06-27 14:55:37 +00007338 VarsWithImplicitDSA, B);
7339 if (NestedLoopCount == 0)
7340 return StmtError();
7341
7342 assert((CurContext->isDependentContext() || B.builtAll()) &&
7343 "omp for loop exprs were not built");
7344
Reid Kleckner87a31802018-03-12 21:43:02 +00007345 setFunctionHasBranchProtectedScope();
Carlo Bertolli9925f152016-06-27 14:55:37 +00007346 return OMPDistributeParallelForDirective::Create(
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00007347 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7348 DSAStack->isCancelRegion());
Carlo Bertolli9925f152016-06-27 14:55:37 +00007349}
7350
Kelvin Li4a39add2016-07-05 05:00:15 +00007351StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
7352 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007353 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li4a39add2016-07-05 05:00:15 +00007354 if (!AStmt)
7355 return StmtError();
7356
Alexey Bataeve3727102018-04-18 15:57:46 +00007357 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li4a39add2016-07-05 05:00:15 +00007358 // 1.2.2 OpenMP Language Terminology
7359 // Structured block - An executable statement with a single entry at the
7360 // top and a single exit at the bottom.
7361 // The point of exit cannot be a branch out of the structured block.
7362 // longjmp() and throw() must not violate the entry/exit criteria.
7363 CS->getCapturedDecl()->setNothrow();
Alexey Bataev974acd62017-11-27 19:38:52 +00007364 for (int ThisCaptureLevel =
7365 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd);
7366 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7367 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7368 // 1.2.2 OpenMP Language Terminology
7369 // Structured block - An executable statement with a single entry at the
7370 // top and a single exit at the bottom.
7371 // The point of exit cannot be a branch out of the structured block.
7372 // longjmp() and throw() must not violate the entry/exit criteria.
7373 CS->getCapturedDecl()->setNothrow();
7374 }
Kelvin Li4a39add2016-07-05 05:00:15 +00007375
7376 OMPLoopDirective::HelperExprs B;
7377 // In presence of clause 'collapse' with number of loops, it will
7378 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007379 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li4a39add2016-07-05 05:00:15 +00007380 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev974acd62017-11-27 19:38:52 +00007381 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li4a39add2016-07-05 05:00:15 +00007382 VarsWithImplicitDSA, B);
7383 if (NestedLoopCount == 0)
7384 return StmtError();
7385
7386 assert((CurContext->isDependentContext() || B.builtAll()) &&
7387 "omp for loop exprs were not built");
7388
Alexey Bataev438388c2017-11-22 18:34:02 +00007389 if (!CurContext->isDependentContext()) {
7390 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007391 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00007392 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7393 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7394 B.NumIterations, *this, CurScope,
7395 DSAStack))
7396 return StmtError();
7397 }
7398 }
7399
Kelvin Lic5609492016-07-15 04:39:07 +00007400 if (checkSimdlenSafelenSpecified(*this, Clauses))
7401 return StmtError();
7402
Reid Kleckner87a31802018-03-12 21:43:02 +00007403 setFunctionHasBranchProtectedScope();
Kelvin Li4a39add2016-07-05 05:00:15 +00007404 return OMPDistributeParallelForSimdDirective::Create(
7405 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7406}
7407
Kelvin Li787f3fc2016-07-06 04:45:38 +00007408StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
7409 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007410 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li787f3fc2016-07-06 04:45:38 +00007411 if (!AStmt)
7412 return StmtError();
7413
Alexey Bataeve3727102018-04-18 15:57:46 +00007414 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li787f3fc2016-07-06 04:45:38 +00007415 // 1.2.2 OpenMP Language Terminology
7416 // Structured block - An executable statement with a single entry at the
7417 // top and a single exit at the bottom.
7418 // The point of exit cannot be a branch out of the structured block.
7419 // longjmp() and throw() must not violate the entry/exit criteria.
7420 CS->getCapturedDecl()->setNothrow();
Alexey Bataev617db5f2017-12-04 15:38:33 +00007421 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd);
7422 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7423 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7424 // 1.2.2 OpenMP Language Terminology
7425 // Structured block - An executable statement with a single entry at the
7426 // top and a single exit at the bottom.
7427 // The point of exit cannot be a branch out of the structured block.
7428 // longjmp() and throw() must not violate the entry/exit criteria.
7429 CS->getCapturedDecl()->setNothrow();
7430 }
Kelvin Li787f3fc2016-07-06 04:45:38 +00007431
7432 OMPLoopDirective::HelperExprs B;
7433 // In presence of clause 'collapse' with number of loops, it will
7434 // define the nested loops number.
7435 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007436 checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev617db5f2017-12-04 15:38:33 +00007437 nullptr /*ordered not a clause on distribute*/, CS, *this,
7438 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li787f3fc2016-07-06 04:45:38 +00007439 if (NestedLoopCount == 0)
7440 return StmtError();
7441
7442 assert((CurContext->isDependentContext() || B.builtAll()) &&
7443 "omp for loop exprs were not built");
7444
Alexey Bataev438388c2017-11-22 18:34:02 +00007445 if (!CurContext->isDependentContext()) {
7446 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007447 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00007448 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7449 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7450 B.NumIterations, *this, CurScope,
7451 DSAStack))
7452 return StmtError();
7453 }
7454 }
7455
Kelvin Lic5609492016-07-15 04:39:07 +00007456 if (checkSimdlenSafelenSpecified(*this, Clauses))
7457 return StmtError();
7458
Reid Kleckner87a31802018-03-12 21:43:02 +00007459 setFunctionHasBranchProtectedScope();
Kelvin Li787f3fc2016-07-06 04:45:38 +00007460 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
7461 NestedLoopCount, Clauses, AStmt, B);
7462}
7463
Kelvin Lia579b912016-07-14 02:54:56 +00007464StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
7465 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007466 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Lia579b912016-07-14 02:54:56 +00007467 if (!AStmt)
7468 return StmtError();
7469
Alexey Bataeve3727102018-04-18 15:57:46 +00007470 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Lia579b912016-07-14 02:54:56 +00007471 // 1.2.2 OpenMP Language Terminology
7472 // Structured block - An executable statement with a single entry at the
7473 // top and a single exit at the bottom.
7474 // The point of exit cannot be a branch out of the structured block.
7475 // longjmp() and throw() must not violate the entry/exit criteria.
7476 CS->getCapturedDecl()->setNothrow();
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007477 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
7478 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7479 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7480 // 1.2.2 OpenMP Language Terminology
7481 // Structured block - An executable statement with a single entry at the
7482 // top and a single exit at the bottom.
7483 // The point of exit cannot be a branch out of the structured block.
7484 // longjmp() and throw() must not violate the entry/exit criteria.
7485 CS->getCapturedDecl()->setNothrow();
7486 }
Kelvin Lia579b912016-07-14 02:54:56 +00007487
7488 OMPLoopDirective::HelperExprs B;
7489 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7490 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007491 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Lia579b912016-07-14 02:54:56 +00007492 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007493 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Kelvin Lia579b912016-07-14 02:54:56 +00007494 VarsWithImplicitDSA, B);
7495 if (NestedLoopCount == 0)
7496 return StmtError();
7497
7498 assert((CurContext->isDependentContext() || B.builtAll()) &&
7499 "omp target parallel for simd loop exprs were not built");
7500
7501 if (!CurContext->isDependentContext()) {
7502 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007503 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007504 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Lia579b912016-07-14 02:54:56 +00007505 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7506 B.NumIterations, *this, CurScope,
7507 DSAStack))
7508 return StmtError();
7509 }
7510 }
Kelvin Lic5609492016-07-15 04:39:07 +00007511 if (checkSimdlenSafelenSpecified(*this, Clauses))
Kelvin Lia579b912016-07-14 02:54:56 +00007512 return StmtError();
7513
Reid Kleckner87a31802018-03-12 21:43:02 +00007514 setFunctionHasBranchProtectedScope();
Kelvin Lia579b912016-07-14 02:54:56 +00007515 return OMPTargetParallelForSimdDirective::Create(
7516 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7517}
7518
Kelvin Li986330c2016-07-20 22:57:10 +00007519StmtResult Sema::ActOnOpenMPTargetSimdDirective(
7520 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007521 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li986330c2016-07-20 22:57:10 +00007522 if (!AStmt)
7523 return StmtError();
7524
Alexey Bataeve3727102018-04-18 15:57:46 +00007525 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li986330c2016-07-20 22:57:10 +00007526 // 1.2.2 OpenMP Language Terminology
7527 // Structured block - An executable statement with a single entry at the
7528 // top and a single exit at the bottom.
7529 // The point of exit cannot be a branch out of the structured block.
7530 // longjmp() and throw() must not violate the entry/exit criteria.
7531 CS->getCapturedDecl()->setNothrow();
Alexey Bataevf8365372017-11-17 17:57:25 +00007532 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd);
7533 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7534 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7535 // 1.2.2 OpenMP Language Terminology
7536 // Structured block - An executable statement with a single entry at the
7537 // top and a single exit at the bottom.
7538 // The point of exit cannot be a branch out of the structured block.
7539 // longjmp() and throw() must not violate the entry/exit criteria.
7540 CS->getCapturedDecl()->setNothrow();
7541 }
7542
Kelvin Li986330c2016-07-20 22:57:10 +00007543 OMPLoopDirective::HelperExprs B;
7544 // In presence of clause 'collapse' with number of loops, it will define the
7545 // nested loops number.
David Majnemer9d168222016-08-05 17:44:54 +00007546 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007547 checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
Alexey Bataevf8365372017-11-17 17:57:25 +00007548 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Kelvin Li986330c2016-07-20 22:57:10 +00007549 VarsWithImplicitDSA, B);
7550 if (NestedLoopCount == 0)
7551 return StmtError();
7552
7553 assert((CurContext->isDependentContext() || B.builtAll()) &&
7554 "omp target simd loop exprs were not built");
7555
7556 if (!CurContext->isDependentContext()) {
7557 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007558 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007559 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Li986330c2016-07-20 22:57:10 +00007560 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7561 B.NumIterations, *this, CurScope,
7562 DSAStack))
7563 return StmtError();
7564 }
7565 }
7566
7567 if (checkSimdlenSafelenSpecified(*this, Clauses))
7568 return StmtError();
7569
Reid Kleckner87a31802018-03-12 21:43:02 +00007570 setFunctionHasBranchProtectedScope();
Kelvin Li986330c2016-07-20 22:57:10 +00007571 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
7572 NestedLoopCount, Clauses, AStmt, B);
7573}
7574
Kelvin Li02532872016-08-05 14:37:37 +00007575StmtResult Sema::ActOnOpenMPTeamsDistributeDirective(
7576 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007577 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li02532872016-08-05 14:37:37 +00007578 if (!AStmt)
7579 return StmtError();
7580
Alexey Bataeve3727102018-04-18 15:57:46 +00007581 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li02532872016-08-05 14:37:37 +00007582 // 1.2.2 OpenMP Language Terminology
7583 // Structured block - An executable statement with a single entry at the
7584 // top and a single exit at the bottom.
7585 // The point of exit cannot be a branch out of the structured block.
7586 // longjmp() and throw() must not violate the entry/exit criteria.
7587 CS->getCapturedDecl()->setNothrow();
Alexey Bataev95c6dd42017-11-29 15:14:16 +00007588 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute);
7589 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7590 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7591 // 1.2.2 OpenMP Language Terminology
7592 // Structured block - An executable statement with a single entry at the
7593 // top and a single exit at the bottom.
7594 // The point of exit cannot be a branch out of the structured block.
7595 // longjmp() and throw() must not violate the entry/exit criteria.
7596 CS->getCapturedDecl()->setNothrow();
7597 }
Kelvin Li02532872016-08-05 14:37:37 +00007598
7599 OMPLoopDirective::HelperExprs B;
7600 // In presence of clause 'collapse' with number of loops, it will
7601 // define the nested loops number.
7602 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007603 checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses),
Alexey Bataev95c6dd42017-11-29 15:14:16 +00007604 nullptr /*ordered not a clause on distribute*/, CS, *this,
7605 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li02532872016-08-05 14:37:37 +00007606 if (NestedLoopCount == 0)
7607 return StmtError();
7608
7609 assert((CurContext->isDependentContext() || B.builtAll()) &&
7610 "omp teams distribute loop exprs were not built");
7611
Reid Kleckner87a31802018-03-12 21:43:02 +00007612 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007613
7614 DSAStack->setParentTeamsRegionLoc(StartLoc);
7615
David Majnemer9d168222016-08-05 17:44:54 +00007616 return OMPTeamsDistributeDirective::Create(
7617 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Kelvin Li02532872016-08-05 14:37:37 +00007618}
7619
Kelvin Li4e325f72016-10-25 12:50:55 +00007620StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective(
7621 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007622 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li4e325f72016-10-25 12:50:55 +00007623 if (!AStmt)
7624 return StmtError();
7625
Alexey Bataeve3727102018-04-18 15:57:46 +00007626 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li4e325f72016-10-25 12:50:55 +00007627 // 1.2.2 OpenMP Language Terminology
7628 // Structured block - An executable statement with a single entry at the
7629 // top and a single exit at the bottom.
7630 // The point of exit cannot be a branch out of the structured block.
7631 // longjmp() and throw() must not violate the entry/exit criteria.
7632 CS->getCapturedDecl()->setNothrow();
Alexey Bataev999277a2017-12-06 14:31:09 +00007633 for (int ThisCaptureLevel =
7634 getOpenMPCaptureLevels(OMPD_teams_distribute_simd);
7635 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7636 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7637 // 1.2.2 OpenMP Language Terminology
7638 // Structured block - An executable statement with a single entry at the
7639 // top and a single exit at the bottom.
7640 // The point of exit cannot be a branch out of the structured block.
7641 // longjmp() and throw() must not violate the entry/exit criteria.
7642 CS->getCapturedDecl()->setNothrow();
7643 }
7644
Kelvin Li4e325f72016-10-25 12:50:55 +00007645
7646 OMPLoopDirective::HelperExprs B;
7647 // In presence of clause 'collapse' with number of loops, it will
7648 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007649 unsigned NestedLoopCount = checkOpenMPLoop(
Samuel Antao4c8035b2016-12-12 18:00:20 +00007650 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev999277a2017-12-06 14:31:09 +00007651 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Samuel Antao4c8035b2016-12-12 18:00:20 +00007652 VarsWithImplicitDSA, B);
Kelvin Li4e325f72016-10-25 12:50:55 +00007653
7654 if (NestedLoopCount == 0)
7655 return StmtError();
7656
7657 assert((CurContext->isDependentContext() || B.builtAll()) &&
7658 "omp teams distribute simd loop exprs were not built");
7659
7660 if (!CurContext->isDependentContext()) {
7661 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007662 for (OMPClause *C : Clauses) {
Kelvin Li4e325f72016-10-25 12:50:55 +00007663 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7664 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7665 B.NumIterations, *this, CurScope,
7666 DSAStack))
7667 return StmtError();
7668 }
7669 }
7670
7671 if (checkSimdlenSafelenSpecified(*this, Clauses))
7672 return StmtError();
7673
Reid Kleckner87a31802018-03-12 21:43:02 +00007674 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007675
7676 DSAStack->setParentTeamsRegionLoc(StartLoc);
7677
Kelvin Li4e325f72016-10-25 12:50:55 +00007678 return OMPTeamsDistributeSimdDirective::Create(
7679 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7680}
7681
Kelvin Li579e41c2016-11-30 23:51:03 +00007682StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective(
7683 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007684 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li579e41c2016-11-30 23:51:03 +00007685 if (!AStmt)
7686 return StmtError();
7687
Alexey Bataeve3727102018-04-18 15:57:46 +00007688 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li579e41c2016-11-30 23:51:03 +00007689 // 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
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00007696 for (int ThisCaptureLevel =
7697 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd);
7698 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7699 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7700 // 1.2.2 OpenMP Language Terminology
7701 // Structured block - An executable statement with a single entry at the
7702 // top and a single exit at the bottom.
7703 // The point of exit cannot be a branch out of the structured block.
7704 // longjmp() and throw() must not violate the entry/exit criteria.
7705 CS->getCapturedDecl()->setNothrow();
7706 }
7707
Kelvin Li579e41c2016-11-30 23:51:03 +00007708 OMPLoopDirective::HelperExprs B;
7709 // In presence of clause 'collapse' with number of loops, it will
7710 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007711 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li579e41c2016-11-30 23:51:03 +00007712 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00007713 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li579e41c2016-11-30 23:51:03 +00007714 VarsWithImplicitDSA, B);
7715
7716 if (NestedLoopCount == 0)
7717 return StmtError();
7718
7719 assert((CurContext->isDependentContext() || B.builtAll()) &&
7720 "omp for loop exprs were not built");
7721
7722 if (!CurContext->isDependentContext()) {
7723 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007724 for (OMPClause *C : Clauses) {
Kelvin Li579e41c2016-11-30 23:51:03 +00007725 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7726 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7727 B.NumIterations, *this, CurScope,
7728 DSAStack))
7729 return StmtError();
7730 }
7731 }
7732
7733 if (checkSimdlenSafelenSpecified(*this, Clauses))
7734 return StmtError();
7735
Reid Kleckner87a31802018-03-12 21:43:02 +00007736 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007737
7738 DSAStack->setParentTeamsRegionLoc(StartLoc);
7739
Kelvin Li579e41c2016-11-30 23:51:03 +00007740 return OMPTeamsDistributeParallelForSimdDirective::Create(
7741 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7742}
7743
Kelvin Li7ade93f2016-12-09 03:24:30 +00007744StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective(
7745 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007746 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li7ade93f2016-12-09 03:24:30 +00007747 if (!AStmt)
7748 return StmtError();
7749
Alexey Bataeve3727102018-04-18 15:57:46 +00007750 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li7ade93f2016-12-09 03:24:30 +00007751 // 1.2.2 OpenMP Language Terminology
7752 // Structured block - An executable statement with a single entry at the
7753 // top and a single exit at the bottom.
7754 // The point of exit cannot be a branch out of the structured block.
7755 // longjmp() and throw() must not violate the entry/exit criteria.
7756 CS->getCapturedDecl()->setNothrow();
7757
Carlo Bertolli62fae152017-11-20 20:46:39 +00007758 for (int ThisCaptureLevel =
7759 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for);
7760 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7761 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7762 // 1.2.2 OpenMP Language Terminology
7763 // Structured block - An executable statement with a single entry at the
7764 // top and a single exit at the bottom.
7765 // The point of exit cannot be a branch out of the structured block.
7766 // longjmp() and throw() must not violate the entry/exit criteria.
7767 CS->getCapturedDecl()->setNothrow();
7768 }
7769
Kelvin Li7ade93f2016-12-09 03:24:30 +00007770 OMPLoopDirective::HelperExprs B;
7771 // In presence of clause 'collapse' with number of loops, it will
7772 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007773 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li7ade93f2016-12-09 03:24:30 +00007774 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
Carlo Bertolli62fae152017-11-20 20:46:39 +00007775 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li7ade93f2016-12-09 03:24:30 +00007776 VarsWithImplicitDSA, B);
7777
7778 if (NestedLoopCount == 0)
7779 return StmtError();
7780
7781 assert((CurContext->isDependentContext() || B.builtAll()) &&
7782 "omp for loop exprs were not built");
7783
Reid Kleckner87a31802018-03-12 21:43:02 +00007784 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007785
7786 DSAStack->setParentTeamsRegionLoc(StartLoc);
7787
Kelvin Li7ade93f2016-12-09 03:24:30 +00007788 return OMPTeamsDistributeParallelForDirective::Create(
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00007789 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7790 DSAStack->isCancelRegion());
Kelvin Li7ade93f2016-12-09 03:24:30 +00007791}
7792
Kelvin Libf594a52016-12-17 05:48:59 +00007793StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses,
7794 Stmt *AStmt,
7795 SourceLocation StartLoc,
7796 SourceLocation EndLoc) {
7797 if (!AStmt)
7798 return StmtError();
7799
Alexey Bataeve3727102018-04-18 15:57:46 +00007800 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Libf594a52016-12-17 05:48:59 +00007801 // 1.2.2 OpenMP Language Terminology
7802 // Structured block - An executable statement with a single entry at the
7803 // top and a single exit at the bottom.
7804 // The point of exit cannot be a branch out of the structured block.
7805 // longjmp() and throw() must not violate the entry/exit criteria.
7806 CS->getCapturedDecl()->setNothrow();
7807
Alexey Bataevf9fc42e2017-11-22 14:25:55 +00007808 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams);
7809 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7810 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7811 // 1.2.2 OpenMP Language Terminology
7812 // Structured block - An executable statement with a single entry at the
7813 // top and a single exit at the bottom.
7814 // The point of exit cannot be a branch out of the structured block.
7815 // longjmp() and throw() must not violate the entry/exit criteria.
7816 CS->getCapturedDecl()->setNothrow();
7817 }
Reid Kleckner87a31802018-03-12 21:43:02 +00007818 setFunctionHasBranchProtectedScope();
Kelvin Libf594a52016-12-17 05:48:59 +00007819
7820 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses,
7821 AStmt);
7822}
7823
Kelvin Li83c451e2016-12-25 04:52:54 +00007824StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective(
7825 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007826 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li83c451e2016-12-25 04:52:54 +00007827 if (!AStmt)
7828 return StmtError();
7829
Alexey Bataeve3727102018-04-18 15:57:46 +00007830 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li83c451e2016-12-25 04:52:54 +00007831 // 1.2.2 OpenMP Language Terminology
7832 // Structured block - An executable statement with a single entry at the
7833 // top and a single exit at the bottom.
7834 // The point of exit cannot be a branch out of the structured block.
7835 // longjmp() and throw() must not violate the entry/exit criteria.
7836 CS->getCapturedDecl()->setNothrow();
Alexey Bataevdfa430f2017-12-08 15:03:50 +00007837 for (int ThisCaptureLevel =
7838 getOpenMPCaptureLevels(OMPD_target_teams_distribute);
7839 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7840 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7841 // 1.2.2 OpenMP Language Terminology
7842 // Structured block - An executable statement with a single entry at the
7843 // top and a single exit at the bottom.
7844 // The point of exit cannot be a branch out of the structured block.
7845 // longjmp() and throw() must not violate the entry/exit criteria.
7846 CS->getCapturedDecl()->setNothrow();
7847 }
Kelvin Li83c451e2016-12-25 04:52:54 +00007848
7849 OMPLoopDirective::HelperExprs B;
7850 // In presence of clause 'collapse' with number of loops, it will
7851 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007852 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataevdfa430f2017-12-08 15:03:50 +00007853 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses),
7854 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li83c451e2016-12-25 04:52:54 +00007855 VarsWithImplicitDSA, B);
7856 if (NestedLoopCount == 0)
7857 return StmtError();
7858
7859 assert((CurContext->isDependentContext() || B.builtAll()) &&
7860 "omp target teams distribute loop exprs were not built");
7861
Reid Kleckner87a31802018-03-12 21:43:02 +00007862 setFunctionHasBranchProtectedScope();
Kelvin Li83c451e2016-12-25 04:52:54 +00007863 return OMPTargetTeamsDistributeDirective::Create(
7864 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7865}
7866
Kelvin Li80e8f562016-12-29 22:16:30 +00007867StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective(
7868 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007869 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li80e8f562016-12-29 22:16:30 +00007870 if (!AStmt)
7871 return StmtError();
7872
Alexey Bataeve3727102018-04-18 15:57:46 +00007873 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li80e8f562016-12-29 22:16:30 +00007874 // 1.2.2 OpenMP Language Terminology
7875 // Structured block - An executable statement with a single entry at the
7876 // top and a single exit at the bottom.
7877 // The point of exit cannot be a branch out of the structured block.
7878 // longjmp() and throw() must not violate the entry/exit criteria.
7879 CS->getCapturedDecl()->setNothrow();
Carlo Bertolli52978c32018-01-03 21:12:44 +00007880 for (int ThisCaptureLevel =
7881 getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for);
7882 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7883 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7884 // 1.2.2 OpenMP Language Terminology
7885 // Structured block - An executable statement with a single entry at the
7886 // top and a single exit at the bottom.
7887 // The point of exit cannot be a branch out of the structured block.
7888 // longjmp() and throw() must not violate the entry/exit criteria.
7889 CS->getCapturedDecl()->setNothrow();
7890 }
7891
Kelvin Li80e8f562016-12-29 22:16:30 +00007892 OMPLoopDirective::HelperExprs B;
7893 // In presence of clause 'collapse' with number of loops, it will
7894 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007895 unsigned NestedLoopCount = checkOpenMPLoop(
Carlo Bertolli52978c32018-01-03 21:12:44 +00007896 OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
7897 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li80e8f562016-12-29 22:16:30 +00007898 VarsWithImplicitDSA, B);
7899 if (NestedLoopCount == 0)
7900 return StmtError();
7901
7902 assert((CurContext->isDependentContext() || B.builtAll()) &&
7903 "omp target teams distribute parallel for loop exprs were not built");
7904
Alexey Bataev647dd842018-01-15 20:59:40 +00007905 if (!CurContext->isDependentContext()) {
7906 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007907 for (OMPClause *C : Clauses) {
Alexey Bataev647dd842018-01-15 20:59:40 +00007908 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7909 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7910 B.NumIterations, *this, CurScope,
7911 DSAStack))
7912 return StmtError();
7913 }
7914 }
7915
Reid Kleckner87a31802018-03-12 21:43:02 +00007916 setFunctionHasBranchProtectedScope();
Kelvin Li80e8f562016-12-29 22:16:30 +00007917 return OMPTargetTeamsDistributeParallelForDirective::Create(
Alexey Bataev16e79882017-11-22 21:12:03 +00007918 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7919 DSAStack->isCancelRegion());
Kelvin Li80e8f562016-12-29 22:16:30 +00007920}
7921
Kelvin Li1851df52017-01-03 05:23:48 +00007922StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
7923 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007924 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li1851df52017-01-03 05:23:48 +00007925 if (!AStmt)
7926 return StmtError();
7927
Alexey Bataeve3727102018-04-18 15:57:46 +00007928 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li1851df52017-01-03 05:23:48 +00007929 // 1.2.2 OpenMP Language Terminology
7930 // Structured block - An executable statement with a single entry at the
7931 // top and a single exit at the bottom.
7932 // The point of exit cannot be a branch out of the structured block.
7933 // longjmp() and throw() must not violate the entry/exit criteria.
7934 CS->getCapturedDecl()->setNothrow();
Alexey Bataev647dd842018-01-15 20:59:40 +00007935 for (int ThisCaptureLevel = getOpenMPCaptureLevels(
7936 OMPD_target_teams_distribute_parallel_for_simd);
7937 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7938 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7939 // 1.2.2 OpenMP Language Terminology
7940 // Structured block - An executable statement with a single entry at the
7941 // top and a single exit at the bottom.
7942 // The point of exit cannot be a branch out of the structured block.
7943 // longjmp() and throw() must not violate the entry/exit criteria.
7944 CS->getCapturedDecl()->setNothrow();
7945 }
Kelvin Li1851df52017-01-03 05:23:48 +00007946
7947 OMPLoopDirective::HelperExprs B;
7948 // In presence of clause 'collapse' with number of loops, it will
7949 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007950 unsigned NestedLoopCount =
7951 checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd,
Alexey Bataev647dd842018-01-15 20:59:40 +00007952 getCollapseNumberExpr(Clauses),
7953 nullptr /*ordered not a clause on distribute*/, CS, *this,
7954 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li1851df52017-01-03 05:23:48 +00007955 if (NestedLoopCount == 0)
7956 return StmtError();
7957
7958 assert((CurContext->isDependentContext() || B.builtAll()) &&
7959 "omp target teams distribute parallel for simd loop exprs were not "
7960 "built");
7961
7962 if (!CurContext->isDependentContext()) {
7963 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007964 for (OMPClause *C : Clauses) {
Kelvin Li1851df52017-01-03 05:23:48 +00007965 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7966 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7967 B.NumIterations, *this, CurScope,
7968 DSAStack))
7969 return StmtError();
7970 }
7971 }
7972
Alexey Bataev438388c2017-11-22 18:34:02 +00007973 if (checkSimdlenSafelenSpecified(*this, Clauses))
7974 return StmtError();
7975
Reid Kleckner87a31802018-03-12 21:43:02 +00007976 setFunctionHasBranchProtectedScope();
Kelvin Li1851df52017-01-03 05:23:48 +00007977 return OMPTargetTeamsDistributeParallelForSimdDirective::Create(
7978 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7979}
7980
Kelvin Lida681182017-01-10 18:08:18 +00007981StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective(
7982 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007983 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Lida681182017-01-10 18:08:18 +00007984 if (!AStmt)
7985 return StmtError();
7986
7987 auto *CS = cast<CapturedStmt>(AStmt);
7988 // 1.2.2 OpenMP Language Terminology
7989 // Structured block - An executable statement with a single entry at the
7990 // top and a single exit at the bottom.
7991 // The point of exit cannot be a branch out of the structured block.
7992 // longjmp() and throw() must not violate the entry/exit criteria.
7993 CS->getCapturedDecl()->setNothrow();
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00007994 for (int ThisCaptureLevel =
7995 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd);
7996 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7997 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7998 // 1.2.2 OpenMP Language Terminology
7999 // Structured block - An executable statement with a single entry at the
8000 // top and a single exit at the bottom.
8001 // The point of exit cannot be a branch out of the structured block.
8002 // longjmp() and throw() must not violate the entry/exit criteria.
8003 CS->getCapturedDecl()->setNothrow();
8004 }
Kelvin Lida681182017-01-10 18:08:18 +00008005
8006 OMPLoopDirective::HelperExprs B;
8007 // In presence of clause 'collapse' with number of loops, it will
8008 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00008009 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Lida681182017-01-10 18:08:18 +00008010 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00008011 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Lida681182017-01-10 18:08:18 +00008012 VarsWithImplicitDSA, B);
8013 if (NestedLoopCount == 0)
8014 return StmtError();
8015
8016 assert((CurContext->isDependentContext() || B.builtAll()) &&
8017 "omp target teams distribute simd loop exprs were not built");
8018
Alexey Bataev438388c2017-11-22 18:34:02 +00008019 if (!CurContext->isDependentContext()) {
8020 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00008021 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00008022 if (auto *LC = dyn_cast<OMPLinearClause>(C))
8023 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8024 B.NumIterations, *this, CurScope,
8025 DSAStack))
8026 return StmtError();
8027 }
8028 }
8029
8030 if (checkSimdlenSafelenSpecified(*this, Clauses))
8031 return StmtError();
8032
Reid Kleckner87a31802018-03-12 21:43:02 +00008033 setFunctionHasBranchProtectedScope();
Kelvin Lida681182017-01-10 18:08:18 +00008034 return OMPTargetTeamsDistributeSimdDirective::Create(
8035 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
8036}
8037
Alexey Bataeved09d242014-05-28 05:53:51 +00008038OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008039 SourceLocation StartLoc,
8040 SourceLocation LParenLoc,
8041 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008042 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008043 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00008044 case OMPC_final:
8045 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
8046 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00008047 case OMPC_num_threads:
8048 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
8049 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00008050 case OMPC_safelen:
8051 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
8052 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00008053 case OMPC_simdlen:
8054 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
8055 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00008056 case OMPC_collapse:
8057 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
8058 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00008059 case OMPC_ordered:
8060 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
8061 break;
Michael Wonge710d542015-08-07 16:16:36 +00008062 case OMPC_device:
8063 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
8064 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00008065 case OMPC_num_teams:
8066 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
8067 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008068 case OMPC_thread_limit:
8069 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
8070 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00008071 case OMPC_priority:
8072 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
8073 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008074 case OMPC_grainsize:
8075 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
8076 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00008077 case OMPC_num_tasks:
8078 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
8079 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00008080 case OMPC_hint:
8081 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
8082 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008083 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008084 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008085 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008086 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008087 case OMPC_private:
8088 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00008089 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008090 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00008091 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008092 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008093 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00008094 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008095 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008096 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008097 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00008098 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008099 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008100 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008101 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00008102 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008103 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008104 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008105 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008106 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008107 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008108 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00008109 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008110 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008111 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00008112 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008113 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008114 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008115 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008116 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00008117 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00008118 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00008119 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00008120 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00008121 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00008122 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00008123 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00008124 case OMPC_dynamic_allocators:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008125 llvm_unreachable("Clause is not allowed.");
8126 }
8127 return Res;
8128}
8129
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008130// An OpenMP directive such as 'target parallel' has two captured regions:
8131// for the 'target' and 'parallel' respectively. This function returns
8132// the region in which to capture expressions associated with a clause.
8133// A return value of OMPD_unknown signifies that the expression should not
8134// be captured.
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008135static OpenMPDirectiveKind getOpenMPCaptureRegionForClause(
8136 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
8137 OpenMPDirectiveKind NameModifier = OMPD_unknown) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008138 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008139 switch (CKind) {
8140 case OMPC_if:
8141 switch (DKind) {
8142 case OMPD_target_parallel:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008143 case OMPD_target_parallel_for:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00008144 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008145 // If this clause applies to the nested 'parallel' region, capture within
8146 // the 'target' region, otherwise do not capture.
8147 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
8148 CaptureRegion = OMPD_target;
8149 break;
Carlo Bertolli52978c32018-01-03 21:12:44 +00008150 case OMPD_target_teams_distribute_parallel_for:
8151 case OMPD_target_teams_distribute_parallel_for_simd:
8152 // If this clause applies to the nested 'parallel' region, capture within
8153 // the 'teams' region, otherwise do not capture.
8154 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
8155 CaptureRegion = OMPD_teams;
8156 break;
Carlo Bertolli62fae152017-11-20 20:46:39 +00008157 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008158 case OMPD_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008159 CaptureRegion = OMPD_teams;
8160 break;
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008161 case OMPD_target_update:
Alexey Bataevfab20e42017-12-27 18:49:38 +00008162 case OMPD_target_enter_data:
8163 case OMPD_target_exit_data:
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008164 CaptureRegion = OMPD_task;
8165 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008166 case OMPD_cancel:
8167 case OMPD_parallel:
8168 case OMPD_parallel_sections:
8169 case OMPD_parallel_for:
8170 case OMPD_parallel_for_simd:
8171 case OMPD_target:
8172 case OMPD_target_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008173 case OMPD_target_teams:
8174 case OMPD_target_teams_distribute:
8175 case OMPD_target_teams_distribute_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008176 case OMPD_distribute_parallel_for:
8177 case OMPD_distribute_parallel_for_simd:
8178 case OMPD_task:
8179 case OMPD_taskloop:
8180 case OMPD_taskloop_simd:
8181 case OMPD_target_data:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008182 // Do not capture if-clause expressions.
8183 break;
8184 case OMPD_threadprivate:
8185 case OMPD_taskyield:
8186 case OMPD_barrier:
8187 case OMPD_taskwait:
8188 case OMPD_cancellation_point:
8189 case OMPD_flush:
8190 case OMPD_declare_reduction:
8191 case OMPD_declare_simd:
8192 case OMPD_declare_target:
8193 case OMPD_end_declare_target:
8194 case OMPD_teams:
8195 case OMPD_simd:
8196 case OMPD_for:
8197 case OMPD_for_simd:
8198 case OMPD_sections:
8199 case OMPD_section:
8200 case OMPD_single:
8201 case OMPD_master:
8202 case OMPD_critical:
8203 case OMPD_taskgroup:
8204 case OMPD_distribute:
8205 case OMPD_ordered:
8206 case OMPD_atomic:
8207 case OMPD_distribute_simd:
8208 case OMPD_teams_distribute:
8209 case OMPD_teams_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008210 case OMPD_requires:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008211 llvm_unreachable("Unexpected OpenMP directive with if-clause");
8212 case OMPD_unknown:
8213 llvm_unreachable("Unknown OpenMP directive");
8214 }
8215 break;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008216 case OMPC_num_threads:
8217 switch (DKind) {
8218 case OMPD_target_parallel:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008219 case OMPD_target_parallel_for:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00008220 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008221 CaptureRegion = OMPD_target;
8222 break;
Carlo Bertolli62fae152017-11-20 20:46:39 +00008223 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008224 case OMPD_teams_distribute_parallel_for_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008225 case OMPD_target_teams_distribute_parallel_for:
8226 case OMPD_target_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008227 CaptureRegion = OMPD_teams;
8228 break;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008229 case OMPD_parallel:
8230 case OMPD_parallel_sections:
8231 case OMPD_parallel_for:
8232 case OMPD_parallel_for_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008233 case OMPD_distribute_parallel_for:
8234 case OMPD_distribute_parallel_for_simd:
8235 // Do not capture num_threads-clause expressions.
8236 break;
8237 case OMPD_target_data:
8238 case OMPD_target_enter_data:
8239 case OMPD_target_exit_data:
8240 case OMPD_target_update:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008241 case OMPD_target:
8242 case OMPD_target_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008243 case OMPD_target_teams:
8244 case OMPD_target_teams_distribute:
8245 case OMPD_target_teams_distribute_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008246 case OMPD_cancel:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008247 case OMPD_task:
8248 case OMPD_taskloop:
8249 case OMPD_taskloop_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008250 case OMPD_threadprivate:
8251 case OMPD_taskyield:
8252 case OMPD_barrier:
8253 case OMPD_taskwait:
8254 case OMPD_cancellation_point:
8255 case OMPD_flush:
8256 case OMPD_declare_reduction:
8257 case OMPD_declare_simd:
8258 case OMPD_declare_target:
8259 case OMPD_end_declare_target:
8260 case OMPD_teams:
8261 case OMPD_simd:
8262 case OMPD_for:
8263 case OMPD_for_simd:
8264 case OMPD_sections:
8265 case OMPD_section:
8266 case OMPD_single:
8267 case OMPD_master:
8268 case OMPD_critical:
8269 case OMPD_taskgroup:
8270 case OMPD_distribute:
8271 case OMPD_ordered:
8272 case OMPD_atomic:
8273 case OMPD_distribute_simd:
8274 case OMPD_teams_distribute:
8275 case OMPD_teams_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008276 case OMPD_requires:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008277 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause");
8278 case OMPD_unknown:
8279 llvm_unreachable("Unknown OpenMP directive");
8280 }
8281 break;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008282 case OMPC_num_teams:
8283 switch (DKind) {
8284 case OMPD_target_teams:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008285 case OMPD_target_teams_distribute:
8286 case OMPD_target_teams_distribute_simd:
8287 case OMPD_target_teams_distribute_parallel_for:
8288 case OMPD_target_teams_distribute_parallel_for_simd:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008289 CaptureRegion = OMPD_target;
8290 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008291 case OMPD_teams_distribute_parallel_for:
8292 case OMPD_teams_distribute_parallel_for_simd:
8293 case OMPD_teams:
8294 case OMPD_teams_distribute:
8295 case OMPD_teams_distribute_simd:
8296 // Do not capture num_teams-clause expressions.
8297 break;
8298 case OMPD_distribute_parallel_for:
8299 case OMPD_distribute_parallel_for_simd:
8300 case OMPD_task:
8301 case OMPD_taskloop:
8302 case OMPD_taskloop_simd:
8303 case OMPD_target_data:
8304 case OMPD_target_enter_data:
8305 case OMPD_target_exit_data:
8306 case OMPD_target_update:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008307 case OMPD_cancel:
8308 case OMPD_parallel:
8309 case OMPD_parallel_sections:
8310 case OMPD_parallel_for:
8311 case OMPD_parallel_for_simd:
8312 case OMPD_target:
8313 case OMPD_target_simd:
8314 case OMPD_target_parallel:
8315 case OMPD_target_parallel_for:
8316 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008317 case OMPD_threadprivate:
8318 case OMPD_taskyield:
8319 case OMPD_barrier:
8320 case OMPD_taskwait:
8321 case OMPD_cancellation_point:
8322 case OMPD_flush:
8323 case OMPD_declare_reduction:
8324 case OMPD_declare_simd:
8325 case OMPD_declare_target:
8326 case OMPD_end_declare_target:
8327 case OMPD_simd:
8328 case OMPD_for:
8329 case OMPD_for_simd:
8330 case OMPD_sections:
8331 case OMPD_section:
8332 case OMPD_single:
8333 case OMPD_master:
8334 case OMPD_critical:
8335 case OMPD_taskgroup:
8336 case OMPD_distribute:
8337 case OMPD_ordered:
8338 case OMPD_atomic:
8339 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008340 case OMPD_requires:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008341 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
8342 case OMPD_unknown:
8343 llvm_unreachable("Unknown OpenMP directive");
8344 }
8345 break;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008346 case OMPC_thread_limit:
8347 switch (DKind) {
8348 case OMPD_target_teams:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008349 case OMPD_target_teams_distribute:
8350 case OMPD_target_teams_distribute_simd:
8351 case OMPD_target_teams_distribute_parallel_for:
8352 case OMPD_target_teams_distribute_parallel_for_simd:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008353 CaptureRegion = OMPD_target;
8354 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008355 case OMPD_teams_distribute_parallel_for:
8356 case OMPD_teams_distribute_parallel_for_simd:
8357 case OMPD_teams:
8358 case OMPD_teams_distribute:
8359 case OMPD_teams_distribute_simd:
8360 // Do not capture thread_limit-clause expressions.
8361 break;
8362 case OMPD_distribute_parallel_for:
8363 case OMPD_distribute_parallel_for_simd:
8364 case OMPD_task:
8365 case OMPD_taskloop:
8366 case OMPD_taskloop_simd:
8367 case OMPD_target_data:
8368 case OMPD_target_enter_data:
8369 case OMPD_target_exit_data:
8370 case OMPD_target_update:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008371 case OMPD_cancel:
8372 case OMPD_parallel:
8373 case OMPD_parallel_sections:
8374 case OMPD_parallel_for:
8375 case OMPD_parallel_for_simd:
8376 case OMPD_target:
8377 case OMPD_target_simd:
8378 case OMPD_target_parallel:
8379 case OMPD_target_parallel_for:
8380 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008381 case OMPD_threadprivate:
8382 case OMPD_taskyield:
8383 case OMPD_barrier:
8384 case OMPD_taskwait:
8385 case OMPD_cancellation_point:
8386 case OMPD_flush:
8387 case OMPD_declare_reduction:
8388 case OMPD_declare_simd:
8389 case OMPD_declare_target:
8390 case OMPD_end_declare_target:
8391 case OMPD_simd:
8392 case OMPD_for:
8393 case OMPD_for_simd:
8394 case OMPD_sections:
8395 case OMPD_section:
8396 case OMPD_single:
8397 case OMPD_master:
8398 case OMPD_critical:
8399 case OMPD_taskgroup:
8400 case OMPD_distribute:
8401 case OMPD_ordered:
8402 case OMPD_atomic:
8403 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008404 case OMPD_requires:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008405 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause");
8406 case OMPD_unknown:
8407 llvm_unreachable("Unknown OpenMP directive");
8408 }
8409 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008410 case OMPC_schedule:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008411 switch (DKind) {
Alexey Bataev2ba67042017-11-28 21:11:44 +00008412 case OMPD_parallel_for:
8413 case OMPD_parallel_for_simd:
Alexey Bataev7f96c372017-11-22 17:19:31 +00008414 case OMPD_distribute_parallel_for:
Alexey Bataev974acd62017-11-27 19:38:52 +00008415 case OMPD_distribute_parallel_for_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008416 case OMPD_teams_distribute_parallel_for:
8417 case OMPD_teams_distribute_parallel_for_simd:
8418 case OMPD_target_parallel_for:
8419 case OMPD_target_parallel_for_simd:
8420 case OMPD_target_teams_distribute_parallel_for:
8421 case OMPD_target_teams_distribute_parallel_for_simd:
Alexey Bataev7f96c372017-11-22 17:19:31 +00008422 CaptureRegion = OMPD_parallel;
8423 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008424 case OMPD_for:
8425 case OMPD_for_simd:
8426 // Do not capture schedule-clause expressions.
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008427 break;
8428 case OMPD_task:
8429 case OMPD_taskloop:
8430 case OMPD_taskloop_simd:
8431 case OMPD_target_data:
8432 case OMPD_target_enter_data:
8433 case OMPD_target_exit_data:
8434 case OMPD_target_update:
8435 case OMPD_teams:
8436 case OMPD_teams_distribute:
8437 case OMPD_teams_distribute_simd:
8438 case OMPD_target_teams_distribute:
8439 case OMPD_target_teams_distribute_simd:
8440 case OMPD_target:
8441 case OMPD_target_simd:
8442 case OMPD_target_parallel:
8443 case OMPD_cancel:
8444 case OMPD_parallel:
8445 case OMPD_parallel_sections:
8446 case OMPD_threadprivate:
8447 case OMPD_taskyield:
8448 case OMPD_barrier:
8449 case OMPD_taskwait:
8450 case OMPD_cancellation_point:
8451 case OMPD_flush:
8452 case OMPD_declare_reduction:
8453 case OMPD_declare_simd:
8454 case OMPD_declare_target:
8455 case OMPD_end_declare_target:
8456 case OMPD_simd:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008457 case OMPD_sections:
8458 case OMPD_section:
8459 case OMPD_single:
8460 case OMPD_master:
8461 case OMPD_critical:
8462 case OMPD_taskgroup:
8463 case OMPD_distribute:
8464 case OMPD_ordered:
8465 case OMPD_atomic:
8466 case OMPD_distribute_simd:
8467 case OMPD_target_teams:
Kelvin Li1408f912018-09-26 04:28:39 +00008468 case OMPD_requires:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008469 llvm_unreachable("Unexpected OpenMP directive with schedule clause");
8470 case OMPD_unknown:
8471 llvm_unreachable("Unknown OpenMP directive");
8472 }
8473 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008474 case OMPC_dist_schedule:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008475 switch (DKind) {
8476 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008477 case OMPD_teams_distribute_parallel_for_simd:
8478 case OMPD_teams_distribute:
8479 case OMPD_teams_distribute_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008480 case OMPD_target_teams_distribute_parallel_for:
8481 case OMPD_target_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008482 case OMPD_target_teams_distribute:
8483 case OMPD_target_teams_distribute_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008484 CaptureRegion = OMPD_teams;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008485 break;
8486 case OMPD_distribute_parallel_for:
8487 case OMPD_distribute_parallel_for_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008488 case OMPD_distribute:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008489 case OMPD_distribute_simd:
8490 // Do not capture thread_limit-clause expressions.
8491 break;
8492 case OMPD_parallel_for:
8493 case OMPD_parallel_for_simd:
8494 case OMPD_target_parallel_for_simd:
8495 case OMPD_target_parallel_for:
8496 case OMPD_task:
8497 case OMPD_taskloop:
8498 case OMPD_taskloop_simd:
8499 case OMPD_target_data:
8500 case OMPD_target_enter_data:
8501 case OMPD_target_exit_data:
8502 case OMPD_target_update:
8503 case OMPD_teams:
8504 case OMPD_target:
8505 case OMPD_target_simd:
8506 case OMPD_target_parallel:
8507 case OMPD_cancel:
8508 case OMPD_parallel:
8509 case OMPD_parallel_sections:
8510 case OMPD_threadprivate:
8511 case OMPD_taskyield:
8512 case OMPD_barrier:
8513 case OMPD_taskwait:
8514 case OMPD_cancellation_point:
8515 case OMPD_flush:
8516 case OMPD_declare_reduction:
8517 case OMPD_declare_simd:
8518 case OMPD_declare_target:
8519 case OMPD_end_declare_target:
8520 case OMPD_simd:
8521 case OMPD_for:
8522 case OMPD_for_simd:
8523 case OMPD_sections:
8524 case OMPD_section:
8525 case OMPD_single:
8526 case OMPD_master:
8527 case OMPD_critical:
8528 case OMPD_taskgroup:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008529 case OMPD_ordered:
8530 case OMPD_atomic:
8531 case OMPD_target_teams:
Kelvin Li1408f912018-09-26 04:28:39 +00008532 case OMPD_requires:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008533 llvm_unreachable("Unexpected OpenMP directive with schedule clause");
8534 case OMPD_unknown:
8535 llvm_unreachable("Unknown OpenMP directive");
8536 }
8537 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008538 case OMPC_device:
8539 switch (DKind) {
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008540 case OMPD_target_update:
Alexey Bataevfab20e42017-12-27 18:49:38 +00008541 case OMPD_target_enter_data:
8542 case OMPD_target_exit_data:
Alexey Bataev8451efa2018-01-15 19:06:12 +00008543 case OMPD_target:
Alexey Bataevf41c88f2018-01-16 15:05:16 +00008544 case OMPD_target_simd:
Alexey Bataev0c869ef2018-01-16 15:57:07 +00008545 case OMPD_target_teams:
Alexey Bataev54d5c7d2018-01-16 16:27:49 +00008546 case OMPD_target_parallel:
Alexey Bataev79df7562018-01-16 16:46:46 +00008547 case OMPD_target_teams_distribute:
Alexey Bataev8d16a432018-01-16 17:22:50 +00008548 case OMPD_target_teams_distribute_simd:
Alexey Bataev8ed895512018-01-16 17:41:04 +00008549 case OMPD_target_parallel_for:
Alexey Bataevd60d1ba2018-01-16 17:55:15 +00008550 case OMPD_target_parallel_for_simd:
Alexey Bataev9f9fb0b2018-01-16 19:02:33 +00008551 case OMPD_target_teams_distribute_parallel_for:
Alexey Bataev9350fc32018-01-16 19:18:24 +00008552 case OMPD_target_teams_distribute_parallel_for_simd:
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008553 CaptureRegion = OMPD_task;
8554 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008555 case OMPD_target_data:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008556 // Do not capture device-clause expressions.
8557 break;
8558 case OMPD_teams_distribute_parallel_for:
8559 case OMPD_teams_distribute_parallel_for_simd:
8560 case OMPD_teams:
8561 case OMPD_teams_distribute:
8562 case OMPD_teams_distribute_simd:
8563 case OMPD_distribute_parallel_for:
8564 case OMPD_distribute_parallel_for_simd:
8565 case OMPD_task:
8566 case OMPD_taskloop:
8567 case OMPD_taskloop_simd:
8568 case OMPD_cancel:
8569 case OMPD_parallel:
8570 case OMPD_parallel_sections:
8571 case OMPD_parallel_for:
8572 case OMPD_parallel_for_simd:
8573 case OMPD_threadprivate:
8574 case OMPD_taskyield:
8575 case OMPD_barrier:
8576 case OMPD_taskwait:
8577 case OMPD_cancellation_point:
8578 case OMPD_flush:
8579 case OMPD_declare_reduction:
8580 case OMPD_declare_simd:
8581 case OMPD_declare_target:
8582 case OMPD_end_declare_target:
8583 case OMPD_simd:
8584 case OMPD_for:
8585 case OMPD_for_simd:
8586 case OMPD_sections:
8587 case OMPD_section:
8588 case OMPD_single:
8589 case OMPD_master:
8590 case OMPD_critical:
8591 case OMPD_taskgroup:
8592 case OMPD_distribute:
8593 case OMPD_ordered:
8594 case OMPD_atomic:
8595 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008596 case OMPD_requires:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008597 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
8598 case OMPD_unknown:
8599 llvm_unreachable("Unknown OpenMP directive");
8600 }
8601 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008602 case OMPC_firstprivate:
8603 case OMPC_lastprivate:
8604 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008605 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008606 case OMPC_in_reduction:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008607 case OMPC_linear:
8608 case OMPC_default:
8609 case OMPC_proc_bind:
8610 case OMPC_final:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008611 case OMPC_safelen:
8612 case OMPC_simdlen:
8613 case OMPC_collapse:
8614 case OMPC_private:
8615 case OMPC_shared:
8616 case OMPC_aligned:
8617 case OMPC_copyin:
8618 case OMPC_copyprivate:
8619 case OMPC_ordered:
8620 case OMPC_nowait:
8621 case OMPC_untied:
8622 case OMPC_mergeable:
8623 case OMPC_threadprivate:
8624 case OMPC_flush:
8625 case OMPC_read:
8626 case OMPC_write:
8627 case OMPC_update:
8628 case OMPC_capture:
8629 case OMPC_seq_cst:
8630 case OMPC_depend:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008631 case OMPC_threads:
8632 case OMPC_simd:
8633 case OMPC_map:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008634 case OMPC_priority:
8635 case OMPC_grainsize:
8636 case OMPC_nogroup:
8637 case OMPC_num_tasks:
8638 case OMPC_hint:
8639 case OMPC_defaultmap:
8640 case OMPC_unknown:
8641 case OMPC_uniform:
8642 case OMPC_to:
8643 case OMPC_from:
8644 case OMPC_use_device_ptr:
8645 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00008646 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00008647 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00008648 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00008649 case OMPC_dynamic_allocators:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008650 llvm_unreachable("Unexpected OpenMP clause.");
8651 }
8652 return CaptureRegion;
8653}
8654
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008655OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
8656 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008657 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008658 SourceLocation NameModifierLoc,
8659 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008660 SourceLocation EndLoc) {
8661 Expr *ValExpr = Condition;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008662 Stmt *HelperValStmt = nullptr;
8663 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008664 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
8665 !Condition->isInstantiationDependent() &&
8666 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00008667 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008668 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008669 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008670
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008671 ValExpr = Val.get();
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008672
8673 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
8674 CaptureRegion =
8675 getOpenMPCaptureRegionForClause(DKind, OMPC_if, NameModifier);
Alexey Bataev2ba67042017-11-28 21:11:44 +00008676 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008677 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00008678 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008679 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8680 HelperValStmt = buildPreInits(Context, Captures);
8681 }
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008682 }
8683
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008684 return new (Context)
8685 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
8686 LParenLoc, NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008687}
8688
Alexey Bataev3778b602014-07-17 07:32:53 +00008689OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
8690 SourceLocation StartLoc,
8691 SourceLocation LParenLoc,
8692 SourceLocation EndLoc) {
8693 Expr *ValExpr = Condition;
8694 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
8695 !Condition->isInstantiationDependent() &&
8696 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00008697 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataev3778b602014-07-17 07:32:53 +00008698 if (Val.isInvalid())
8699 return nullptr;
8700
Richard Smith03a4aa32016-06-23 19:02:52 +00008701 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataev3778b602014-07-17 07:32:53 +00008702 }
8703
8704 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8705}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00008706ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
8707 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00008708 if (!Op)
8709 return ExprError();
8710
8711 class IntConvertDiagnoser : public ICEConvertDiagnoser {
8712 public:
8713 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00008714 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00008715 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
8716 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008717 return S.Diag(Loc, diag::err_omp_not_integral) << T;
8718 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008719 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
8720 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008721 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
8722 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008723 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
8724 QualType T,
8725 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008726 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
8727 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008728 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
8729 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008730 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00008731 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00008732 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008733 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
8734 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008735 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
8736 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008737 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
8738 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008739 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00008740 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00008741 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008742 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
8743 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008744 llvm_unreachable("conversion functions are permitted");
8745 }
8746 } ConvertDiagnoser;
8747 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
8748}
8749
Alexey Bataeve3727102018-04-18 15:57:46 +00008750static bool isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00008751 OpenMPClauseKind CKind,
8752 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008753 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
8754 !ValExpr->isInstantiationDependent()) {
8755 SourceLocation Loc = ValExpr->getExprLoc();
8756 ExprResult Value =
8757 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
8758 if (Value.isInvalid())
8759 return false;
8760
8761 ValExpr = Value.get();
8762 // The expression must evaluate to a non-negative integer value.
8763 llvm::APSInt Result;
8764 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00008765 Result.isSigned() &&
8766 !((!StrictlyPositive && Result.isNonNegative()) ||
8767 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008768 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00008769 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
8770 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008771 return false;
8772 }
8773 }
8774 return true;
8775}
8776
Alexey Bataev568a8332014-03-06 06:15:19 +00008777OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
8778 SourceLocation StartLoc,
8779 SourceLocation LParenLoc,
8780 SourceLocation EndLoc) {
8781 Expr *ValExpr = NumThreads;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008782 Stmt *HelperValStmt = nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00008783
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008784 // OpenMP [2.5, Restrictions]
8785 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +00008786 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
Alexey Bataeva0569352015-12-01 10:17:31 +00008787 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008788 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00008789
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008790 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +00008791 OpenMPDirectiveKind CaptureRegion =
8792 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads);
8793 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008794 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00008795 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008796 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8797 HelperValStmt = buildPreInits(Context, Captures);
8798 }
8799
8800 return new (Context) OMPNumThreadsClause(
8801 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00008802}
8803
Alexey Bataev62c87d22014-03-21 04:51:18 +00008804ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008805 OpenMPClauseKind CKind,
8806 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00008807 if (!E)
8808 return ExprError();
8809 if (E->isValueDependent() || E->isTypeDependent() ||
8810 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00008811 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00008812 llvm::APSInt Result;
8813 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
8814 if (ICE.isInvalid())
8815 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008816 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
8817 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00008818 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008819 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
8820 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00008821 return ExprError();
8822 }
Alexander Musman09184fe2014-09-30 05:29:28 +00008823 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
8824 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
8825 << E->getSourceRange();
8826 return ExprError();
8827 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008828 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
8829 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00008830 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008831 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00008832 return ICE;
8833}
8834
8835OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
8836 SourceLocation LParenLoc,
8837 SourceLocation EndLoc) {
8838 // OpenMP [2.8.1, simd construct, Description]
8839 // The parameter of the safelen clause must be a constant
8840 // positive integer expression.
8841 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
8842 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008843 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00008844 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00008845 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00008846}
8847
Alexey Bataev66b15b52015-08-21 11:14:16 +00008848OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
8849 SourceLocation LParenLoc,
8850 SourceLocation EndLoc) {
8851 // OpenMP [2.8.1, simd construct, Description]
8852 // The parameter of the simdlen clause must be a constant
8853 // positive integer expression.
8854 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
8855 if (Simdlen.isInvalid())
8856 return nullptr;
8857 return new (Context)
8858 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
8859}
8860
Alexander Musman64d33f12014-06-04 07:53:32 +00008861OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
8862 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00008863 SourceLocation LParenLoc,
8864 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00008865 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00008866 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00008867 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00008868 // The parameter of the collapse clause must be a constant
8869 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00008870 ExprResult NumForLoopsResult =
8871 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
8872 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00008873 return nullptr;
8874 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00008875 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00008876}
8877
Alexey Bataev10e775f2015-07-30 11:36:16 +00008878OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
8879 SourceLocation EndLoc,
8880 SourceLocation LParenLoc,
8881 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00008882 // OpenMP [2.7.1, loop construct, Description]
8883 // OpenMP [2.8.1, simd construct, Description]
8884 // OpenMP [2.9.6, distribute construct, Description]
8885 // The parameter of the ordered clause must be a constant
8886 // positive integer expression if any.
8887 if (NumForLoops && LParenLoc.isValid()) {
8888 ExprResult NumForLoopsResult =
8889 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
8890 if (NumForLoopsResult.isInvalid())
8891 return nullptr;
8892 NumForLoops = NumForLoopsResult.get();
Alexey Bataeve3727102018-04-18 15:57:46 +00008893 } else {
Alexey Bataev346265e2015-09-25 10:37:12 +00008894 NumForLoops = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +00008895 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00008896 auto *Clause = OMPOrderedClause::Create(
8897 Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0,
8898 StartLoc, LParenLoc, EndLoc);
8899 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause);
8900 return Clause;
Alexey Bataev10e775f2015-07-30 11:36:16 +00008901}
8902
Alexey Bataeved09d242014-05-28 05:53:51 +00008903OMPClause *Sema::ActOnOpenMPSimpleClause(
8904 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
8905 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008906 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008907 switch (Kind) {
8908 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00008909 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00008910 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
8911 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008912 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008913 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00008914 Res = ActOnOpenMPProcBindClause(
8915 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
8916 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008917 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008918 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00008919 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00008920 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00008921 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00008922 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00008923 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008924 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008925 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008926 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00008927 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00008928 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00008929 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008930 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008931 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00008932 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008933 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008934 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008935 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008936 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00008937 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008938 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008939 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008940 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00008941 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008942 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008943 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008944 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008945 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008946 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008947 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00008948 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00008949 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008950 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008951 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00008952 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008953 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00008954 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008955 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00008956 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00008957 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00008958 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008959 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008960 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008961 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008962 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00008963 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00008964 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00008965 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00008966 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00008967 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00008968 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00008969 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00008970 case OMPC_dynamic_allocators:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008971 llvm_unreachable("Clause is not allowed.");
8972 }
8973 return Res;
8974}
8975
Alexey Bataev6402bca2015-12-28 07:25:51 +00008976static std::string
8977getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
8978 ArrayRef<unsigned> Exclude = llvm::None) {
Alexey Bataeve3727102018-04-18 15:57:46 +00008979 SmallString<256> Buffer;
8980 llvm::raw_svector_ostream Out(Buffer);
Alexey Bataev6402bca2015-12-28 07:25:51 +00008981 unsigned Bound = Last >= 2 ? Last - 2 : 0;
8982 unsigned Skipped = Exclude.size();
8983 auto S = Exclude.begin(), E = Exclude.end();
Alexey Bataeve3727102018-04-18 15:57:46 +00008984 for (unsigned I = First; I < Last; ++I) {
8985 if (std::find(S, E, I) != E) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00008986 --Skipped;
8987 continue;
8988 }
Alexey Bataeve3727102018-04-18 15:57:46 +00008989 Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'";
8990 if (I == Bound - Skipped)
8991 Out << " or ";
8992 else if (I != Bound + 1 - Skipped)
8993 Out << ", ";
Alexey Bataev6402bca2015-12-28 07:25:51 +00008994 }
Alexey Bataeve3727102018-04-18 15:57:46 +00008995 return Out.str();
Alexey Bataev6402bca2015-12-28 07:25:51 +00008996}
8997
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008998OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
8999 SourceLocation KindKwLoc,
9000 SourceLocation StartLoc,
9001 SourceLocation LParenLoc,
9002 SourceLocation EndLoc) {
9003 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00009004 static_assert(OMPC_DEFAULT_unknown > 0,
9005 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009006 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009007 << getListOfPossibleValues(OMPC_default, /*First=*/0,
9008 /*Last=*/OMPC_DEFAULT_unknown)
9009 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009010 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009011 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00009012 switch (Kind) {
9013 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009014 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009015 break;
9016 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009017 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009018 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009019 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009020 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00009021 break;
9022 }
Alexey Bataeved09d242014-05-28 05:53:51 +00009023 return new (Context)
9024 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009025}
9026
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009027OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
9028 SourceLocation KindKwLoc,
9029 SourceLocation StartLoc,
9030 SourceLocation LParenLoc,
9031 SourceLocation EndLoc) {
9032 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009033 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009034 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
9035 /*Last=*/OMPC_PROC_BIND_unknown)
9036 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009037 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009038 }
Alexey Bataeved09d242014-05-28 05:53:51 +00009039 return new (Context)
9040 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009041}
9042
Alexey Bataev56dafe82014-06-20 07:16:17 +00009043OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00009044 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00009045 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00009046 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00009047 SourceLocation EndLoc) {
9048 OMPClause *Res = nullptr;
9049 switch (Kind) {
9050 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00009051 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
9052 assert(Argument.size() == NumberOfElements &&
9053 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009054 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00009055 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
9056 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
9057 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
9058 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
9059 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009060 break;
9061 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00009062 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
9063 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
9064 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
9065 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009066 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00009067 case OMPC_dist_schedule:
9068 Res = ActOnOpenMPDistScheduleClause(
9069 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
9070 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
9071 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009072 case OMPC_defaultmap:
9073 enum { Modifier, DefaultmapKind };
9074 Res = ActOnOpenMPDefaultmapClause(
9075 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
9076 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
David Majnemer9d168222016-08-05 17:44:54 +00009077 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind],
9078 EndLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009079 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00009080 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009081 case OMPC_num_threads:
9082 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009083 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009084 case OMPC_collapse:
9085 case OMPC_default:
9086 case OMPC_proc_bind:
9087 case OMPC_private:
9088 case OMPC_firstprivate:
9089 case OMPC_lastprivate:
9090 case OMPC_shared:
9091 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00009092 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00009093 case OMPC_in_reduction:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009094 case OMPC_linear:
9095 case OMPC_aligned:
9096 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009097 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009098 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00009099 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009100 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009101 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009102 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00009103 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009104 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00009105 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00009106 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00009107 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009108 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009109 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00009110 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00009111 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009112 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00009113 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009114 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009115 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009116 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009117 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00009118 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00009119 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009120 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009121 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009122 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00009123 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00009124 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00009125 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00009126 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00009127 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00009128 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009129 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009130 case OMPC_dynamic_allocators:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009131 llvm_unreachable("Clause is not allowed.");
9132 }
9133 return Res;
9134}
9135
Alexey Bataev6402bca2015-12-28 07:25:51 +00009136static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
9137 OpenMPScheduleClauseModifier M2,
9138 SourceLocation M1Loc, SourceLocation M2Loc) {
9139 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
9140 SmallVector<unsigned, 2> Excluded;
9141 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
9142 Excluded.push_back(M2);
9143 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
9144 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
9145 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
9146 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
9147 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
9148 << getListOfPossibleValues(OMPC_schedule,
9149 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
9150 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
9151 Excluded)
9152 << getOpenMPClauseName(OMPC_schedule);
9153 return true;
9154 }
9155 return false;
9156}
9157
Alexey Bataev56dafe82014-06-20 07:16:17 +00009158OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00009159 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00009160 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00009161 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
9162 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
9163 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
9164 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
9165 return nullptr;
9166 // OpenMP, 2.7.1, Loop Construct, Restrictions
9167 // Either the monotonic modifier or the nonmonotonic modifier can be specified
9168 // but not both.
9169 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
9170 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
9171 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
9172 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
9173 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
9174 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
9175 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
9176 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
9177 return nullptr;
9178 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00009179 if (Kind == OMPC_SCHEDULE_unknown) {
9180 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00009181 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
9182 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
9183 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
9184 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
9185 Exclude);
9186 } else {
9187 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
9188 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009189 }
9190 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
9191 << Values << getOpenMPClauseName(OMPC_schedule);
9192 return nullptr;
9193 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00009194 // OpenMP, 2.7.1, Loop Construct, Restrictions
9195 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
9196 // schedule(guided).
9197 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
9198 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
9199 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
9200 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
9201 diag::err_omp_schedule_nonmonotonic_static);
9202 return nullptr;
9203 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00009204 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00009205 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00009206 if (ChunkSize) {
9207 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
9208 !ChunkSize->isInstantiationDependent() &&
9209 !ChunkSize->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00009210 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
Alexey Bataev56dafe82014-06-20 07:16:17 +00009211 ExprResult Val =
9212 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
9213 if (Val.isInvalid())
9214 return nullptr;
9215
9216 ValExpr = Val.get();
9217
9218 // OpenMP [2.7.1, Restrictions]
9219 // chunk_size must be a loop invariant integer expression with a positive
9220 // value.
9221 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00009222 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
9223 if (Result.isSigned() && !Result.isStrictlyPositive()) {
9224 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00009225 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00009226 return nullptr;
9227 }
Alexey Bataev2ba67042017-11-28 21:11:44 +00009228 } else if (getOpenMPCaptureRegionForClause(
9229 DSAStack->getCurrentDirective(), OMPC_schedule) !=
9230 OMPD_unknown &&
Alexey Bataevb46cdea2016-06-15 11:20:48 +00009231 !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00009232 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00009233 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev5a3af132016-03-29 08:58:54 +00009234 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
9235 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009236 }
9237 }
9238 }
9239
Alexey Bataev6402bca2015-12-28 07:25:51 +00009240 return new (Context)
9241 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00009242 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009243}
9244
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009245OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
9246 SourceLocation StartLoc,
9247 SourceLocation EndLoc) {
9248 OMPClause *Res = nullptr;
9249 switch (Kind) {
9250 case OMPC_ordered:
9251 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
9252 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00009253 case OMPC_nowait:
9254 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
9255 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009256 case OMPC_untied:
9257 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
9258 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009259 case OMPC_mergeable:
9260 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
9261 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009262 case OMPC_read:
9263 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
9264 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00009265 case OMPC_write:
9266 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
9267 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00009268 case OMPC_update:
9269 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
9270 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00009271 case OMPC_capture:
9272 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
9273 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009274 case OMPC_seq_cst:
9275 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
9276 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00009277 case OMPC_threads:
9278 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
9279 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009280 case OMPC_simd:
9281 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
9282 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00009283 case OMPC_nogroup:
9284 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
9285 break;
Kelvin Li1408f912018-09-26 04:28:39 +00009286 case OMPC_unified_address:
9287 Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc);
9288 break;
Patrick Lyster4a370b92018-10-01 13:47:43 +00009289 case OMPC_unified_shared_memory:
9290 Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
9291 break;
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009292 case OMPC_reverse_offload:
9293 Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc);
9294 break;
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009295 case OMPC_dynamic_allocators:
9296 Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc);
9297 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009298 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00009299 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009300 case OMPC_num_threads:
9301 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009302 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009303 case OMPC_collapse:
9304 case OMPC_schedule:
9305 case OMPC_private:
9306 case OMPC_firstprivate:
9307 case OMPC_lastprivate:
9308 case OMPC_shared:
9309 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00009310 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00009311 case OMPC_in_reduction:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009312 case OMPC_linear:
9313 case OMPC_aligned:
9314 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009315 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009316 case OMPC_default:
9317 case OMPC_proc_bind:
9318 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00009319 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009320 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00009321 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00009322 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009323 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009324 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009325 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009326 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00009327 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009328 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00009329 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009330 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009331 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009332 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00009333 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00009334 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00009335 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00009336 case OMPC_is_device_ptr:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009337 llvm_unreachable("Clause is not allowed.");
9338 }
9339 return Res;
9340}
9341
Alexey Bataev236070f2014-06-20 11:19:47 +00009342OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
9343 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00009344 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00009345 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
9346}
9347
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009348OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
9349 SourceLocation EndLoc) {
9350 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
9351}
9352
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009353OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
9354 SourceLocation EndLoc) {
9355 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
9356}
9357
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009358OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
9359 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009360 return new (Context) OMPReadClause(StartLoc, EndLoc);
9361}
9362
Alexey Bataevdea47612014-07-23 07:46:59 +00009363OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
9364 SourceLocation EndLoc) {
9365 return new (Context) OMPWriteClause(StartLoc, EndLoc);
9366}
9367
Alexey Bataev67a4f222014-07-23 10:25:33 +00009368OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
9369 SourceLocation EndLoc) {
9370 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
9371}
9372
Alexey Bataev459dec02014-07-24 06:46:57 +00009373OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
9374 SourceLocation EndLoc) {
9375 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
9376}
9377
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009378OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
9379 SourceLocation EndLoc) {
9380 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
9381}
9382
Alexey Bataev346265e2015-09-25 10:37:12 +00009383OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
9384 SourceLocation EndLoc) {
9385 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
9386}
9387
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009388OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
9389 SourceLocation EndLoc) {
9390 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
9391}
9392
Alexey Bataevb825de12015-12-07 10:51:44 +00009393OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
9394 SourceLocation EndLoc) {
9395 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
9396}
9397
Kelvin Li1408f912018-09-26 04:28:39 +00009398OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc,
9399 SourceLocation EndLoc) {
9400 return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc);
9401}
9402
Patrick Lyster4a370b92018-10-01 13:47:43 +00009403OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc,
9404 SourceLocation EndLoc) {
9405 return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
9406}
9407
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009408OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc,
9409 SourceLocation EndLoc) {
9410 return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc);
9411}
9412
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009413OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc,
9414 SourceLocation EndLoc) {
9415 return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc);
9416}
9417
Alexey Bataevc5e02582014-06-16 07:08:35 +00009418OMPClause *Sema::ActOnOpenMPVarListClause(
9419 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
9420 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
9421 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009422 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00009423 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
9424 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
9425 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009426 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009427 switch (Kind) {
9428 case OMPC_private:
9429 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9430 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009431 case OMPC_firstprivate:
9432 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9433 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00009434 case OMPC_lastprivate:
9435 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9436 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00009437 case OMPC_shared:
9438 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
9439 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009440 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00009441 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9442 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00009443 break;
Alexey Bataev169d96a2017-07-18 20:17:46 +00009444 case OMPC_task_reduction:
9445 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9446 EndLoc, ReductionIdScopeSpec,
9447 ReductionId);
9448 break;
Alexey Bataevfa312f32017-07-21 18:48:21 +00009449 case OMPC_in_reduction:
9450 Res =
9451 ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9452 EndLoc, ReductionIdScopeSpec, ReductionId);
9453 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00009454 case OMPC_linear:
9455 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00009456 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00009457 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009458 case OMPC_aligned:
9459 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
9460 ColonLoc, EndLoc);
9461 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009462 case OMPC_copyin:
9463 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
9464 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009465 case OMPC_copyprivate:
9466 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9467 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00009468 case OMPC_flush:
9469 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
9470 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009471 case OMPC_depend:
David Majnemer9d168222016-08-05 17:44:54 +00009472 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
Kelvin Li0bff7af2015-11-23 05:32:03 +00009473 StartLoc, LParenLoc, EndLoc);
9474 break;
9475 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00009476 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
9477 DepLinMapLoc, ColonLoc, VarList, StartLoc,
9478 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009479 break;
Samuel Antao661c0902016-05-26 17:39:58 +00009480 case OMPC_to:
9481 Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc);
9482 break;
Samuel Antaoec172c62016-05-26 17:49:04 +00009483 case OMPC_from:
9484 Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc);
9485 break;
Carlo Bertolli2404b172016-07-13 15:37:16 +00009486 case OMPC_use_device_ptr:
9487 Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
9488 break;
Carlo Bertolli70594e92016-07-13 17:16:49 +00009489 case OMPC_is_device_ptr:
9490 Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
9491 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009492 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00009493 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00009494 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00009495 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009496 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00009497 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009498 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009499 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009500 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009501 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00009502 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009503 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009504 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009505 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009506 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00009507 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00009508 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00009509 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009510 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00009511 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00009512 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009513 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009514 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009515 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009516 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009517 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00009518 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00009519 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009520 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00009521 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009522 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009523 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009524 case OMPC_uniform:
Kelvin Li1408f912018-09-26 04:28:39 +00009525 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00009526 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009527 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009528 case OMPC_dynamic_allocators:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009529 llvm_unreachable("Clause is not allowed.");
9530 }
9531 return Res;
9532}
9533
Alexey Bataev90c228f2016-02-08 09:29:13 +00009534ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00009535 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00009536 ExprResult Res = BuildDeclRefExpr(
9537 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
9538 if (!Res.isUsable())
9539 return ExprError();
9540 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
9541 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
9542 if (!Res.isUsable())
9543 return ExprError();
9544 }
9545 if (VK != VK_LValue && Res.get()->isGLValue()) {
9546 Res = DefaultLvalueConversion(Res.get());
9547 if (!Res.isUsable())
9548 return ExprError();
9549 }
9550 return Res;
9551}
9552
Alexey Bataev60da77e2016-02-29 05:54:20 +00009553static std::pair<ValueDecl *, bool>
9554getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
9555 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00009556 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
9557 RefExpr->containsUnexpandedParameterPack())
9558 return std::make_pair(nullptr, true);
9559
Alexey Bataevd985eda2016-02-10 11:29:16 +00009560 // OpenMP [3.1, C/C++]
9561 // A list item is a variable name.
9562 // OpenMP [2.9.3.3, Restrictions, p.1]
9563 // A variable that is part of another variable (as an array or
9564 // structure element) cannot appear in a private clause.
9565 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009566 enum {
9567 NoArrayExpr = -1,
9568 ArraySubscript = 0,
9569 OMPArraySection = 1
9570 } IsArrayExpr = NoArrayExpr;
9571 if (AllowArraySection) {
9572 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009573 Expr *Base = ASE->getBase()->IgnoreParenImpCasts();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009574 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
9575 Base = TempASE->getBase()->IgnoreParenImpCasts();
9576 RefExpr = Base;
9577 IsArrayExpr = ArraySubscript;
9578 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009579 Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009580 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
9581 Base = TempOASE->getBase()->IgnoreParenImpCasts();
9582 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
9583 Base = TempASE->getBase()->IgnoreParenImpCasts();
9584 RefExpr = Base;
9585 IsArrayExpr = OMPArraySection;
9586 }
9587 }
9588 ELoc = RefExpr->getExprLoc();
9589 ERange = RefExpr->getSourceRange();
9590 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00009591 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
9592 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
9593 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
9594 (S.getCurrentThisType().isNull() || !ME ||
9595 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
9596 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009597 if (IsArrayExpr != NoArrayExpr) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009598 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
9599 << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +00009600 } else {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009601 S.Diag(ELoc,
9602 AllowArraySection
9603 ? diag::err_omp_expected_var_name_member_expr_or_array_item
9604 : diag::err_omp_expected_var_name_member_expr)
9605 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
9606 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009607 return std::make_pair(nullptr, false);
9608 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +00009609 return std::make_pair(
9610 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009611}
9612
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009613OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
9614 SourceLocation StartLoc,
9615 SourceLocation LParenLoc,
9616 SourceLocation EndLoc) {
9617 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00009618 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeve3727102018-04-18 15:57:46 +00009619 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +00009620 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009621 SourceLocation ELoc;
9622 SourceRange ERange;
9623 Expr *SimpleRefExpr = RefExpr;
9624 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009625 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009626 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009627 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009628 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009629 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009630 ValueDecl *D = Res.first;
9631 if (!D)
9632 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009633
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009634 QualType Type = D->getType();
9635 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009636
9637 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9638 // A variable that appears in a private clause must not have an incomplete
9639 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009640 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009641 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009642 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009643
Alexey Bataev758e55e2013-09-06 18:03:48 +00009644 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9645 // in a Construct]
9646 // Variables with the predetermined data-sharing attributes may not be
9647 // listed in data-sharing attributes clauses, except for the cases
9648 // listed below. For these exceptions only, listing a predetermined
9649 // variable in a data-sharing attribute clause is allowed and overrides
9650 // the variable's predetermined data-sharing attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +00009651 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009652 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00009653 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9654 << getOpenMPClauseName(OMPC_private);
Alexey Bataeve3727102018-04-18 15:57:46 +00009655 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009656 continue;
9657 }
9658
Alexey Bataeve3727102018-04-18 15:57:46 +00009659 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009660 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00009661 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Kelvin Libf594a52016-12-17 05:48:59 +00009662 isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009663 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
9664 << getOpenMPClauseName(OMPC_private) << Type
Kelvin Libf594a52016-12-17 05:48:59 +00009665 << getOpenMPDirectiveName(CurrDir);
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009666 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009667 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009668 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009669 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009670 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009671 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009672 continue;
9673 }
9674
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009675 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
9676 // A list item cannot appear in both a map clause and a data-sharing
9677 // attribute clause on the same construct
Alexey Bataeve3727102018-04-18 15:57:46 +00009678 if (isOpenMPTargetExecutionDirective(CurrDir)) {
Samuel Antao6890b092016-07-28 14:25:09 +00009679 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +00009680 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +00009681 VD, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +00009682 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
9683 OpenMPClauseKind WhereFoundClauseKind) -> bool {
9684 ConflictKind = WhereFoundClauseKind;
9685 return true;
9686 })) {
9687 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009688 << getOpenMPClauseName(OMPC_private)
Samuel Antao6890b092016-07-28 14:25:09 +00009689 << getOpenMPClauseName(ConflictKind)
Kelvin Libf594a52016-12-17 05:48:59 +00009690 << getOpenMPDirectiveName(CurrDir);
Alexey Bataeve3727102018-04-18 15:57:46 +00009691 reportOriginalDsa(*this, DSAStack, D, DVar);
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009692 continue;
9693 }
9694 }
9695
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009696 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
9697 // A variable of class type (or array thereof) that appears in a private
9698 // clause requires an accessible, unambiguous default constructor for the
9699 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00009700 // Generate helper private variable and initialize it with the default
9701 // value. The address of the original variable is replaced by the address of
9702 // the new private variable in CodeGen. This new variable is not added to
9703 // IdResolver, so the code in the OpenMP region uses original variable for
9704 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009705 Type = Type.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +00009706 VarDecl *VDPrivate =
Alexey Bataev63cc8e92018-03-20 14:45:59 +00009707 buildVarDecl(*this, ELoc, Type, D->getName(),
9708 D->hasAttrs() ? &D->getAttrs() : nullptr,
9709 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Richard Smith3beb7c62017-01-12 02:27:38 +00009710 ActOnUninitializedDecl(VDPrivate);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009711 if (VDPrivate->isInvalidDecl())
9712 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00009713 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009714 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009715
Alexey Bataev90c228f2016-02-08 09:29:13 +00009716 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009717 if (!VD && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00009718 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00009719 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009720 Vars.push_back((VD || CurContext->isDependentContext())
9721 ? RefExpr->IgnoreParens()
9722 : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009723 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009724 }
9725
Alexey Bataeved09d242014-05-28 05:53:51 +00009726 if (Vars.empty())
9727 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009728
Alexey Bataev03b340a2014-10-21 03:16:40 +00009729 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
9730 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009731}
9732
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009733namespace {
9734class DiagsUninitializedSeveretyRAII {
9735private:
9736 DiagnosticsEngine &Diags;
9737 SourceLocation SavedLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00009738 bool IsIgnored = false;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009739
9740public:
9741 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
9742 bool IsIgnored)
9743 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
9744 if (!IsIgnored) {
9745 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
9746 /*Map*/ diag::Severity::Ignored, Loc);
9747 }
9748 }
9749 ~DiagsUninitializedSeveretyRAII() {
9750 if (!IsIgnored)
9751 Diags.popMappings(SavedLoc);
9752 }
9753};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00009754}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009755
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009756OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
9757 SourceLocation StartLoc,
9758 SourceLocation LParenLoc,
9759 SourceLocation EndLoc) {
9760 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009761 SmallVector<Expr *, 8> PrivateCopies;
9762 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00009763 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009764 bool IsImplicitClause =
9765 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
Alexey Bataeve3727102018-04-18 15:57:46 +00009766 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009767
Alexey Bataeve3727102018-04-18 15:57:46 +00009768 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +00009769 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009770 SourceLocation ELoc;
9771 SourceRange ERange;
9772 Expr *SimpleRefExpr = RefExpr;
9773 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009774 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009775 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009776 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009777 PrivateCopies.push_back(nullptr);
9778 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009779 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009780 ValueDecl *D = Res.first;
9781 if (!D)
9782 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009783
Alexey Bataev60da77e2016-02-29 05:54:20 +00009784 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00009785 QualType Type = D->getType();
9786 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009787
9788 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9789 // A variable that appears in a private clause must not have an incomplete
9790 // type or a reference type.
9791 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00009792 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009793 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009794 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009795
9796 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
9797 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00009798 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009799 // class type.
Alexey Bataeve3727102018-04-18 15:57:46 +00009800 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009801
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009802 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00009803 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009804 if (!IsImplicitClause) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009805 DSAStackTy::DSAVarData DVar =
9806 DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00009807 TopDVar = DVar;
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009808 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009809 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009810 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
9811 // A list item that specifies a given variable may not appear in more
9812 // than one clause on the same directive, except that a variable may be
9813 // specified in both firstprivate and lastprivate clauses.
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009814 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
9815 // A list item may appear in a firstprivate or lastprivate clause but not
9816 // both.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009817 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevb358f992017-12-01 17:40:15 +00009818 (isOpenMPDistributeDirective(CurrDir) ||
9819 DVar.CKind != OMPC_lastprivate) &&
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009820 DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009821 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00009822 << getOpenMPClauseName(DVar.CKind)
9823 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +00009824 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009825 continue;
9826 }
9827
9828 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9829 // in a Construct]
9830 // Variables with the predetermined data-sharing attributes may not be
9831 // listed in data-sharing attributes clauses, except for the cases
9832 // listed below. For these exceptions only, listing a predetermined
9833 // variable in a data-sharing attribute clause is allowed and overrides
9834 // the variable's predetermined data-sharing attributes.
9835 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9836 // in a Construct, C/C++, p.2]
9837 // Variables with const-qualified type having no mutable member may be
9838 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00009839 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009840 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
9841 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00009842 << getOpenMPClauseName(DVar.CKind)
9843 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +00009844 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009845 continue;
9846 }
9847
9848 // OpenMP [2.9.3.4, Restrictions, p.2]
9849 // A list item that is private within a parallel region must not appear
9850 // in a firstprivate clause on a worksharing construct if any of the
9851 // worksharing regions arising from the worksharing construct ever bind
9852 // to any of the parallel regions arising from the parallel construct.
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009853 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
9854 // A list item that is private within a teams region must not appear in a
9855 // firstprivate clause on a distribute construct if any of the distribute
9856 // regions arising from the distribute construct ever bind to any of the
9857 // teams regions arising from the teams construct.
9858 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
9859 // A list item that appears in a reduction clause of a teams construct
9860 // must not appear in a firstprivate clause on a distribute construct if
9861 // any of the distribute regions arising from the distribute construct
9862 // ever bind to any of the teams regions arising from the teams construct.
9863 if ((isOpenMPWorksharingDirective(CurrDir) ||
9864 isOpenMPDistributeDirective(CurrDir)) &&
Kelvin Li579e41c2016-11-30 23:51:03 +00009865 !isOpenMPParallelDirective(CurrDir) &&
9866 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00009867 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009868 if (DVar.CKind != OMPC_shared &&
9869 (isOpenMPParallelDirective(DVar.DKind) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009870 isOpenMPTeamsDirective(DVar.DKind) ||
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009871 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00009872 Diag(ELoc, diag::err_omp_required_access)
9873 << getOpenMPClauseName(OMPC_firstprivate)
9874 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +00009875 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00009876 continue;
9877 }
9878 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009879 // OpenMP [2.9.3.4, Restrictions, p.3]
9880 // A list item that appears in a reduction clause of a parallel construct
9881 // must not appear in a firstprivate clause on a worksharing or task
9882 // construct if any of the worksharing or task regions arising from the
9883 // worksharing or task construct ever bind to any of the parallel regions
9884 // arising from the parallel construct.
9885 // OpenMP [2.9.3.4, Restrictions, p.4]
9886 // A list item that appears in a reduction clause in worksharing
9887 // construct must not appear in a firstprivate clause in a task construct
9888 // encountered during execution of any of the worksharing regions arising
9889 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +00009890 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00009891 DVar = DSAStack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00009892 D, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
9893 [](OpenMPDirectiveKind K) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00009894 return isOpenMPParallelDirective(K) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009895 isOpenMPWorksharingDirective(K) ||
9896 isOpenMPTeamsDirective(K);
Alexey Bataev7ace49d2016-05-17 08:55:33 +00009897 },
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009898 /*FromParent=*/true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009899 if (DVar.CKind == OMPC_reduction &&
9900 (isOpenMPParallelDirective(DVar.DKind) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009901 isOpenMPWorksharingDirective(DVar.DKind) ||
9902 isOpenMPTeamsDirective(DVar.DKind))) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009903 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
9904 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataeve3727102018-04-18 15:57:46 +00009905 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009906 continue;
9907 }
9908 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00009909
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009910 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
9911 // A list item cannot appear in both a map clause and a data-sharing
9912 // attribute clause on the same construct
Alexey Bataevb358f992017-12-01 17:40:15 +00009913 if (isOpenMPTargetExecutionDirective(CurrDir)) {
Samuel Antao6890b092016-07-28 14:25:09 +00009914 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +00009915 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +00009916 VD, /*CurrentRegionOnly=*/true,
Alexey Bataeve3727102018-04-18 15:57:46 +00009917 [&ConflictKind](
9918 OMPClauseMappableExprCommon::MappableExprComponentListRef,
9919 OpenMPClauseKind WhereFoundClauseKind) {
Samuel Antao6890b092016-07-28 14:25:09 +00009920 ConflictKind = WhereFoundClauseKind;
9921 return true;
9922 })) {
9923 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009924 << getOpenMPClauseName(OMPC_firstprivate)
Samuel Antao6890b092016-07-28 14:25:09 +00009925 << getOpenMPClauseName(ConflictKind)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009926 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +00009927 reportOriginalDsa(*this, DSAStack, D, DVar);
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009928 continue;
9929 }
9930 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009931 }
9932
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009933 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00009934 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00009935 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009936 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
9937 << getOpenMPClauseName(OMPC_firstprivate) << Type
9938 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
9939 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00009940 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009941 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00009942 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009943 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00009944 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009945 continue;
9946 }
9947
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009948 Type = Type.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +00009949 VarDecl *VDPrivate =
Alexey Bataev63cc8e92018-03-20 14:45:59 +00009950 buildVarDecl(*this, ELoc, Type, D->getName(),
9951 D->hasAttrs() ? &D->getAttrs() : nullptr,
9952 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009953 // Generate helper private variable and initialize it with the value of the
9954 // original variable. The address of the original variable is replaced by
9955 // the address of the new private variable in the CodeGen. This new variable
9956 // is not added to IdResolver, so the code in the OpenMP region uses
9957 // original variable for proper diagnostics and variable capturing.
9958 Expr *VDInitRefExpr = nullptr;
9959 // For arrays generate initializer for single element and replace it by the
9960 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009961 if (Type->isArrayType()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009962 VarDecl *VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00009963 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009964 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataeve3727102018-04-18 15:57:46 +00009965 Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009966 ElemType = ElemType.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +00009967 VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
9968 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00009969 InitializedEntity Entity =
9970 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009971 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
9972
9973 InitializationSequence InitSeq(*this, Entity, Kind, Init);
9974 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
9975 if (Result.isInvalid())
9976 VDPrivate->setInvalidDecl();
9977 else
9978 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009979 // Remove temp variable declaration.
9980 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009981 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +00009982 VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
9983 ".firstprivate.temp");
Alexey Bataevd985eda2016-02-10 11:29:16 +00009984 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
9985 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00009986 AddInitializerToDecl(VDPrivate,
9987 DefaultLvalueConversion(VDInitRefExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +00009988 /*DirectInit=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009989 }
9990 if (VDPrivate->isInvalidDecl()) {
9991 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00009992 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009993 diag::note_omp_task_predetermined_firstprivate_here);
9994 }
9995 continue;
9996 }
9997 CurContext->addDecl(VDPrivate);
Alexey Bataeve3727102018-04-18 15:57:46 +00009998 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00009999 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
10000 RefExpr->getExprLoc());
10001 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010002 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010003 if (TopDVar.CKind == OMPC_lastprivate) {
Alexey Bataev005248a2016-02-25 05:25:57 +000010004 Ref = TopDVar.PrivateCopy;
Alexey Bataeve3727102018-04-18 15:57:46 +000010005 } else {
Alexey Bataev61205072016-03-02 04:57:40 +000010006 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataeve3727102018-04-18 15:57:46 +000010007 if (!isOpenMPCapturedDecl(D))
Alexey Bataev005248a2016-02-25 05:25:57 +000010008 ExprCaptures.push_back(Ref->getDecl());
10009 }
Alexey Bataev417089f2016-02-17 13:19:37 +000010010 }
Alexey Bataevd985eda2016-02-10 11:29:16 +000010011 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010012 Vars.push_back((VD || CurContext->isDependentContext())
10013 ? RefExpr->IgnoreParens()
10014 : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010015 PrivateCopies.push_back(VDPrivateRefExpr);
10016 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010017 }
10018
Alexey Bataeved09d242014-05-28 05:53:51 +000010019 if (Vars.empty())
10020 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010021
10022 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +000010023 Vars, PrivateCopies, Inits,
10024 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010025}
10026
Alexander Musman1bb328c2014-06-04 13:06:39 +000010027OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
10028 SourceLocation StartLoc,
10029 SourceLocation LParenLoc,
10030 SourceLocation EndLoc) {
10031 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +000010032 SmallVector<Expr *, 8> SrcExprs;
10033 SmallVector<Expr *, 8> DstExprs;
10034 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +000010035 SmallVector<Decl *, 4> ExprCaptures;
10036 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeve3727102018-04-18 15:57:46 +000010037 for (Expr *RefExpr : VarList) {
Alexander Musman1bb328c2014-06-04 13:06:39 +000010038 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +000010039 SourceLocation ELoc;
10040 SourceRange ERange;
10041 Expr *SimpleRefExpr = RefExpr;
10042 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +000010043 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +000010044 // It will be analyzed later.
10045 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +000010046 SrcExprs.push_back(nullptr);
10047 DstExprs.push_back(nullptr);
10048 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010049 }
Alexey Bataev74caaf22016-02-20 04:09:36 +000010050 ValueDecl *D = Res.first;
10051 if (!D)
10052 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +000010053
Alexey Bataev74caaf22016-02-20 04:09:36 +000010054 QualType Type = D->getType();
10055 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010056
10057 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
10058 // A variable that appears in a lastprivate clause must not have an
10059 // incomplete type or a reference type.
10060 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +000010061 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +000010062 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010063 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +000010064
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010065 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexander Musman1bb328c2014-06-04 13:06:39 +000010066 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
10067 // in a Construct]
10068 // Variables with the predetermined data-sharing attributes may not be
10069 // listed in data-sharing attributes clauses, except for the cases
10070 // listed below.
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010071 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
10072 // A list item may appear in a firstprivate or lastprivate clause but not
10073 // both.
Alexey Bataeve3727102018-04-18 15:57:46 +000010074 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010075 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
Alexey Bataevb358f992017-12-01 17:40:15 +000010076 (isOpenMPDistributeDirective(CurrDir) ||
10077 DVar.CKind != OMPC_firstprivate) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +000010078 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
10079 Diag(ELoc, diag::err_omp_wrong_dsa)
10080 << getOpenMPClauseName(DVar.CKind)
10081 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000010082 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010083 continue;
10084 }
10085
Alexey Bataevf29276e2014-06-18 04:14:57 +000010086 // OpenMP [2.14.3.5, Restrictions, p.2]
10087 // A list item that is private within a parallel region, or that appears in
10088 // the reduction clause of a parallel construct, must not appear in a
10089 // lastprivate clause on a worksharing construct if any of the corresponding
10090 // worksharing regions ever binds to any of the corresponding parallel
10091 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +000010092 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +000010093 if (isOpenMPWorksharingDirective(CurrDir) &&
Kelvin Li579e41c2016-11-30 23:51:03 +000010094 !isOpenMPParallelDirective(CurrDir) &&
10095 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +000010096 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +000010097 if (DVar.CKind != OMPC_shared) {
10098 Diag(ELoc, diag::err_omp_required_access)
10099 << getOpenMPClauseName(OMPC_lastprivate)
10100 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +000010101 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +000010102 continue;
10103 }
10104 }
Alexey Bataev74caaf22016-02-20 04:09:36 +000010105
Alexander Musman1bb328c2014-06-04 13:06:39 +000010106 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +000010107 // A variable of class type (or array thereof) that appears in a
10108 // lastprivate clause requires an accessible, unambiguous default
10109 // constructor for the class type, unless the list item is also specified
10110 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +000010111 // A variable of class type (or array thereof) that appears in a
10112 // lastprivate clause requires an accessible, unambiguous copy assignment
10113 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +000010114 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010115 VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(),
10116 Type.getUnqualifiedType(), ".lastprivate.src",
10117 D->hasAttrs() ? &D->getAttrs() : nullptr);
10118 DeclRefExpr *PseudoSrcExpr =
Alexey Bataev74caaf22016-02-20 04:09:36 +000010119 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataeve3727102018-04-18 15:57:46 +000010120 VarDecl *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +000010121 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +000010122 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000010123 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +000010124 // For arrays generate assignment operation for single element and replace
10125 // it by the original array element in CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +000010126 ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
10127 PseudoDstExpr, PseudoSrcExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +000010128 if (AssignmentOp.isInvalid())
10129 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +000010130 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +000010131 /*DiscardedValue=*/true);
10132 if (AssignmentOp.isInvalid())
10133 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +000010134
Alexey Bataev74caaf22016-02-20 04:09:36 +000010135 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010136 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010137 if (TopDVar.CKind == OMPC_firstprivate) {
Alexey Bataev005248a2016-02-25 05:25:57 +000010138 Ref = TopDVar.PrivateCopy;
Alexey Bataeve3727102018-04-18 15:57:46 +000010139 } else {
Alexey Bataev61205072016-03-02 04:57:40 +000010140 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000010141 if (!isOpenMPCapturedDecl(D))
Alexey Bataev005248a2016-02-25 05:25:57 +000010142 ExprCaptures.push_back(Ref->getDecl());
10143 }
10144 if (TopDVar.CKind == OMPC_firstprivate ||
Alexey Bataeve3727102018-04-18 15:57:46 +000010145 (!isOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010146 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +000010147 ExprResult RefRes = DefaultLvalueConversion(Ref);
10148 if (!RefRes.isUsable())
10149 continue;
10150 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +000010151 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
10152 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +000010153 if (!PostUpdateRes.isUsable())
10154 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +000010155 ExprPostUpdates.push_back(
10156 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +000010157 }
10158 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010159 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010160 Vars.push_back((VD || CurContext->isDependentContext())
10161 ? RefExpr->IgnoreParens()
10162 : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +000010163 SrcExprs.push_back(PseudoSrcExpr);
10164 DstExprs.push_back(PseudoDstExpr);
10165 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +000010166 }
10167
10168 if (Vars.empty())
10169 return nullptr;
10170
10171 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +000010172 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +000010173 buildPreInits(Context, ExprCaptures),
10174 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +000010175}
10176
Alexey Bataev758e55e2013-09-06 18:03:48 +000010177OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
10178 SourceLocation StartLoc,
10179 SourceLocation LParenLoc,
10180 SourceLocation EndLoc) {
10181 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +000010182 for (Expr *RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010183 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +000010184 SourceLocation ELoc;
10185 SourceRange ERange;
10186 Expr *SimpleRefExpr = RefExpr;
10187 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010188 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +000010189 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000010190 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010191 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010192 ValueDecl *D = Res.first;
10193 if (!D)
10194 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +000010195
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010196 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010197 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
10198 // in a Construct]
10199 // Variables with the predetermined data-sharing attributes may not be
10200 // listed in data-sharing attributes clauses, except for the cases
10201 // listed below. For these exceptions only, listing a predetermined
10202 // variable in a data-sharing attribute clause is allowed and overrides
10203 // the variable's predetermined data-sharing attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +000010204 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataeved09d242014-05-28 05:53:51 +000010205 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
10206 DVar.RefExpr) {
10207 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
10208 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +000010209 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010210 continue;
10211 }
10212
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010213 DeclRefExpr *Ref = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000010214 if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +000010215 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010216 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010217 Vars.push_back((VD || !Ref || CurContext->isDependentContext())
10218 ? RefExpr->IgnoreParens()
10219 : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010220 }
10221
Alexey Bataeved09d242014-05-28 05:53:51 +000010222 if (Vars.empty())
10223 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +000010224
10225 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
10226}
10227
Alexey Bataevc5e02582014-06-16 07:08:35 +000010228namespace {
10229class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
10230 DSAStackTy *Stack;
10231
10232public:
10233 bool VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010234 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
10235 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataevc5e02582014-06-16 07:08:35 +000010236 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
10237 return false;
10238 if (DVar.CKind != OMPC_unknown)
10239 return true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010240 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +000010241 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) { return true; },
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010242 /*FromParent=*/true);
Alexey Bataeve3727102018-04-18 15:57:46 +000010243 return DVarPrivate.CKind != OMPC_unknown;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010244 }
10245 return false;
10246 }
10247 bool VisitStmt(Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010248 for (Stmt *Child : S->children()) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010249 if (Child && Visit(Child))
10250 return true;
10251 }
10252 return false;
10253 }
Alexey Bataev23b69422014-06-18 07:08:49 +000010254 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +000010255};
Alexey Bataev23b69422014-06-18 07:08:49 +000010256} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +000010257
Alexey Bataev60da77e2016-02-29 05:54:20 +000010258namespace {
10259// Transform MemberExpression for specified FieldDecl of current class to
10260// DeclRefExpr to specified OMPCapturedExprDecl.
10261class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
10262 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
Alexey Bataeve3727102018-04-18 15:57:46 +000010263 ValueDecl *Field = nullptr;
10264 DeclRefExpr *CapturedExpr = nullptr;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010265
10266public:
10267 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
10268 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
10269
10270 ExprResult TransformMemberExpr(MemberExpr *E) {
10271 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
10272 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +000010273 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +000010274 return CapturedExpr;
10275 }
10276 return BaseTransform::TransformMemberExpr(E);
10277 }
10278 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
10279};
10280} // namespace
10281
Alexey Bataev97d18bf2018-04-11 19:21:00 +000010282template <typename T, typename U>
10283static T filterLookupForUDR(SmallVectorImpl<U> &Lookups,
10284 const llvm::function_ref<T(ValueDecl *)> Gen) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010285 for (U &Set : Lookups) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010286 for (auto *D : Set) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010287 if (T Res = Gen(cast<ValueDecl>(D)))
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010288 return Res;
10289 }
10290 }
10291 return T();
10292}
10293
Alexey Bataev43b90b72018-09-12 16:31:59 +000010294static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) {
10295 assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case");
10296
10297 for (auto RD : D->redecls()) {
10298 // Don't bother with extra checks if we already know this one isn't visible.
10299 if (RD == D)
10300 continue;
10301
10302 auto ND = cast<NamedDecl>(RD);
10303 if (LookupResult::isVisible(SemaRef, ND))
10304 return ND;
10305 }
10306
10307 return nullptr;
10308}
10309
10310static void
10311argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &ReductionId,
10312 SourceLocation Loc, QualType Ty,
10313 SmallVectorImpl<UnresolvedSet<8>> &Lookups) {
10314 // Find all of the associated namespaces and classes based on the
10315 // arguments we have.
10316 Sema::AssociatedNamespaceSet AssociatedNamespaces;
10317 Sema::AssociatedClassSet AssociatedClasses;
10318 OpaqueValueExpr OVE(Loc, Ty, VK_LValue);
10319 SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces,
10320 AssociatedClasses);
10321
10322 // C++ [basic.lookup.argdep]p3:
10323 // Let X be the lookup set produced by unqualified lookup (3.4.1)
10324 // and let Y be the lookup set produced by argument dependent
10325 // lookup (defined as follows). If X contains [...] then Y is
10326 // empty. Otherwise Y is the set of declarations found in the
10327 // namespaces associated with the argument types as described
10328 // below. The set of declarations found by the lookup of the name
10329 // is the union of X and Y.
10330 //
10331 // Here, we compute Y and add its members to the overloaded
10332 // candidate set.
10333 for (auto *NS : AssociatedNamespaces) {
10334 // When considering an associated namespace, the lookup is the
10335 // same as the lookup performed when the associated namespace is
10336 // used as a qualifier (3.4.3.2) except that:
10337 //
10338 // -- Any using-directives in the associated namespace are
10339 // ignored.
10340 //
10341 // -- Any namespace-scope friend functions declared in
10342 // associated classes are visible within their respective
10343 // namespaces even if they are not visible during an ordinary
10344 // lookup (11.4).
10345 DeclContext::lookup_result R = NS->lookup(ReductionId.getName());
10346 for (auto *D : R) {
10347 auto *Underlying = D;
10348 if (auto *USD = dyn_cast<UsingShadowDecl>(D))
10349 Underlying = USD->getTargetDecl();
10350
10351 if (!isa<OMPDeclareReductionDecl>(Underlying))
10352 continue;
10353
10354 if (!SemaRef.isVisible(D)) {
10355 D = findAcceptableDecl(SemaRef, D);
10356 if (!D)
10357 continue;
10358 if (auto *USD = dyn_cast<UsingShadowDecl>(D))
10359 Underlying = USD->getTargetDecl();
10360 }
10361 Lookups.emplace_back();
10362 Lookups.back().addDecl(Underlying);
10363 }
10364 }
10365}
10366
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010367static ExprResult
10368buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
10369 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
10370 const DeclarationNameInfo &ReductionId, QualType Ty,
10371 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
10372 if (ReductionIdScopeSpec.isInvalid())
10373 return ExprError();
10374 SmallVector<UnresolvedSet<8>, 4> Lookups;
10375 if (S) {
10376 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
10377 Lookup.suppressDiagnostics();
10378 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010379 NamedDecl *D = Lookup.getRepresentativeDecl();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010380 do {
10381 S = S->getParent();
10382 } while (S && !S->isDeclScope(D));
10383 if (S)
10384 S = S->getParent();
Alexey Bataev43b90b72018-09-12 16:31:59 +000010385 Lookups.emplace_back();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010386 Lookups.back().append(Lookup.begin(), Lookup.end());
10387 Lookup.clear();
10388 }
10389 } else if (auto *ULE =
10390 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
10391 Lookups.push_back(UnresolvedSet<8>());
10392 Decl *PrevD = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000010393 for (NamedDecl *D : ULE->decls()) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010394 if (D == PrevD)
10395 Lookups.push_back(UnresolvedSet<8>());
10396 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
10397 Lookups.back().addDecl(DRD);
10398 PrevD = D;
10399 }
10400 }
Alexey Bataevfdc20352017-08-25 15:43:55 +000010401 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() ||
10402 Ty->isInstantiationDependentType() ||
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010403 Ty->containsUnexpandedParameterPack() ||
Alexey Bataeve3727102018-04-18 15:57:46 +000010404 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010405 return !D->isInvalidDecl() &&
10406 (D->getType()->isDependentType() ||
10407 D->getType()->isInstantiationDependentType() ||
10408 D->getType()->containsUnexpandedParameterPack());
10409 })) {
10410 UnresolvedSet<8> ResSet;
Alexey Bataeve3727102018-04-18 15:57:46 +000010411 for (const UnresolvedSet<8> &Set : Lookups) {
Alexey Bataev43b90b72018-09-12 16:31:59 +000010412 if (Set.empty())
10413 continue;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010414 ResSet.append(Set.begin(), Set.end());
10415 // The last item marks the end of all declarations at the specified scope.
10416 ResSet.addDecl(Set[Set.size() - 1]);
10417 }
10418 return UnresolvedLookupExpr::Create(
10419 SemaRef.Context, /*NamingClass=*/nullptr,
10420 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
10421 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
10422 }
Alexey Bataev43b90b72018-09-12 16:31:59 +000010423 // Lookup inside the classes.
10424 // C++ [over.match.oper]p3:
10425 // For a unary operator @ with an operand of a type whose
10426 // cv-unqualified version is T1, and for a binary operator @ with
10427 // a left operand of a type whose cv-unqualified version is T1 and
10428 // a right operand of a type whose cv-unqualified version is T2,
10429 // three sets of candidate functions, designated member
10430 // candidates, non-member candidates and built-in candidates, are
10431 // constructed as follows:
10432 // -- If T1 is a complete class type or a class currently being
10433 // defined, the set of member candidates is the result of the
10434 // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise,
10435 // the set of member candidates is empty.
10436 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
10437 Lookup.suppressDiagnostics();
10438 if (const auto *TyRec = Ty->getAs<RecordType>()) {
10439 // Complete the type if it can be completed.
10440 // If the type is neither complete nor being defined, bail out now.
10441 if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() ||
10442 TyRec->getDecl()->getDefinition()) {
10443 Lookup.clear();
10444 SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl());
10445 if (Lookup.empty()) {
10446 Lookups.emplace_back();
10447 Lookups.back().append(Lookup.begin(), Lookup.end());
10448 }
10449 }
10450 }
10451 // Perform ADL.
10452 argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010453 if (auto *VD = filterLookupForUDR<ValueDecl *>(
10454 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
10455 if (!D->isInvalidDecl() &&
10456 SemaRef.Context.hasSameType(D->getType(), Ty))
10457 return D;
10458 return nullptr;
10459 }))
10460 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
10461 if (auto *VD = filterLookupForUDR<ValueDecl *>(
10462 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
10463 if (!D->isInvalidDecl() &&
10464 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
10465 !Ty.isMoreQualifiedThan(D->getType()))
10466 return D;
10467 return nullptr;
10468 })) {
10469 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
10470 /*DetectVirtual=*/false);
10471 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
10472 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
10473 VD->getType().getUnqualifiedType()))) {
10474 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
10475 /*DiagID=*/0) !=
10476 Sema::AR_inaccessible) {
10477 SemaRef.BuildBasePathArray(Paths, BasePath);
10478 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
10479 }
10480 }
10481 }
10482 }
10483 if (ReductionIdScopeSpec.isSet()) {
10484 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
10485 return ExprError();
10486 }
10487 return ExprEmpty();
10488}
10489
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010490namespace {
10491/// Data for the reduction-based clauses.
10492struct ReductionData {
10493 /// List of original reduction items.
10494 SmallVector<Expr *, 8> Vars;
10495 /// List of private copies of the reduction items.
10496 SmallVector<Expr *, 8> Privates;
10497 /// LHS expressions for the reduction_op expressions.
10498 SmallVector<Expr *, 8> LHSs;
10499 /// RHS expressions for the reduction_op expressions.
10500 SmallVector<Expr *, 8> RHSs;
10501 /// Reduction operation expression.
10502 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev88202be2017-07-27 13:20:36 +000010503 /// Taskgroup descriptors for the corresponding reduction items in
10504 /// in_reduction clauses.
10505 SmallVector<Expr *, 8> TaskgroupDescriptors;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010506 /// List of captures for clause.
10507 SmallVector<Decl *, 4> ExprCaptures;
10508 /// List of postupdate expressions.
10509 SmallVector<Expr *, 4> ExprPostUpdates;
10510 ReductionData() = delete;
10511 /// Reserves required memory for the reduction data.
10512 ReductionData(unsigned Size) {
10513 Vars.reserve(Size);
10514 Privates.reserve(Size);
10515 LHSs.reserve(Size);
10516 RHSs.reserve(Size);
10517 ReductionOps.reserve(Size);
Alexey Bataev88202be2017-07-27 13:20:36 +000010518 TaskgroupDescriptors.reserve(Size);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010519 ExprCaptures.reserve(Size);
10520 ExprPostUpdates.reserve(Size);
10521 }
10522 /// Stores reduction item and reduction operation only (required for dependent
10523 /// reduction item).
10524 void push(Expr *Item, Expr *ReductionOp) {
10525 Vars.emplace_back(Item);
10526 Privates.emplace_back(nullptr);
10527 LHSs.emplace_back(nullptr);
10528 RHSs.emplace_back(nullptr);
10529 ReductionOps.emplace_back(ReductionOp);
Alexey Bataev88202be2017-07-27 13:20:36 +000010530 TaskgroupDescriptors.emplace_back(nullptr);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010531 }
10532 /// Stores reduction data.
Alexey Bataev88202be2017-07-27 13:20:36 +000010533 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp,
10534 Expr *TaskgroupDescriptor) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010535 Vars.emplace_back(Item);
10536 Privates.emplace_back(Private);
10537 LHSs.emplace_back(LHS);
10538 RHSs.emplace_back(RHS);
10539 ReductionOps.emplace_back(ReductionOp);
Alexey Bataev88202be2017-07-27 13:20:36 +000010540 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010541 }
10542};
10543} // namespace
10544
Alexey Bataeve3727102018-04-18 15:57:46 +000010545static bool checkOMPArraySectionConstantForReduction(
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010546 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement,
10547 SmallVectorImpl<llvm::APSInt> &ArraySizes) {
10548 const Expr *Length = OASE->getLength();
10549 if (Length == nullptr) {
10550 // For array sections of the form [1:] or [:], we would need to analyze
10551 // the lower bound...
10552 if (OASE->getColonLoc().isValid())
10553 return false;
10554
10555 // This is an array subscript which has implicit length 1!
10556 SingleElement = true;
10557 ArraySizes.push_back(llvm::APSInt::get(1));
10558 } else {
10559 llvm::APSInt ConstantLengthValue;
10560 if (!Length->EvaluateAsInt(ConstantLengthValue, Context))
10561 return false;
10562
10563 SingleElement = (ConstantLengthValue.getSExtValue() == 1);
10564 ArraySizes.push_back(ConstantLengthValue);
10565 }
10566
10567 // Get the base of this array section and walk up from there.
10568 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
10569
10570 // We require length = 1 for all array sections except the right-most to
10571 // guarantee that the memory region is contiguous and has no holes in it.
10572 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) {
10573 Length = TempOASE->getLength();
10574 if (Length == nullptr) {
10575 // For array sections of the form [1:] or [:], we would need to analyze
10576 // the lower bound...
10577 if (OASE->getColonLoc().isValid())
10578 return false;
10579
10580 // This is an array subscript which has implicit length 1!
10581 ArraySizes.push_back(llvm::APSInt::get(1));
10582 } else {
10583 llvm::APSInt ConstantLengthValue;
10584 if (!Length->EvaluateAsInt(ConstantLengthValue, Context) ||
10585 ConstantLengthValue.getSExtValue() != 1)
10586 return false;
10587
10588 ArraySizes.push_back(ConstantLengthValue);
10589 }
10590 Base = TempOASE->getBase()->IgnoreParenImpCasts();
10591 }
10592
10593 // If we have a single element, we don't need to add the implicit lengths.
10594 if (!SingleElement) {
10595 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) {
10596 // Has implicit length 1!
10597 ArraySizes.push_back(llvm::APSInt::get(1));
10598 Base = TempASE->getBase()->IgnoreParenImpCasts();
10599 }
10600 }
10601
10602 // This array section can be privatized as a single value or as a constant
10603 // sized array.
10604 return true;
10605}
10606
Alexey Bataeve3727102018-04-18 15:57:46 +000010607static bool actOnOMPReductionKindClause(
Alexey Bataev169d96a2017-07-18 20:17:46 +000010608 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind,
10609 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
10610 SourceLocation ColonLoc, SourceLocation EndLoc,
10611 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010612 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010613 DeclarationName DN = ReductionId.getName();
10614 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator();
Alexey Bataevc5e02582014-06-16 07:08:35 +000010615 BinaryOperatorKind BOK = BO_Comma;
10616
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010617 ASTContext &Context = S.Context;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010618 // OpenMP [2.14.3.6, reduction clause]
10619 // C
10620 // reduction-identifier is either an identifier or one of the following
10621 // operators: +, -, *, &, |, ^, && and ||
10622 // C++
10623 // reduction-identifier is either an id-expression or one of the following
10624 // operators: +, -, *, &, |, ^, && and ||
Alexey Bataevc5e02582014-06-16 07:08:35 +000010625 switch (OOK) {
10626 case OO_Plus:
10627 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010628 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010629 break;
10630 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010631 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010632 break;
10633 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010634 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010635 break;
10636 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010637 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010638 break;
10639 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010640 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010641 break;
10642 case OO_AmpAmp:
10643 BOK = BO_LAnd;
10644 break;
10645 case OO_PipePipe:
10646 BOK = BO_LOr;
10647 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010648 case OO_New:
10649 case OO_Delete:
10650 case OO_Array_New:
10651 case OO_Array_Delete:
10652 case OO_Slash:
10653 case OO_Percent:
10654 case OO_Tilde:
10655 case OO_Exclaim:
10656 case OO_Equal:
10657 case OO_Less:
10658 case OO_Greater:
10659 case OO_LessEqual:
10660 case OO_GreaterEqual:
10661 case OO_PlusEqual:
10662 case OO_MinusEqual:
10663 case OO_StarEqual:
10664 case OO_SlashEqual:
10665 case OO_PercentEqual:
10666 case OO_CaretEqual:
10667 case OO_AmpEqual:
10668 case OO_PipeEqual:
10669 case OO_LessLess:
10670 case OO_GreaterGreater:
10671 case OO_LessLessEqual:
10672 case OO_GreaterGreaterEqual:
10673 case OO_EqualEqual:
10674 case OO_ExclaimEqual:
Richard Smithd30b23d2017-12-01 02:13:10 +000010675 case OO_Spaceship:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010676 case OO_PlusPlus:
10677 case OO_MinusMinus:
10678 case OO_Comma:
10679 case OO_ArrowStar:
10680 case OO_Arrow:
10681 case OO_Call:
10682 case OO_Subscript:
10683 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +000010684 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010685 case NUM_OVERLOADED_OPERATORS:
10686 llvm_unreachable("Unexpected reduction identifier");
10687 case OO_None:
Alexey Bataeve3727102018-04-18 15:57:46 +000010688 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010689 if (II->isStr("max"))
10690 BOK = BO_GT;
10691 else if (II->isStr("min"))
10692 BOK = BO_LT;
10693 }
10694 break;
10695 }
10696 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010697 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +000010698 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataev4d4624c2017-07-20 16:47:47 +000010699 else
10700 ReductionIdRange.setBegin(ReductionId.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +000010701 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +000010702
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010703 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
10704 bool FirstIter = true;
Alexey Bataeve3727102018-04-18 15:57:46 +000010705 for (Expr *RefExpr : VarList) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010706 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +000010707 // OpenMP [2.1, C/C++]
10708 // A list item is a variable or array section, subject to the restrictions
10709 // specified in Section 2.4 on page 42 and in each of the sections
10710 // describing clauses and directives for which a list appears.
10711 // OpenMP [2.14.3.3, Restrictions, p.1]
10712 // A variable that is part of another variable (as an array or
10713 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010714 if (!FirstIter && IR != ER)
10715 ++IR;
10716 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010717 SourceLocation ELoc;
10718 SourceRange ERange;
10719 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010720 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
Alexey Bataev60da77e2016-02-29 05:54:20 +000010721 /*AllowArraySection=*/true);
10722 if (Res.second) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010723 // Try to find 'declare reduction' corresponding construct before using
10724 // builtin/overloaded operators.
10725 QualType Type = Context.DependentTy;
10726 CXXCastPath BasePath;
10727 ExprResult DeclareReductionRef = buildDeclareReductionRef(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010728 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010729 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010730 Expr *ReductionOp = nullptr;
10731 if (S.CurContext->isDependentContext() &&
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010732 (DeclareReductionRef.isUnset() ||
10733 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010734 ReductionOp = DeclareReductionRef.get();
10735 // It will be analyzed later.
10736 RD.push(RefExpr, ReductionOp);
Alexey Bataevc5e02582014-06-16 07:08:35 +000010737 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000010738 ValueDecl *D = Res.first;
10739 if (!D)
10740 continue;
10741
Alexey Bataev88202be2017-07-27 13:20:36 +000010742 Expr *TaskgroupDescriptor = nullptr;
Alexey Bataeva1764212015-09-30 09:22:36 +000010743 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010744 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
10745 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
Alexey Bataeve3727102018-04-18 15:57:46 +000010746 if (ASE) {
Alexey Bataev31300ed2016-02-04 11:27:03 +000010747 Type = ASE->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010748 } else if (OASE) {
10749 QualType BaseType =
10750 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
10751 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
Alexey Bataeva1764212015-09-30 09:22:36 +000010752 Type = ATy->getElementType();
10753 else
10754 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +000010755 Type = Type.getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010756 } else {
Alexey Bataev60da77e2016-02-29 05:54:20 +000010757 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
Alexey Bataeve3727102018-04-18 15:57:46 +000010758 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000010759 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +000010760
Alexey Bataevc5e02582014-06-16 07:08:35 +000010761 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
10762 // A variable that appears in a private clause must not have an incomplete
10763 // type or a reference type.
Joel E. Denny3cabf732018-06-28 19:54:49 +000010764 if (S.RequireCompleteType(ELoc, D->getType(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010765 diag::err_omp_reduction_incomplete_type))
Alexey Bataevc5e02582014-06-16 07:08:35 +000010766 continue;
10767 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +000010768 // A list item that appears in a reduction clause must not be
10769 // const-qualified.
10770 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataev169d96a2017-07-18 20:17:46 +000010771 S.Diag(ELoc, diag::err_omp_const_reduction_list_item) << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010772 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010773 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10774 VarDecl::DeclarationOnly;
10775 S.Diag(D->getLocation(),
10776 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +000010777 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +000010778 }
Alexey Bataevc5e02582014-06-16 07:08:35 +000010779 continue;
10780 }
Alexey Bataevbc529672018-09-28 19:33:14 +000010781
10782 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective();
Alexey Bataevc5e02582014-06-16 07:08:35 +000010783 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
10784 // If a list-item is a reference type then it must bind to the same object
10785 // for all threads of the team.
Alexey Bataevbc529672018-09-28 19:33:14 +000010786 if (!ASE && !OASE) {
10787 if (VD) {
10788 VarDecl *VDDef = VD->getDefinition();
10789 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) {
10790 DSARefChecker Check(Stack);
10791 if (Check.Visit(VDDef->getInit())) {
10792 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg)
10793 << getOpenMPClauseName(ClauseKind) << ERange;
10794 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
10795 continue;
10796 }
Alexey Bataeva1764212015-09-30 09:22:36 +000010797 }
Alexey Bataevc5e02582014-06-16 07:08:35 +000010798 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010799
Alexey Bataevbc529672018-09-28 19:33:14 +000010800 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
10801 // in a Construct]
10802 // Variables with the predetermined data-sharing attributes may not be
10803 // listed in data-sharing attributes clauses, except for the cases
10804 // listed below. For these exceptions only, listing a predetermined
10805 // variable in a data-sharing attribute clause is allowed and overrides
10806 // the variable's predetermined data-sharing attributes.
10807 // OpenMP [2.14.3.6, Restrictions, p.3]
10808 // Any number of reduction clauses can be specified on the directive,
10809 // but a list item can appear only once in the reduction clauses for that
10810 // directive.
10811 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false);
10812 if (DVar.CKind == OMPC_reduction) {
10813 S.Diag(ELoc, diag::err_omp_once_referenced)
10814 << getOpenMPClauseName(ClauseKind);
10815 if (DVar.RefExpr)
10816 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
10817 continue;
10818 }
10819 if (DVar.CKind != OMPC_unknown) {
10820 S.Diag(ELoc, diag::err_omp_wrong_dsa)
10821 << getOpenMPClauseName(DVar.CKind)
10822 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataeve3727102018-04-18 15:57:46 +000010823 reportOriginalDsa(S, Stack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010824 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +000010825 }
Alexey Bataevbc529672018-09-28 19:33:14 +000010826
10827 // OpenMP [2.14.3.6, Restrictions, p.1]
10828 // A list item that appears in a reduction clause of a worksharing
10829 // construct must be shared in the parallel regions to which any of the
10830 // worksharing regions arising from the worksharing construct bind.
10831 if (isOpenMPWorksharingDirective(CurrDir) &&
10832 !isOpenMPParallelDirective(CurrDir) &&
10833 !isOpenMPTeamsDirective(CurrDir)) {
10834 DVar = Stack->getImplicitDSA(D, true);
10835 if (DVar.CKind != OMPC_shared) {
10836 S.Diag(ELoc, diag::err_omp_required_access)
10837 << getOpenMPClauseName(OMPC_reduction)
10838 << getOpenMPClauseName(OMPC_shared);
10839 reportOriginalDsa(S, Stack, D, DVar);
10840 continue;
10841 }
10842 }
Alexey Bataevf29276e2014-06-18 04:14:57 +000010843 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010844
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010845 // Try to find 'declare reduction' corresponding construct before using
10846 // builtin/overloaded operators.
10847 CXXCastPath BasePath;
10848 ExprResult DeclareReductionRef = buildDeclareReductionRef(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010849 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010850 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
10851 if (DeclareReductionRef.isInvalid())
10852 continue;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010853 if (S.CurContext->isDependentContext() &&
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010854 (DeclareReductionRef.isUnset() ||
10855 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010856 RD.push(RefExpr, DeclareReductionRef.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010857 continue;
10858 }
10859 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
10860 // Not allowed reduction identifier is found.
Stephen Kellyf2ceec42018-08-09 21:08:08 +000010861 S.Diag(ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010862 diag::err_omp_unknown_reduction_identifier)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010863 << Type << ReductionIdRange;
10864 continue;
10865 }
10866
10867 // OpenMP [2.14.3.6, reduction clause, Restrictions]
10868 // The type of a list item that appears in a reduction clause must be valid
10869 // for the reduction-identifier. For a max or min reduction in C, the type
10870 // of the list item must be an allowed arithmetic data type: char, int,
10871 // float, double, or _Bool, possibly modified with long, short, signed, or
10872 // unsigned. For a max or min reduction in C++, the type of the list item
10873 // must be an allowed arithmetic data type: char, wchar_t, int, float,
10874 // double, or bool, possibly modified with long, short, signed, or unsigned.
10875 if (DeclareReductionRef.isUnset()) {
10876 if ((BOK == BO_GT || BOK == BO_LT) &&
10877 !(Type->isScalarType() ||
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010878 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
10879 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
Alexey Bataev169d96a2017-07-18 20:17:46 +000010880 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010881 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010882 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10883 VarDecl::DeclarationOnly;
10884 S.Diag(D->getLocation(),
10885 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010886 << D;
10887 }
10888 continue;
10889 }
10890 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010891 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) {
Alexey Bataev169d96a2017-07-18 20:17:46 +000010892 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg)
10893 << getOpenMPClauseName(ClauseKind);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010894 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010895 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10896 VarDecl::DeclarationOnly;
10897 S.Diag(D->getLocation(),
10898 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010899 << D;
10900 }
10901 continue;
10902 }
10903 }
10904
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010905 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010906 VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs",
10907 D->hasAttrs() ? &D->getAttrs() : nullptr);
10908 VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(),
10909 D->hasAttrs() ? &D->getAttrs() : nullptr);
10910 QualType PrivateTy = Type;
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010911
10912 // Try if we can determine constant lengths for all array sections and avoid
10913 // the VLA.
10914 bool ConstantLengthOASE = false;
10915 if (OASE) {
10916 bool SingleElement;
10917 llvm::SmallVector<llvm::APSInt, 4> ArraySizes;
Alexey Bataeve3727102018-04-18 15:57:46 +000010918 ConstantLengthOASE = checkOMPArraySectionConstantForReduction(
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010919 Context, OASE, SingleElement, ArraySizes);
10920
10921 // If we don't have a single element, we must emit a constant array type.
10922 if (ConstantLengthOASE && !SingleElement) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010923 for (llvm::APSInt &Size : ArraySizes)
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010924 PrivateTy = Context.getConstantArrayType(
10925 PrivateTy, Size, ArrayType::Normal, /*IndexTypeQuals=*/0);
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010926 }
10927 }
10928
10929 if ((OASE && !ConstantLengthOASE) ||
Jonas Hahnfeld96087f32017-11-02 13:30:42 +000010930 (!OASE && !ASE &&
Alexey Bataev60da77e2016-02-29 05:54:20 +000010931 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Jonas Hahnfeld87d44262017-11-18 21:00:46 +000010932 if (!Context.getTargetInfo().isVLASupported() &&
10933 S.shouldDiagnoseTargetSupportFromOpenMP()) {
10934 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE;
10935 S.Diag(ELoc, diag::note_vla_unsupported);
10936 continue;
10937 }
David Majnemer9d168222016-08-05 17:44:54 +000010938 // For arrays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010939 // Create pseudo array type for private copy. The size for this array will
10940 // be generated during codegen.
10941 // For array subscripts or single variables Private Ty is the same as Type
10942 // (type of the variable or single array element).
10943 PrivateTy = Context.getVariableArrayType(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010944 Type,
Alexey Bataevd070a582017-10-25 15:54:04 +000010945 new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue),
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010946 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +000010947 } else if (!ASE && !OASE &&
Alexey Bataeve3727102018-04-18 15:57:46 +000010948 Context.getAsArrayType(D->getType().getNonReferenceType())) {
Alexey Bataev60da77e2016-02-29 05:54:20 +000010949 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010950 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010951 // Private copy.
Alexey Bataeve3727102018-04-18 15:57:46 +000010952 VarDecl *PrivateVD =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000010953 buildVarDecl(S, ELoc, PrivateTy, D->getName(),
10954 D->hasAttrs() ? &D->getAttrs() : nullptr,
10955 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010956 // Add initializer for private variable.
10957 Expr *Init = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000010958 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc);
10959 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010960 if (DeclareReductionRef.isUsable()) {
10961 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
10962 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
10963 if (DRD->getInitializer()) {
10964 Init = DRDRef;
10965 RHSVD->setInit(DRDRef);
10966 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010967 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010968 } else {
10969 switch (BOK) {
10970 case BO_Add:
10971 case BO_Xor:
10972 case BO_Or:
10973 case BO_LOr:
10974 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
10975 if (Type->isScalarType() || Type->isAnyComplexType())
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010976 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010977 break;
10978 case BO_Mul:
10979 case BO_LAnd:
10980 if (Type->isScalarType() || Type->isAnyComplexType()) {
10981 // '*' and '&&' reduction ops - initializer is '1'.
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010982 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +000010983 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010984 break;
10985 case BO_And: {
10986 // '&' reduction op - initializer is '~0'.
10987 QualType OrigType = Type;
10988 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
10989 Type = ComplexTy->getElementType();
10990 if (Type->isRealFloatingType()) {
10991 llvm::APFloat InitValue =
10992 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
10993 /*isIEEE=*/true);
10994 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
10995 Type, ELoc);
10996 } else if (Type->isScalarType()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010997 uint64_t Size = Context.getTypeSize(Type);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010998 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
10999 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
11000 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
11001 }
11002 if (Init && OrigType->isAnyComplexType()) {
11003 // Init = 0xFFFF + 0xFFFFi;
11004 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011005 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011006 }
11007 Type = OrigType;
11008 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011009 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011010 case BO_LT:
11011 case BO_GT: {
11012 // 'min' reduction op - initializer is 'Largest representable number in
11013 // the reduction list item type'.
11014 // 'max' reduction op - initializer is 'Least representable number in
11015 // the reduction list item type'.
11016 if (Type->isIntegerType() || Type->isPointerType()) {
11017 bool IsSigned = Type->hasSignedIntegerRepresentation();
Alexey Bataeve3727102018-04-18 15:57:46 +000011018 uint64_t Size = Context.getTypeSize(Type);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011019 QualType IntTy =
11020 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
11021 llvm::APInt InitValue =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011022 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
11023 : llvm::APInt::getMinValue(Size)
11024 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
11025 : llvm::APInt::getMaxValue(Size);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011026 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
11027 if (Type->isPointerType()) {
11028 // Cast to pointer type.
Alexey Bataeve3727102018-04-18 15:57:46 +000011029 ExprResult CastExpr = S.BuildCStyleCastExpr(
Alexey Bataevd070a582017-10-25 15:54:04 +000011030 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011031 if (CastExpr.isInvalid())
11032 continue;
11033 Init = CastExpr.get();
11034 }
11035 } else if (Type->isRealFloatingType()) {
11036 llvm::APFloat InitValue = llvm::APFloat::getLargest(
11037 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
11038 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
11039 Type, ELoc);
11040 }
11041 break;
11042 }
11043 case BO_PtrMemD:
11044 case BO_PtrMemI:
11045 case BO_MulAssign:
11046 case BO_Div:
11047 case BO_Rem:
11048 case BO_Sub:
11049 case BO_Shl:
11050 case BO_Shr:
11051 case BO_LE:
11052 case BO_GE:
11053 case BO_EQ:
11054 case BO_NE:
Richard Smithc70f1d62017-12-14 15:16:18 +000011055 case BO_Cmp:
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011056 case BO_AndAssign:
11057 case BO_XorAssign:
11058 case BO_OrAssign:
11059 case BO_Assign:
11060 case BO_AddAssign:
11061 case BO_SubAssign:
11062 case BO_DivAssign:
11063 case BO_RemAssign:
11064 case BO_ShlAssign:
11065 case BO_ShrAssign:
11066 case BO_Comma:
11067 llvm_unreachable("Unexpected reduction operation");
11068 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011069 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011070 if (Init && DeclareReductionRef.isUnset())
11071 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false);
11072 else if (!Init)
11073 S.ActOnUninitializedDecl(RHSVD);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011074 if (RHSVD->isInvalidDecl())
11075 continue;
11076 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011077 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible)
11078 << Type << ReductionIdRange;
11079 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
11080 VarDecl::DeclarationOnly;
11081 S.Diag(D->getLocation(),
11082 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +000011083 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011084 continue;
11085 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011086 // Store initializer for single element in private copy. Will be used during
11087 // codegen.
11088 PrivateVD->setInit(RHSVD->getInit());
11089 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataeve3727102018-04-18 15:57:46 +000011090 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011091 ExprResult ReductionOp;
11092 if (DeclareReductionRef.isUsable()) {
11093 QualType RedTy = DeclareReductionRef.get()->getType();
11094 QualType PtrRedTy = Context.getPointerType(RedTy);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011095 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
11096 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011097 if (!BasePath.empty()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011098 LHS = S.DefaultLvalueConversion(LHS.get());
11099 RHS = S.DefaultLvalueConversion(RHS.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011100 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
11101 CK_UncheckedDerivedToBase, LHS.get(),
11102 &BasePath, LHS.get()->getValueKind());
11103 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
11104 CK_UncheckedDerivedToBase, RHS.get(),
11105 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011106 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011107 FunctionProtoType::ExtProtoInfo EPI;
11108 QualType Params[] = {PtrRedTy, PtrRedTy};
11109 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
11110 auto *OVE = new (Context) OpaqueValueExpr(
11111 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011112 S.DefaultLvalueConversion(DeclareReductionRef.get()).get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011113 Expr *Args[] = {LHS.get(), RHS.get()};
11114 ReductionOp = new (Context)
11115 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
11116 } else {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011117 ReductionOp = S.BuildBinOp(
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011118 Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, LHSDRE, RHSDRE);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011119 if (ReductionOp.isUsable()) {
11120 if (BOK != BO_LT && BOK != BO_GT) {
11121 ReductionOp =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011122 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011123 BO_Assign, LHSDRE, ReductionOp.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011124 } else {
Alexey Bataevd070a582017-10-25 15:54:04 +000011125 auto *ConditionalOp = new (Context)
11126 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE,
11127 Type, VK_LValue, OK_Ordinary);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011128 ReductionOp =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011129 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011130 BO_Assign, LHSDRE, ConditionalOp);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011131 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +000011132 if (ReductionOp.isUsable())
11133 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011134 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +000011135 if (!ReductionOp.isUsable())
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011136 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +000011137 }
11138
Alexey Bataevfa312f32017-07-21 18:48:21 +000011139 // OpenMP [2.15.4.6, Restrictions, p.2]
11140 // A list item that appears in an in_reduction clause of a task construct
11141 // must appear in a task_reduction clause of a construct associated with a
11142 // taskgroup region that includes the participating task in its taskgroup
11143 // set. The construct associated with the innermost region that meets this
11144 // condition must specify the same reduction-identifier as the in_reduction
11145 // clause.
11146 if (ClauseKind == OMPC_in_reduction) {
Alexey Bataevfa312f32017-07-21 18:48:21 +000011147 SourceRange ParentSR;
11148 BinaryOperatorKind ParentBOK;
11149 const Expr *ParentReductionOp;
Alexey Bataev88202be2017-07-27 13:20:36 +000011150 Expr *ParentBOKTD, *ParentReductionOpTD;
Alexey Bataevf189cb72017-07-24 14:52:13 +000011151 DSAStackTy::DSAVarData ParentBOKDSA =
Alexey Bataev88202be2017-07-27 13:20:36 +000011152 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK,
11153 ParentBOKTD);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011154 DSAStackTy::DSAVarData ParentReductionOpDSA =
Alexey Bataev88202be2017-07-27 13:20:36 +000011155 Stack->getTopMostTaskgroupReductionData(
11156 D, ParentSR, ParentReductionOp, ParentReductionOpTD);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011157 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown;
11158 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown;
11159 if (!IsParentBOK && !IsParentReductionOp) {
11160 S.Diag(ELoc, diag::err_omp_in_reduction_not_task_reduction);
11161 continue;
11162 }
Alexey Bataevfa312f32017-07-21 18:48:21 +000011163 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) ||
11164 (DeclareReductionRef.isUsable() && IsParentBOK) || BOK != ParentBOK ||
11165 IsParentReductionOp) {
11166 bool EmitError = true;
11167 if (IsParentReductionOp && DeclareReductionRef.isUsable()) {
11168 llvm::FoldingSetNodeID RedId, ParentRedId;
11169 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true);
11170 DeclareReductionRef.get()->Profile(RedId, Context,
11171 /*Canonical=*/true);
11172 EmitError = RedId != ParentRedId;
11173 }
11174 if (EmitError) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011175 S.Diag(ReductionId.getBeginLoc(),
Alexey Bataevfa312f32017-07-21 18:48:21 +000011176 diag::err_omp_reduction_identifier_mismatch)
11177 << ReductionIdRange << RefExpr->getSourceRange();
11178 S.Diag(ParentSR.getBegin(),
11179 diag::note_omp_previous_reduction_identifier)
Alexey Bataevf189cb72017-07-24 14:52:13 +000011180 << ParentSR
11181 << (IsParentBOK ? ParentBOKDSA.RefExpr
11182 : ParentReductionOpDSA.RefExpr)
11183 ->getSourceRange();
Alexey Bataevfa312f32017-07-21 18:48:21 +000011184 continue;
11185 }
11186 }
Alexey Bataev88202be2017-07-27 13:20:36 +000011187 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD;
11188 assert(TaskgroupDescriptor && "Taskgroup descriptor must be defined.");
Alexey Bataevfa312f32017-07-21 18:48:21 +000011189 }
11190
Alexey Bataev60da77e2016-02-29 05:54:20 +000011191 DeclRefExpr *Ref = nullptr;
11192 Expr *VarsExpr = RefExpr->IgnoreParens();
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011193 if (!VD && !S.CurContext->isDependentContext()) {
Alexey Bataev60da77e2016-02-29 05:54:20 +000011194 if (ASE || OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011195 TransformExprToCaptures RebuildToCapture(S, D);
Alexey Bataev60da77e2016-02-29 05:54:20 +000011196 VarsExpr =
11197 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
11198 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +000011199 } else {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011200 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +000011201 }
Alexey Bataeve3727102018-04-18 15:57:46 +000011202 if (!S.isOpenMPCapturedDecl(D)) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011203 RD.ExprCaptures.emplace_back(Ref->getDecl());
Alexey Bataev5a3af132016-03-29 08:58:54 +000011204 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011205 ExprResult RefRes = S.DefaultLvalueConversion(Ref);
Alexey Bataev5a3af132016-03-29 08:58:54 +000011206 if (!RefRes.isUsable())
11207 continue;
11208 ExprResult PostUpdateRes =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011209 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
11210 RefRes.get());
Alexey Bataev5a3af132016-03-29 08:58:54 +000011211 if (!PostUpdateRes.isUsable())
11212 continue;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011213 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
11214 Stack->getCurrentDirective() == OMPD_taskgroup) {
11215 S.Diag(RefExpr->getExprLoc(),
11216 diag::err_omp_reduction_non_addressable_expression)
Alexey Bataevbcd0ae02017-07-11 19:16:44 +000011217 << RefExpr->getSourceRange();
11218 continue;
11219 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011220 RD.ExprPostUpdates.emplace_back(
11221 S.IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +000011222 }
11223 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000011224 }
Alexey Bataev169d96a2017-07-18 20:17:46 +000011225 // All reduction items are still marked as reduction (to do not increase
11226 // code base size).
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011227 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011228 if (CurrDir == OMPD_taskgroup) {
11229 if (DeclareReductionRef.isUsable())
Alexey Bataev3b1b8952017-07-25 15:53:26 +000011230 Stack->addTaskgroupReductionData(D, ReductionIdRange,
11231 DeclareReductionRef.get());
Alexey Bataevf189cb72017-07-24 14:52:13 +000011232 else
Alexey Bataev3b1b8952017-07-25 15:53:26 +000011233 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011234 }
Alexey Bataev88202be2017-07-27 13:20:36 +000011235 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(),
11236 TaskgroupDescriptor);
Alexey Bataevc5e02582014-06-16 07:08:35 +000011237 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011238 return RD.Vars.empty();
11239}
Alexey Bataevc5e02582014-06-16 07:08:35 +000011240
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011241OMPClause *Sema::ActOnOpenMPReductionClause(
11242 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11243 SourceLocation ColonLoc, SourceLocation EndLoc,
11244 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11245 ArrayRef<Expr *> UnresolvedReductions) {
11246 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000011247 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList,
Alexey Bataev169d96a2017-07-18 20:17:46 +000011248 StartLoc, LParenLoc, ColonLoc, EndLoc,
11249 ReductionIdScopeSpec, ReductionId,
11250 UnresolvedReductions, RD))
Alexey Bataevc5e02582014-06-16 07:08:35 +000011251 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +000011252
Alexey Bataevc5e02582014-06-16 07:08:35 +000011253 return OMPReductionClause::Create(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011254 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11255 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
11256 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
11257 buildPreInits(Context, RD.ExprCaptures),
11258 buildPostUpdate(*this, RD.ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +000011259}
11260
Alexey Bataev169d96a2017-07-18 20:17:46 +000011261OMPClause *Sema::ActOnOpenMPTaskReductionClause(
11262 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11263 SourceLocation ColonLoc, SourceLocation EndLoc,
11264 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11265 ArrayRef<Expr *> UnresolvedReductions) {
11266 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000011267 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList,
11268 StartLoc, LParenLoc, ColonLoc, EndLoc,
11269 ReductionIdScopeSpec, ReductionId,
Alexey Bataev169d96a2017-07-18 20:17:46 +000011270 UnresolvedReductions, RD))
11271 return nullptr;
11272
11273 return OMPTaskReductionClause::Create(
11274 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11275 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
11276 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
11277 buildPreInits(Context, RD.ExprCaptures),
11278 buildPostUpdate(*this, RD.ExprPostUpdates));
11279}
11280
Alexey Bataevfa312f32017-07-21 18:48:21 +000011281OMPClause *Sema::ActOnOpenMPInReductionClause(
11282 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11283 SourceLocation ColonLoc, SourceLocation EndLoc,
11284 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11285 ArrayRef<Expr *> UnresolvedReductions) {
11286 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000011287 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList,
Alexey Bataevfa312f32017-07-21 18:48:21 +000011288 StartLoc, LParenLoc, ColonLoc, EndLoc,
11289 ReductionIdScopeSpec, ReductionId,
11290 UnresolvedReductions, RD))
11291 return nullptr;
11292
11293 return OMPInReductionClause::Create(
11294 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11295 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
Alexey Bataev88202be2017-07-27 13:20:36 +000011296 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors,
Alexey Bataevfa312f32017-07-21 18:48:21 +000011297 buildPreInits(Context, RD.ExprCaptures),
11298 buildPostUpdate(*this, RD.ExprPostUpdates));
11299}
11300
Alexey Bataevecba70f2016-04-12 11:02:11 +000011301bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
11302 SourceLocation LinLoc) {
11303 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
11304 LinKind == OMPC_LINEAR_unknown) {
11305 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
11306 return true;
11307 }
11308 return false;
11309}
11310
Alexey Bataeve3727102018-04-18 15:57:46 +000011311bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc,
Alexey Bataevecba70f2016-04-12 11:02:11 +000011312 OpenMPLinearClauseKind LinKind,
11313 QualType Type) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011314 const auto *VD = dyn_cast_or_null<VarDecl>(D);
Alexey Bataevecba70f2016-04-12 11:02:11 +000011315 // A variable must not have an incomplete type or a reference type.
11316 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
11317 return true;
11318 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
11319 !Type->isReferenceType()) {
11320 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
11321 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
11322 return true;
11323 }
11324 Type = Type.getNonReferenceType();
11325
11326 // A list item must not be const-qualified.
11327 if (Type.isConstant(Context)) {
11328 Diag(ELoc, diag::err_omp_const_variable)
11329 << getOpenMPClauseName(OMPC_linear);
11330 if (D) {
11331 bool IsDecl =
11332 !VD ||
11333 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
11334 Diag(D->getLocation(),
11335 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
11336 << D;
11337 }
11338 return true;
11339 }
11340
11341 // A list item must be of integral or pointer type.
11342 Type = Type.getUnqualifiedType().getCanonicalType();
11343 const auto *Ty = Type.getTypePtrOrNull();
11344 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
11345 !Ty->isPointerType())) {
11346 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
11347 if (D) {
11348 bool IsDecl =
11349 !VD ||
11350 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
11351 Diag(D->getLocation(),
11352 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
11353 << D;
11354 }
11355 return true;
11356 }
11357 return false;
11358}
11359
Alexey Bataev182227b2015-08-20 10:54:39 +000011360OMPClause *Sema::ActOnOpenMPLinearClause(
11361 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
11362 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
11363 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +000011364 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011365 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +000011366 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +000011367 SmallVector<Decl *, 4> ExprCaptures;
11368 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +000011369 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +000011370 LinKind = OMPC_LINEAR_val;
Alexey Bataeve3727102018-04-18 15:57:46 +000011371 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000011372 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011373 SourceLocation ELoc;
11374 SourceRange ERange;
11375 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011376 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011377 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +000011378 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000011379 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011380 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +000011381 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +000011382 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011383 ValueDecl *D = Res.first;
11384 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +000011385 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +000011386
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011387 QualType Type = D->getType();
11388 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +000011389
11390 // OpenMP [2.14.3.7, linear clause]
11391 // A list-item cannot appear in more than one linear clause.
11392 // A list-item that appears in a linear clause cannot appear in any
11393 // other data-sharing attribute clause.
Alexey Bataeve3727102018-04-18 15:57:46 +000011394 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexander Musman8dba6642014-04-22 13:09:42 +000011395 if (DVar.RefExpr) {
11396 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
11397 << getOpenMPClauseName(OMPC_linear);
Alexey Bataeve3727102018-04-18 15:57:46 +000011398 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +000011399 continue;
11400 }
11401
Alexey Bataevecba70f2016-04-12 11:02:11 +000011402 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +000011403 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +000011404 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +000011405
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011406 // Build private copy of original var.
Alexey Bataeve3727102018-04-18 15:57:46 +000011407 VarDecl *Private =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000011408 buildVarDecl(*this, ELoc, Type, D->getName(),
11409 D->hasAttrs() ? &D->getAttrs() : nullptr,
11410 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011411 DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +000011412 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011413 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011414 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011415 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000011416 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev78849fb2016-03-09 09:49:00 +000011417 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011418 if (!isOpenMPCapturedDecl(D)) {
Alexey Bataev78849fb2016-03-09 09:49:00 +000011419 ExprCaptures.push_back(Ref->getDecl());
11420 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
11421 ExprResult RefRes = DefaultLvalueConversion(Ref);
11422 if (!RefRes.isUsable())
11423 continue;
11424 ExprResult PostUpdateRes =
11425 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
11426 SimpleRefExpr, RefRes.get());
11427 if (!PostUpdateRes.isUsable())
11428 continue;
11429 ExprPostUpdates.push_back(
11430 IgnoredValueConversions(PostUpdateRes.get()).get());
11431 }
11432 }
11433 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011434 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011435 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011436 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011437 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011438 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000011439 /*DirectInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011440 DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011441
11442 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000011443 Vars.push_back((VD || CurContext->isDependentContext())
11444 ? RefExpr->IgnoreParens()
11445 : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011446 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +000011447 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +000011448 }
11449
11450 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000011451 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +000011452
11453 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +000011454 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +000011455 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
11456 !Step->isInstantiationDependent() &&
11457 !Step->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011458 SourceLocation StepLoc = Step->getBeginLoc();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +000011459 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +000011460 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000011461 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +000011462 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +000011463
Alexander Musman3276a272015-03-21 10:12:56 +000011464 // Build var to save the step value.
11465 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +000011466 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +000011467 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +000011468 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +000011469 ExprResult CalcStep =
11470 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +000011471 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +000011472
Alexander Musman8dba6642014-04-22 13:09:42 +000011473 // Warn about zero linear step (it would be probably better specified as
11474 // making corresponding variables 'const').
11475 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +000011476 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
11477 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +000011478 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
11479 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +000011480 if (!IsConstant && CalcStep.isUsable()) {
11481 // Calculate the step beforehand instead of doing this on each iteration.
11482 // (This is not used if the number of iterations may be kfold-ed).
11483 CalcStepExpr = CalcStep.get();
11484 }
Alexander Musman8dba6642014-04-22 13:09:42 +000011485 }
11486
Alexey Bataev182227b2015-08-20 10:54:39 +000011487 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
11488 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +000011489 StepExpr, CalcStepExpr,
11490 buildPreInits(Context, ExprCaptures),
11491 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +000011492}
11493
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011494static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
11495 Expr *NumIterations, Sema &SemaRef,
11496 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +000011497 // Walk the vars and build update/final expressions for the CodeGen.
11498 SmallVector<Expr *, 8> Updates;
11499 SmallVector<Expr *, 8> Finals;
11500 Expr *Step = Clause.getStep();
11501 Expr *CalcStep = Clause.getCalcStep();
11502 // OpenMP [2.14.3.7, linear clause]
11503 // If linear-step is not specified it is assumed to be 1.
Alexey Bataeve3727102018-04-18 15:57:46 +000011504 if (!Step)
Alexander Musman3276a272015-03-21 10:12:56 +000011505 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000011506 else if (CalcStep)
Alexander Musman3276a272015-03-21 10:12:56 +000011507 Step = cast<BinaryOperator>(CalcStep)->getLHS();
11508 bool HasErrors = false;
11509 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011510 auto CurPrivate = Clause.privates().begin();
Alexey Bataeve3727102018-04-18 15:57:46 +000011511 OpenMPLinearClauseKind LinKind = Clause.getModifier();
11512 for (Expr *RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011513 SourceLocation ELoc;
11514 SourceRange ERange;
11515 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011516 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange);
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011517 ValueDecl *D = Res.first;
11518 if (Res.second || !D) {
11519 Updates.push_back(nullptr);
11520 Finals.push_back(nullptr);
11521 HasErrors = true;
11522 continue;
11523 }
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011524 auto &&Info = Stack->isLoopControlVariable(D);
Alexey Bataev2b86f212017-11-29 21:31:48 +000011525 // OpenMP [2.15.11, distribute simd Construct]
11526 // A list item may not appear in a linear clause, unless it is the loop
11527 // iteration variable.
11528 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) &&
11529 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) {
11530 SemaRef.Diag(ELoc,
11531 diag::err_omp_linear_distribute_var_non_loop_iteration);
11532 Updates.push_back(nullptr);
11533 Finals.push_back(nullptr);
11534 HasErrors = true;
11535 continue;
11536 }
Alexander Musman3276a272015-03-21 10:12:56 +000011537 Expr *InitExpr = *CurInit;
11538
11539 // Build privatized reference to the current linear var.
David Majnemer9d168222016-08-05 17:44:54 +000011540 auto *DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011541 Expr *CapturedRef;
11542 if (LinKind == OMPC_LINEAR_uval)
11543 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
11544 else
11545 CapturedRef =
11546 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
11547 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
11548 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +000011549
11550 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011551 ExprResult Update;
Alexey Bataeve3727102018-04-18 15:57:46 +000011552 if (!Info.first)
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011553 Update =
Alexey Bataeve3727102018-04-18 15:57:46 +000011554 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011555 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011556 else
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011557 Update = *CurPrivate;
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011558 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +000011559 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +000011560
11561 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011562 ExprResult Final;
Alexey Bataeve3727102018-04-18 15:57:46 +000011563 if (!Info.first)
11564 Final =
11565 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
11566 InitExpr, NumIterations, Step, /*Subtract=*/false);
11567 else
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011568 Final = *CurPrivate;
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011569 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +000011570 /*DiscardedValue=*/true);
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011571
Alexander Musman3276a272015-03-21 10:12:56 +000011572 if (!Update.isUsable() || !Final.isUsable()) {
11573 Updates.push_back(nullptr);
11574 Finals.push_back(nullptr);
11575 HasErrors = true;
11576 } else {
11577 Updates.push_back(Update.get());
11578 Finals.push_back(Final.get());
11579 }
Richard Trieucc3949d2016-02-18 22:34:54 +000011580 ++CurInit;
11581 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +000011582 }
11583 Clause.setUpdates(Updates);
11584 Clause.setFinals(Finals);
11585 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +000011586}
11587
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011588OMPClause *Sema::ActOnOpenMPAlignedClause(
11589 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
11590 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011591 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +000011592 for (Expr *RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +000011593 assert(RefExpr && "NULL expr in OpenMP linear clause.");
11594 SourceLocation ELoc;
11595 SourceRange ERange;
11596 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011597 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev1efd1662016-03-29 10:59:56 +000011598 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011599 // It will be analyzed later.
11600 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011601 }
Alexey Bataev1efd1662016-03-29 10:59:56 +000011602 ValueDecl *D = Res.first;
11603 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011604 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011605
Alexey Bataev1efd1662016-03-29 10:59:56 +000011606 QualType QType = D->getType();
11607 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011608
11609 // OpenMP [2.8.1, simd construct, Restrictions]
11610 // The type of list items appearing in the aligned clause must be
11611 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011612 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011613 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +000011614 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011615 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +000011616 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011617 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +000011618 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011619 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +000011620 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011621 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +000011622 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011623 continue;
11624 }
11625
11626 // OpenMP [2.8.1, simd construct, Restrictions]
11627 // A list-item cannot appear in more than one aligned clause.
Alexey Bataeve3727102018-04-18 15:57:46 +000011628 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +000011629 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011630 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
11631 << getOpenMPClauseName(OMPC_aligned);
11632 continue;
11633 }
11634
Alexey Bataev1efd1662016-03-29 10:59:56 +000011635 DeclRefExpr *Ref = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000011636 if (!VD && isOpenMPCapturedDecl(D))
Alexey Bataev1efd1662016-03-29 10:59:56 +000011637 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
11638 Vars.push_back(DefaultFunctionArrayConversion(
11639 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
11640 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011641 }
11642
11643 // OpenMP [2.8.1, simd construct, Description]
11644 // The parameter of the aligned clause, alignment, must be a constant
11645 // positive integer expression.
11646 // If no optional parameter is specified, implementation-defined default
11647 // alignments for SIMD instructions on the target platforms are assumed.
11648 if (Alignment != nullptr) {
11649 ExprResult AlignResult =
11650 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
11651 if (AlignResult.isInvalid())
11652 return nullptr;
11653 Alignment = AlignResult.get();
11654 }
11655 if (Vars.empty())
11656 return nullptr;
11657
11658 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
11659 EndLoc, Vars, Alignment);
11660}
11661
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011662OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
11663 SourceLocation StartLoc,
11664 SourceLocation LParenLoc,
11665 SourceLocation EndLoc) {
11666 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011667 SmallVector<Expr *, 8> SrcExprs;
11668 SmallVector<Expr *, 8> DstExprs;
11669 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeve3727102018-04-18 15:57:46 +000011670 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000011671 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
11672 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011673 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000011674 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011675 SrcExprs.push_back(nullptr);
11676 DstExprs.push_back(nullptr);
11677 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011678 continue;
11679 }
11680
Alexey Bataeved09d242014-05-28 05:53:51 +000011681 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011682 // OpenMP [2.1, C/C++]
11683 // A list item is a variable name.
11684 // OpenMP [2.14.4.1, Restrictions, p.1]
11685 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeve3727102018-04-18 15:57:46 +000011686 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011687 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000011688 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
11689 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011690 continue;
11691 }
11692
11693 Decl *D = DE->getDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000011694 auto *VD = cast<VarDecl>(D);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011695
11696 QualType Type = VD->getType();
11697 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
11698 // It will be analyzed later.
11699 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011700 SrcExprs.push_back(nullptr);
11701 DstExprs.push_back(nullptr);
11702 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011703 continue;
11704 }
11705
11706 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
11707 // A list item that appears in a copyin clause must be threadprivate.
11708 if (!DSAStack->isThreadPrivate(VD)) {
11709 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +000011710 << getOpenMPClauseName(OMPC_copyin)
11711 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011712 continue;
11713 }
11714
11715 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
11716 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +000011717 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011718 // operator for the class type.
Alexey Bataeve3727102018-04-18 15:57:46 +000011719 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
11720 VarDecl *SrcVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011721 buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000011722 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011723 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011724 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +000011725 VarDecl *DstVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011726 buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst",
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000011727 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011728 DeclRefExpr *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011729 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011730 // For arrays generate assignment operation for single element and replace
11731 // it by the original array element in CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +000011732 ExprResult AssignmentOp =
11733 BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr,
11734 PseudoSrcExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011735 if (AssignmentOp.isInvalid())
11736 continue;
11737 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
11738 /*DiscardedValue=*/true);
11739 if (AssignmentOp.isInvalid())
11740 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011741
11742 DSAStack->addDSA(VD, DE, OMPC_copyin);
11743 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011744 SrcExprs.push_back(PseudoSrcExpr);
11745 DstExprs.push_back(PseudoDstExpr);
11746 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011747 }
11748
Alexey Bataeved09d242014-05-28 05:53:51 +000011749 if (Vars.empty())
11750 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011751
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011752 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
11753 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011754}
11755
Alexey Bataevbae9a792014-06-27 10:37:06 +000011756OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
11757 SourceLocation StartLoc,
11758 SourceLocation LParenLoc,
11759 SourceLocation EndLoc) {
11760 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +000011761 SmallVector<Expr *, 8> SrcExprs;
11762 SmallVector<Expr *, 8> DstExprs;
11763 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeve3727102018-04-18 15:57:46 +000011764 for (Expr *RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +000011765 assert(RefExpr && "NULL expr in OpenMP linear clause.");
11766 SourceLocation ELoc;
11767 SourceRange ERange;
11768 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011769 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataeve122da12016-03-17 10:50:17 +000011770 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000011771 // It will be analyzed later.
11772 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000011773 SrcExprs.push_back(nullptr);
11774 DstExprs.push_back(nullptr);
11775 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011776 }
Alexey Bataeve122da12016-03-17 10:50:17 +000011777 ValueDecl *D = Res.first;
11778 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +000011779 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000011780
Alexey Bataeve122da12016-03-17 10:50:17 +000011781 QualType Type = D->getType();
11782 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011783
11784 // OpenMP [2.14.4.2, Restrictions, p.2]
11785 // A list item that appears in a copyprivate clause may not appear in a
11786 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +000011787 if (!VD || !DSAStack->isThreadPrivate(VD)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011788 DSAStackTy::DSAVarData DVar =
11789 DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataeva63048e2015-03-23 06:18:07 +000011790 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
11791 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000011792 Diag(ELoc, diag::err_omp_wrong_dsa)
11793 << getOpenMPClauseName(DVar.CKind)
11794 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000011795 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011796 continue;
11797 }
11798
11799 // OpenMP [2.11.4.2, Restrictions, p.1]
11800 // All list items that appear in a copyprivate clause must be either
11801 // threadprivate or private in the enclosing context.
11802 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +000011803 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011804 if (DVar.CKind == OMPC_shared) {
11805 Diag(ELoc, diag::err_omp_required_access)
11806 << getOpenMPClauseName(OMPC_copyprivate)
11807 << "threadprivate or private in the enclosing context";
Alexey Bataeve3727102018-04-18 15:57:46 +000011808 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011809 continue;
11810 }
11811 }
11812 }
11813
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011814 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000011815 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011816 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +000011817 << getOpenMPClauseName(OMPC_copyprivate) << Type
11818 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011819 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +000011820 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011821 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +000011822 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011823 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +000011824 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011825 continue;
11826 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +000011827
Alexey Bataevbae9a792014-06-27 10:37:06 +000011828 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
11829 // A variable of class type (or array thereof) that appears in a
11830 // copyin clause requires an accessible, unambiguous copy assignment
11831 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011832 Type = Context.getBaseElementType(Type.getNonReferenceType())
11833 .getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000011834 VarDecl *SrcVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011835 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src",
Alexey Bataeve122da12016-03-17 10:50:17 +000011836 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011837 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
11838 VarDecl *DstVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011839 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst",
Alexey Bataeve122da12016-03-17 10:50:17 +000011840 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011841 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
11842 ExprResult AssignmentOp = BuildBinOp(
11843 DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000011844 if (AssignmentOp.isInvalid())
11845 continue;
Alexey Bataeve122da12016-03-17 10:50:17 +000011846 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataeva63048e2015-03-23 06:18:07 +000011847 /*DiscardedValue=*/true);
11848 if (AssignmentOp.isInvalid())
11849 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000011850
11851 // No need to mark vars as copyprivate, they are already threadprivate or
11852 // implicitly private.
Alexey Bataeve3727102018-04-18 15:57:46 +000011853 assert(VD || isOpenMPCapturedDecl(D));
Alexey Bataeve122da12016-03-17 10:50:17 +000011854 Vars.push_back(
11855 VD ? RefExpr->IgnoreParens()
11856 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +000011857 SrcExprs.push_back(PseudoSrcExpr);
11858 DstExprs.push_back(PseudoDstExpr);
11859 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +000011860 }
11861
11862 if (Vars.empty())
11863 return nullptr;
11864
Alexey Bataeva63048e2015-03-23 06:18:07 +000011865 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11866 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011867}
11868
Alexey Bataev6125da92014-07-21 11:26:11 +000011869OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
11870 SourceLocation StartLoc,
11871 SourceLocation LParenLoc,
11872 SourceLocation EndLoc) {
11873 if (VarList.empty())
11874 return nullptr;
11875
11876 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
11877}
Alexey Bataevdea47612014-07-23 07:46:59 +000011878
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011879OMPClause *
11880Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
11881 SourceLocation DepLoc, SourceLocation ColonLoc,
11882 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
11883 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +000011884 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011885 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +000011886 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000011887 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +000011888 return nullptr;
11889 }
11890 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011891 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
11892 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +000011893 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011894 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000011895 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
11896 /*Last=*/OMPC_DEPEND_unknown, Except)
11897 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011898 return nullptr;
11899 }
11900 SmallVector<Expr *, 8> Vars;
Alexey Bataev8b427062016-05-25 12:36:08 +000011901 DSAStackTy::OperatorOffsetTy OpsOffs;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011902 llvm::APSInt DepCounter(/*BitWidth=*/32);
11903 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
Alexey Bataevf138fda2018-08-13 19:04:24 +000011904 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) {
11905 if (const Expr *OrderedCountExpr =
11906 DSAStack->getParentOrderedRegionParam().first) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011907 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
11908 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011909 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011910 }
Alexey Bataeve3727102018-04-18 15:57:46 +000011911 for (Expr *RefExpr : VarList) {
Alexey Bataev17daedf2018-02-15 22:42:57 +000011912 assert(RefExpr && "NULL expr in OpenMP shared clause.");
11913 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
11914 // It will be analyzed later.
11915 Vars.push_back(RefExpr);
11916 continue;
11917 }
11918
11919 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataeve3727102018-04-18 15:57:46 +000011920 Expr *SimpleExpr = RefExpr->IgnoreParenCasts();
Alexey Bataev17daedf2018-02-15 22:42:57 +000011921 if (DepKind == OMPC_DEPEND_sink) {
Alexey Bataevf138fda2018-08-13 19:04:24 +000011922 if (DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000011923 DepCounter >= TotalDepCount) {
11924 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
11925 continue;
11926 }
11927 ++DepCounter;
11928 // OpenMP [2.13.9, Summary]
11929 // depend(dependence-type : vec), where dependence-type is:
11930 // 'sink' and where vec is the iteration vector, which has the form:
11931 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
11932 // where n is the value specified by the ordered clause in the loop
11933 // directive, xi denotes the loop iteration variable of the i-th nested
11934 // loop associated with the loop directive, and di is a constant
11935 // non-negative integer.
11936 if (CurContext->isDependentContext()) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011937 // It will be analyzed later.
11938 Vars.push_back(RefExpr);
11939 continue;
11940 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000011941 SimpleExpr = SimpleExpr->IgnoreImplicit();
11942 OverloadedOperatorKind OOK = OO_None;
11943 SourceLocation OOLoc;
11944 Expr *LHS = SimpleExpr;
11945 Expr *RHS = nullptr;
11946 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
11947 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
11948 OOLoc = BO->getOperatorLoc();
11949 LHS = BO->getLHS()->IgnoreParenImpCasts();
11950 RHS = BO->getRHS()->IgnoreParenImpCasts();
11951 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
11952 OOK = OCE->getOperator();
11953 OOLoc = OCE->getOperatorLoc();
11954 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
11955 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
11956 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
11957 OOK = MCE->getMethodDecl()
11958 ->getNameInfo()
11959 .getName()
11960 .getCXXOverloadedOperator();
11961 OOLoc = MCE->getCallee()->getExprLoc();
11962 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
11963 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011964 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000011965 SourceLocation ELoc;
11966 SourceRange ERange;
Alexey Bataevbc529672018-09-28 19:33:14 +000011967 auto Res = getPrivateItem(*this, LHS, ELoc, ERange);
Alexey Bataev17daedf2018-02-15 22:42:57 +000011968 if (Res.second) {
11969 // It will be analyzed later.
11970 Vars.push_back(RefExpr);
11971 }
11972 ValueDecl *D = Res.first;
11973 if (!D)
11974 continue;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011975
Alexey Bataev17daedf2018-02-15 22:42:57 +000011976 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
11977 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
11978 continue;
11979 }
11980 if (RHS) {
11981 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
11982 RHS, OMPC_depend, /*StrictlyPositive=*/false);
11983 if (RHSRes.isInvalid())
11984 continue;
11985 }
11986 if (!CurContext->isDependentContext() &&
Alexey Bataevf138fda2018-08-13 19:04:24 +000011987 DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000011988 DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011989 const ValueDecl *VD =
Alexey Bataev17daedf2018-02-15 22:42:57 +000011990 DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue());
Alexey Bataeve3727102018-04-18 15:57:46 +000011991 if (VD)
Alexey Bataev17daedf2018-02-15 22:42:57 +000011992 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
11993 << 1 << VD;
Alexey Bataeve3727102018-04-18 15:57:46 +000011994 else
Alexey Bataev17daedf2018-02-15 22:42:57 +000011995 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0;
Alexey Bataev17daedf2018-02-15 22:42:57 +000011996 continue;
11997 }
Alexey Bataeve3727102018-04-18 15:57:46 +000011998 OpsOffs.emplace_back(RHS, OOK);
Alexey Bataev17daedf2018-02-15 22:42:57 +000011999 } else {
12000 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
12001 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
12002 (ASE &&
12003 !ASE->getBase()->getType().getNonReferenceType()->isPointerType() &&
12004 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
12005 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
12006 << RefExpr->getSourceRange();
12007 continue;
12008 }
12009 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
12010 getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
12011 ExprResult Res =
12012 CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RefExpr->IgnoreParenImpCasts());
12013 getDiagnostics().setSuppressAllDiagnostics(Suppress);
12014 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr)) {
12015 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
12016 << RefExpr->getSourceRange();
12017 continue;
12018 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012019 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000012020 Vars.push_back(RefExpr->IgnoreParenImpCasts());
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012021 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000012022
12023 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
12024 TotalDepCount > VarList.size() &&
Alexey Bataevf138fda2018-08-13 19:04:24 +000012025 DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000012026 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) {
12027 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
12028 << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
12029 }
12030 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
12031 Vars.empty())
12032 return nullptr;
12033
Alexey Bataev8b427062016-05-25 12:36:08 +000012034 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataevf138fda2018-08-13 19:04:24 +000012035 DepKind, DepLoc, ColonLoc, Vars,
12036 TotalDepCount.getZExtValue());
Alexey Bataev17daedf2018-02-15 22:42:57 +000012037 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) &&
12038 DSAStack->isParentOrderedRegion())
Alexey Bataev8b427062016-05-25 12:36:08 +000012039 DSAStack->addDoacrossDependClause(C, OpsOffs);
12040 return C;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012041}
Michael Wonge710d542015-08-07 16:16:36 +000012042
12043OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
12044 SourceLocation LParenLoc,
12045 SourceLocation EndLoc) {
12046 Expr *ValExpr = Device;
Alexey Bataev931e19b2017-10-02 16:32:39 +000012047 Stmt *HelperValStmt = nullptr;
Michael Wonge710d542015-08-07 16:16:36 +000012048
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012049 // OpenMP [2.9.1, Restrictions]
12050 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000012051 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
Alexey Bataeva0569352015-12-01 10:17:31 +000012052 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012053 return nullptr;
12054
Alexey Bataev931e19b2017-10-02 16:32:39 +000012055 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000012056 OpenMPDirectiveKind CaptureRegion =
12057 getOpenMPCaptureRegionForClause(DKind, OMPC_device);
12058 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000012059 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000012060 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev931e19b2017-10-02 16:32:39 +000012061 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
12062 HelperValStmt = buildPreInits(Context, Captures);
12063 }
12064
Alexey Bataev8451efa2018-01-15 19:06:12 +000012065 return new (Context) OMPDeviceClause(ValExpr, HelperValStmt, CaptureRegion,
12066 StartLoc, LParenLoc, EndLoc);
Michael Wonge710d542015-08-07 16:16:36 +000012067}
Kelvin Li0bff7af2015-11-23 05:32:03 +000012068
Alexey Bataeve3727102018-04-18 15:57:46 +000012069static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
Alexey Bataev95c23e72018-02-27 21:31:11 +000012070 DSAStackTy *Stack, QualType QTy,
12071 bool FullCheck = true) {
Kelvin Li0bff7af2015-11-23 05:32:03 +000012072 NamedDecl *ND;
12073 if (QTy->isIncompleteType(&ND)) {
12074 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
12075 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +000012076 }
Alexey Bataev95c23e72018-02-27 21:31:11 +000012077 if (FullCheck && !SemaRef.CurContext->isDependentContext() &&
12078 !QTy.isTrivialType(SemaRef.Context))
12079 SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR;
Kelvin Li0bff7af2015-11-23 05:32:03 +000012080 return true;
12081}
12082
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000012083/// Return true if it can be proven that the provided array expression
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012084/// (array section or array subscript) does NOT specify the whole size of the
12085/// array whose base type is \a BaseQTy.
Alexey Bataeve3727102018-04-18 15:57:46 +000012086static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012087 const Expr *E,
12088 QualType BaseQTy) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012089 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012090
12091 // If this is an array subscript, it refers to the whole size if the size of
12092 // the dimension is constant and equals 1. Also, an array section assumes the
12093 // format of an array subscript if no colon is used.
12094 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012095 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012096 return ATy->getSize().getSExtValue() != 1;
12097 // Size can't be evaluated statically.
12098 return false;
12099 }
12100
12101 assert(OASE && "Expecting array section if not an array subscript.");
Alexey Bataeve3727102018-04-18 15:57:46 +000012102 const Expr *LowerBound = OASE->getLowerBound();
12103 const Expr *Length = OASE->getLength();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012104
12105 // If there is a lower bound that does not evaluates to zero, we are not
David Majnemer9d168222016-08-05 17:44:54 +000012106 // covering the whole dimension.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012107 if (LowerBound) {
12108 llvm::APSInt ConstLowerBound;
12109 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
12110 return false; // Can't get the integer value as a constant.
12111 if (ConstLowerBound.getSExtValue())
12112 return true;
12113 }
12114
12115 // If we don't have a length we covering the whole dimension.
12116 if (!Length)
12117 return false;
12118
12119 // If the base is a pointer, we don't have a way to get the size of the
12120 // pointee.
12121 if (BaseQTy->isPointerType())
12122 return false;
12123
12124 // We can only check if the length is the same as the size of the dimension
12125 // if we have a constant array.
Alexey Bataeve3727102018-04-18 15:57:46 +000012126 const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012127 if (!CATy)
12128 return false;
12129
12130 llvm::APSInt ConstLength;
12131 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
12132 return false; // Can't get the integer value as a constant.
12133
12134 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
12135}
12136
12137// Return true if it can be proven that the provided array expression (array
12138// section or array subscript) does NOT specify a single element of the array
12139// whose base type is \a BaseQTy.
Alexey Bataeve3727102018-04-18 15:57:46 +000012140static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
David Majnemer9d168222016-08-05 17:44:54 +000012141 const Expr *E,
12142 QualType BaseQTy) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012143 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012144
12145 // An array subscript always refer to a single element. Also, an array section
12146 // assumes the format of an array subscript if no colon is used.
12147 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
12148 return false;
12149
12150 assert(OASE && "Expecting array section if not an array subscript.");
Alexey Bataeve3727102018-04-18 15:57:46 +000012151 const Expr *Length = OASE->getLength();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012152
12153 // If we don't have a length we have to check if the array has unitary size
12154 // for this dimension. Also, we should always expect a length if the base type
12155 // is pointer.
12156 if (!Length) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012157 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012158 return ATy->getSize().getSExtValue() != 1;
12159 // We cannot assume anything.
12160 return false;
12161 }
12162
12163 // Check if the length evaluates to 1.
12164 llvm::APSInt ConstLength;
12165 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
12166 return false; // Can't get the integer value as a constant.
12167
12168 return ConstLength.getSExtValue() != 1;
12169}
12170
Samuel Antao661c0902016-05-26 17:39:58 +000012171// Return the expression of the base of the mappable expression or null if it
12172// cannot be determined and do all the necessary checks to see if the expression
12173// is valid as a standalone mappable expression. In the process, record all the
Samuel Antao90927002016-04-26 14:54:23 +000012174// components of the expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000012175static const Expr *checkMapClauseExpressionBase(
Samuel Antao90927002016-04-26 14:54:23 +000012176 Sema &SemaRef, Expr *E,
Samuel Antao661c0902016-05-26 17:39:58 +000012177 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012178 OpenMPClauseKind CKind, bool NoDiagnose) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012179 SourceLocation ELoc = E->getExprLoc();
12180 SourceRange ERange = E->getSourceRange();
12181
12182 // The base of elements of list in a map clause have to be either:
12183 // - a reference to variable or field.
12184 // - a member expression.
12185 // - an array expression.
12186 //
12187 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
12188 // reference to 'r'.
12189 //
12190 // If we have:
12191 //
12192 // struct SS {
12193 // Bla S;
12194 // foo() {
12195 // #pragma omp target map (S.Arr[:12]);
12196 // }
12197 // }
12198 //
12199 // We want to retrieve the member expression 'this->S';
12200
Alexey Bataeve3727102018-04-18 15:57:46 +000012201 const Expr *RelevantExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012202
Samuel Antao5de996e2016-01-22 20:21:36 +000012203 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
12204 // If a list item is an array section, it must specify contiguous storage.
12205 //
12206 // For this restriction it is sufficient that we make sure only references
12207 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012208 // exist except in the rightmost expression (unless they cover the whole
12209 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +000012210 //
12211 // r.ArrS[3:5].Arr[6:7]
12212 //
12213 // r.ArrS[3:5].x
12214 //
12215 // but these would be valid:
12216 // r.ArrS[3].Arr[6:7]
12217 //
12218 // r.ArrS[3].x
12219
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012220 bool AllowUnitySizeArraySection = true;
12221 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +000012222
Dmitry Polukhin644a9252016-03-11 07:58:34 +000012223 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012224 E = E->IgnoreParenImpCasts();
12225
12226 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
12227 if (!isa<VarDecl>(CurE->getDecl()))
Alexey Bataev27041fa2017-12-05 15:22:49 +000012228 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012229
12230 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012231
12232 // If we got a reference to a declaration, we should not expect any array
12233 // section before that.
12234 AllowUnitySizeArraySection = false;
12235 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000012236
12237 // Record the component.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012238 CurComponents.emplace_back(CurE, CurE->getDecl());
12239 } else if (auto *CurE = dyn_cast<MemberExpr>(E)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012240 Expr *BaseE = CurE->getBase()->IgnoreParenImpCasts();
Samuel Antao5de996e2016-01-22 20:21:36 +000012241
12242 if (isa<CXXThisExpr>(BaseE))
12243 // We found a base expression: this->Val.
12244 RelevantExpr = CurE;
12245 else
12246 E = BaseE;
12247
12248 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012249 if (!NoDiagnose) {
12250 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
12251 << CurE->getSourceRange();
12252 return nullptr;
12253 }
12254 if (RelevantExpr)
12255 return nullptr;
12256 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000012257 }
12258
12259 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
12260
12261 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
12262 // A bit-field cannot appear in a map clause.
12263 //
12264 if (FD->isBitField()) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012265 if (!NoDiagnose) {
12266 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
12267 << CurE->getSourceRange() << getOpenMPClauseName(CKind);
12268 return nullptr;
12269 }
12270 if (RelevantExpr)
12271 return nullptr;
12272 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000012273 }
12274
12275 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12276 // If the type of a list item is a reference to a type T then the type
12277 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012278 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000012279
12280 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
12281 // A list item cannot be a variable that is a member of a structure with
12282 // a union type.
12283 //
Alexey Bataeve3727102018-04-18 15:57:46 +000012284 if (CurType->isUnionType()) {
12285 if (!NoDiagnose) {
12286 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
12287 << CurE->getSourceRange();
12288 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012289 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012290 continue;
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012291 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012292
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012293 // If we got a member expression, we should not expect any array section
12294 // before that:
12295 //
12296 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
12297 // If a list item is an element of a structure, only the rightmost symbol
12298 // of the variable reference can be an array section.
12299 //
12300 AllowUnitySizeArraySection = false;
12301 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000012302
12303 // Record the component.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012304 CurComponents.emplace_back(CurE, FD);
12305 } else if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012306 E = CurE->getBase()->IgnoreParenImpCasts();
12307
12308 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012309 if (!NoDiagnose) {
12310 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
12311 << 0 << CurE->getSourceRange();
12312 return nullptr;
12313 }
12314 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000012315 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012316
12317 // If we got an array subscript that express the whole dimension we
12318 // can have any array expressions before. If it only expressing part of
12319 // the dimension, we can only have unitary-size array expressions.
Alexey Bataeve3727102018-04-18 15:57:46 +000012320 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012321 E->getType()))
12322 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000012323
12324 // Record the component - we don't have any declaration associated.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012325 CurComponents.emplace_back(CurE, nullptr);
12326 } else if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012327 assert(!NoDiagnose && "Array sections cannot be implicitly mapped.");
Samuel Antao5de996e2016-01-22 20:21:36 +000012328 E = CurE->getBase()->IgnoreParenImpCasts();
12329
Alexey Bataev27041fa2017-12-05 15:22:49 +000012330 QualType CurType =
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012331 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
12332
Samuel Antao5de996e2016-01-22 20:21:36 +000012333 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12334 // If the type of a list item is a reference to a type T then the type
12335 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +000012336 if (CurType->isReferenceType())
12337 CurType = CurType->getPointeeType();
12338
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012339 bool IsPointer = CurType->isAnyPointerType();
12340
12341 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012342 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
12343 << 0 << CurE->getSourceRange();
Alexey Bataev27041fa2017-12-05 15:22:49 +000012344 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012345 }
12346
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012347 bool NotWhole =
Alexey Bataeve3727102018-04-18 15:57:46 +000012348 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012349 bool NotUnity =
Alexey Bataeve3727102018-04-18 15:57:46 +000012350 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012351
Samuel Antaodab51bb2016-07-18 23:22:11 +000012352 if (AllowWholeSizeArraySection) {
12353 // Any array section is currently allowed. Allowing a whole size array
12354 // section implies allowing a unity array section as well.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012355 //
12356 // If this array section refers to the whole dimension we can still
12357 // accept other array sections before this one, except if the base is a
12358 // pointer. Otherwise, only unitary sections are accepted.
12359 if (NotWhole || IsPointer)
12360 AllowWholeSizeArraySection = false;
Samuel Antaodab51bb2016-07-18 23:22:11 +000012361 } else if (AllowUnitySizeArraySection && NotUnity) {
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012362 // A unity or whole array section is not allowed and that is not
12363 // compatible with the properties of the current array section.
12364 SemaRef.Diag(
12365 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
12366 << CurE->getSourceRange();
Alexey Bataev27041fa2017-12-05 15:22:49 +000012367 return nullptr;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012368 }
Samuel Antao90927002016-04-26 14:54:23 +000012369
12370 // Record the component - we don't have any declaration associated.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012371 CurComponents.emplace_back(CurE, nullptr);
12372 } else {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012373 if (!NoDiagnose) {
12374 // If nothing else worked, this is not a valid map clause expression.
12375 SemaRef.Diag(
12376 ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
12377 << ERange;
12378 }
Alexey Bataev27041fa2017-12-05 15:22:49 +000012379 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012380 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012381 }
12382
12383 return RelevantExpr;
12384}
12385
12386// Return true if expression E associated with value VD has conflicts with other
12387// map information.
Alexey Bataeve3727102018-04-18 15:57:46 +000012388static bool checkMapConflicts(
12389 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E,
Samuel Antao90927002016-04-26 14:54:23 +000012390 bool CurrentRegionOnly,
Samuel Antao661c0902016-05-26 17:39:58 +000012391 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
12392 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012393 assert(VD && E);
Samuel Antao5de996e2016-01-22 20:21:36 +000012394 SourceLocation ELoc = E->getExprLoc();
12395 SourceRange ERange = E->getSourceRange();
12396
12397 // In order to easily check the conflicts we need to match each component of
12398 // the expression under test with the components of the expressions that are
12399 // already in the stack.
12400
Samuel Antao5de996e2016-01-22 20:21:36 +000012401 assert(!CurComponents.empty() && "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000012402 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000012403 "Map clause expression with unexpected base!");
12404
12405 // Variables to help detecting enclosing problems in data environment nests.
12406 bool IsEnclosedByDataEnvironmentExpr = false;
Samuel Antao90927002016-04-26 14:54:23 +000012407 const Expr *EnclosingExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012408
Samuel Antao90927002016-04-26 14:54:23 +000012409 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
12410 VD, CurrentRegionOnly,
Alexey Bataeve3727102018-04-18 15:57:46 +000012411 [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc,
12412 ERange, CKind, &EnclosingExpr,
12413 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef
12414 StackComponents,
12415 OpenMPClauseKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012416 assert(!StackComponents.empty() &&
12417 "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000012418 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000012419 "Map clause expression with unexpected base!");
Fangrui Song16fe49a2018-04-18 19:32:01 +000012420 (void)VD;
Samuel Antao5de996e2016-01-22 20:21:36 +000012421
Samuel Antao90927002016-04-26 14:54:23 +000012422 // The whole expression in the stack.
Alexey Bataeve3727102018-04-18 15:57:46 +000012423 const Expr *RE = StackComponents.front().getAssociatedExpression();
Samuel Antao90927002016-04-26 14:54:23 +000012424
Samuel Antao5de996e2016-01-22 20:21:36 +000012425 // Expressions must start from the same base. Here we detect at which
12426 // point both expressions diverge from each other and see if we can
12427 // detect if the memory referred to both expressions is contiguous and
12428 // do not overlap.
12429 auto CI = CurComponents.rbegin();
12430 auto CE = CurComponents.rend();
12431 auto SI = StackComponents.rbegin();
12432 auto SE = StackComponents.rend();
12433 for (; CI != CE && SI != SE; ++CI, ++SI) {
12434
12435 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
12436 // At most one list item can be an array item derived from a given
12437 // variable in map clauses of the same construct.
Samuel Antao90927002016-04-26 14:54:23 +000012438 if (CurrentRegionOnly &&
12439 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
12440 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
12441 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
12442 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
12443 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
Samuel Antao5de996e2016-01-22 20:21:36 +000012444 diag::err_omp_multiple_array_items_in_map_clause)
Samuel Antao90927002016-04-26 14:54:23 +000012445 << CI->getAssociatedExpression()->getSourceRange();
12446 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
12447 diag::note_used_here)
12448 << SI->getAssociatedExpression()->getSourceRange();
Samuel Antao5de996e2016-01-22 20:21:36 +000012449 return true;
12450 }
12451
12452 // Do both expressions have the same kind?
Samuel Antao90927002016-04-26 14:54:23 +000012453 if (CI->getAssociatedExpression()->getStmtClass() !=
12454 SI->getAssociatedExpression()->getStmtClass())
Samuel Antao5de996e2016-01-22 20:21:36 +000012455 break;
12456
12457 // Are we dealing with different variables/fields?
Samuel Antao90927002016-04-26 14:54:23 +000012458 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
Samuel Antao5de996e2016-01-22 20:21:36 +000012459 break;
12460 }
Kelvin Li9f645ae2016-07-18 22:49:16 +000012461 // Check if the extra components of the expressions in the enclosing
12462 // data environment are redundant for the current base declaration.
12463 // If they are, the maps completely overlap, which is legal.
12464 for (; SI != SE; ++SI) {
12465 QualType Type;
Alexey Bataeve3727102018-04-18 15:57:46 +000012466 if (const auto *ASE =
David Majnemer9d168222016-08-05 17:44:54 +000012467 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
Kelvin Li9f645ae2016-07-18 22:49:16 +000012468 Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
Alexey Bataeve3727102018-04-18 15:57:46 +000012469 } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(
David Majnemer9d168222016-08-05 17:44:54 +000012470 SI->getAssociatedExpression())) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012471 const Expr *E = OASE->getBase()->IgnoreParenImpCasts();
Kelvin Li9f645ae2016-07-18 22:49:16 +000012472 Type =
12473 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
12474 }
12475 if (Type.isNull() || Type->isAnyPointerType() ||
Alexey Bataeve3727102018-04-18 15:57:46 +000012476 checkArrayExpressionDoesNotReferToWholeSize(
Kelvin Li9f645ae2016-07-18 22:49:16 +000012477 SemaRef, SI->getAssociatedExpression(), Type))
12478 break;
12479 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012480
12481 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
12482 // List items of map clauses in the same construct must not share
12483 // original storage.
12484 //
12485 // If the expressions are exactly the same or one is a subset of the
12486 // other, it means they are sharing storage.
12487 if (CI == CE && SI == SE) {
12488 if (CurrentRegionOnly) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012489 if (CKind == OMPC_map) {
Samuel Antao661c0902016-05-26 17:39:58 +000012490 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +000012491 } else {
Samuel Antaoec172c62016-05-26 17:49:04 +000012492 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000012493 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
12494 << ERange;
12495 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012496 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12497 << RE->getSourceRange();
12498 return true;
Samuel Antao5de996e2016-01-22 20:21:36 +000012499 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012500 // If we find the same expression in the enclosing data environment,
12501 // that is legal.
12502 IsEnclosedByDataEnvironmentExpr = true;
12503 return false;
Samuel Antao5de996e2016-01-22 20:21:36 +000012504 }
12505
Samuel Antao90927002016-04-26 14:54:23 +000012506 QualType DerivedType =
12507 std::prev(CI)->getAssociatedDeclaration()->getType();
12508 SourceLocation DerivedLoc =
12509 std::prev(CI)->getAssociatedExpression()->getExprLoc();
Samuel Antao5de996e2016-01-22 20:21:36 +000012510
12511 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12512 // If the type of a list item is a reference to a type T then the type
12513 // will be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000012514 DerivedType = DerivedType.getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000012515
12516 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
12517 // A variable for which the type is pointer and an array section
12518 // derived from that variable must not appear as list items of map
12519 // clauses of the same construct.
12520 //
12521 // Also, cover one of the cases in:
12522 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
12523 // If any part of the original storage of a list item has corresponding
12524 // storage in the device data environment, all of the original storage
12525 // must have corresponding storage in the device data environment.
12526 //
12527 if (DerivedType->isAnyPointerType()) {
12528 if (CI == CE || SI == SE) {
12529 SemaRef.Diag(
12530 DerivedLoc,
12531 diag::err_omp_pointer_mapped_along_with_derived_section)
12532 << DerivedLoc;
Alexey Bataev2819260b2018-02-27 17:42:00 +000012533 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12534 << RE->getSourceRange();
12535 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +000012536 }
12537 if (CI->getAssociatedExpression()->getStmtClass() !=
Alexey Bataev2819260b2018-02-27 17:42:00 +000012538 SI->getAssociatedExpression()->getStmtClass() ||
12539 CI->getAssociatedDeclaration()->getCanonicalDecl() ==
12540 SI->getAssociatedDeclaration()->getCanonicalDecl()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012541 assert(CI != CE && SI != SE);
Alexey Bataev2819260b2018-02-27 17:42:00 +000012542 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced)
Samuel Antao5de996e2016-01-22 20:21:36 +000012543 << DerivedLoc;
Alexey Bataev2819260b2018-02-27 17:42:00 +000012544 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12545 << RE->getSourceRange();
12546 return true;
Samuel Antao5de996e2016-01-22 20:21:36 +000012547 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012548 }
12549
12550 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
12551 // List items of map clauses in the same construct must not share
12552 // original storage.
12553 //
12554 // An expression is a subset of the other.
12555 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012556 if (CKind == OMPC_map) {
Alexey Bataeve82445f2018-09-20 13:54:02 +000012557 if (CI != CE || SI != SE) {
12558 // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is
12559 // a pointer.
12560 auto Begin =
12561 CI != CE ? CurComponents.begin() : StackComponents.begin();
12562 auto End = CI != CE ? CurComponents.end() : StackComponents.end();
12563 auto It = Begin;
12564 while (It != End && !It->getAssociatedDeclaration())
12565 std::advance(It, 1);
12566 assert(It != End &&
12567 "Expected at least one component with the declaration.");
12568 if (It != Begin && It->getAssociatedDeclaration()
12569 ->getType()
12570 .getCanonicalType()
12571 ->isAnyPointerType()) {
12572 IsEnclosedByDataEnvironmentExpr = false;
12573 EnclosingExpr = nullptr;
12574 return false;
12575 }
12576 }
Samuel Antao661c0902016-05-26 17:39:58 +000012577 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +000012578 } else {
Samuel Antaoec172c62016-05-26 17:49:04 +000012579 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000012580 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
12581 << ERange;
12582 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012583 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12584 << RE->getSourceRange();
12585 return true;
12586 }
12587
12588 // The current expression uses the same base as other expression in the
Samuel Antao90927002016-04-26 14:54:23 +000012589 // data environment but does not contain it completely.
Samuel Antao5de996e2016-01-22 20:21:36 +000012590 if (!CurrentRegionOnly && SI != SE)
12591 EnclosingExpr = RE;
12592
12593 // The current expression is a subset of the expression in the data
12594 // environment.
12595 IsEnclosedByDataEnvironmentExpr |=
12596 (!CurrentRegionOnly && CI != CE && SI == SE);
12597
12598 return false;
12599 });
12600
12601 if (CurrentRegionOnly)
12602 return FoundError;
12603
12604 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
12605 // If any part of the original storage of a list item has corresponding
12606 // storage in the device data environment, all of the original storage must
12607 // have corresponding storage in the device data environment.
12608 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
12609 // If a list item is an element of a structure, and a different element of
12610 // the structure has a corresponding list item in the device data environment
12611 // prior to a task encountering the construct associated with the map clause,
Samuel Antao90927002016-04-26 14:54:23 +000012612 // then the list item must also have a corresponding list item in the device
Samuel Antao5de996e2016-01-22 20:21:36 +000012613 // data environment prior to the task encountering the construct.
12614 //
12615 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
12616 SemaRef.Diag(ELoc,
12617 diag::err_omp_original_storage_is_shared_and_does_not_contain)
12618 << ERange;
12619 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
12620 << EnclosingExpr->getSourceRange();
12621 return true;
12622 }
12623
12624 return FoundError;
12625}
12626
Samuel Antao661c0902016-05-26 17:39:58 +000012627namespace {
12628// Utility struct that gathers all the related lists associated with a mappable
12629// expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000012630struct MappableVarListInfo {
Samuel Antao661c0902016-05-26 17:39:58 +000012631 // The list of expressions.
12632 ArrayRef<Expr *> VarList;
12633 // The list of processed expressions.
12634 SmallVector<Expr *, 16> ProcessedVarList;
12635 // The mappble components for each expression.
12636 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
12637 // The base declaration of the variable.
12638 SmallVector<ValueDecl *, 16> VarBaseDeclarations;
12639
12640 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
12641 // We have a list of components and base declarations for each entry in the
12642 // variable list.
12643 VarComponents.reserve(VarList.size());
12644 VarBaseDeclarations.reserve(VarList.size());
12645 }
12646};
12647}
12648
12649// Check the validity of the provided variable list for the provided clause kind
12650// \a CKind. In the check process the valid expressions, and mappable expression
12651// components and variables are extracted and used to fill \a Vars,
12652// \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and
12653// \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'.
12654static void
12655checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS,
12656 OpenMPClauseKind CKind, MappableVarListInfo &MVLI,
12657 SourceLocation StartLoc,
12658 OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
12659 bool IsMapTypeImplicit = false) {
Samuel Antaoec172c62016-05-26 17:49:04 +000012660 // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
12661 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
Samuel Antao661c0902016-05-26 17:39:58 +000012662 "Unexpected clause kind with mappable expressions!");
Kelvin Li0bff7af2015-11-23 05:32:03 +000012663
Samuel Antao90927002016-04-26 14:54:23 +000012664 // Keep track of the mappable components and base declarations in this clause.
12665 // Each entry in the list is going to have a list of components associated. We
12666 // record each set of the components so that we can build the clause later on.
12667 // In the end we should have the same amount of declarations and component
12668 // lists.
Samuel Antao90927002016-04-26 14:54:23 +000012669
Alexey Bataeve3727102018-04-18 15:57:46 +000012670 for (Expr *RE : MVLI.VarList) {
Samuel Antaoec172c62016-05-26 17:49:04 +000012671 assert(RE && "Null expr in omp to/from/map clause");
Kelvin Li0bff7af2015-11-23 05:32:03 +000012672 SourceLocation ELoc = RE->getExprLoc();
12673
Alexey Bataeve3727102018-04-18 15:57:46 +000012674 const Expr *VE = RE->IgnoreParenLValueCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000012675
12676 if (VE->isValueDependent() || VE->isTypeDependent() ||
12677 VE->isInstantiationDependent() ||
12678 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012679 // We can only analyze this information once the missing information is
12680 // resolved.
Samuel Antao661c0902016-05-26 17:39:58 +000012681 MVLI.ProcessedVarList.push_back(RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012682 continue;
12683 }
12684
Alexey Bataeve3727102018-04-18 15:57:46 +000012685 Expr *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000012686
Samuel Antao5de996e2016-01-22 20:21:36 +000012687 if (!RE->IgnoreParenImpCasts()->isLValue()) {
Samuel Antao661c0902016-05-26 17:39:58 +000012688 SemaRef.Diag(ELoc,
12689 diag::err_omp_expected_named_var_member_or_array_expression)
Samuel Antao5de996e2016-01-22 20:21:36 +000012690 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000012691 continue;
12692 }
12693
Samuel Antao90927002016-04-26 14:54:23 +000012694 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
12695 ValueDecl *CurDeclaration = nullptr;
12696
12697 // Obtain the array or member expression bases if required. Also, fill the
12698 // components array with all the components identified in the process.
Alexey Bataeve3727102018-04-18 15:57:46 +000012699 const Expr *BE = checkMapClauseExpressionBase(
12700 SemaRef, SimpleExpr, CurComponents, CKind, /*NoDiagnose=*/false);
Samuel Antao5de996e2016-01-22 20:21:36 +000012701 if (!BE)
12702 continue;
12703
Samuel Antao90927002016-04-26 14:54:23 +000012704 assert(!CurComponents.empty() &&
12705 "Invalid mappable expression information.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000012706
Samuel Antao90927002016-04-26 14:54:23 +000012707 // For the following checks, we rely on the base declaration which is
12708 // expected to be associated with the last component. The declaration is
12709 // expected to be a variable or a field (if 'this' is being mapped).
12710 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
12711 assert(CurDeclaration && "Null decl on map clause.");
12712 assert(
12713 CurDeclaration->isCanonicalDecl() &&
12714 "Expecting components to have associated only canonical declarations.");
12715
12716 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
Alexey Bataeve3727102018-04-18 15:57:46 +000012717 const auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
Samuel Antao5de996e2016-01-22 20:21:36 +000012718
12719 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000012720 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000012721
12722 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
Samuel Antao661c0902016-05-26 17:39:58 +000012723 // threadprivate variables cannot appear in a map clause.
12724 // OpenMP 4.5 [2.10.5, target update Construct]
12725 // threadprivate variables cannot appear in a from clause.
12726 if (VD && DSAS->isThreadPrivate(VD)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012727 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
Samuel Antao661c0902016-05-26 17:39:58 +000012728 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
12729 << getOpenMPClauseName(CKind);
Alexey Bataeve3727102018-04-18 15:57:46 +000012730 reportOriginalDsa(SemaRef, DSAS, VD, DVar);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012731 continue;
12732 }
12733
Samuel Antao5de996e2016-01-22 20:21:36 +000012734 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
12735 // A list item cannot appear in both a map clause and a data-sharing
12736 // attribute clause on the same construct.
Kelvin Li0bff7af2015-11-23 05:32:03 +000012737
Samuel Antao5de996e2016-01-22 20:21:36 +000012738 // Check conflicts with other map clause expressions. We check the conflicts
12739 // with the current construct separately from the enclosing data
Samuel Antao661c0902016-05-26 17:39:58 +000012740 // environment, because the restrictions are different. We only have to
12741 // check conflicts across regions for the map clauses.
Alexey Bataeve3727102018-04-18 15:57:46 +000012742 if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
Samuel Antao661c0902016-05-26 17:39:58 +000012743 /*CurrentRegionOnly=*/true, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000012744 break;
Samuel Antao661c0902016-05-26 17:39:58 +000012745 if (CKind == OMPC_map &&
Alexey Bataeve3727102018-04-18 15:57:46 +000012746 checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
Samuel Antao661c0902016-05-26 17:39:58 +000012747 /*CurrentRegionOnly=*/false, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000012748 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000012749
Samuel Antao661c0902016-05-26 17:39:58 +000012750 // OpenMP 4.5 [2.10.5, target update Construct]
Samuel Antao5de996e2016-01-22 20:21:36 +000012751 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12752 // If the type of a list item is a reference to a type T then the type will
12753 // be considered to be T for all purposes of this clause.
Alexey Bataev354df2e2018-05-02 18:44:10 +000012754 auto I = llvm::find_if(
12755 CurComponents,
12756 [](const OMPClauseMappableExprCommon::MappableComponent &MC) {
12757 return MC.getAssociatedDeclaration();
12758 });
12759 assert(I != CurComponents.end() && "Null decl on map clause.");
12760 QualType Type =
12761 I->getAssociatedDeclaration()->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000012762
Samuel Antao661c0902016-05-26 17:39:58 +000012763 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
12764 // A list item in a to or from clause must have a mappable type.
Samuel Antao5de996e2016-01-22 20:21:36 +000012765 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000012766 // A list item must have a mappable type.
Alexey Bataeve3727102018-04-18 15:57:46 +000012767 if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
Samuel Antao661c0902016-05-26 17:39:58 +000012768 DSAS, Type))
Kelvin Li0bff7af2015-11-23 05:32:03 +000012769 continue;
12770
Samuel Antao661c0902016-05-26 17:39:58 +000012771 if (CKind == OMPC_map) {
12772 // target enter data
12773 // OpenMP [2.10.2, Restrictions, p. 99]
12774 // A map-type must be specified in all map clauses and must be either
12775 // to or alloc.
12776 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
12777 if (DKind == OMPD_target_enter_data &&
12778 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
12779 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
12780 << (IsMapTypeImplicit ? 1 : 0)
12781 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
12782 << getOpenMPDirectiveName(DKind);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000012783 continue;
12784 }
Samuel Antao661c0902016-05-26 17:39:58 +000012785
12786 // target exit_data
12787 // OpenMP [2.10.3, Restrictions, p. 102]
12788 // A map-type must be specified in all map clauses and must be either
12789 // from, release, or delete.
12790 if (DKind == OMPD_target_exit_data &&
12791 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
12792 MapType == OMPC_MAP_delete)) {
12793 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
12794 << (IsMapTypeImplicit ? 1 : 0)
12795 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
12796 << getOpenMPDirectiveName(DKind);
12797 continue;
12798 }
12799
12800 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
12801 // A list item cannot appear in both a map clause and a data-sharing
12802 // attribute clause on the same construct
Alexey Bataeve3727102018-04-18 15:57:46 +000012803 if (VD && isOpenMPTargetExecutionDirective(DKind)) {
12804 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
Samuel Antao661c0902016-05-26 17:39:58 +000012805 if (isOpenMPPrivate(DVar.CKind)) {
Samuel Antao6890b092016-07-28 14:25:09 +000012806 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Samuel Antao661c0902016-05-26 17:39:58 +000012807 << getOpenMPClauseName(DVar.CKind)
Samuel Antao6890b092016-07-28 14:25:09 +000012808 << getOpenMPClauseName(OMPC_map)
Samuel Antao661c0902016-05-26 17:39:58 +000012809 << getOpenMPDirectiveName(DSAS->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000012810 reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar);
Samuel Antao661c0902016-05-26 17:39:58 +000012811 continue;
12812 }
12813 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +000012814 }
12815
Samuel Antao90927002016-04-26 14:54:23 +000012816 // Save the current expression.
Samuel Antao661c0902016-05-26 17:39:58 +000012817 MVLI.ProcessedVarList.push_back(RE);
Samuel Antao90927002016-04-26 14:54:23 +000012818
12819 // Store the components in the stack so that they can be used to check
12820 // against other clauses later on.
Samuel Antao6890b092016-07-28 14:25:09 +000012821 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents,
12822 /*WhereFoundClauseKind=*/OMPC_map);
Samuel Antao90927002016-04-26 14:54:23 +000012823
12824 // Save the components and declaration to create the clause. For purposes of
12825 // the clause creation, any component list that has has base 'this' uses
Samuel Antao686c70c2016-05-26 17:30:50 +000012826 // null as base declaration.
Samuel Antao661c0902016-05-26 17:39:58 +000012827 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
12828 MVLI.VarComponents.back().append(CurComponents.begin(),
12829 CurComponents.end());
12830 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
12831 : CurDeclaration);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012832 }
Samuel Antao661c0902016-05-26 17:39:58 +000012833}
12834
12835OMPClause *
12836Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
12837 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
12838 SourceLocation MapLoc, SourceLocation ColonLoc,
12839 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
12840 SourceLocation LParenLoc, SourceLocation EndLoc) {
12841 MappableVarListInfo MVLI(VarList);
12842 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc,
12843 MapType, IsMapTypeImplicit);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012844
Samuel Antao5de996e2016-01-22 20:21:36 +000012845 // We need to produce a map clause even if we don't have variables so that
12846 // other diagnostics related with non-existing map clauses are accurate.
Samuel Antao661c0902016-05-26 17:39:58 +000012847 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc,
12848 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
12849 MVLI.VarComponents, MapTypeModifier, MapType,
12850 IsMapTypeImplicit, MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012851}
Kelvin Li099bb8c2015-11-24 20:50:12 +000012852
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012853QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
12854 TypeResult ParsedType) {
12855 assert(ParsedType.isUsable());
12856
12857 QualType ReductionType = GetTypeFromParser(ParsedType.get());
12858 if (ReductionType.isNull())
12859 return QualType();
12860
12861 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
12862 // A type name in a declare reduction directive cannot be a function type, an
12863 // array type, a reference type, or a type qualified with const, volatile or
12864 // restrict.
12865 if (ReductionType.hasQualifiers()) {
12866 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
12867 return QualType();
12868 }
12869
12870 if (ReductionType->isFunctionType()) {
12871 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
12872 return QualType();
12873 }
12874 if (ReductionType->isReferenceType()) {
12875 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
12876 return QualType();
12877 }
12878 if (ReductionType->isArrayType()) {
12879 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
12880 return QualType();
12881 }
12882 return ReductionType;
12883}
12884
12885Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
12886 Scope *S, DeclContext *DC, DeclarationName Name,
12887 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
12888 AccessSpecifier AS, Decl *PrevDeclInScope) {
12889 SmallVector<Decl *, 8> Decls;
12890 Decls.reserve(ReductionTypes.size());
12891
12892 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
Richard Smithbecb92d2017-10-10 22:33:17 +000012893 forRedeclarationInCurContext());
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012894 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
12895 // A reduction-identifier may not be re-declared in the current scope for the
12896 // same type or for a type that is compatible according to the base language
12897 // rules.
12898 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
12899 OMPDeclareReductionDecl *PrevDRD = nullptr;
12900 bool InCompoundScope = true;
12901 if (S != nullptr) {
12902 // Find previous declaration with the same name not referenced in other
12903 // declarations.
12904 FunctionScopeInfo *ParentFn = getEnclosingFunction();
12905 InCompoundScope =
12906 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
12907 LookupName(Lookup, S);
12908 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
12909 /*AllowInlineNamespace=*/false);
12910 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
Alexey Bataeve3727102018-04-18 15:57:46 +000012911 LookupResult::Filter Filter = Lookup.makeFilter();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012912 while (Filter.hasNext()) {
12913 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
12914 if (InCompoundScope) {
12915 auto I = UsedAsPrevious.find(PrevDecl);
12916 if (I == UsedAsPrevious.end())
12917 UsedAsPrevious[PrevDecl] = false;
Alexey Bataeve3727102018-04-18 15:57:46 +000012918 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope())
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012919 UsedAsPrevious[D] = true;
12920 }
12921 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
12922 PrevDecl->getLocation();
12923 }
12924 Filter.done();
12925 if (InCompoundScope) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012926 for (const auto &PrevData : UsedAsPrevious) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012927 if (!PrevData.second) {
12928 PrevDRD = PrevData.first;
12929 break;
12930 }
12931 }
12932 }
12933 } else if (PrevDeclInScope != nullptr) {
12934 auto *PrevDRDInScope = PrevDRD =
12935 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
12936 do {
12937 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
12938 PrevDRDInScope->getLocation();
12939 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
12940 } while (PrevDRDInScope != nullptr);
12941 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012942 for (const auto &TyData : ReductionTypes) {
12943 const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012944 bool Invalid = false;
12945 if (I != PreviousRedeclTypes.end()) {
12946 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
12947 << TyData.first;
12948 Diag(I->second, diag::note_previous_definition);
12949 Invalid = true;
12950 }
12951 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
12952 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
12953 Name, TyData.first, PrevDRD);
12954 DC->addDecl(DRD);
12955 DRD->setAccess(AS);
12956 Decls.push_back(DRD);
12957 if (Invalid)
12958 DRD->setInvalidDecl();
12959 else
12960 PrevDRD = DRD;
12961 }
12962
12963 return DeclGroupPtrTy::make(
12964 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
12965}
12966
12967void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
12968 auto *DRD = cast<OMPDeclareReductionDecl>(D);
12969
12970 // Enter new function scope.
12971 PushFunctionScope();
Reid Kleckner87a31802018-03-12 21:43:02 +000012972 setFunctionHasBranchProtectedScope();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012973 getCurFunction()->setHasOMPDeclareReductionCombiner();
12974
12975 if (S != nullptr)
12976 PushDeclContext(S, DRD);
12977 else
12978 CurContext = DRD;
12979
Faisal Valid143a0c2017-04-01 21:30:49 +000012980 PushExpressionEvaluationContext(
12981 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012982
12983 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012984 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
12985 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
12986 // uses semantics of argument handles by value, but it should be passed by
12987 // reference. C lang does not support references, so pass all parameters as
12988 // pointers.
12989 // Create 'T omp_in;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000012990 VarDecl *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012991 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012992 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
12993 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
12994 // uses semantics of argument handles by value, but it should be passed by
12995 // reference. C lang does not support references, so pass all parameters as
12996 // pointers.
12997 // Create 'T omp_out;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000012998 VarDecl *OmpOutParm =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012999 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
13000 if (S != nullptr) {
13001 PushOnScopeChains(OmpInParm, S);
13002 PushOnScopeChains(OmpOutParm, S);
13003 } else {
13004 DRD->addDecl(OmpInParm);
13005 DRD->addDecl(OmpOutParm);
13006 }
Alexey Bataeve6aa4692018-09-13 16:54:05 +000013007 Expr *InE =
13008 ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation());
13009 Expr *OutE =
13010 ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation());
13011 DRD->setCombinerData(InE, OutE);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013012}
13013
13014void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
13015 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13016 DiscardCleanupsInEvaluationContext();
13017 PopExpressionEvaluationContext();
13018
13019 PopDeclContext();
13020 PopFunctionScopeInfo();
13021
13022 if (Combiner != nullptr)
13023 DRD->setCombiner(Combiner);
13024 else
13025 DRD->setInvalidDecl();
13026}
13027
Alexey Bataev070f43a2017-09-06 14:49:58 +000013028VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013029 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13030
13031 // Enter new function scope.
13032 PushFunctionScope();
Reid Kleckner87a31802018-03-12 21:43:02 +000013033 setFunctionHasBranchProtectedScope();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013034
13035 if (S != nullptr)
13036 PushDeclContext(S, DRD);
13037 else
13038 CurContext = DRD;
13039
Faisal Valid143a0c2017-04-01 21:30:49 +000013040 PushExpressionEvaluationContext(
13041 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013042
13043 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013044 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
13045 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
13046 // uses semantics of argument handles by value, but it should be passed by
13047 // reference. C lang does not support references, so pass all parameters as
13048 // pointers.
13049 // Create 'T omp_priv;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000013050 VarDecl *OmpPrivParm =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013051 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013052 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
13053 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
13054 // uses semantics of argument handles by value, but it should be passed by
13055 // reference. C lang does not support references, so pass all parameters as
13056 // pointers.
13057 // Create 'T omp_orig;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000013058 VarDecl *OmpOrigParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013059 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013060 if (S != nullptr) {
13061 PushOnScopeChains(OmpPrivParm, S);
13062 PushOnScopeChains(OmpOrigParm, S);
13063 } else {
13064 DRD->addDecl(OmpPrivParm);
13065 DRD->addDecl(OmpOrigParm);
13066 }
Alexey Bataeve6aa4692018-09-13 16:54:05 +000013067 Expr *OrigE =
13068 ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation());
13069 Expr *PrivE =
13070 ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation());
13071 DRD->setInitializerData(OrigE, PrivE);
Alexey Bataev070f43a2017-09-06 14:49:58 +000013072 return OmpPrivParm;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013073}
13074
Alexey Bataev070f43a2017-09-06 14:49:58 +000013075void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer,
13076 VarDecl *OmpPrivParm) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013077 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13078 DiscardCleanupsInEvaluationContext();
13079 PopExpressionEvaluationContext();
13080
13081 PopDeclContext();
13082 PopFunctionScopeInfo();
13083
Alexey Bataev070f43a2017-09-06 14:49:58 +000013084 if (Initializer != nullptr) {
13085 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit);
13086 } else if (OmpPrivParm->hasInit()) {
13087 DRD->setInitializer(OmpPrivParm->getInit(),
13088 OmpPrivParm->isDirectInit()
13089 ? OMPDeclareReductionDecl::DirectInit
13090 : OMPDeclareReductionDecl::CopyInit);
13091 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013092 DRD->setInvalidDecl();
Alexey Bataev070f43a2017-09-06 14:49:58 +000013093 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013094}
13095
13096Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
13097 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013098 for (Decl *D : DeclReductions.get()) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013099 if (IsValid) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013100 if (S)
13101 PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S,
13102 /*AddToContext=*/false);
13103 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013104 D->setInvalidDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000013105 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013106 }
13107 return DeclReductions;
13108}
13109
David Majnemer9d168222016-08-05 17:44:54 +000013110OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
Kelvin Li099bb8c2015-11-24 20:50:12 +000013111 SourceLocation StartLoc,
13112 SourceLocation LParenLoc,
13113 SourceLocation EndLoc) {
13114 Expr *ValExpr = NumTeams;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000013115 Stmt *HelperValStmt = nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000013116
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013117 // OpenMP [teams Constrcut, Restrictions]
13118 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000013119 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
Alexey Bataeva0569352015-12-01 10:17:31 +000013120 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013121 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000013122
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000013123 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000013124 OpenMPDirectiveKind CaptureRegion =
13125 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams);
13126 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000013127 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000013128 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000013129 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
13130 HelperValStmt = buildPreInits(Context, Captures);
13131 }
13132
13133 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion,
13134 StartLoc, LParenLoc, EndLoc);
Kelvin Li099bb8c2015-11-24 20:50:12 +000013135}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013136
13137OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
13138 SourceLocation StartLoc,
13139 SourceLocation LParenLoc,
13140 SourceLocation EndLoc) {
13141 Expr *ValExpr = ThreadLimit;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000013142 Stmt *HelperValStmt = nullptr;
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013143
13144 // OpenMP [teams Constrcut, Restrictions]
13145 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000013146 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
Alexey Bataeva0569352015-12-01 10:17:31 +000013147 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013148 return nullptr;
13149
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000013150 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000013151 OpenMPDirectiveKind CaptureRegion =
13152 getOpenMPCaptureRegionForClause(DKind, OMPC_thread_limit);
13153 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000013154 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000013155 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000013156 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
13157 HelperValStmt = buildPreInits(Context, Captures);
13158 }
13159
13160 return new (Context) OMPThreadLimitClause(
13161 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013162}
Alexey Bataeva0569352015-12-01 10:17:31 +000013163
13164OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
13165 SourceLocation StartLoc,
13166 SourceLocation LParenLoc,
13167 SourceLocation EndLoc) {
13168 Expr *ValExpr = Priority;
13169
13170 // OpenMP [2.9.1, task Constrcut]
13171 // The priority-value is a non-negative numerical scalar expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000013172 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
Alexey Bataeva0569352015-12-01 10:17:31 +000013173 /*StrictlyPositive=*/false))
13174 return nullptr;
13175
13176 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
13177}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000013178
13179OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
13180 SourceLocation StartLoc,
13181 SourceLocation LParenLoc,
13182 SourceLocation EndLoc) {
13183 Expr *ValExpr = Grainsize;
13184
13185 // OpenMP [2.9.2, taskloop Constrcut]
13186 // The parameter of the grainsize clause must be a positive integer
13187 // expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000013188 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000013189 /*StrictlyPositive=*/true))
13190 return nullptr;
13191
13192 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
13193}
Alexey Bataev382967a2015-12-08 12:06:20 +000013194
13195OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
13196 SourceLocation StartLoc,
13197 SourceLocation LParenLoc,
13198 SourceLocation EndLoc) {
13199 Expr *ValExpr = NumTasks;
13200
13201 // OpenMP [2.9.2, taskloop Constrcut]
13202 // The parameter of the num_tasks clause must be a positive integer
13203 // expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000013204 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
Alexey Bataev382967a2015-12-08 12:06:20 +000013205 /*StrictlyPositive=*/true))
13206 return nullptr;
13207
13208 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
13209}
13210
Alexey Bataev28c75412015-12-15 08:19:24 +000013211OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
13212 SourceLocation LParenLoc,
13213 SourceLocation EndLoc) {
13214 // OpenMP [2.13.2, critical construct, Description]
13215 // ... where hint-expression is an integer constant expression that evaluates
13216 // to a valid lock hint.
13217 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
13218 if (HintExpr.isInvalid())
13219 return nullptr;
13220 return new (Context)
13221 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
13222}
13223
Carlo Bertollib4adf552016-01-15 18:50:31 +000013224OMPClause *Sema::ActOnOpenMPDistScheduleClause(
13225 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
13226 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
13227 SourceLocation EndLoc) {
13228 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
13229 std::string Values;
13230 Values += "'";
13231 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
13232 Values += "'";
13233 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
13234 << Values << getOpenMPClauseName(OMPC_dist_schedule);
13235 return nullptr;
13236 }
13237 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000013238 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000013239 if (ChunkSize) {
13240 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
13241 !ChunkSize->isInstantiationDependent() &&
13242 !ChunkSize->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000013243 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
Carlo Bertollib4adf552016-01-15 18:50:31 +000013244 ExprResult Val =
13245 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
13246 if (Val.isInvalid())
13247 return nullptr;
13248
13249 ValExpr = Val.get();
13250
13251 // OpenMP [2.7.1, Restrictions]
13252 // chunk_size must be a loop invariant integer expression with a positive
13253 // value.
13254 llvm::APSInt Result;
13255 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
13256 if (Result.isSigned() && !Result.isStrictlyPositive()) {
13257 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
13258 << "dist_schedule" << ChunkSize->getSourceRange();
13259 return nullptr;
13260 }
Alexey Bataev2ba67042017-11-28 21:11:44 +000013261 } else if (getOpenMPCaptureRegionForClause(
13262 DSAStack->getCurrentDirective(), OMPC_dist_schedule) !=
13263 OMPD_unknown &&
Alexey Bataevb46cdea2016-06-15 11:20:48 +000013264 !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000013265 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000013266 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev5a3af132016-03-29 08:58:54 +000013267 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
13268 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000013269 }
13270 }
13271 }
13272
13273 return new (Context)
13274 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000013275 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000013276}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013277
13278OMPClause *Sema::ActOnOpenMPDefaultmapClause(
13279 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
13280 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
13281 SourceLocation KindLoc, SourceLocation EndLoc) {
13282 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
David Majnemer9d168222016-08-05 17:44:54 +000013283 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) {
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013284 std::string Value;
13285 SourceLocation Loc;
13286 Value += "'";
13287 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
13288 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000013289 OMPC_DEFAULTMAP_MODIFIER_tofrom);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013290 Loc = MLoc;
13291 } else {
13292 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000013293 OMPC_DEFAULTMAP_scalar);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013294 Loc = KindLoc;
13295 }
13296 Value += "'";
13297 Diag(Loc, diag::err_omp_unexpected_clause_value)
13298 << Value << getOpenMPClauseName(OMPC_defaultmap);
13299 return nullptr;
13300 }
Alexey Bataev2fd0cb22017-10-05 17:51:39 +000013301 DSAStack->setDefaultDMAToFromScalar(StartLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013302
13303 return new (Context)
13304 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
13305}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013306
13307bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
13308 DeclContext *CurLexicalContext = getCurLexicalContext();
13309 if (!CurLexicalContext->isFileContext() &&
13310 !CurLexicalContext->isExternCContext() &&
Alexey Bataev502ec492017-10-03 20:00:00 +000013311 !CurLexicalContext->isExternCXXContext() &&
13312 !isa<CXXRecordDecl>(CurLexicalContext) &&
13313 !isa<ClassTemplateDecl>(CurLexicalContext) &&
13314 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) &&
13315 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013316 Diag(Loc, diag::err_omp_region_not_file_context);
13317 return false;
13318 }
Kelvin Libc38e632018-09-10 02:07:09 +000013319 ++DeclareTargetNestingLevel;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013320 return true;
13321}
13322
13323void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
Kelvin Libc38e632018-09-10 02:07:09 +000013324 assert(DeclareTargetNestingLevel > 0 &&
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013325 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
Kelvin Libc38e632018-09-10 02:07:09 +000013326 --DeclareTargetNestingLevel;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013327}
13328
David Majnemer9d168222016-08-05 17:44:54 +000013329void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope,
13330 CXXScopeSpec &ScopeSpec,
13331 const DeclarationNameInfo &Id,
13332 OMPDeclareTargetDeclAttr::MapTypeTy MT,
13333 NamedDeclSetType &SameDirectiveDecls) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013334 LookupResult Lookup(*this, Id, LookupOrdinaryName);
13335 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
13336
13337 if (Lookup.isAmbiguous())
13338 return;
13339 Lookup.suppressDiagnostics();
13340
13341 if (!Lookup.isSingleResult()) {
13342 if (TypoCorrection Corrected =
13343 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
13344 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
13345 CTK_ErrorRecovery)) {
13346 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
13347 << Id.getName());
13348 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
13349 return;
13350 }
13351
13352 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
13353 return;
13354 }
13355
13356 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
Alexey Bataev30a78212018-09-11 13:59:10 +000013357 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND) ||
13358 isa<FunctionTemplateDecl>(ND)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013359 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
13360 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
Alexey Bataev30a78212018-09-11 13:59:10 +000013361 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
13362 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
13363 cast<ValueDecl>(ND));
13364 if (!Res) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013365 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013366 ND->addAttr(A);
13367 if (ASTMutationListener *ML = Context.getASTMutationListener())
13368 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
Kelvin Li1ce87c72017-12-12 20:08:12 +000013369 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Id.getLoc());
Alexey Bataev30a78212018-09-11 13:59:10 +000013370 } else if (*Res != MT) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013371 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
13372 << Id.getName();
13373 }
Alexey Bataeve3727102018-04-18 15:57:46 +000013374 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013375 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
Alexey Bataeve3727102018-04-18 15:57:46 +000013376 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013377}
13378
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013379static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
13380 Sema &SemaRef, Decl *D) {
Alexey Bataev30a78212018-09-11 13:59:10 +000013381 if (!D || !isa<VarDecl>(D))
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013382 return;
Alexey Bataev30a78212018-09-11 13:59:10 +000013383 auto *VD = cast<VarDecl>(D);
13384 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
13385 return;
13386 SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context);
13387 SemaRef.Diag(SL, diag::note_used_here) << SR;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013388}
13389
13390static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
13391 Sema &SemaRef, DSAStackTy *Stack,
13392 ValueDecl *VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013393 return VD->hasAttr<OMPDeclareTargetDeclAttr>() ||
13394 checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(),
13395 /*FullCheck=*/false);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013396}
13397
Kelvin Li1ce87c72017-12-12 20:08:12 +000013398void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D,
13399 SourceLocation IdLoc) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013400 if (!D || D->isInvalidDecl())
13401 return;
13402 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
Stephen Kellyf2ceec42018-08-09 21:08:08 +000013403 SourceLocation SL = E ? E->getBeginLoc() : D->getLocation();
Alexey Bataeve3727102018-04-18 15:57:46 +000013404 if (auto *VD = dyn_cast<VarDecl>(D)) {
Alexey Bataevc1943e72018-07-09 19:58:08 +000013405 // Only global variables can be marked as declare target.
Alexey Bataev30a78212018-09-11 13:59:10 +000013406 if (!VD->isFileVarDecl() && !VD->isStaticLocal() &&
13407 !VD->isStaticDataMember())
Alexey Bataevc1943e72018-07-09 19:58:08 +000013408 return;
13409 // 2.10.6: threadprivate variable cannot appear in a declare target
13410 // directive.
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013411 if (DSAStack->isThreadPrivate(VD)) {
13412 Diag(SL, diag::err_omp_threadprivate_in_target);
Alexey Bataeve3727102018-04-18 15:57:46 +000013413 reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013414 return;
13415 }
13416 }
Alexey Bataev97b72212018-08-14 18:31:20 +000013417 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
13418 D = FTD->getTemplatedDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000013419 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Alexey Bataev30a78212018-09-11 13:59:10 +000013420 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
13421 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD);
13422 if (Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) {
Kelvin Li1ce87c72017-12-12 20:08:12 +000013423 assert(IdLoc.isValid() && "Source location is expected");
13424 Diag(IdLoc, diag::err_omp_function_in_link_clause);
13425 Diag(FD->getLocation(), diag::note_defined_here) << FD;
13426 return;
13427 }
13428 }
Alexey Bataev30a78212018-09-11 13:59:10 +000013429 if (auto *VD = dyn_cast<ValueDecl>(D)) {
13430 // Problem if any with var declared with incomplete type will be reported
13431 // as normal, so no need to check it here.
13432 if ((E || !VD->getType()->isIncompleteType()) &&
13433 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD))
13434 return;
13435 if (!E && !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) {
13436 // Checking declaration inside declare target region.
13437 if (isa<VarDecl>(D) || isa<FunctionDecl>(D) ||
13438 isa<FunctionTemplateDecl>(D)) {
13439 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
13440 Context, OMPDeclareTargetDeclAttr::MT_To);
13441 D->addAttr(A);
13442 if (ASTMutationListener *ML = Context.getASTMutationListener())
13443 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
13444 }
13445 return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013446 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013447 }
Alexey Bataev30a78212018-09-11 13:59:10 +000013448 if (!E)
13449 return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013450 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
13451}
Samuel Antao661c0902016-05-26 17:39:58 +000013452
13453OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
13454 SourceLocation StartLoc,
13455 SourceLocation LParenLoc,
13456 SourceLocation EndLoc) {
13457 MappableVarListInfo MVLI(VarList);
13458 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc);
13459 if (MVLI.ProcessedVarList.empty())
13460 return nullptr;
13461
13462 return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13463 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
13464 MVLI.VarComponents);
13465}
Samuel Antaoec172c62016-05-26 17:49:04 +000013466
13467OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
13468 SourceLocation StartLoc,
13469 SourceLocation LParenLoc,
13470 SourceLocation EndLoc) {
13471 MappableVarListInfo MVLI(VarList);
13472 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc);
13473 if (MVLI.ProcessedVarList.empty())
13474 return nullptr;
13475
13476 return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13477 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
13478 MVLI.VarComponents);
13479}
Carlo Bertolli2404b172016-07-13 15:37:16 +000013480
13481OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
13482 SourceLocation StartLoc,
13483 SourceLocation LParenLoc,
13484 SourceLocation EndLoc) {
Samuel Antaocc10b852016-07-28 14:23:26 +000013485 MappableVarListInfo MVLI(VarList);
13486 SmallVector<Expr *, 8> PrivateCopies;
13487 SmallVector<Expr *, 8> Inits;
13488
Alexey Bataeve3727102018-04-18 15:57:46 +000013489 for (Expr *RefExpr : VarList) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000013490 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
13491 SourceLocation ELoc;
13492 SourceRange ERange;
13493 Expr *SimpleRefExpr = RefExpr;
13494 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
13495 if (Res.second) {
13496 // It will be analyzed later.
Samuel Antaocc10b852016-07-28 14:23:26 +000013497 MVLI.ProcessedVarList.push_back(RefExpr);
13498 PrivateCopies.push_back(nullptr);
13499 Inits.push_back(nullptr);
Carlo Bertolli2404b172016-07-13 15:37:16 +000013500 }
13501 ValueDecl *D = Res.first;
13502 if (!D)
13503 continue;
13504
13505 QualType Type = D->getType();
Samuel Antaocc10b852016-07-28 14:23:26 +000013506 Type = Type.getNonReferenceType().getUnqualifiedType();
13507
13508 auto *VD = dyn_cast<VarDecl>(D);
13509
13510 // Item should be a pointer or reference to pointer.
13511 if (!Type->isPointerType()) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000013512 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
13513 << 0 << RefExpr->getSourceRange();
13514 continue;
13515 }
Samuel Antaocc10b852016-07-28 14:23:26 +000013516
13517 // Build the private variable and the expression that refers to it.
Alexey Bataev63cc8e92018-03-20 14:45:59 +000013518 auto VDPrivate =
13519 buildVarDecl(*this, ELoc, Type, D->getName(),
13520 D->hasAttrs() ? &D->getAttrs() : nullptr,
13521 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Samuel Antaocc10b852016-07-28 14:23:26 +000013522 if (VDPrivate->isInvalidDecl())
13523 continue;
13524
13525 CurContext->addDecl(VDPrivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000013526 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Samuel Antaocc10b852016-07-28 14:23:26 +000013527 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
13528
13529 // Add temporary variable to initialize the private copy of the pointer.
Alexey Bataeve3727102018-04-18 15:57:46 +000013530 VarDecl *VDInit =
Samuel Antaocc10b852016-07-28 14:23:26 +000013531 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp");
Alexey Bataeve3727102018-04-18 15:57:46 +000013532 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr(
13533 *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc());
Samuel Antaocc10b852016-07-28 14:23:26 +000013534 AddInitializerToDecl(VDPrivate,
13535 DefaultLvalueConversion(VDInitRefExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000013536 /*DirectInit=*/false);
Samuel Antaocc10b852016-07-28 14:23:26 +000013537
13538 // If required, build a capture to implement the privatization initialized
13539 // with the current list item value.
13540 DeclRefExpr *Ref = nullptr;
13541 if (!VD)
13542 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
13543 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
13544 PrivateCopies.push_back(VDPrivateRefExpr);
13545 Inits.push_back(VDInitRefExpr);
13546
13547 // We need to add a data sharing attribute for this variable to make sure it
13548 // is correctly captured. A variable that shows up in a use_device_ptr has
13549 // similar properties of a first private variable.
13550 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
13551
13552 // Create a mappable component for the list item. List items in this clause
13553 // only need a component.
13554 MVLI.VarBaseDeclarations.push_back(D);
13555 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13556 MVLI.VarComponents.back().push_back(
13557 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D));
Carlo Bertolli2404b172016-07-13 15:37:16 +000013558 }
13559
Samuel Antaocc10b852016-07-28 14:23:26 +000013560 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli2404b172016-07-13 15:37:16 +000013561 return nullptr;
13562
Samuel Antaocc10b852016-07-28 14:23:26 +000013563 return OMPUseDevicePtrClause::Create(
13564 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
13565 PrivateCopies, Inits, MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli2404b172016-07-13 15:37:16 +000013566}
Carlo Bertolli70594e92016-07-13 17:16:49 +000013567
13568OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
13569 SourceLocation StartLoc,
13570 SourceLocation LParenLoc,
13571 SourceLocation EndLoc) {
Samuel Antao6890b092016-07-28 14:25:09 +000013572 MappableVarListInfo MVLI(VarList);
Alexey Bataeve3727102018-04-18 15:57:46 +000013573 for (Expr *RefExpr : VarList) {
Kelvin Li84376252016-12-14 15:39:58 +000013574 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause.");
Carlo Bertolli70594e92016-07-13 17:16:49 +000013575 SourceLocation ELoc;
13576 SourceRange ERange;
13577 Expr *SimpleRefExpr = RefExpr;
13578 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
13579 if (Res.second) {
13580 // It will be analyzed later.
Samuel Antao6890b092016-07-28 14:25:09 +000013581 MVLI.ProcessedVarList.push_back(RefExpr);
Carlo Bertolli70594e92016-07-13 17:16:49 +000013582 }
13583 ValueDecl *D = Res.first;
13584 if (!D)
13585 continue;
13586
13587 QualType Type = D->getType();
13588 // item should be a pointer or array or reference to pointer or array
13589 if (!Type.getNonReferenceType()->isPointerType() &&
13590 !Type.getNonReferenceType()->isArrayType()) {
13591 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
13592 << 0 << RefExpr->getSourceRange();
13593 continue;
13594 }
Samuel Antao6890b092016-07-28 14:25:09 +000013595
13596 // Check if the declaration in the clause does not show up in any data
13597 // sharing attribute.
Alexey Bataeve3727102018-04-18 15:57:46 +000013598 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Samuel Antao6890b092016-07-28 14:25:09 +000013599 if (isOpenMPPrivate(DVar.CKind)) {
13600 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
13601 << getOpenMPClauseName(DVar.CKind)
13602 << getOpenMPClauseName(OMPC_is_device_ptr)
13603 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000013604 reportOriginalDsa(*this, DSAStack, D, DVar);
Samuel Antao6890b092016-07-28 14:25:09 +000013605 continue;
13606 }
13607
Alexey Bataeve3727102018-04-18 15:57:46 +000013608 const Expr *ConflictExpr;
Samuel Antao6890b092016-07-28 14:25:09 +000013609 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +000013610 D, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +000013611 [&ConflictExpr](
13612 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
13613 OpenMPClauseKind) -> bool {
13614 ConflictExpr = R.front().getAssociatedExpression();
13615 return true;
13616 })) {
13617 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
13618 Diag(ConflictExpr->getExprLoc(), diag::note_used_here)
13619 << ConflictExpr->getSourceRange();
13620 continue;
13621 }
13622
13623 // Store the components in the stack so that they can be used to check
13624 // against other clauses later on.
13625 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D);
13626 DSAStack->addMappableExpressionComponents(
13627 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
13628
13629 // Record the expression we've just processed.
13630 MVLI.ProcessedVarList.push_back(SimpleRefExpr);
13631
13632 // Create a mappable component for the list item. List items in this clause
13633 // only need a component. We use a null declaration to signal fields in
13634 // 'this'.
13635 assert((isa<DeclRefExpr>(SimpleRefExpr) ||
13636 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
13637 "Unexpected device pointer expression!");
13638 MVLI.VarBaseDeclarations.push_back(
13639 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr);
13640 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13641 MVLI.VarComponents.back().push_back(MC);
Carlo Bertolli70594e92016-07-13 17:16:49 +000013642 }
13643
Samuel Antao6890b092016-07-28 14:25:09 +000013644 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli70594e92016-07-13 17:16:49 +000013645 return nullptr;
13646
Samuel Antao6890b092016-07-28 14:25:09 +000013647 return OMPIsDevicePtrClause::Create(
13648 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
13649 MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli70594e92016-07-13 17:16:49 +000013650}