blob: 92d1514ee0c8aa6a25a4184d1b283b4b23590102 [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 Bataev7ace49d2016-05-17 08:55:33 +0000141 bool NowaitRegion = false;
142 bool CancelRegion = false;
143 unsigned AssociatedLoops = 1;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000144 SourceLocation InnerTeamsRegionLoc;
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000145 /// Reference to the taskgroup task_reduction reference expression.
146 Expr *TaskgroupReductionRef = nullptr;
Alexey Bataeved09d242014-05-28 05:53:51 +0000147 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000148 Scope *CurScope, SourceLocation Loc)
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000149 : Directive(DKind), DirectiveName(Name), CurScope(CurScope),
150 ConstructLoc(Loc) {}
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000151 SharingMapTy() = default;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000152 };
153
Alexey Bataeve3727102018-04-18 15:57:46 +0000154 using StackTy = SmallVector<SharingMapTy, 4>;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000155
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000156 /// Stack of used declaration and their data-sharing attributes.
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000157 DeclSAMapTy Threadprivates;
Alexey Bataev4b465392017-04-26 15:06:24 +0000158 const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr;
159 SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000160 /// true, if check for DSA must be from parent directive, false, if
Alexey Bataev39f915b82015-05-08 10:41:21 +0000161 /// from current directive.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000162 OpenMPClauseKind ClauseKindMode = OMPC_unknown;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000163 Sema &SemaRef;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000164 bool ForceCapturing = false;
Alexey Bataev28c75412015-12-15 08:19:24 +0000165 CriticalsWithHintsTy Criticals;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000166
Alexey Bataeve3727102018-04-18 15:57:46 +0000167 using iterator = StackTy::const_reverse_iterator;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000168
Alexey Bataeve3727102018-04-18 15:57:46 +0000169 DSAVarData getDSA(iterator &Iter, ValueDecl *D) const;
Alexey Bataevec3da872014-01-31 05:15:34 +0000170
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000171 /// Checks if the variable is a local for OpenMP region.
Alexey Bataeve3727102018-04-18 15:57:46 +0000172 bool isOpenMPLocal(VarDecl *D, iterator Iter) const;
Alexey Bataeved09d242014-05-28 05:53:51 +0000173
Alexey Bataev4b465392017-04-26 15:06:24 +0000174 bool isStackEmpty() const {
175 return Stack.empty() ||
176 Stack.back().second != CurrentNonCapturingFunctionScope ||
177 Stack.back().first.empty();
178 }
179
Kelvin Li1408f912018-09-26 04:28:39 +0000180 /// Vector of previously declared requires directives
181 SmallVector<const OMPRequiresDecl *, 2> RequiresDecls;
182
Alexey Bataev758e55e2013-09-06 18:03:48 +0000183public:
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000184 explicit DSAStackTy(Sema &S) : SemaRef(S) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000185
Alexey Bataevaac108a2015-06-23 04:51:00 +0000186 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
Alexey Bataev3f82cfc2017-12-13 15:28:44 +0000187 OpenMPClauseKind getClauseParsingMode() const {
188 assert(isClauseParsingMode() && "Must be in clause parsing mode.");
189 return ClauseKindMode;
190 }
Alexey Bataevaac108a2015-06-23 04:51:00 +0000191 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000192
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000193 bool isForceVarCapturing() const { return ForceCapturing; }
194 void setForceVarCapturing(bool V) { ForceCapturing = V; }
195
Alexey Bataev758e55e2013-09-06 18:03:48 +0000196 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000197 Scope *CurScope, SourceLocation Loc) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000198 if (Stack.empty() ||
199 Stack.back().second != CurrentNonCapturingFunctionScope)
200 Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope);
201 Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc);
202 Stack.back().first.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000203 }
204
205 void pop() {
Alexey Bataev4b465392017-04-26 15:06:24 +0000206 assert(!Stack.back().first.empty() &&
207 "Data-sharing attributes stack is empty!");
208 Stack.back().first.pop_back();
209 }
210
211 /// Start new OpenMP region stack in new non-capturing function.
212 void pushFunction() {
213 const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction();
214 assert(!isa<CapturingScopeInfo>(CurFnScope));
215 CurrentNonCapturingFunctionScope = CurFnScope;
216 }
217 /// Pop region stack for non-capturing function.
218 void popFunction(const FunctionScopeInfo *OldFSI) {
219 if (!Stack.empty() && Stack.back().second == OldFSI) {
220 assert(Stack.back().first.empty());
221 Stack.pop_back();
222 }
223 CurrentNonCapturingFunctionScope = nullptr;
224 for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) {
225 if (!isa<CapturingScopeInfo>(FSI)) {
226 CurrentNonCapturingFunctionScope = FSI;
227 break;
228 }
229 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000230 }
231
Alexey Bataeve3727102018-04-18 15:57:46 +0000232 void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) {
Alexey Bataev43a919f2018-04-13 17:48:43 +0000233 Criticals.try_emplace(D->getDirectiveName().getAsString(), D, Hint);
Alexey Bataev28c75412015-12-15 08:19:24 +0000234 }
Alexey Bataeve3727102018-04-18 15:57:46 +0000235 const std::pair<const OMPCriticalDirective *, llvm::APSInt>
Alexey Bataev28c75412015-12-15 08:19:24 +0000236 getCriticalWithHint(const DeclarationNameInfo &Name) const {
237 auto I = Criticals.find(Name.getAsString());
238 if (I != Criticals.end())
239 return I->second;
240 return std::make_pair(nullptr, llvm::APSInt());
241 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000242 /// If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000243 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000244 /// for diagnostics.
Alexey Bataeve3727102018-04-18 15:57:46 +0000245 const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000246
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000247 /// Register specified variable as loop control variable.
Alexey Bataeve3727102018-04-18 15:57:46 +0000248 void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000249 /// Check if the specified variable is a loop control variable for
Alexey Bataev9c821032015-04-30 04:23:23 +0000250 /// current region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000251 /// \return The index of the loop control variable in the list of associated
252 /// for-loops (from outer to inner).
Alexey Bataeve3727102018-04-18 15:57:46 +0000253 const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000254 /// Check if the specified variable is a loop control variable for
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000255 /// parent region.
256 /// \return The index of the loop control variable in the list of associated
257 /// for-loops (from outer to inner).
Alexey Bataeve3727102018-04-18 15:57:46 +0000258 const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000259 /// Get the loop control variable for the I-th loop (or nullptr) in
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000260 /// parent directive.
Alexey Bataeve3727102018-04-18 15:57:46 +0000261 const ValueDecl *getParentLoopControlVariable(unsigned I) const;
Alexey Bataev9c821032015-04-30 04:23:23 +0000262
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000263 /// Adds explicit data sharing attribute to the specified declaration.
Alexey Bataeve3727102018-04-18 15:57:46 +0000264 void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
Alexey Bataev90c228f2016-02-08 09:29:13 +0000265 DeclRefExpr *PrivateCopy = nullptr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000266
Alexey Bataevfa312f32017-07-21 18:48:21 +0000267 /// Adds additional information for the reduction items with the reduction id
268 /// represented as an operator.
Alexey Bataeve3727102018-04-18 15:57:46 +0000269 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000270 BinaryOperatorKind BOK);
Alexey Bataevfa312f32017-07-21 18:48:21 +0000271 /// Adds additional information for the reduction items with the reduction id
272 /// represented as reduction identifier.
Alexey Bataeve3727102018-04-18 15:57:46 +0000273 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000274 const Expr *ReductionRef);
Alexey Bataevfa312f32017-07-21 18:48:21 +0000275 /// Returns the location and reduction operation from the innermost parent
276 /// region for the given \p D.
Alexey Bataeve3727102018-04-18 15:57:46 +0000277 const DSAVarData
278 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
279 BinaryOperatorKind &BOK,
280 Expr *&TaskgroupDescriptor) const;
Alexey Bataevfa312f32017-07-21 18:48:21 +0000281 /// Returns the location and reduction operation from the innermost parent
282 /// region for the given \p D.
Alexey Bataeve3727102018-04-18 15:57:46 +0000283 const DSAVarData
284 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
285 const Expr *&ReductionRef,
286 Expr *&TaskgroupDescriptor) const;
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000287 /// Return reduction reference expression for the current taskgroup.
288 Expr *getTaskgroupReductionRef() const {
289 assert(Stack.back().first.back().Directive == OMPD_taskgroup &&
290 "taskgroup reference expression requested for non taskgroup "
291 "directive.");
292 return Stack.back().first.back().TaskgroupReductionRef;
293 }
Alexey Bataev88202be2017-07-27 13:20:36 +0000294 /// Checks if the given \p VD declaration is actually a taskgroup reduction
295 /// descriptor variable at the \p Level of OpenMP regions.
Alexey Bataeve3727102018-04-18 15:57:46 +0000296 bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const {
Alexey Bataev88202be2017-07-27 13:20:36 +0000297 return Stack.back().first[Level].TaskgroupReductionRef &&
298 cast<DeclRefExpr>(Stack.back().first[Level].TaskgroupReductionRef)
299 ->getDecl() == VD;
300 }
Alexey Bataevfa312f32017-07-21 18:48:21 +0000301
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000302 /// Returns data sharing attributes from top of the stack for the
Alexey Bataev758e55e2013-09-06 18:03:48 +0000303 /// specified declaration.
Alexey Bataeve3727102018-04-18 15:57:46 +0000304 const DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000305 /// Returns data-sharing attributes for the specified declaration.
Alexey Bataeve3727102018-04-18 15:57:46 +0000306 const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000307 /// Checks if the specified variables has data-sharing attributes which
Alexey Bataevf29276e2014-06-18 04:14:57 +0000308 /// match specified \a CPred predicate in any directive which matches \a DPred
309 /// predicate.
Alexey Bataeve3727102018-04-18 15:57:46 +0000310 const DSAVarData
311 hasDSA(ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
312 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
313 bool FromParent) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000314 /// Checks if the specified variables has data-sharing attributes which
Alexey Bataevf29276e2014-06-18 04:14:57 +0000315 /// match specified \a CPred predicate in any innermost directive which
316 /// matches \a DPred predicate.
Alexey Bataeve3727102018-04-18 15:57:46 +0000317 const DSAVarData
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000318 hasInnermostDSA(ValueDecl *D,
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000319 const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
320 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +0000321 bool FromParent) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000322 /// Checks if the specified variables has explicit data-sharing
Alexey Bataevaac108a2015-06-23 04:51:00 +0000323 /// attributes which match specified \a CPred predicate at the specified
324 /// OpenMP region.
Alexey Bataeve3727102018-04-18 15:57:46 +0000325 bool hasExplicitDSA(const ValueDecl *D,
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000326 const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
Alexey Bataeve3727102018-04-18 15:57:46 +0000327 unsigned Level, bool NotLastprivate = false) const;
Samuel Antao4be30e92015-10-02 17:14:03 +0000328
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000329 /// Returns true if the directive at level \Level matches in the
Samuel Antao4be30e92015-10-02 17:14:03 +0000330 /// specified \a DPred predicate.
331 bool hasExplicitDirective(
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000332 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +0000333 unsigned Level) const;
Samuel Antao4be30e92015-10-02 17:14:03 +0000334
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000335 /// Finds a directive which matches specified \a DPred predicate.
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000336 bool hasDirective(
337 const llvm::function_ref<bool(
338 OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)>
339 DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +0000340 bool FromParent) const;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000341
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000342 /// Returns currently analyzed directive.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000343 OpenMPDirectiveKind getCurrentDirective() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000344 return isStackEmpty() ? OMPD_unknown : Stack.back().first.back().Directive;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000345 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000346 /// Returns directive kind at specified level.
Alexey Bataevdfa430f2017-12-08 15:03:50 +0000347 OpenMPDirectiveKind getDirective(unsigned Level) const {
348 assert(!isStackEmpty() && "No directive at specified level.");
349 return Stack.back().first[Level].Directive;
350 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000351 /// Returns parent directive.
Alexey Bataev549210e2014-06-24 04:39:47 +0000352 OpenMPDirectiveKind getParentDirective() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000353 if (isStackEmpty() || Stack.back().first.size() == 1)
354 return OMPD_unknown;
355 return std::next(Stack.back().first.rbegin())->Directive;
Alexey Bataev549210e2014-06-24 04:39:47 +0000356 }
Kelvin Li1408f912018-09-26 04:28:39 +0000357
358 /// Add requires decl to internal vector
359 void addRequiresDecl(OMPRequiresDecl *RD) {
360 RequiresDecls.push_back(RD);
361 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000362
Kelvin Li1408f912018-09-26 04:28:39 +0000363 /// Checks for a duplicate clause amongst previously declared requires
364 /// directives
365 bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const {
366 bool IsDuplicate = false;
367 for (OMPClause *CNew : ClauseList) {
368 for (const OMPRequiresDecl *D : RequiresDecls) {
369 for (const OMPClause *CPrev : D->clauselists()) {
370 if (CNew->getClauseKind() == CPrev->getClauseKind()) {
371 SemaRef.Diag(CNew->getBeginLoc(),
372 diag::err_omp_requires_clause_redeclaration)
373 << getOpenMPClauseName(CNew->getClauseKind());
374 SemaRef.Diag(CPrev->getBeginLoc(),
375 diag::note_omp_requires_previous_clause)
376 << getOpenMPClauseName(CPrev->getClauseKind());
377 IsDuplicate = true;
378 }
379 }
380 }
381 }
382 return IsDuplicate;
383 }
384
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000385 /// Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000386 void setDefaultDSANone(SourceLocation Loc) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000387 assert(!isStackEmpty());
388 Stack.back().first.back().DefaultAttr = DSA_none;
389 Stack.back().first.back().DefaultAttrLoc = Loc;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000390 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000391 /// Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000392 void setDefaultDSAShared(SourceLocation Loc) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000393 assert(!isStackEmpty());
394 Stack.back().first.back().DefaultAttr = DSA_shared;
395 Stack.back().first.back().DefaultAttrLoc = Loc;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000396 }
Alexey Bataev2fd0cb22017-10-05 17:51:39 +0000397 /// Set default data mapping attribute to 'tofrom:scalar'.
398 void setDefaultDMAToFromScalar(SourceLocation Loc) {
399 assert(!isStackEmpty());
400 Stack.back().first.back().DefaultMapAttr = DMA_tofrom_scalar;
401 Stack.back().first.back().DefaultMapAttrLoc = Loc;
402 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000403
404 DefaultDataSharingAttributes getDefaultDSA() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000405 return isStackEmpty() ? DSA_unspecified
406 : Stack.back().first.back().DefaultAttr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000407 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000408 SourceLocation getDefaultDSALocation() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000409 return isStackEmpty() ? SourceLocation()
410 : Stack.back().first.back().DefaultAttrLoc;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000411 }
Alexey Bataev2fd0cb22017-10-05 17:51:39 +0000412 DefaultMapAttributes getDefaultDMA() const {
413 return isStackEmpty() ? DMA_unspecified
414 : Stack.back().first.back().DefaultMapAttr;
415 }
416 DefaultMapAttributes getDefaultDMAAtLevel(unsigned Level) const {
417 return Stack.back().first[Level].DefaultMapAttr;
418 }
419 SourceLocation getDefaultDMALocation() const {
420 return isStackEmpty() ? SourceLocation()
421 : Stack.back().first.back().DefaultMapAttrLoc;
422 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000423
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000424 /// Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000425 bool isThreadPrivate(VarDecl *D) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000426 const DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000427 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000428 }
429
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000430 /// Marks current region as ordered (it has an 'ordered' clause).
Alexey Bataevf138fda2018-08-13 19:04:24 +0000431 void setOrderedRegion(bool IsOrdered, const Expr *Param,
432 OMPOrderedClause *Clause) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000433 assert(!isStackEmpty());
Alexey Bataevf138fda2018-08-13 19:04:24 +0000434 if (IsOrdered)
435 Stack.back().first.back().OrderedRegion.emplace(Param, Clause);
436 else
437 Stack.back().first.back().OrderedRegion.reset();
438 }
439 /// Returns true, if region is ordered (has associated 'ordered' clause),
440 /// false - otherwise.
441 bool isOrderedRegion() const {
442 if (isStackEmpty())
443 return false;
444 return Stack.back().first.rbegin()->OrderedRegion.hasValue();
445 }
446 /// Returns optional parameter for the ordered region.
447 std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const {
448 if (isStackEmpty() ||
449 !Stack.back().first.rbegin()->OrderedRegion.hasValue())
450 return std::make_pair(nullptr, nullptr);
451 return Stack.back().first.rbegin()->OrderedRegion.getValue();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000452 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000453 /// Returns true, if parent region is ordered (has associated
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000454 /// 'ordered' clause), false - otherwise.
455 bool isParentOrderedRegion() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000456 if (isStackEmpty() || Stack.back().first.size() == 1)
457 return false;
Alexey Bataevf138fda2018-08-13 19:04:24 +0000458 return std::next(Stack.back().first.rbegin())->OrderedRegion.hasValue();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000459 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000460 /// Returns optional parameter for the ordered region.
Alexey Bataevf138fda2018-08-13 19:04:24 +0000461 std::pair<const Expr *, OMPOrderedClause *>
462 getParentOrderedRegionParam() const {
463 if (isStackEmpty() || Stack.back().first.size() == 1 ||
464 !std::next(Stack.back().first.rbegin())->OrderedRegion.hasValue())
465 return std::make_pair(nullptr, nullptr);
466 return std::next(Stack.back().first.rbegin())->OrderedRegion.getValue();
Alexey Bataev346265e2015-09-25 10:37:12 +0000467 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000468 /// Marks current region as nowait (it has a 'nowait' clause).
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000469 void setNowaitRegion(bool IsNowait = true) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000470 assert(!isStackEmpty());
471 Stack.back().first.back().NowaitRegion = IsNowait;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000472 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000473 /// Returns true, if parent region is nowait (has associated
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000474 /// 'nowait' clause), false - otherwise.
475 bool isParentNowaitRegion() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000476 if (isStackEmpty() || Stack.back().first.size() == 1)
477 return false;
478 return std::next(Stack.back().first.rbegin())->NowaitRegion;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000479 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000480 /// Marks parent region as cancel region.
Alexey Bataev25e5b442015-09-15 12:52:43 +0000481 void setParentCancelRegion(bool Cancel = true) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000482 if (!isStackEmpty() && Stack.back().first.size() > 1) {
483 auto &StackElemRef = *std::next(Stack.back().first.rbegin());
484 StackElemRef.CancelRegion |= StackElemRef.CancelRegion || Cancel;
485 }
Alexey Bataev25e5b442015-09-15 12:52:43 +0000486 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000487 /// Return true if current region has inner cancel construct.
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000488 bool isCancelRegion() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000489 return isStackEmpty() ? false : Stack.back().first.back().CancelRegion;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000490 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000491
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000492 /// Set collapse value for the region.
Alexey Bataev4b465392017-04-26 15:06:24 +0000493 void setAssociatedLoops(unsigned Val) {
494 assert(!isStackEmpty());
495 Stack.back().first.back().AssociatedLoops = Val;
496 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000497 /// Return collapse value for region.
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000498 unsigned getAssociatedLoops() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000499 return isStackEmpty() ? 0 : Stack.back().first.back().AssociatedLoops;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000500 }
Alexey Bataev9c821032015-04-30 04:23:23 +0000501
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000502 /// Marks current target region as one with closely nested teams
Alexey Bataev13314bf2014-10-09 04:18:56 +0000503 /// region.
504 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000505 if (!isStackEmpty() && Stack.back().first.size() > 1) {
506 std::next(Stack.back().first.rbegin())->InnerTeamsRegionLoc =
507 TeamsRegionLoc;
508 }
Alexey Bataev13314bf2014-10-09 04:18:56 +0000509 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000510 /// Returns true, if current region has closely nested teams region.
Alexey Bataev13314bf2014-10-09 04:18:56 +0000511 bool hasInnerTeamsRegion() const {
512 return getInnerTeamsRegionLoc().isValid();
513 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000514 /// Returns location of the nested teams region (if any).
Alexey Bataev13314bf2014-10-09 04:18:56 +0000515 SourceLocation getInnerTeamsRegionLoc() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000516 return isStackEmpty() ? SourceLocation()
517 : Stack.back().first.back().InnerTeamsRegionLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000518 }
519
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000520 Scope *getCurScope() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000521 return isStackEmpty() ? nullptr : Stack.back().first.back().CurScope;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000522 }
Alexey Bataeve3727102018-04-18 15:57:46 +0000523 SourceLocation getConstructLoc() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000524 return isStackEmpty() ? SourceLocation()
525 : Stack.back().first.back().ConstructLoc;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000526 }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000527
Samuel Antao4c8035b2016-12-12 18:00:20 +0000528 /// Do the check specified in \a Check to all component lists and return true
529 /// if any issue is found.
Samuel Antao90927002016-04-26 14:54:23 +0000530 bool checkMappableExprComponentListsForDecl(
Alexey Bataeve3727102018-04-18 15:57:46 +0000531 const ValueDecl *VD, bool CurrentRegionOnly,
Samuel Antao6890b092016-07-28 14:25:09 +0000532 const llvm::function_ref<
533 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000534 OpenMPClauseKind)>
Alexey Bataeve3727102018-04-18 15:57:46 +0000535 Check) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000536 if (isStackEmpty())
537 return false;
538 auto SI = Stack.back().first.rbegin();
539 auto SE = Stack.back().first.rend();
Samuel Antao5de996e2016-01-22 20:21:36 +0000540
541 if (SI == SE)
542 return false;
543
Alexey Bataeve3727102018-04-18 15:57:46 +0000544 if (CurrentRegionOnly)
Samuel Antao5de996e2016-01-22 20:21:36 +0000545 SE = std::next(SI);
Alexey Bataeve3727102018-04-18 15:57:46 +0000546 else
547 std::advance(SI, 1);
Samuel Antao5de996e2016-01-22 20:21:36 +0000548
549 for (; SI != SE; ++SI) {
Samuel Antao90927002016-04-26 14:54:23 +0000550 auto MI = SI->MappedExprComponents.find(VD);
551 if (MI != SI->MappedExprComponents.end())
Alexey Bataeve3727102018-04-18 15:57:46 +0000552 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
553 MI->second.Components)
Samuel Antao6890b092016-07-28 14:25:09 +0000554 if (Check(L, MI->second.Kind))
Samuel Antao5de996e2016-01-22 20:21:36 +0000555 return true;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000556 }
Samuel Antao5de996e2016-01-22 20:21:36 +0000557 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000558 }
559
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +0000560 /// Do the check specified in \a Check to all component lists at a given level
561 /// and return true if any issue is found.
562 bool checkMappableExprComponentListsForDeclAtLevel(
Alexey Bataeve3727102018-04-18 15:57:46 +0000563 const ValueDecl *VD, unsigned Level,
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +0000564 const llvm::function_ref<
565 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000566 OpenMPClauseKind)>
Alexey Bataeve3727102018-04-18 15:57:46 +0000567 Check) const {
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +0000568 if (isStackEmpty())
569 return false;
570
571 auto StartI = Stack.back().first.begin();
572 auto EndI = Stack.back().first.end();
573 if (std::distance(StartI, EndI) <= (int)Level)
574 return false;
575 std::advance(StartI, Level);
576
577 auto MI = StartI->MappedExprComponents.find(VD);
578 if (MI != StartI->MappedExprComponents.end())
Alexey Bataeve3727102018-04-18 15:57:46 +0000579 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
580 MI->second.Components)
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +0000581 if (Check(L, MI->second.Kind))
582 return true;
583 return false;
584 }
585
Samuel Antao4c8035b2016-12-12 18:00:20 +0000586 /// Create a new mappable expression component list associated with a given
587 /// declaration and initialize it with the provided list of components.
Samuel Antao90927002016-04-26 14:54:23 +0000588 void addMappableExpressionComponents(
Alexey Bataeve3727102018-04-18 15:57:46 +0000589 const ValueDecl *VD,
Samuel Antao6890b092016-07-28 14:25:09 +0000590 OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
591 OpenMPClauseKind WhereFoundClauseKind) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000592 assert(!isStackEmpty() &&
Samuel Antao90927002016-04-26 14:54:23 +0000593 "Not expecting to retrieve components from a empty stack!");
Alexey Bataeve3727102018-04-18 15:57:46 +0000594 MappedExprComponentTy &MEC =
595 Stack.back().first.back().MappedExprComponents[VD];
Samuel Antao90927002016-04-26 14:54:23 +0000596 // Create new entry and append the new components there.
Samuel Antao6890b092016-07-28 14:25:09 +0000597 MEC.Components.resize(MEC.Components.size() + 1);
598 MEC.Components.back().append(Components.begin(), Components.end());
599 MEC.Kind = WhereFoundClauseKind;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000600 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000601
602 unsigned getNestingLevel() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000603 assert(!isStackEmpty());
604 return Stack.back().first.size() - 1;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000605 }
Alexey Bataeve3727102018-04-18 15:57:46 +0000606 void addDoacrossDependClause(OMPDependClause *C,
607 const OperatorOffsetTy &OpsOffs) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000608 assert(!isStackEmpty() && Stack.back().first.size() > 1);
Alexey Bataeve3727102018-04-18 15:57:46 +0000609 SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
Alexey Bataev4b465392017-04-26 15:06:24 +0000610 assert(isOpenMPWorksharingDirective(StackElem.Directive));
Alexey Bataeve3727102018-04-18 15:57:46 +0000611 StackElem.DoacrossDepends.try_emplace(C, OpsOffs);
Alexey Bataev8b427062016-05-25 12:36:08 +0000612 }
613 llvm::iterator_range<DoacrossDependMapTy::const_iterator>
614 getDoacrossDependClauses() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000615 assert(!isStackEmpty());
Alexey Bataeve3727102018-04-18 15:57:46 +0000616 const SharingMapTy &StackElem = Stack.back().first.back();
Alexey Bataev4b465392017-04-26 15:06:24 +0000617 if (isOpenMPWorksharingDirective(StackElem.Directive)) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000618 const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends;
Alexey Bataev8b427062016-05-25 12:36:08 +0000619 return llvm::make_range(Ref.begin(), Ref.end());
620 }
Alexey Bataev4b465392017-04-26 15:06:24 +0000621 return llvm::make_range(StackElem.DoacrossDepends.end(),
622 StackElem.DoacrossDepends.end());
Alexey Bataev8b427062016-05-25 12:36:08 +0000623 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000624};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000625bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
Alexey Bataev35aaee62016-04-13 13:36:48 +0000626 return isOpenMPParallelDirective(DKind) || isOpenMPTaskingDirective(DKind) ||
627 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000628}
Alexey Bataeve3727102018-04-18 15:57:46 +0000629
Alexey Bataeved09d242014-05-28 05:53:51 +0000630} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000631
Alexey Bataeve3727102018-04-18 15:57:46 +0000632static const Expr *getExprAsWritten(const Expr *E) {
633 if (const auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000634 E = ExprTemp->getSubExpr();
635
Alexey Bataeve3727102018-04-18 15:57:46 +0000636 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000637 E = MTE->GetTemporaryExpr();
638
Alexey Bataeve3727102018-04-18 15:57:46 +0000639 while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000640 E = Binder->getSubExpr();
641
Alexey Bataeve3727102018-04-18 15:57:46 +0000642 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000643 E = ICE->getSubExprAsWritten();
644 return E->IgnoreParens();
645}
646
Alexey Bataeve3727102018-04-18 15:57:46 +0000647static Expr *getExprAsWritten(Expr *E) {
648 return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E)));
649}
650
651static const ValueDecl *getCanonicalDecl(const ValueDecl *D) {
652 if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D))
653 if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000654 D = ME->getMemberDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +0000655 const auto *VD = dyn_cast<VarDecl>(D);
656 const auto *FD = dyn_cast<FieldDecl>(D);
David Majnemer9d168222016-08-05 17:44:54 +0000657 if (VD != nullptr) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000658 VD = VD->getCanonicalDecl();
659 D = VD;
660 } else {
661 assert(FD);
662 FD = FD->getCanonicalDecl();
663 D = FD;
664 }
665 return D;
666}
667
Alexey Bataeve3727102018-04-18 15:57:46 +0000668static ValueDecl *getCanonicalDecl(ValueDecl *D) {
669 return const_cast<ValueDecl *>(
670 getCanonicalDecl(const_cast<const ValueDecl *>(D)));
671}
672
673DSAStackTy::DSAVarData DSAStackTy::getDSA(iterator &Iter,
674 ValueDecl *D) const {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000675 D = getCanonicalDecl(D);
676 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000677 const auto *FD = dyn_cast<FieldDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000678 DSAVarData DVar;
Alexey Bataev4b465392017-04-26 15:06:24 +0000679 if (isStackEmpty() || Iter == Stack.back().first.rend()) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000680 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
681 // in a region but not in construct]
682 // File-scope or namespace-scope variables referenced in called routines
683 // in the region are shared unless they appear in a threadprivate
684 // directive.
Alexey Bataeve3727102018-04-18 15:57:46 +0000685 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000686 DVar.CKind = OMPC_shared;
687
688 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
689 // in a region but not in construct]
690 // Variables with static storage duration that are declared in called
691 // routines in the region are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000692 if (VD && VD->hasGlobalStorage())
693 DVar.CKind = OMPC_shared;
694
695 // Non-static data members are shared by default.
696 if (FD)
Alexey Bataev750a58b2014-03-18 12:19:12 +0000697 DVar.CKind = OMPC_shared;
698
Alexey Bataev758e55e2013-09-06 18:03:48 +0000699 return DVar;
700 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000701
Alexey Bataevec3da872014-01-31 05:15:34 +0000702 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
703 // in a Construct, C/C++, predetermined, p.1]
704 // Variables with automatic storage duration that are declared in a scope
705 // inside the construct are private.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000706 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
707 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000708 DVar.CKind = OMPC_private;
709 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000710 }
711
Alexey Bataeveffbdf12017-07-21 17:24:30 +0000712 DVar.DKind = Iter->Directive;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000713 // Explicitly specified attributes and local variables with predetermined
714 // attributes.
715 if (Iter->SharingMap.count(D)) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000716 const DSAInfo &Data = Iter->SharingMap.lookup(D);
717 DVar.RefExpr = Data.RefExpr.getPointer();
718 DVar.PrivateCopy = Data.PrivateCopy;
719 DVar.CKind = Data.Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000720 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000721 return DVar;
722 }
723
724 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
725 // in a Construct, C/C++, implicitly determined, p.1]
726 // In a parallel or task construct, the data-sharing attributes of these
727 // variables are determined by the default clause, if present.
728 switch (Iter->DefaultAttr) {
729 case DSA_shared:
730 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000731 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000732 return DVar;
733 case DSA_none:
734 return DVar;
735 case DSA_unspecified:
736 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
737 // in a Construct, implicitly determined, p.2]
738 // In a parallel construct, if no default clause is present, these
739 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000740 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000741 if (isOpenMPParallelDirective(DVar.DKind) ||
742 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000743 DVar.CKind = OMPC_shared;
744 return DVar;
745 }
746
747 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
748 // in a Construct, implicitly determined, p.4]
749 // In a task construct, if no default clause is present, a variable that in
750 // the enclosing context is determined to be shared by all implicit tasks
751 // bound to the current team is shared.
Alexey Bataev35aaee62016-04-13 13:36:48 +0000752 if (isOpenMPTaskingDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000753 DSAVarData DVarTemp;
Alexey Bataeve3727102018-04-18 15:57:46 +0000754 iterator I = Iter, E = Stack.back().first.rend();
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000755 do {
756 ++I;
Alexey Bataeved09d242014-05-28 05:53:51 +0000757 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
Alexey Bataev35aaee62016-04-13 13:36:48 +0000758 // Referenced in a Construct, implicitly determined, p.6]
Alexey Bataev758e55e2013-09-06 18:03:48 +0000759 // In a task construct, if no default clause is present, a variable
760 // whose data-sharing attribute is not determined by the rules above is
761 // firstprivate.
762 DVarTemp = getDSA(I, D);
763 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000764 DVar.RefExpr = nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000765 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000766 return DVar;
767 }
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000768 } while (I != E && !isParallelOrTaskRegion(I->Directive));
Alexey Bataev758e55e2013-09-06 18:03:48 +0000769 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000770 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000771 return DVar;
772 }
773 }
774 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
775 // in a Construct, implicitly determined, p.3]
776 // For constructs other than task, if no default clause is present, these
777 // variables inherit their data-sharing attributes from the enclosing
778 // context.
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000779 return getDSA(++Iter, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000780}
781
Alexey Bataeve3727102018-04-18 15:57:46 +0000782const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D,
783 const Expr *NewDE) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000784 assert(!isStackEmpty() && "Data sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000785 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000786 SharingMapTy &StackElem = Stack.back().first.back();
Alexey Bataev4b465392017-04-26 15:06:24 +0000787 auto It = StackElem.AlignedMap.find(D);
788 if (It == StackElem.AlignedMap.end()) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000789 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
Alexey Bataev4b465392017-04-26 15:06:24 +0000790 StackElem.AlignedMap[D] = NewDE;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000791 return nullptr;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000792 }
Alexey Bataeve3727102018-04-18 15:57:46 +0000793 assert(It->second && "Unexpected nullptr expr in the aligned map");
794 return It->second;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000795}
796
Alexey Bataeve3727102018-04-18 15:57:46 +0000797void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000798 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000799 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000800 SharingMapTy &StackElem = Stack.back().first.back();
801 StackElem.LCVMap.try_emplace(
802 D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture));
Alexey Bataev9c821032015-04-30 04:23:23 +0000803}
804
Alexey Bataeve3727102018-04-18 15:57:46 +0000805const DSAStackTy::LCDeclInfo
806DSAStackTy::isLoopControlVariable(const ValueDecl *D) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000807 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000808 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000809 const SharingMapTy &StackElem = Stack.back().first.back();
Alexey Bataev4b465392017-04-26 15:06:24 +0000810 auto It = StackElem.LCVMap.find(D);
811 if (It != StackElem.LCVMap.end())
812 return It->second;
813 return {0, nullptr};
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000814}
815
Alexey Bataeve3727102018-04-18 15:57:46 +0000816const DSAStackTy::LCDeclInfo
817DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000818 assert(!isStackEmpty() && Stack.back().first.size() > 1 &&
819 "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000820 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000821 const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
Alexey Bataev4b465392017-04-26 15:06:24 +0000822 auto It = StackElem.LCVMap.find(D);
823 if (It != StackElem.LCVMap.end())
824 return It->second;
825 return {0, nullptr};
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000826}
827
Alexey Bataeve3727102018-04-18 15:57:46 +0000828const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000829 assert(!isStackEmpty() && Stack.back().first.size() > 1 &&
830 "Data-sharing attributes stack is empty");
Alexey Bataeve3727102018-04-18 15:57:46 +0000831 const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
Alexey Bataev4b465392017-04-26 15:06:24 +0000832 if (StackElem.LCVMap.size() < I)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000833 return nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +0000834 for (const auto &Pair : StackElem.LCVMap)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000835 if (Pair.second.first == I)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000836 return Pair.first;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000837 return nullptr;
Alexey Bataev9c821032015-04-30 04:23:23 +0000838}
839
Alexey Bataeve3727102018-04-18 15:57:46 +0000840void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
Alexey Bataev90c228f2016-02-08 09:29:13 +0000841 DeclRefExpr *PrivateCopy) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000842 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000843 if (A == OMPC_threadprivate) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000844 DSAInfo &Data = Threadprivates[D];
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000845 Data.Attributes = A;
846 Data.RefExpr.setPointer(E);
847 Data.PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000848 } else {
Alexey Bataev4b465392017-04-26 15:06:24 +0000849 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataeve3727102018-04-18 15:57:46 +0000850 DSAInfo &Data = Stack.back().first.back().SharingMap[D];
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000851 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
852 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
853 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
854 (isLoopControlVariable(D).first && A == OMPC_private));
855 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
856 Data.RefExpr.setInt(/*IntVal=*/true);
857 return;
858 }
859 const bool IsLastprivate =
860 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
861 Data.Attributes = A;
862 Data.RefExpr.setPointerAndInt(E, IsLastprivate);
863 Data.PrivateCopy = PrivateCopy;
864 if (PrivateCopy) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000865 DSAInfo &Data =
866 Stack.back().first.back().SharingMap[PrivateCopy->getDecl()];
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000867 Data.Attributes = A;
868 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
869 Data.PrivateCopy = nullptr;
870 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000871 }
872}
873
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000874/// Build a variable declaration for OpenMP loop iteration variable.
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000875static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev63cc8e92018-03-20 14:45:59 +0000876 StringRef Name, const AttrVec *Attrs = nullptr,
877 DeclRefExpr *OrigRef = nullptr) {
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000878 DeclContext *DC = SemaRef.CurContext;
879 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
880 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
Alexey Bataeve3727102018-04-18 15:57:46 +0000881 auto *Decl =
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000882 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
883 if (Attrs) {
884 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
885 I != E; ++I)
886 Decl->addAttr(*I);
887 }
888 Decl->setImplicit();
Alexey Bataev63cc8e92018-03-20 14:45:59 +0000889 if (OrigRef) {
890 Decl->addAttr(
891 OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef));
892 }
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000893 return Decl;
894}
895
896static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
897 SourceLocation Loc,
898 bool RefersToCapture = false) {
899 D->setReferenced();
900 D->markUsed(S.Context);
901 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
902 SourceLocation(), D, RefersToCapture, Loc, Ty,
903 VK_LValue);
904}
905
Alexey Bataeve3727102018-04-18 15:57:46 +0000906void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000907 BinaryOperatorKind BOK) {
Alexey Bataevfa312f32017-07-21 18:48:21 +0000908 D = getCanonicalDecl(D);
909 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataevfa312f32017-07-21 18:48:21 +0000910 assert(
Richard Trieu09f14112017-07-21 21:29:35 +0000911 Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000912 "Additional reduction info may be specified only for reduction items.");
Alexey Bataeve3727102018-04-18 15:57:46 +0000913 ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D];
Alexey Bataevfa312f32017-07-21 18:48:21 +0000914 assert(ReductionData.ReductionRange.isInvalid() &&
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000915 Stack.back().first.back().Directive == OMPD_taskgroup &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000916 "Additional reduction info may be specified only once for reduction "
917 "items.");
918 ReductionData.set(BOK, SR);
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000919 Expr *&TaskgroupReductionRef =
920 Stack.back().first.back().TaskgroupReductionRef;
921 if (!TaskgroupReductionRef) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000922 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
923 SemaRef.Context.VoidPtrTy, ".task_red.");
Alexey Bataevd070a582017-10-25 15:54:04 +0000924 TaskgroupReductionRef =
925 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000926 }
Alexey Bataevfa312f32017-07-21 18:48:21 +0000927}
928
Alexey Bataeve3727102018-04-18 15:57:46 +0000929void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000930 const Expr *ReductionRef) {
Alexey Bataevfa312f32017-07-21 18:48:21 +0000931 D = getCanonicalDecl(D);
932 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataevfa312f32017-07-21 18:48:21 +0000933 assert(
Richard Trieu09f14112017-07-21 21:29:35 +0000934 Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000935 "Additional reduction info may be specified only for reduction items.");
Alexey Bataeve3727102018-04-18 15:57:46 +0000936 ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D];
Alexey Bataevfa312f32017-07-21 18:48:21 +0000937 assert(ReductionData.ReductionRange.isInvalid() &&
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000938 Stack.back().first.back().Directive == OMPD_taskgroup &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000939 "Additional reduction info may be specified only once for reduction "
940 "items.");
941 ReductionData.set(ReductionRef, SR);
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000942 Expr *&TaskgroupReductionRef =
943 Stack.back().first.back().TaskgroupReductionRef;
944 if (!TaskgroupReductionRef) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000945 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
946 SemaRef.Context.VoidPtrTy, ".task_red.");
Alexey Bataevd070a582017-10-25 15:54:04 +0000947 TaskgroupReductionRef =
948 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000949 }
Alexey Bataevfa312f32017-07-21 18:48:21 +0000950}
951
Alexey Bataeve3727102018-04-18 15:57:46 +0000952const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
953 const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK,
954 Expr *&TaskgroupDescriptor) const {
Alexey Bataevfa312f32017-07-21 18:48:21 +0000955 D = getCanonicalDecl(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +0000956 assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
957 if (Stack.back().first.empty())
958 return DSAVarData();
Alexey Bataeve3727102018-04-18 15:57:46 +0000959 for (iterator I = std::next(Stack.back().first.rbegin(), 1),
960 E = Stack.back().first.rend();
Alexey Bataevfa312f32017-07-21 18:48:21 +0000961 I != E; std::advance(I, 1)) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000962 const DSAInfo &Data = I->SharingMap.lookup(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +0000963 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
Alexey Bataevfa312f32017-07-21 18:48:21 +0000964 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +0000965 const ReductionData &ReductionData = I->ReductionMap.lookup(D);
Alexey Bataevfa312f32017-07-21 18:48:21 +0000966 if (!ReductionData.ReductionOp ||
967 ReductionData.ReductionOp.is<const Expr *>())
Alexey Bataevf189cb72017-07-24 14:52:13 +0000968 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +0000969 SR = ReductionData.ReductionRange;
Alexey Bataevf87fa882017-07-21 19:26:22 +0000970 BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>();
Alexey Bataev88202be2017-07-27 13:20:36 +0000971 assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
972 "expression for the descriptor is not "
973 "set.");
974 TaskgroupDescriptor = I->TaskgroupReductionRef;
Alexey Bataevf189cb72017-07-24 14:52:13 +0000975 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
976 Data.PrivateCopy, I->DefaultAttrLoc);
Alexey Bataevfa312f32017-07-21 18:48:21 +0000977 }
Alexey Bataevf189cb72017-07-24 14:52:13 +0000978 return DSAVarData();
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, const Expr *&ReductionRef,
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;
999 ReductionRef = ReductionData.ReductionOp.get<const Expr *>();
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 +00001010bool DSAStackTy::isOpenMPLocal(VarDecl *D, iterator Iter) const {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001011 D = D->getCanonicalDecl();
Alexey Bataev852525d2018-03-02 17:17:12 +00001012 if (!isStackEmpty()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001013 iterator I = Iter, E = Stack.back().first.rend();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001014 Scope *TopScope = nullptr;
Alexey Bataev852525d2018-03-02 17:17:12 +00001015 while (I != E && !isParallelOrTaskRegion(I->Directive) &&
1016 !isOpenMPTargetExecutionDirective(I->Directive))
Alexey Bataevec3da872014-01-31 05:15:34 +00001017 ++I;
Alexey Bataeved09d242014-05-28 05:53:51 +00001018 if (I == E)
1019 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001020 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001021 Scope *CurScope = getCurScope();
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001022 while (CurScope != TopScope && !CurScope->isDeclScope(D))
Alexey Bataev758e55e2013-09-06 18:03:48 +00001023 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +00001024 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001025 }
Alexey Bataevec3da872014-01-31 05:15:34 +00001026 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001027}
1028
Alexey Bataeve3727102018-04-18 15:57:46 +00001029const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
1030 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001031 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001032 DSAVarData DVar;
1033
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001034 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001035 auto TI = Threadprivates.find(D);
1036 if (TI != Threadprivates.end()) {
1037 DVar.RefExpr = TI->getSecond().RefExpr.getPointer();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001038 DVar.CKind = OMPC_threadprivate;
1039 return DVar;
Alexey Bataeve3727102018-04-18 15:57:46 +00001040 }
1041 if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
Alexey Bataev817d7f32017-11-14 21:01:01 +00001042 DVar.RefExpr = buildDeclRefExpr(
1043 SemaRef, VD, D->getType().getNonReferenceType(),
1044 VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation());
1045 DVar.CKind = OMPC_threadprivate;
1046 addDSA(D, DVar.RefExpr, OMPC_threadprivate);
Alexey Bataev852525d2018-03-02 17:17:12 +00001047 return DVar;
1048 }
1049 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1050 // in a Construct, C/C++, predetermined, p.1]
1051 // Variables appearing in threadprivate directives are threadprivate.
1052 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
1053 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1054 SemaRef.getLangOpts().OpenMPUseTLS &&
1055 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
1056 (VD && VD->getStorageClass() == SC_Register &&
1057 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
1058 DVar.RefExpr = buildDeclRefExpr(
1059 SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation());
1060 DVar.CKind = OMPC_threadprivate;
1061 addDSA(D, DVar.RefExpr, OMPC_threadprivate);
1062 return DVar;
1063 }
1064 if (SemaRef.getLangOpts().OpenMPCUDAMode && VD &&
1065 VD->isLocalVarDeclOrParm() && !isStackEmpty() &&
1066 !isLoopControlVariable(D).first) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001067 iterator IterTarget =
Alexey Bataev852525d2018-03-02 17:17:12 +00001068 std::find_if(Stack.back().first.rbegin(), Stack.back().first.rend(),
1069 [](const SharingMapTy &Data) {
1070 return isOpenMPTargetExecutionDirective(Data.Directive);
1071 });
1072 if (IterTarget != Stack.back().first.rend()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001073 iterator ParentIterTarget = std::next(IterTarget, 1);
1074 for (iterator Iter = Stack.back().first.rbegin();
1075 Iter != ParentIterTarget; std::advance(Iter, 1)) {
Alexey Bataev852525d2018-03-02 17:17:12 +00001076 if (isOpenMPLocal(VD, Iter)) {
1077 DVar.RefExpr =
1078 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1079 D->getLocation());
1080 DVar.CKind = OMPC_threadprivate;
1081 return DVar;
1082 }
Alexey Bataev852525d2018-03-02 17:17:12 +00001083 }
1084 if (!isClauseParsingMode() || IterTarget != Stack.back().first.rbegin()) {
1085 auto DSAIter = IterTarget->SharingMap.find(D);
1086 if (DSAIter != IterTarget->SharingMap.end() &&
1087 isOpenMPPrivate(DSAIter->getSecond().Attributes)) {
1088 DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer();
1089 DVar.CKind = OMPC_threadprivate;
1090 return DVar;
Alexey Bataeve3727102018-04-18 15:57:46 +00001091 }
1092 iterator End = Stack.back().first.rend();
1093 if (!SemaRef.isOpenMPCapturedByRef(
1094 D, std::distance(ParentIterTarget, End))) {
Alexey Bataev852525d2018-03-02 17:17:12 +00001095 DVar.RefExpr =
1096 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1097 IterTarget->ConstructLoc);
1098 DVar.CKind = OMPC_threadprivate;
1099 return DVar;
1100 }
1101 }
1102 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001103 }
1104
Alexey Bataev4b465392017-04-26 15:06:24 +00001105 if (isStackEmpty())
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001106 // Not in OpenMP execution region and top scope was already checked.
1107 return DVar;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001108
Alexey Bataev758e55e2013-09-06 18:03:48 +00001109 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +00001110 // in a Construct, C/C++, predetermined, p.4]
1111 // Static data members are shared.
1112 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1113 // in a Construct, C/C++, predetermined, p.7]
1114 // Variables with static storage duration that are declared in a scope
1115 // inside the construct are shared.
Alexey Bataeve3727102018-04-18 15:57:46 +00001116 auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; };
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001117 if (VD && VD->isStaticDataMember()) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001118 DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent);
Alexey Bataevdffa93a2015-12-10 08:20:58 +00001119 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
Alexey Bataevec3da872014-01-31 05:15:34 +00001120 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001121
Alexey Bataevdffa93a2015-12-10 08:20:58 +00001122 DVar.CKind = OMPC_shared;
1123 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001124 }
1125
1126 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00001127 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
1128 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001129 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1130 // in a Construct, C/C++, predetermined, p.6]
1131 // Variables with const qualified type having no mutable member are
1132 // shared.
Alexey Bataeve3727102018-04-18 15:57:46 +00001133 const CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +00001134 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +00001135 if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
1136 if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate())
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00001137 RD = CTD->getTemplatedDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001138 if (IsConstant &&
Alexey Bataev4bcad7f2016-02-10 10:50:12 +00001139 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasDefinition() &&
1140 RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001141 // Variables with const-qualified type having no mutable member may be
1142 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataeve3727102018-04-18 15:57:46 +00001143 DSAVarData DVarTemp =
1144 hasDSA(D, [](OpenMPClauseKind C) { return C == OMPC_firstprivate; },
1145 MatchesAlways, FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001146 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
Alexey Bataev9a757382018-02-16 19:16:54 +00001147 return DVarTemp;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001148
Alexey Bataev758e55e2013-09-06 18:03:48 +00001149 DVar.CKind = OMPC_shared;
1150 return DVar;
1151 }
1152
Alexey Bataev758e55e2013-09-06 18:03:48 +00001153 // Explicitly specified attributes and local variables with predetermined
1154 // attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +00001155 iterator I = Stack.back().first.rbegin();
1156 iterator EndI = Stack.back().first.rend();
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001157 if (FromParent && I != EndI)
1158 std::advance(I, 1);
Alexey Bataeve3727102018-04-18 15:57:46 +00001159 auto It = I->SharingMap.find(D);
1160 if (It != I->SharingMap.end()) {
1161 const DSAInfo &Data = It->getSecond();
1162 DVar.RefExpr = Data.RefExpr.getPointer();
1163 DVar.PrivateCopy = Data.PrivateCopy;
1164 DVar.CKind = Data.Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001165 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev4d4624c2017-07-20 16:47:47 +00001166 DVar.DKind = I->Directive;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001167 }
1168
1169 return DVar;
1170}
1171
Alexey Bataeve3727102018-04-18 15:57:46 +00001172const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1173 bool FromParent) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001174 if (isStackEmpty()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001175 iterator I;
Alexey Bataev4b465392017-04-26 15:06:24 +00001176 return getDSA(I, D);
1177 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001178 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +00001179 iterator StartI = Stack.back().first.rbegin();
1180 iterator EndI = Stack.back().first.rend();
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001181 if (FromParent && StartI != EndI)
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001182 std::advance(StartI, 1);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001183 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001184}
1185
Alexey Bataeve3727102018-04-18 15:57:46 +00001186const DSAStackTy::DSAVarData
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001187DSAStackTy::hasDSA(ValueDecl *D,
Alexey Bataev97d18bf2018-04-11 19:21:00 +00001188 const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1189 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +00001190 bool FromParent) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001191 if (isStackEmpty())
1192 return {};
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001193 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +00001194 iterator I = Stack.back().first.rbegin();
1195 iterator EndI = Stack.back().first.rend();
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001196 if (FromParent && I != EndI)
Alexey Bataev0e6fc1c2017-04-27 14:46:26 +00001197 std::advance(I, 1);
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001198 for (; I != EndI; std::advance(I, 1)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001199 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +00001200 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00001201 iterator NewI = I;
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001202 DSAVarData DVar = getDSA(NewI, D);
1203 if (I == NewI && CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001204 return DVar;
Alexey Bataev60859c02017-04-27 15:10:33 +00001205 }
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001206 return {};
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001207}
1208
Alexey Bataeve3727102018-04-18 15:57:46 +00001209const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
Alexey Bataev97d18bf2018-04-11 19:21:00 +00001210 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1211 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +00001212 bool FromParent) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001213 if (isStackEmpty())
1214 return {};
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001215 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +00001216 iterator StartI = Stack.back().first.rbegin();
1217 iterator EndI = Stack.back().first.rend();
Alexey Bataeve3978122016-07-19 05:06:39 +00001218 if (FromParent && StartI != EndI)
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001219 std::advance(StartI, 1);
Alexey Bataeve3978122016-07-19 05:06:39 +00001220 if (StartI == EndI || !DPred(StartI->Directive))
Alexey Bataev4b465392017-04-26 15:06:24 +00001221 return {};
Alexey Bataeve3727102018-04-18 15:57:46 +00001222 iterator NewI = StartI;
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001223 DSAVarData DVar = getDSA(NewI, D);
1224 return (NewI == StartI && CPred(DVar.CKind)) ? DVar : DSAVarData();
Alexey Bataevc5e02582014-06-16 07:08:35 +00001225}
1226
Alexey Bataevaac108a2015-06-23 04:51:00 +00001227bool DSAStackTy::hasExplicitDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00001228 const ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1229 unsigned Level, bool NotLastprivate) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001230 if (isStackEmpty())
1231 return false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001232 D = getCanonicalDecl(D);
Alexey Bataev4b465392017-04-26 15:06:24 +00001233 auto StartI = Stack.back().first.begin();
1234 auto EndI = Stack.back().first.end();
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +00001235 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +00001236 return false;
1237 std::advance(StartI, Level);
Alexey Bataeve3727102018-04-18 15:57:46 +00001238 auto I = StartI->SharingMap.find(D);
1239 return (I != StartI->SharingMap.end()) &&
1240 I->getSecond().RefExpr.getPointer() &&
1241 CPred(I->getSecond().Attributes) &&
1242 (!NotLastprivate || !I->getSecond().RefExpr.getInt());
Alexey Bataevaac108a2015-06-23 04:51:00 +00001243}
1244
Samuel Antao4be30e92015-10-02 17:14:03 +00001245bool DSAStackTy::hasExplicitDirective(
Alexey Bataeve3727102018-04-18 15:57:46 +00001246 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1247 unsigned Level) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001248 if (isStackEmpty())
1249 return false;
1250 auto StartI = Stack.back().first.begin();
1251 auto EndI = Stack.back().first.end();
Samuel Antao4be30e92015-10-02 17:14:03 +00001252 if (std::distance(StartI, EndI) <= (int)Level)
1253 return false;
1254 std::advance(StartI, Level);
1255 return DPred(StartI->Directive);
1256}
1257
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001258bool DSAStackTy::hasDirective(
1259 const llvm::function_ref<bool(OpenMPDirectiveKind,
1260 const DeclarationNameInfo &, SourceLocation)>
Alexey Bataev97d18bf2018-04-11 19:21:00 +00001261 DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +00001262 bool FromParent) const {
Samuel Antaof0d79752016-05-27 15:21:27 +00001263 // We look only in the enclosing region.
Alexey Bataev4b465392017-04-26 15:06:24 +00001264 if (isStackEmpty())
Samuel Antaof0d79752016-05-27 15:21:27 +00001265 return false;
Alexey Bataev4b465392017-04-26 15:06:24 +00001266 auto StartI = std::next(Stack.back().first.rbegin());
1267 auto EndI = Stack.back().first.rend();
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001268 if (FromParent && StartI != EndI)
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001269 StartI = std::next(StartI);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001270 for (auto I = StartI, EE = EndI; I != EE; ++I) {
1271 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
1272 return true;
1273 }
1274 return false;
1275}
1276
Alexey Bataev758e55e2013-09-06 18:03:48 +00001277void Sema::InitDataSharingAttributesStack() {
1278 VarDataSharingAttributesStack = new DSAStackTy(*this);
1279}
1280
1281#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
1282
Alexey Bataev4b465392017-04-26 15:06:24 +00001283void Sema::pushOpenMPFunctionRegion() {
1284 DSAStack->pushFunction();
1285}
1286
1287void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) {
1288 DSAStack->popFunction(OldFSI);
1289}
1290
Alexey Bataeve3727102018-04-18 15:57:46 +00001291bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level) const {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001292 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1293
Alexey Bataeve3727102018-04-18 15:57:46 +00001294 ASTContext &Ctx = getASTContext();
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001295 bool IsByRef = true;
1296
1297 // Find the directive that is associated with the provided scope.
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00001298 D = cast<ValueDecl>(D->getCanonicalDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00001299 QualType Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001300
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001301 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001302 // This table summarizes how a given variable should be passed to the device
1303 // given its type and the clauses where it appears. This table is based on
1304 // the description in OpenMP 4.5 [2.10.4, target Construct] and
1305 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
1306 //
1307 // =========================================================================
1308 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
1309 // | |(tofrom:scalar)| | pvt | | | |
1310 // =========================================================================
1311 // | scl | | | | - | | bycopy|
1312 // | scl | | - | x | - | - | bycopy|
1313 // | scl | | x | - | - | - | null |
1314 // | scl | x | | | - | | byref |
1315 // | scl | x | - | x | - | - | bycopy|
1316 // | scl | x | x | - | - | - | null |
1317 // | scl | | - | - | - | x | byref |
1318 // | scl | x | - | - | - | x | byref |
1319 //
1320 // | agg | n.a. | | | - | | byref |
1321 // | agg | n.a. | - | x | - | - | byref |
1322 // | agg | n.a. | x | - | - | - | null |
1323 // | agg | n.a. | - | - | - | x | byref |
1324 // | agg | n.a. | - | - | - | x[] | byref |
1325 //
1326 // | ptr | n.a. | | | - | | bycopy|
1327 // | ptr | n.a. | - | x | - | - | bycopy|
1328 // | ptr | n.a. | x | - | - | - | null |
1329 // | ptr | n.a. | - | - | - | x | byref |
1330 // | ptr | n.a. | - | - | - | x[] | bycopy|
1331 // | ptr | n.a. | - | - | x | | bycopy|
1332 // | ptr | n.a. | - | - | x | x | bycopy|
1333 // | ptr | n.a. | - | - | x | x[] | bycopy|
1334 // =========================================================================
1335 // Legend:
1336 // scl - scalar
1337 // ptr - pointer
1338 // agg - aggregate
1339 // x - applies
1340 // - - invalid in this combination
1341 // [] - mapped with an array section
1342 // byref - should be mapped by reference
1343 // byval - should be mapped by value
1344 // null - initialize a local variable to null on the device
1345 //
1346 // Observations:
1347 // - All scalar declarations that show up in a map clause have to be passed
1348 // by reference, because they may have been mapped in the enclosing data
1349 // environment.
1350 // - If the scalar value does not fit the size of uintptr, it has to be
1351 // passed by reference, regardless the result in the table above.
1352 // - For pointers mapped by value that have either an implicit map or an
1353 // array section, the runtime library may pass the NULL value to the
1354 // device instead of the value passed to it by the compiler.
1355
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001356 if (Ty->isReferenceType())
1357 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
Samuel Antao86ace552016-04-27 22:40:57 +00001358
1359 // Locate map clauses and see if the variable being captured is referred to
1360 // in any of those clauses. Here we only care about variables, not fields,
1361 // because fields are part of aggregates.
1362 bool IsVariableUsedInMapClause = false;
1363 bool IsVariableAssociatedWithSection = false;
1364
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +00001365 DSAStack->checkMappableExprComponentListsForDeclAtLevel(
Alexey Bataeve3727102018-04-18 15:57:46 +00001366 D, Level,
1367 [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D](
1368 OMPClauseMappableExprCommon::MappableExprComponentListRef
Samuel Antao6890b092016-07-28 14:25:09 +00001369 MapExprComponents,
1370 OpenMPClauseKind WhereFoundClauseKind) {
1371 // Only the map clause information influences how a variable is
1372 // captured. E.g. is_device_ptr does not require changing the default
Samuel Antao4c8035b2016-12-12 18:00:20 +00001373 // behavior.
Samuel Antao6890b092016-07-28 14:25:09 +00001374 if (WhereFoundClauseKind != OMPC_map)
1375 return false;
Samuel Antao86ace552016-04-27 22:40:57 +00001376
1377 auto EI = MapExprComponents.rbegin();
1378 auto EE = MapExprComponents.rend();
1379
1380 assert(EI != EE && "Invalid map expression!");
1381
1382 if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
1383 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
1384
1385 ++EI;
1386 if (EI == EE)
1387 return false;
1388
1389 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
1390 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
1391 isa<MemberExpr>(EI->getAssociatedExpression())) {
1392 IsVariableAssociatedWithSection = true;
1393 // There is nothing more we need to know about this variable.
1394 return true;
1395 }
1396
1397 // Keep looking for more map info.
1398 return false;
1399 });
1400
1401 if (IsVariableUsedInMapClause) {
1402 // If variable is identified in a map clause it is always captured by
1403 // reference except if it is a pointer that is dereferenced somehow.
1404 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
1405 } else {
Alexey Bataev3f96fe62017-12-13 17:31:39 +00001406 // By default, all the data that has a scalar type is mapped by copy
1407 // (except for reduction variables).
1408 IsByRef =
1409 !Ty->isScalarType() ||
1410 DSAStack->getDefaultDMAAtLevel(Level) == DMA_tofrom_scalar ||
1411 DSAStack->hasExplicitDSA(
1412 D, [](OpenMPClauseKind K) { return K == OMPC_reduction; }, Level);
Samuel Antao86ace552016-04-27 22:40:57 +00001413 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001414 }
1415
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001416 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00001417 IsByRef =
1418 !DSAStack->hasExplicitDSA(
1419 D,
1420 [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; },
1421 Level, /*NotLastprivate=*/true) &&
1422 // If the variable is artificial and must be captured by value - try to
1423 // capture by value.
Alexey Bataevd2202ca2017-12-27 17:58:32 +00001424 !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() &&
1425 !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue());
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001426 }
1427
Samuel Antao86ace552016-04-27 22:40:57 +00001428 // When passing data by copy, we need to make sure it fits the uintptr size
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001429 // and alignment, because the runtime library only deals with uintptr types.
1430 // If it does not fit the uintptr size, we need to pass the data by reference
1431 // instead.
1432 if (!IsByRef &&
1433 (Ctx.getTypeSizeInChars(Ty) >
1434 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001435 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001436 IsByRef = true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001437 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001438
1439 return IsByRef;
1440}
1441
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001442unsigned Sema::getOpenMPNestingLevel() const {
1443 assert(getLangOpts().OpenMP);
1444 return DSAStack->getNestingLevel();
1445}
1446
Jonas Hahnfeld87d44262017-11-18 21:00:46 +00001447bool Sema::isInOpenMPTargetExecutionDirective() const {
1448 return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) &&
1449 !DSAStack->isClauseParsingMode()) ||
1450 DSAStack->hasDirective(
1451 [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
1452 SourceLocation) -> bool {
1453 return isOpenMPTargetExecutionDirective(K);
1454 },
1455 false);
1456}
1457
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001458VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D) {
Alexey Bataevf841bd92014-12-16 07:00:22 +00001459 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001460 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +00001461
1462 // If we are attempting to capture a global variable in a directive with
1463 // 'target' we return true so that this global is also mapped to the device.
1464 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001465 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001466 if (VD && !VD->hasLocalStorage()) {
1467 if (isInOpenMPDeclareTargetContext() &&
1468 (getCurCapturedRegion() || getCurBlock() || getCurLambda())) {
1469 // Try to mark variable as declare target if it is used in capturing
1470 // regions.
Alexey Bataev97b72212018-08-14 18:31:20 +00001471 if (!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001472 checkDeclIsAllowedInOpenMPTarget(nullptr, VD);
Alexey Bataev92327c52018-03-26 16:40:55 +00001473 return nullptr;
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001474 } else if (isInOpenMPTargetExecutionDirective()) {
1475 // If the declaration is enclosed in a 'declare target' directive,
1476 // then it should not be captured.
1477 //
Alexey Bataev97b72212018-08-14 18:31:20 +00001478 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001479 return nullptr;
1480 return VD;
1481 }
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001482 }
Samuel Antao4be30e92015-10-02 17:14:03 +00001483
Alexey Bataev48977c32015-08-04 08:10:48 +00001484 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
1485 (!DSAStack->isClauseParsingMode() ||
1486 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001487 auto &&Info = DSAStack->isLoopControlVariable(D);
1488 if (Info.first ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001489 (VD && VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001490 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001491 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001492 return VD ? VD : Info.second;
Alexey Bataeve3727102018-04-18 15:57:46 +00001493 DSAStackTy::DSAVarData DVarPrivate =
1494 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001495 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
Alexey Bataev90c228f2016-02-08 09:29:13 +00001496 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00001497 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate,
1498 [](OpenMPDirectiveKind) { return true; },
1499 DSAStack->isClauseParsingMode());
Alexey Bataev90c228f2016-02-08 09:29:13 +00001500 if (DVarPrivate.CKind != OMPC_unknown)
1501 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001502 }
Alexey Bataev90c228f2016-02-08 09:29:13 +00001503 return nullptr;
Alexey Bataevf841bd92014-12-16 07:00:22 +00001504}
1505
Alexey Bataevdfa430f2017-12-08 15:03:50 +00001506void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex,
1507 unsigned Level) const {
1508 SmallVector<OpenMPDirectiveKind, 4> Regions;
1509 getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level));
1510 FunctionScopesIndex -= Regions.size();
1511}
1512
Alexey Bataeve3727102018-04-18 15:57:46 +00001513bool Sema::isOpenMPPrivateDecl(const ValueDecl *D, unsigned Level) const {
Alexey Bataevaac108a2015-06-23 04:51:00 +00001514 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1515 return DSAStack->hasExplicitDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00001516 D, [](OpenMPClauseKind K) { return K == OMPC_private; }, Level) ||
Alexey Bataev3f82cfc2017-12-13 15:28:44 +00001517 (DSAStack->isClauseParsingMode() &&
1518 DSAStack->getClauseParsingMode() == OMPC_private) ||
Alexey Bataev88202be2017-07-27 13:20:36 +00001519 // Consider taskgroup reduction descriptor variable a private to avoid
1520 // possible capture in the region.
1521 (DSAStack->hasExplicitDirective(
1522 [](OpenMPDirectiveKind K) { return K == OMPD_taskgroup; },
1523 Level) &&
1524 DSAStack->isTaskgroupReductionRef(D, Level));
Alexey Bataevaac108a2015-06-23 04:51:00 +00001525}
1526
Alexey Bataeve3727102018-04-18 15:57:46 +00001527void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D,
1528 unsigned Level) {
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001529 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1530 D = getCanonicalDecl(D);
1531 OpenMPClauseKind OMPC = OMPC_unknown;
1532 for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) {
1533 const unsigned NewLevel = I - 1;
1534 if (DSAStack->hasExplicitDSA(D,
1535 [&OMPC](const OpenMPClauseKind K) {
1536 if (isOpenMPPrivate(K)) {
1537 OMPC = K;
1538 return true;
1539 }
1540 return false;
1541 },
1542 NewLevel))
1543 break;
1544 if (DSAStack->checkMappableExprComponentListsForDeclAtLevel(
1545 D, NewLevel,
1546 [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
1547 OpenMPClauseKind) { return true; })) {
1548 OMPC = OMPC_map;
1549 break;
1550 }
1551 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1552 NewLevel)) {
Alexey Bataev63cc8e92018-03-20 14:45:59 +00001553 OMPC = OMPC_map;
1554 if (D->getType()->isScalarType() &&
1555 DSAStack->getDefaultDMAAtLevel(NewLevel) !=
1556 DefaultMapAttributes::DMA_tofrom_scalar)
1557 OMPC = OMPC_firstprivate;
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001558 break;
1559 }
1560 }
1561 if (OMPC != OMPC_unknown)
1562 FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, OMPC));
1563}
1564
Alexey Bataeve3727102018-04-18 15:57:46 +00001565bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D,
1566 unsigned Level) const {
Samuel Antao4be30e92015-10-02 17:14:03 +00001567 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1568 // Return true if the current level is no longer enclosed in a target region.
1569
Alexey Bataeve3727102018-04-18 15:57:46 +00001570 const auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001571 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00001572 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1573 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +00001574}
1575
Alexey Bataeved09d242014-05-28 05:53:51 +00001576void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001577
1578void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
1579 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001580 Scope *CurScope, SourceLocation Loc) {
1581 DSAStack->push(DKind, DirName, CurScope, Loc);
Faisal Valid143a0c2017-04-01 21:30:49 +00001582 PushExpressionEvaluationContext(
1583 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001584}
1585
Alexey Bataevaac108a2015-06-23 04:51:00 +00001586void Sema::StartOpenMPClause(OpenMPClauseKind K) {
1587 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001588}
1589
Alexey Bataevaac108a2015-06-23 04:51:00 +00001590void Sema::EndOpenMPClause() {
1591 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001592}
1593
Alexey Bataev758e55e2013-09-06 18:03:48 +00001594void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001595 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
1596 // A variable of class type (or array thereof) that appears in a lastprivate
1597 // clause requires an accessible, unambiguous default constructor for the
1598 // class type, unless the list item is also specified in a firstprivate
1599 // clause.
Alexey Bataeve3727102018-04-18 15:57:46 +00001600 if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
1601 for (OMPClause *C : D->clauses()) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001602 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
1603 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeve3727102018-04-18 15:57:46 +00001604 for (Expr *DE : Clause->varlists()) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001605 if (DE->isValueDependent() || DE->isTypeDependent()) {
1606 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001607 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +00001608 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00001609 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
Alexey Bataeve3727102018-04-18 15:57:46 +00001610 auto *VD = cast<VarDecl>(DRE->getDecl());
Alexey Bataev005248a2016-02-25 05:25:57 +00001611 QualType Type = VD->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +00001612 const DSAStackTy::DSAVarData DVar =
1613 DSAStack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001614 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001615 // Generate helper private variable and initialize it with the
1616 // default value. The address of the original variable is replaced
1617 // by the address of the new private variable in CodeGen. This new
1618 // variable is not added to IdResolver, so the code in the OpenMP
1619 // region uses original variable for proper diagnostics.
Alexey Bataeve3727102018-04-18 15:57:46 +00001620 VarDecl *VDPrivate = buildVarDecl(
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001621 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev63cc8e92018-03-20 14:45:59 +00001622 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE);
Richard Smith3beb7c62017-01-12 02:27:38 +00001623 ActOnUninitializedDecl(VDPrivate);
Alexey Bataev38e89532015-04-16 04:54:05 +00001624 if (VDPrivate->isInvalidDecl())
1625 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001626 PrivateCopies.push_back(buildDeclRefExpr(
1627 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001628 } else {
1629 // The variable is also a firstprivate, so initialization sequence
1630 // for private copy is generated already.
1631 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001632 }
1633 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001634 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001635 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001636 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001637 }
1638 }
1639 }
1640
Alexey Bataev758e55e2013-09-06 18:03:48 +00001641 DSAStack->pop();
1642 DiscardCleanupsInEvaluationContext();
1643 PopExpressionEvaluationContext();
1644}
1645
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001646static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1647 Expr *NumIterations, Sema &SemaRef,
1648 Scope *S, DSAStackTy *Stack);
Alexander Musman3276a272015-03-21 10:12:56 +00001649
Alexey Bataeva769e072013-03-22 06:34:35 +00001650namespace {
1651
Alexey Bataeve3727102018-04-18 15:57:46 +00001652class VarDeclFilterCCC final : public CorrectionCandidateCallback {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001653private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001654 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001655
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001656public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001657 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001658 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001659 NamedDecl *ND = Candidate.getCorrectionDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +00001660 if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001661 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001662 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1663 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001664 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001665 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001666 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001667};
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001668
Alexey Bataeve3727102018-04-18 15:57:46 +00001669class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001670private:
1671 Sema &SemaRef;
1672
1673public:
1674 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
1675 bool ValidateCandidate(const TypoCorrection &Candidate) override {
1676 NamedDecl *ND = Candidate.getCorrectionDecl();
Kelvin Li59e3d192017-11-30 18:52:06 +00001677 if (ND && (isa<VarDecl>(ND) || isa<FunctionDecl>(ND))) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001678 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1679 SemaRef.getCurScope());
1680 }
1681 return false;
1682 }
1683};
1684
Alexey Bataeved09d242014-05-28 05:53:51 +00001685} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001686
1687ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1688 CXXScopeSpec &ScopeSpec,
1689 const DeclarationNameInfo &Id) {
1690 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1691 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1692
1693 if (Lookup.isAmbiguous())
1694 return ExprError();
1695
1696 VarDecl *VD;
1697 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001698 if (TypoCorrection Corrected = CorrectTypo(
1699 Id, LookupOrdinaryName, CurScope, nullptr,
1700 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001701 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001702 PDiag(Lookup.empty()
1703 ? diag::err_undeclared_var_use_suggest
1704 : diag::err_omp_expected_var_arg_suggest)
1705 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001706 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001707 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001708 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1709 : diag::err_omp_expected_var_arg)
1710 << Id.getName();
1711 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001712 }
Alexey Bataeve3727102018-04-18 15:57:46 +00001713 } else if (!(VD = Lookup.getAsSingle<VarDecl>())) {
1714 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
1715 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1716 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001717 }
1718 Lookup.suppressDiagnostics();
1719
1720 // OpenMP [2.9.2, Syntax, C/C++]
1721 // Variables must be file-scope, namespace-scope, or static block-scope.
1722 if (!VD->hasGlobalStorage()) {
1723 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001724 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1725 bool IsDecl =
1726 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001727 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001728 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1729 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001730 return ExprError();
1731 }
1732
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001733 VarDecl *CanonicalVD = VD->getCanonicalDecl();
George Burgess IV00f70bd2018-03-01 05:43:23 +00001734 NamedDecl *ND = CanonicalVD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001735 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1736 // A threadprivate directive for file-scope variables must appear outside
1737 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001738 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1739 !getCurLexicalContext()->isTranslationUnit()) {
1740 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001741 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1742 bool IsDecl =
1743 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1744 Diag(VD->getLocation(),
1745 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1746 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001747 return ExprError();
1748 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001749 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1750 // A threadprivate directive for static class member variables must appear
1751 // in the class definition, in the same scope in which the member
1752 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001753 if (CanonicalVD->isStaticDataMember() &&
1754 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1755 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001756 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1757 bool IsDecl =
1758 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1759 Diag(VD->getLocation(),
1760 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1761 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001762 return ExprError();
1763 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001764 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1765 // A threadprivate directive for namespace-scope variables must appear
1766 // outside any definition or declaration other than the namespace
1767 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001768 if (CanonicalVD->getDeclContext()->isNamespace() &&
1769 (!getCurLexicalContext()->isFileContext() ||
1770 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1771 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001772 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1773 bool IsDecl =
1774 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1775 Diag(VD->getLocation(),
1776 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1777 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001778 return ExprError();
1779 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001780 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1781 // A threadprivate directive for static block-scope variables must appear
1782 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001783 if (CanonicalVD->isStaticLocal() && CurScope &&
1784 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001785 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001786 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1787 bool IsDecl =
1788 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1789 Diag(VD->getLocation(),
1790 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1791 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001792 return ExprError();
1793 }
1794
1795 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1796 // A threadprivate directive must lexically precede all references to any
1797 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001798 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001799 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001800 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001801 return ExprError();
1802 }
1803
1804 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev376b4a42016-02-09 09:41:09 +00001805 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1806 SourceLocation(), VD,
1807 /*RefersToEnclosingVariableOrCapture=*/false,
1808 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001809}
1810
Alexey Bataeved09d242014-05-28 05:53:51 +00001811Sema::DeclGroupPtrTy
1812Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1813 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001814 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001815 CurContext->addDecl(D);
1816 return DeclGroupPtrTy::make(DeclGroupRef(D));
1817 }
David Blaikie0403cb12016-01-15 23:43:25 +00001818 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001819}
1820
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001821namespace {
Alexey Bataeve3727102018-04-18 15:57:46 +00001822class LocalVarRefChecker final
1823 : public ConstStmtVisitor<LocalVarRefChecker, bool> {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001824 Sema &SemaRef;
1825
1826public:
1827 bool VisitDeclRefExpr(const DeclRefExpr *E) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001828 if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001829 if (VD->hasLocalStorage()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001830 SemaRef.Diag(E->getBeginLoc(),
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001831 diag::err_omp_local_var_in_threadprivate_init)
1832 << E->getSourceRange();
1833 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1834 << VD << VD->getSourceRange();
1835 return true;
1836 }
1837 }
1838 return false;
1839 }
1840 bool VisitStmt(const Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001841 for (const Stmt *Child : S->children()) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001842 if (Child && Visit(Child))
1843 return true;
1844 }
1845 return false;
1846 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001847 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001848};
1849} // namespace
1850
Alexey Bataeved09d242014-05-28 05:53:51 +00001851OMPThreadPrivateDecl *
1852Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001853 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +00001854 for (Expr *RefExpr : VarList) {
1855 auto *DE = cast<DeclRefExpr>(RefExpr);
1856 auto *VD = cast<VarDecl>(DE->getDecl());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001857 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001858
Alexey Bataev376b4a42016-02-09 09:41:09 +00001859 // Mark variable as used.
1860 VD->setReferenced();
1861 VD->markUsed(Context);
1862
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001863 QualType QType = VD->getType();
1864 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1865 // It will be analyzed later.
1866 Vars.push_back(DE);
1867 continue;
1868 }
1869
Alexey Bataeva769e072013-03-22 06:34:35 +00001870 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1871 // A threadprivate variable must not have an incomplete type.
1872 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001873 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001874 continue;
1875 }
1876
1877 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1878 // A threadprivate variable must not have a reference type.
1879 if (VD->getType()->isReferenceType()) {
1880 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001881 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1882 bool IsDecl =
1883 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1884 Diag(VD->getLocation(),
1885 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1886 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001887 continue;
1888 }
1889
Samuel Antaof8b50122015-07-13 22:54:53 +00001890 // Check if this is a TLS variable. If TLS is not being supported, produce
1891 // the corresponding diagnostic.
1892 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1893 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1894 getLangOpts().OpenMPUseTLS &&
1895 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001896 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1897 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001898 Diag(ILoc, diag::err_omp_var_thread_local)
1899 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001900 bool IsDecl =
1901 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1902 Diag(VD->getLocation(),
1903 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1904 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001905 continue;
1906 }
1907
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001908 // Check if initial value of threadprivate variable reference variable with
1909 // local storage (it is not supported by runtime).
Alexey Bataeve3727102018-04-18 15:57:46 +00001910 if (const Expr *Init = VD->getAnyInitializer()) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001911 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001912 if (Checker.Visit(Init))
1913 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001914 }
1915
Alexey Bataeved09d242014-05-28 05:53:51 +00001916 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001917 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001918 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1919 Context, SourceRange(Loc, Loc)));
Alexey Bataeve3727102018-04-18 15:57:46 +00001920 if (ASTMutationListener *ML = Context.getASTMutationListener())
Alexey Bataev97720002014-11-11 04:05:39 +00001921 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001922 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001923 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001924 if (!Vars.empty()) {
1925 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1926 Vars);
1927 D->setAccess(AS_public);
1928 }
1929 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001930}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001931
Kelvin Li1408f912018-09-26 04:28:39 +00001932Sema::DeclGroupPtrTy
1933Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc,
1934 ArrayRef<OMPClause *> ClauseList) {
1935 OMPRequiresDecl *D = nullptr;
1936 if (!CurContext->isFileContext()) {
1937 Diag(Loc, diag::err_omp_invalid_scope) << "requires";
1938 } else {
1939 D = CheckOMPRequiresDecl(Loc, ClauseList);
1940 if (D) {
1941 CurContext->addDecl(D);
1942 DSAStack->addRequiresDecl(D);
1943 }
1944 }
1945 return DeclGroupPtrTy::make(DeclGroupRef(D));
1946}
1947
1948OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc,
1949 ArrayRef<OMPClause *> ClauseList) {
1950 if (!DSAStack->hasDuplicateRequiresClause(ClauseList))
1951 return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc,
1952 ClauseList);
1953 return nullptr;
1954}
1955
Alexey Bataeve3727102018-04-18 15:57:46 +00001956static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
1957 const ValueDecl *D,
1958 const DSAStackTy::DSAVarData &DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00001959 bool IsLoopIterVar = false) {
1960 if (DVar.RefExpr) {
1961 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1962 << getOpenMPClauseName(DVar.CKind);
1963 return;
1964 }
1965 enum {
1966 PDSA_StaticMemberShared,
1967 PDSA_StaticLocalVarShared,
1968 PDSA_LoopIterVarPrivate,
1969 PDSA_LoopIterVarLinear,
1970 PDSA_LoopIterVarLastprivate,
1971 PDSA_ConstVarShared,
1972 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001973 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001974 PDSA_LocalVarPrivate,
1975 PDSA_Implicit
1976 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001977 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001978 auto ReportLoc = D->getLocation();
1979 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00001980 if (IsLoopIterVar) {
1981 if (DVar.CKind == OMPC_private)
1982 Reason = PDSA_LoopIterVarPrivate;
1983 else if (DVar.CKind == OMPC_lastprivate)
1984 Reason = PDSA_LoopIterVarLastprivate;
1985 else
1986 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev35aaee62016-04-13 13:36:48 +00001987 } else if (isOpenMPTaskingDirective(DVar.DKind) &&
1988 DVar.CKind == OMPC_firstprivate) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001989 Reason = PDSA_TaskVarFirstprivate;
1990 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001991 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001992 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001993 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001994 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001995 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00001996 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001997 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00001998 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001999 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00002000 ReportHint = true;
2001 Reason = PDSA_LocalVarPrivate;
2002 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00002003 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002004 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00002005 << Reason << ReportHint
2006 << getOpenMPDirectiveName(Stack->getCurrentDirective());
2007 } else if (DVar.ImplicitDSALoc.isValid()) {
2008 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
2009 << getOpenMPClauseName(DVar.CKind);
2010 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00002011}
2012
Alexey Bataev758e55e2013-09-06 18:03:48 +00002013namespace {
Alexey Bataeve3727102018-04-18 15:57:46 +00002014class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> {
Alexey Bataev758e55e2013-09-06 18:03:48 +00002015 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00002016 Sema &SemaRef;
Alexey Bataeve3727102018-04-18 15:57:46 +00002017 bool ErrorFound = false;
2018 CapturedStmt *CS = nullptr;
2019 llvm::SmallVector<Expr *, 4> ImplicitFirstprivate;
2020 llvm::SmallVector<Expr *, 4> ImplicitMap;
2021 Sema::VarsWithInheritedDSAType VarsWithInheritedDSA;
2022 llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations;
Alexey Bataeved09d242014-05-28 05:53:51 +00002023
Alexey Bataev758e55e2013-09-06 18:03:48 +00002024public:
2025 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00002026 if (E->isTypeDependent() || E->isValueDependent() ||
2027 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
2028 return;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002029 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00002030 VD = VD->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002031 // Skip internally declared variables.
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002032 if (VD->hasLocalStorage() && !CS->capturesVariable(VD))
Alexey Bataeved09d242014-05-28 05:53:51 +00002033 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002034
Alexey Bataeve3727102018-04-18 15:57:46 +00002035 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002036 // Check if the variable has explicit DSA set and stop analysis if it so.
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00002037 if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second)
David Majnemer9d168222016-08-05 17:44:54 +00002038 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002039
Alexey Bataevafe50572017-10-06 17:00:28 +00002040 // Skip internally declared static variables.
Alexey Bataev92327c52018-03-26 16:40:55 +00002041 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
Alexey Bataev97b72212018-08-14 18:31:20 +00002042 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
Alexey Bataev92327c52018-03-26 16:40:55 +00002043 if (VD->hasGlobalStorage() && !CS->capturesVariable(VD) &&
2044 (!Res || *Res != OMPDeclareTargetDeclAttr::MT_Link))
Alexey Bataevafe50572017-10-06 17:00:28 +00002045 return;
2046
Alexey Bataeve3727102018-04-18 15:57:46 +00002047 SourceLocation ELoc = E->getExprLoc();
2048 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002049 // The default(none) clause requires that each variable that is referenced
2050 // in the construct, and does not have a predetermined data-sharing
2051 // attribute, must have its data-sharing attribute explicitly determined
2052 // by being listed in a data-sharing attribute clause.
2053 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002054 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00002055 VarsWithInheritedDSA.count(VD) == 0) {
2056 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002057 return;
2058 }
2059
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002060 if (isOpenMPTargetExecutionDirective(DKind) &&
2061 !Stack->isLoopControlVariable(VD).first) {
2062 if (!Stack->checkMappableExprComponentListsForDecl(
2063 VD, /*CurrentRegionOnly=*/true,
2064 [](OMPClauseMappableExprCommon::MappableExprComponentListRef
2065 StackComponents,
2066 OpenMPClauseKind) {
2067 // Variable is used if it has been marked as an array, array
2068 // section or the variable iself.
2069 return StackComponents.size() == 1 ||
2070 std::all_of(
2071 std::next(StackComponents.rbegin()),
2072 StackComponents.rend(),
2073 [](const OMPClauseMappableExprCommon::
2074 MappableComponent &MC) {
2075 return MC.getAssociatedDeclaration() ==
2076 nullptr &&
2077 (isa<OMPArraySectionExpr>(
2078 MC.getAssociatedExpression()) ||
2079 isa<ArraySubscriptExpr>(
2080 MC.getAssociatedExpression()));
2081 });
2082 })) {
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002083 bool IsFirstprivate = false;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002084 // By default lambdas are captured as firstprivates.
2085 if (const auto *RD =
2086 VD->getType().getNonReferenceType()->getAsCXXRecordDecl())
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002087 IsFirstprivate = RD->isLambda();
2088 IsFirstprivate =
2089 IsFirstprivate ||
2090 (VD->getType().getNonReferenceType()->isScalarType() &&
Alexey Bataev92327c52018-03-26 16:40:55 +00002091 Stack->getDefaultDMA() != DMA_tofrom_scalar && !Res);
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002092 if (IsFirstprivate)
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002093 ImplicitFirstprivate.emplace_back(E);
2094 else
2095 ImplicitMap.emplace_back(E);
2096 return;
2097 }
2098 }
2099
Alexey Bataev758e55e2013-09-06 18:03:48 +00002100 // OpenMP [2.9.3.6, Restrictions, p.2]
2101 // A list item that appears in a reduction clause of the innermost
2102 // enclosing worksharing or parallel construct may not be accessed in an
2103 // explicit task.
Alexey Bataev7ace49d2016-05-17 08:55:33 +00002104 DVar = Stack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00002105 VD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
2106 [](OpenMPDirectiveKind K) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00002107 return isOpenMPParallelDirective(K) ||
2108 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
2109 },
Alexey Bataeveffbdf12017-07-21 17:24:30 +00002110 /*FromParent=*/true);
Alexey Bataev35aaee62016-04-13 13:36:48 +00002111 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00002112 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00002113 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
Alexey Bataeve3727102018-04-18 15:57:46 +00002114 reportOriginalDsa(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002115 return;
2116 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002117
2118 // Define implicit data-sharing attributes for task.
Alexey Bataeve3727102018-04-18 15:57:46 +00002119 DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00002120 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
2121 !Stack->isLoopControlVariable(VD).first)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002122 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002123 }
2124 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002125 void VisitMemberExpr(MemberExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00002126 if (E->isTypeDependent() || E->isValueDependent() ||
2127 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
2128 return;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002129 auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002130 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002131 if (isa<CXXThisExpr>(E->getBase()->IgnoreParens())) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002132 if (!FD)
2133 return;
Alexey Bataeve3727102018-04-18 15:57:46 +00002134 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002135 // Check if the variable has explicit DSA set and stop analysis if it
2136 // so.
2137 if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second)
2138 return;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002139
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002140 if (isOpenMPTargetExecutionDirective(DKind) &&
2141 !Stack->isLoopControlVariable(FD).first &&
2142 !Stack->checkMappableExprComponentListsForDecl(
2143 FD, /*CurrentRegionOnly=*/true,
2144 [](OMPClauseMappableExprCommon::MappableExprComponentListRef
2145 StackComponents,
2146 OpenMPClauseKind) {
2147 return isa<CXXThisExpr>(
2148 cast<MemberExpr>(
2149 StackComponents.back().getAssociatedExpression())
2150 ->getBase()
2151 ->IgnoreParens());
2152 })) {
2153 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
2154 // A bit-field cannot appear in a map clause.
2155 //
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002156 if (FD->isBitField())
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002157 return;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002158 ImplicitMap.emplace_back(E);
2159 return;
2160 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002161
Alexey Bataeve3727102018-04-18 15:57:46 +00002162 SourceLocation ELoc = E->getExprLoc();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002163 // OpenMP [2.9.3.6, Restrictions, p.2]
2164 // A list item that appears in a reduction clause of the innermost
2165 // enclosing worksharing or parallel construct may not be accessed in
2166 // an explicit task.
2167 DVar = Stack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00002168 FD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
2169 [](OpenMPDirectiveKind K) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002170 return isOpenMPParallelDirective(K) ||
2171 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
2172 },
2173 /*FromParent=*/true);
2174 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
2175 ErrorFound = true;
2176 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
Alexey Bataeve3727102018-04-18 15:57:46 +00002177 reportOriginalDsa(SemaRef, Stack, FD, DVar);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002178 return;
2179 }
2180
2181 // Define implicit data-sharing attributes for task.
Alexey Bataeve3727102018-04-18 15:57:46 +00002182 DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002183 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
Alexey Bataevb40e05202018-10-24 18:53:12 +00002184 !Stack->isLoopControlVariable(FD).first) {
2185 // Check if there is a captured expression for the current field in the
2186 // region. Do not mark it as firstprivate unless there is no captured
2187 // expression.
2188 // TODO: try to make it firstprivate.
2189 if (DVar.CKind != OMPC_unknown)
2190 ImplicitFirstprivate.push_back(E);
2191 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002192 return;
2193 }
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002194 if (isOpenMPTargetExecutionDirective(DKind)) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002195 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
Alexey Bataeve3727102018-04-18 15:57:46 +00002196 if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map,
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002197 /*NoDiagnose=*/true))
Alexey Bataev27041fa2017-12-05 15:22:49 +00002198 return;
Alexey Bataeve3727102018-04-18 15:57:46 +00002199 const auto *VD = cast<ValueDecl>(
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002200 CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl());
2201 if (!Stack->checkMappableExprComponentListsForDecl(
2202 VD, /*CurrentRegionOnly=*/true,
2203 [&CurComponents](
2204 OMPClauseMappableExprCommon::MappableExprComponentListRef
2205 StackComponents,
2206 OpenMPClauseKind) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002207 auto CCI = CurComponents.rbegin();
Alexey Bataev5ec38932017-09-26 16:19:04 +00002208 auto CCE = CurComponents.rend();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002209 for (const auto &SC : llvm::reverse(StackComponents)) {
2210 // Do both expressions have the same kind?
2211 if (CCI->getAssociatedExpression()->getStmtClass() !=
2212 SC.getAssociatedExpression()->getStmtClass())
2213 if (!(isa<OMPArraySectionExpr>(
2214 SC.getAssociatedExpression()) &&
2215 isa<ArraySubscriptExpr>(
2216 CCI->getAssociatedExpression())))
2217 return false;
2218
Alexey Bataeve3727102018-04-18 15:57:46 +00002219 const Decl *CCD = CCI->getAssociatedDeclaration();
2220 const Decl *SCD = SC.getAssociatedDeclaration();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002221 CCD = CCD ? CCD->getCanonicalDecl() : nullptr;
2222 SCD = SCD ? SCD->getCanonicalDecl() : nullptr;
2223 if (SCD != CCD)
2224 return false;
2225 std::advance(CCI, 1);
Alexey Bataev5ec38932017-09-26 16:19:04 +00002226 if (CCI == CCE)
2227 break;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002228 }
2229 return true;
2230 })) {
2231 Visit(E->getBase());
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002232 }
Alexey Bataeve3727102018-04-18 15:57:46 +00002233 } else {
Alexey Bataev7fcacd82016-11-28 15:55:15 +00002234 Visit(E->getBase());
Alexey Bataeve3727102018-04-18 15:57:46 +00002235 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002236 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002237 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002238 for (OMPClause *C : S->clauses()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002239 // Skip analysis of arguments of implicitly defined firstprivate clause
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002240 // for task|target directives.
2241 // Skip analysis of arguments of implicitly defined map clause for target
2242 // directives.
2243 if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) &&
2244 C->isImplicit())) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002245 for (Stmt *CC : C->children()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002246 if (CC)
2247 Visit(CC);
2248 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002249 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002250 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002251 }
2252 void VisitStmt(Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002253 for (Stmt *C : S->children()) {
Alexey Bataev8fc7b5f2018-10-25 15:35:27 +00002254 if (C) {
2255 if (auto *OED = dyn_cast<OMPExecutableDirective>(C)) {
2256 // Check implicitly captured vriables in the task-based directives to
2257 // check if they must be firstprivatized.
2258 if (!OED->hasAssociatedStmt())
2259 continue;
2260 const Stmt *AS = OED->getAssociatedStmt();
2261 if (!AS)
2262 continue;
2263 for (const CapturedStmt::Capture &Cap :
2264 cast<CapturedStmt>(AS)->captures()) {
2265 if (Cap.capturesVariable()) {
2266 DeclRefExpr *DRE = buildDeclRefExpr(
2267 SemaRef, Cap.getCapturedVar(),
2268 Cap.getCapturedVar()->getType().getNonLValueExprType(
2269 SemaRef.Context),
2270 Cap.getLocation(),
2271 /*RefersToCapture=*/true);
2272 Visit(DRE);
2273 }
2274 }
2275 } else {
2276 Visit(C);
2277 }
2278 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002279 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002280 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002281
Alexey Bataeve3727102018-04-18 15:57:46 +00002282 bool isErrorFound() const { return ErrorFound; }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002283 ArrayRef<Expr *> getImplicitFirstprivate() const {
2284 return ImplicitFirstprivate;
2285 }
2286 ArrayRef<Expr *> getImplicitMap() const { return ImplicitMap; }
Alexey Bataeve3727102018-04-18 15:57:46 +00002287 const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const {
Alexey Bataev4acb8592014-07-07 13:01:15 +00002288 return VarsWithInheritedDSA;
2289 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002290
Alexey Bataev7ff55242014-06-19 09:13:45 +00002291 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
2292 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00002293};
Alexey Bataeved09d242014-05-28 05:53:51 +00002294} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00002295
Alexey Bataevbae9a792014-06-27 10:37:06 +00002296void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00002297 switch (DKind) {
Kelvin Li70a12c52016-07-13 21:51:49 +00002298 case OMPD_parallel:
2299 case OMPD_parallel_for:
2300 case OMPD_parallel_for_simd:
2301 case OMPD_parallel_sections:
Carlo Bertolliba1487b2017-10-04 14:12:09 +00002302 case OMPD_teams:
Alexey Bataev999277a2017-12-06 14:31:09 +00002303 case OMPD_teams_distribute:
2304 case OMPD_teams_distribute_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002305 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Alexey Bataev2377fe92015-09-10 08:12:02 +00002306 QualType KmpInt32PtrTy =
2307 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002308 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002309 std::make_pair(".global_tid.", KmpInt32PtrTy),
2310 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2311 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00002312 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00002313 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2314 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00002315 break;
2316 }
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002317 case OMPD_target_teams:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00002318 case OMPD_target_parallel:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00002319 case OMPD_target_parallel_for:
Alexey Bataevdfa430f2017-12-08 15:03:50 +00002320 case OMPD_target_parallel_for_simd:
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00002321 case OMPD_target_teams_distribute:
2322 case OMPD_target_teams_distribute_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002323 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2324 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2325 QualType KmpInt32PtrTy =
2326 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2327 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00002328 FunctionProtoType::ExtProtoInfo EPI;
2329 EPI.Variadic = true;
2330 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2331 Sema::CapturedParamNameType Params[] = {
2332 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002333 std::make_pair(".part_id.", KmpInt32PtrTy),
2334 std::make_pair(".privates.", VoidPtrTy),
2335 std::make_pair(
2336 ".copy_fn.",
2337 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00002338 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2339 std::make_pair(StringRef(), QualType()) // __context with shared vars
2340 };
2341 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2342 Params);
Alexey Bataev0c869ef2018-01-16 15:57:07 +00002343 // Mark this captured region as inlined, because we don't use outlined
2344 // function directly.
2345 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2346 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002347 Context, AlwaysInlineAttr::Keyword_forceinline));
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002348 Sema::CapturedParamNameType ParamsTarget[] = {
2349 std::make_pair(StringRef(), QualType()) // __context with shared vars
2350 };
2351 // Start a captured region for 'target' with no implicit parameters.
2352 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2353 ParamsTarget);
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002354 Sema::CapturedParamNameType ParamsTeamsOrParallel[] = {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002355 std::make_pair(".global_tid.", KmpInt32PtrTy),
2356 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2357 std::make_pair(StringRef(), QualType()) // __context with shared vars
2358 };
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002359 // Start a captured region for 'teams' or 'parallel'. Both regions have
2360 // the same implicit parameters.
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002361 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002362 ParamsTeamsOrParallel);
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002363 break;
2364 }
Alexey Bataev8451efa2018-01-15 19:06:12 +00002365 case OMPD_target:
2366 case OMPD_target_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002367 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2368 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2369 QualType KmpInt32PtrTy =
2370 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2371 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00002372 FunctionProtoType::ExtProtoInfo EPI;
2373 EPI.Variadic = true;
2374 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2375 Sema::CapturedParamNameType Params[] = {
2376 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002377 std::make_pair(".part_id.", KmpInt32PtrTy),
2378 std::make_pair(".privates.", VoidPtrTy),
2379 std::make_pair(
2380 ".copy_fn.",
2381 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00002382 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2383 std::make_pair(StringRef(), QualType()) // __context with shared vars
2384 };
2385 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2386 Params);
2387 // Mark this captured region as inlined, because we don't use outlined
2388 // function directly.
2389 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2390 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002391 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev8451efa2018-01-15 19:06:12 +00002392 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2393 std::make_pair(StringRef(), QualType()));
2394 break;
2395 }
Kelvin Li70a12c52016-07-13 21:51:49 +00002396 case OMPD_simd:
2397 case OMPD_for:
2398 case OMPD_for_simd:
2399 case OMPD_sections:
2400 case OMPD_section:
2401 case OMPD_single:
2402 case OMPD_master:
2403 case OMPD_critical:
Kelvin Lia579b912016-07-14 02:54:56 +00002404 case OMPD_taskgroup:
2405 case OMPD_distribute:
Alexey Bataev46506272017-12-05 17:41:34 +00002406 case OMPD_distribute_simd:
Kelvin Li70a12c52016-07-13 21:51:49 +00002407 case OMPD_ordered:
2408 case OMPD_atomic:
Alexey Bataev8451efa2018-01-15 19:06:12 +00002409 case OMPD_target_data: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002410 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002411 std::make_pair(StringRef(), QualType()) // __context with shared vars
2412 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00002413 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2414 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002415 break;
2416 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002417 case OMPD_task: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002418 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2419 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2420 QualType KmpInt32PtrTy =
2421 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2422 QualType Args[] = {VoidPtrTy};
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002423 FunctionProtoType::ExtProtoInfo EPI;
2424 EPI.Variadic = true;
2425 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002426 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00002427 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002428 std::make_pair(".part_id.", KmpInt32PtrTy),
2429 std::make_pair(".privates.", VoidPtrTy),
2430 std::make_pair(
2431 ".copy_fn.",
2432 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev48591dd2016-04-20 04:01:36 +00002433 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002434 std::make_pair(StringRef(), QualType()) // __context with shared vars
2435 };
2436 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2437 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00002438 // Mark this captured region as inlined, because we don't use outlined
2439 // function directly.
2440 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2441 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002442 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002443 break;
2444 }
Alexey Bataev1e73ef32016-04-28 12:14:51 +00002445 case OMPD_taskloop:
2446 case OMPD_taskloop_simd: {
Alexey Bataev7292c292016-04-25 12:22:29 +00002447 QualType KmpInt32Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002448 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1)
2449 .withConst();
Alexey Bataev7292c292016-04-25 12:22:29 +00002450 QualType KmpUInt64Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002451 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0)
2452 .withConst();
Alexey Bataev7292c292016-04-25 12:22:29 +00002453 QualType KmpInt64Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002454 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1)
2455 .withConst();
2456 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2457 QualType KmpInt32PtrTy =
2458 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2459 QualType Args[] = {VoidPtrTy};
Alexey Bataev7292c292016-04-25 12:22:29 +00002460 FunctionProtoType::ExtProtoInfo EPI;
2461 EPI.Variadic = true;
2462 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev49f6e782015-12-01 04:18:41 +00002463 Sema::CapturedParamNameType Params[] = {
Alexey Bataev7292c292016-04-25 12:22:29 +00002464 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002465 std::make_pair(".part_id.", KmpInt32PtrTy),
2466 std::make_pair(".privates.", VoidPtrTy),
Alexey Bataev7292c292016-04-25 12:22:29 +00002467 std::make_pair(
2468 ".copy_fn.",
2469 Context.getPointerType(CopyFnType).withConst().withRestrict()),
2470 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2471 std::make_pair(".lb.", KmpUInt64Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002472 std::make_pair(".ub.", KmpUInt64Ty),
2473 std::make_pair(".st.", KmpInt64Ty),
Alexey Bataev7292c292016-04-25 12:22:29 +00002474 std::make_pair(".liter.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002475 std::make_pair(".reductions.", VoidPtrTy),
Alexey Bataev49f6e782015-12-01 04:18:41 +00002476 std::make_pair(StringRef(), QualType()) // __context with shared vars
2477 };
2478 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2479 Params);
Alexey Bataev7292c292016-04-25 12:22:29 +00002480 // Mark this captured region as inlined, because we don't use outlined
2481 // function directly.
2482 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2483 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002484 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev49f6e782015-12-01 04:18:41 +00002485 break;
2486 }
Kelvin Li4a39add2016-07-05 05:00:15 +00002487 case OMPD_distribute_parallel_for_simd:
Alexey Bataev647dd842018-01-15 20:59:40 +00002488 case OMPD_distribute_parallel_for: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002489 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli9925f152016-06-27 14:55:37 +00002490 QualType KmpInt32PtrTy =
2491 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2492 Sema::CapturedParamNameType Params[] = {
2493 std::make_pair(".global_tid.", KmpInt32PtrTy),
2494 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002495 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2496 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli9925f152016-06-27 14:55:37 +00002497 std::make_pair(StringRef(), QualType()) // __context with shared vars
2498 };
2499 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2500 Params);
2501 break;
2502 }
Alexey Bataev647dd842018-01-15 20:59:40 +00002503 case OMPD_target_teams_distribute_parallel_for:
2504 case OMPD_target_teams_distribute_parallel_for_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002505 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli52978c32018-01-03 21:12:44 +00002506 QualType KmpInt32PtrTy =
2507 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002508 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
Carlo Bertolli52978c32018-01-03 21:12:44 +00002509
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002510 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00002511 FunctionProtoType::ExtProtoInfo EPI;
2512 EPI.Variadic = true;
2513 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2514 Sema::CapturedParamNameType Params[] = {
2515 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002516 std::make_pair(".part_id.", KmpInt32PtrTy),
2517 std::make_pair(".privates.", VoidPtrTy),
2518 std::make_pair(
2519 ".copy_fn.",
2520 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00002521 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2522 std::make_pair(StringRef(), QualType()) // __context with shared vars
2523 };
2524 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2525 Params);
Alexey Bataev9f9fb0b2018-01-16 19:02:33 +00002526 // Mark this captured region as inlined, because we don't use outlined
2527 // function directly.
2528 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2529 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002530 Context, AlwaysInlineAttr::Keyword_forceinline));
Carlo Bertolli52978c32018-01-03 21:12:44 +00002531 Sema::CapturedParamNameType ParamsTarget[] = {
2532 std::make_pair(StringRef(), QualType()) // __context with shared vars
2533 };
2534 // Start a captured region for 'target' with no implicit parameters.
2535 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2536 ParamsTarget);
2537
2538 Sema::CapturedParamNameType ParamsTeams[] = {
2539 std::make_pair(".global_tid.", KmpInt32PtrTy),
2540 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2541 std::make_pair(StringRef(), QualType()) // __context with shared vars
2542 };
2543 // Start a captured region for 'target' with no implicit parameters.
2544 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2545 ParamsTeams);
2546
2547 Sema::CapturedParamNameType ParamsParallel[] = {
2548 std::make_pair(".global_tid.", KmpInt32PtrTy),
2549 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002550 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2551 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli52978c32018-01-03 21:12:44 +00002552 std::make_pair(StringRef(), QualType()) // __context with shared vars
2553 };
2554 // Start a captured region for 'teams' or 'parallel'. Both regions have
2555 // the same implicit parameters.
2556 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2557 ParamsParallel);
2558 break;
2559 }
2560
Alexey Bataev46506272017-12-05 17:41:34 +00002561 case OMPD_teams_distribute_parallel_for:
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00002562 case OMPD_teams_distribute_parallel_for_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002563 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli62fae152017-11-20 20:46:39 +00002564 QualType KmpInt32PtrTy =
2565 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2566
2567 Sema::CapturedParamNameType ParamsTeams[] = {
2568 std::make_pair(".global_tid.", KmpInt32PtrTy),
2569 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2570 std::make_pair(StringRef(), QualType()) // __context with shared vars
2571 };
2572 // Start a captured region for 'target' with no implicit parameters.
2573 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2574 ParamsTeams);
2575
2576 Sema::CapturedParamNameType ParamsParallel[] = {
2577 std::make_pair(".global_tid.", KmpInt32PtrTy),
2578 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002579 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2580 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli62fae152017-11-20 20:46:39 +00002581 std::make_pair(StringRef(), QualType()) // __context with shared vars
2582 };
2583 // Start a captured region for 'teams' or 'parallel'. Both regions have
2584 // the same implicit parameters.
2585 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2586 ParamsParallel);
2587 break;
2588 }
Alexey Bataev7828b252017-11-21 17:08:48 +00002589 case OMPD_target_update:
2590 case OMPD_target_enter_data:
2591 case OMPD_target_exit_data: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002592 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2593 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2594 QualType KmpInt32PtrTy =
2595 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2596 QualType Args[] = {VoidPtrTy};
Alexey Bataev7828b252017-11-21 17:08:48 +00002597 FunctionProtoType::ExtProtoInfo EPI;
2598 EPI.Variadic = true;
2599 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2600 Sema::CapturedParamNameType Params[] = {
2601 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002602 std::make_pair(".part_id.", KmpInt32PtrTy),
2603 std::make_pair(".privates.", VoidPtrTy),
2604 std::make_pair(
2605 ".copy_fn.",
2606 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev7828b252017-11-21 17:08:48 +00002607 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2608 std::make_pair(StringRef(), QualType()) // __context with shared vars
2609 };
2610 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2611 Params);
2612 // Mark this captured region as inlined, because we don't use outlined
2613 // function directly.
2614 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2615 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002616 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev7828b252017-11-21 17:08:48 +00002617 break;
2618 }
Alexey Bataev9959db52014-05-06 10:08:46 +00002619 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00002620 case OMPD_taskyield:
2621 case OMPD_barrier:
2622 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002623 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00002624 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00002625 case OMPD_flush:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002626 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00002627 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00002628 case OMPD_declare_target:
2629 case OMPD_end_declare_target:
Kelvin Li1408f912018-09-26 04:28:39 +00002630 case OMPD_requires:
Alexey Bataev9959db52014-05-06 10:08:46 +00002631 llvm_unreachable("OpenMP Directive is not allowed");
2632 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00002633 llvm_unreachable("Unknown OpenMP directive");
2634 }
2635}
2636
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002637int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) {
2638 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2639 getOpenMPCaptureRegions(CaptureRegions, DKind);
2640 return CaptureRegions.size();
2641}
2642
Alexey Bataev3392d762016-02-16 11:18:12 +00002643static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00002644 Expr *CaptureExpr, bool WithInit,
2645 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00002646 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00002647 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00002648 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00002649 QualType Ty = Init->getType();
2650 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002651 if (S.getLangOpts().CPlusPlus) {
Alexey Bataev4244be22016-02-11 05:35:55 +00002652 Ty = C.getLValueReferenceType(Ty);
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002653 } else {
Alexey Bataev4244be22016-02-11 05:35:55 +00002654 Ty = C.getPointerType(Ty);
2655 ExprResult Res =
2656 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
2657 if (!Res.isUsable())
2658 return nullptr;
2659 Init = Res.get();
2660 }
Alexey Bataev61205072016-03-02 04:57:40 +00002661 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00002662 }
Alexey Bataeva7206b92016-12-20 16:51:02 +00002663 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty,
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002664 CaptureExpr->getBeginLoc());
Alexey Bataev2bbf7212016-03-03 03:52:24 +00002665 if (!WithInit)
Alexey Bataeve3727102018-04-18 15:57:46 +00002666 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C));
Alexey Bataev4244be22016-02-11 05:35:55 +00002667 S.CurContext->addHiddenDecl(CED);
Richard Smith3beb7c62017-01-12 02:27:38 +00002668 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00002669 return CED;
2670}
2671
Alexey Bataev61205072016-03-02 04:57:40 +00002672static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
2673 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00002674 OMPCapturedExprDecl *CD;
Alexey Bataeve3727102018-04-18 15:57:46 +00002675 if (VarDecl *VD = S.isOpenMPCapturedDecl(D))
Alexey Bataevb7a34b62016-02-25 03:59:29 +00002676 CD = cast<OMPCapturedExprDecl>(VD);
Alexey Bataeve3727102018-04-18 15:57:46 +00002677 else
Alexey Bataev5a3af132016-03-29 08:58:54 +00002678 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
2679 /*AsExpression=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00002680 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00002681 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00002682}
2683
Alexey Bataev5a3af132016-03-29 08:58:54 +00002684static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002685 CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00002686 if (!Ref) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002687 OMPCapturedExprDecl *CD = buildCaptureDecl(
2688 S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr,
2689 /*WithInit=*/true, /*AsExpression=*/true);
Alexey Bataev5a3af132016-03-29 08:58:54 +00002690 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
2691 CaptureExpr->getExprLoc());
2692 }
2693 ExprResult Res = Ref;
2694 if (!S.getLangOpts().CPlusPlus &&
2695 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002696 Ref->getType()->isPointerType()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00002697 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002698 if (!Res.isUsable())
2699 return ExprError();
2700 }
2701 return S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00002702}
2703
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002704namespace {
2705// OpenMP directives parsed in this section are represented as a
2706// CapturedStatement with an associated statement. If a syntax error
2707// is detected during the parsing of the associated statement, the
2708// compiler must abort processing and close the CapturedStatement.
2709//
2710// Combined directives such as 'target parallel' have more than one
2711// nested CapturedStatements. This RAII ensures that we unwind out
2712// of all the nested CapturedStatements when an error is found.
2713class CaptureRegionUnwinderRAII {
2714private:
2715 Sema &S;
2716 bool &ErrorFound;
Alexey Bataeve3727102018-04-18 15:57:46 +00002717 OpenMPDirectiveKind DKind = OMPD_unknown;
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002718
2719public:
2720 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound,
2721 OpenMPDirectiveKind DKind)
2722 : S(S), ErrorFound(ErrorFound), DKind(DKind) {}
2723 ~CaptureRegionUnwinderRAII() {
2724 if (ErrorFound) {
2725 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind);
2726 while (--ThisCaptureLevel >= 0)
2727 S.ActOnCapturedRegionError();
2728 }
2729 }
2730};
2731} // namespace
2732
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002733StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
2734 ArrayRef<OMPClause *> Clauses) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002735 bool ErrorFound = false;
2736 CaptureRegionUnwinderRAII CaptureRegionUnwinder(
2737 *this, ErrorFound, DSAStack->getCurrentDirective());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002738 if (!S.isUsable()) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002739 ErrorFound = true;
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002740 return StmtError();
2741 }
Alexey Bataev993d2802015-12-28 06:23:08 +00002742
Alexey Bataev2ba67042017-11-28 21:11:44 +00002743 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2744 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective());
Alexey Bataev993d2802015-12-28 06:23:08 +00002745 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00002746 OMPScheduleClause *SC = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +00002747 SmallVector<const OMPLinearClause *, 4> LCs;
2748 SmallVector<const OMPClauseWithPreInit *, 4> PICs;
Alexey Bataev040d5402015-05-12 08:35:28 +00002749 // This is required for proper codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00002750 for (OMPClause *Clause : Clauses) {
Alexey Bataev88202be2017-07-27 13:20:36 +00002751 if (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) &&
2752 Clause->getClauseKind() == OMPC_in_reduction) {
2753 // Capture taskgroup task_reduction descriptors inside the tasking regions
2754 // with the corresponding in_reduction items.
2755 auto *IRC = cast<OMPInReductionClause>(Clause);
Alexey Bataeve3727102018-04-18 15:57:46 +00002756 for (Expr *E : IRC->taskgroup_descriptors())
Alexey Bataev88202be2017-07-27 13:20:36 +00002757 if (E)
2758 MarkDeclarationsReferencedInExpr(E);
2759 }
Alexey Bataev16dc7b62015-05-20 03:46:04 +00002760 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00002761 Clause->getClauseKind() == OMPC_copyprivate ||
2762 (getLangOpts().OpenMPUseTLS &&
2763 getASTContext().getTargetInfo().isTLSSupported() &&
2764 Clause->getClauseKind() == OMPC_copyin)) {
2765 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00002766 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeve3727102018-04-18 15:57:46 +00002767 for (Stmt *VarRef : Clause->children()) {
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002768 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00002769 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002770 }
2771 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00002772 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev2ba67042017-11-28 21:11:44 +00002773 } else if (CaptureRegions.size() > 1 ||
2774 CaptureRegions.back() != OMPD_unknown) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002775 if (auto *C = OMPClauseWithPreInit::get(Clause))
2776 PICs.push_back(C);
Alexey Bataev005248a2016-02-25 05:25:57 +00002777 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002778 if (Expr *E = C->getPostUpdateExpr())
Alexey Bataev005248a2016-02-25 05:25:57 +00002779 MarkDeclarationsReferencedInExpr(E);
2780 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002781 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002782 if (Clause->getClauseKind() == OMPC_schedule)
2783 SC = cast<OMPScheduleClause>(Clause);
2784 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00002785 OC = cast<OMPOrderedClause>(Clause);
2786 else if (Clause->getClauseKind() == OMPC_linear)
2787 LCs.push_back(cast<OMPLinearClause>(Clause));
2788 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002789 // OpenMP, 2.7.1 Loop Construct, Restrictions
2790 // The nonmonotonic modifier cannot be specified if an ordered clause is
2791 // specified.
2792 if (SC &&
2793 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
2794 SC->getSecondScheduleModifier() ==
2795 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
2796 OC) {
2797 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
2798 ? SC->getFirstScheduleModifierLoc()
2799 : SC->getSecondScheduleModifierLoc(),
2800 diag::err_omp_schedule_nonmonotonic_ordered)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00002801 << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
Alexey Bataev6402bca2015-12-28 07:25:51 +00002802 ErrorFound = true;
2803 }
Alexey Bataev993d2802015-12-28 06:23:08 +00002804 if (!LCs.empty() && OC && OC->getNumForLoops()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002805 for (const OMPLinearClause *C : LCs) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002806 Diag(C->getBeginLoc(), diag::err_omp_linear_ordered)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00002807 << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
Alexey Bataev993d2802015-12-28 06:23:08 +00002808 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002809 ErrorFound = true;
2810 }
Alexey Bataev113438c2015-12-30 12:06:23 +00002811 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
2812 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
2813 OC->getNumForLoops()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002814 Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd)
Alexey Bataev113438c2015-12-30 12:06:23 +00002815 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
2816 ErrorFound = true;
2817 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002818 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00002819 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002820 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002821 StmtResult SR = S;
Alexey Bataev2ba67042017-11-28 21:11:44 +00002822 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002823 // Mark all variables in private list clauses as used in inner region.
2824 // Required for proper codegen of combined directives.
2825 // TODO: add processing for other clauses.
Alexey Bataev2ba67042017-11-28 21:11:44 +00002826 if (ThisCaptureRegion != OMPD_unknown) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002827 for (const clang::OMPClauseWithPreInit *C : PICs) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002828 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion();
2829 // Find the particular capture region for the clause if the
2830 // directive is a combined one with multiple capture regions.
2831 // If the directive is not a combined one, the capture region
2832 // associated with the clause is OMPD_unknown and is generated
2833 // only once.
2834 if (CaptureRegion == ThisCaptureRegion ||
2835 CaptureRegion == OMPD_unknown) {
2836 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002837 for (Decl *D : DS->decls())
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002838 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
2839 }
2840 }
2841 }
2842 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002843 SR = ActOnCapturedRegionEnd(SR.get());
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002844 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002845 return SR;
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002846}
2847
Jonas Hahnfeld64a9e3c2017-02-22 06:49:10 +00002848static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion,
2849 OpenMPDirectiveKind CancelRegion,
2850 SourceLocation StartLoc) {
2851 // CancelRegion is only needed for cancel and cancellation_point.
2852 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point)
2853 return false;
2854
2855 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for ||
2856 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup)
2857 return false;
2858
2859 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region)
2860 << getOpenMPDirectiveName(CancelRegion);
2861 return true;
2862}
2863
Alexey Bataeve3727102018-04-18 15:57:46 +00002864static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002865 OpenMPDirectiveKind CurrentRegion,
2866 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002867 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002868 SourceLocation StartLoc) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002869 if (Stack->getCurScope()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002870 OpenMPDirectiveKind ParentRegion = Stack->getParentDirective();
2871 OpenMPDirectiveKind OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002872 bool NestingProhibited = false;
2873 bool CloseNesting = true;
David Majnemer9d168222016-08-05 17:44:54 +00002874 bool OrphanSeen = false;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002875 enum {
2876 NoRecommend,
2877 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002878 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002879 ShouldBeInTargetRegion,
2880 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002881 } Recommend = NoRecommend;
Kelvin Lifd8b5742016-07-01 14:30:25 +00002882 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002883 // OpenMP [2.16, Nesting of Regions]
2884 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002885 // OpenMP [2.8.1,simd Construct, Restrictions]
Kelvin Lifd8b5742016-07-01 14:30:25 +00002886 // An ordered construct with the simd clause is the only OpenMP
2887 // construct that can appear in the simd region.
David Majnemer9d168222016-08-05 17:44:54 +00002888 // Allowing a SIMD construct nested in another SIMD construct is an
Kelvin Lifd8b5742016-07-01 14:30:25 +00002889 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
2890 // message.
2891 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
2892 ? diag::err_omp_prohibited_region_simd
2893 : diag::warn_omp_nesting_simd);
2894 return CurrentRegion != OMPD_simd;
Alexey Bataev549210e2014-06-24 04:39:47 +00002895 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002896 if (ParentRegion == OMPD_atomic) {
2897 // OpenMP [2.16, Nesting of Regions]
2898 // OpenMP constructs may not be nested inside an atomic region.
2899 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2900 return true;
2901 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002902 if (CurrentRegion == OMPD_section) {
2903 // OpenMP [2.7.2, sections Construct, Restrictions]
2904 // Orphaned section directives are prohibited. That is, the section
2905 // directives must appear within the sections construct and must not be
2906 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002907 if (ParentRegion != OMPD_sections &&
2908 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002909 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2910 << (ParentRegion != OMPD_unknown)
2911 << getOpenMPDirectiveName(ParentRegion);
2912 return true;
2913 }
2914 return false;
2915 }
Kelvin Li2b51f722016-07-26 04:32:50 +00002916 // Allow some constructs (except teams) to be orphaned (they could be
David Majnemer9d168222016-08-05 17:44:54 +00002917 // used in functions, called from OpenMP regions with the required
Kelvin Li2b51f722016-07-26 04:32:50 +00002918 // preconditions).
Kelvin Libf594a52016-12-17 05:48:59 +00002919 if (ParentRegion == OMPD_unknown &&
2920 !isOpenMPNestingTeamsDirective(CurrentRegion))
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002921 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002922 if (CurrentRegion == OMPD_cancellation_point ||
2923 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002924 // OpenMP [2.16, Nesting of Regions]
2925 // A cancellation point construct for which construct-type-clause is
2926 // taskgroup must be nested inside a task construct. A cancellation
2927 // point construct for which construct-type-clause is not taskgroup must
2928 // be closely nested inside an OpenMP construct that matches the type
2929 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002930 // A cancel construct for which construct-type-clause is taskgroup must be
2931 // nested inside a task construct. A cancel construct for which
2932 // construct-type-clause is not taskgroup must be closely nested inside an
2933 // OpenMP construct that matches the type specified in
2934 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002935 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00002936 !((CancelRegion == OMPD_parallel &&
2937 (ParentRegion == OMPD_parallel ||
2938 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002939 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00002940 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00002941 ParentRegion == OMPD_target_parallel_for ||
2942 ParentRegion == OMPD_distribute_parallel_for ||
Alexey Bataev16e79882017-11-22 21:12:03 +00002943 ParentRegion == OMPD_teams_distribute_parallel_for ||
2944 ParentRegion == OMPD_target_teams_distribute_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002945 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2946 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002947 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2948 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002949 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002950 // OpenMP [2.16, Nesting of Regions]
2951 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002952 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002953 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00002954 isOpenMPTaskingDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002955 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2956 // OpenMP [2.16, Nesting of Regions]
2957 // A critical region may not be nested (closely or otherwise) inside a
2958 // critical region with the same name. Note that this restriction is not
2959 // sufficient to prevent deadlock.
2960 SourceLocation PreviousCriticalLoc;
David Majnemer9d168222016-08-05 17:44:54 +00002961 bool DeadLock = Stack->hasDirective(
2962 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
2963 const DeclarationNameInfo &DNI,
Alexey Bataeve3727102018-04-18 15:57:46 +00002964 SourceLocation Loc) {
David Majnemer9d168222016-08-05 17:44:54 +00002965 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
2966 PreviousCriticalLoc = Loc;
2967 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00002968 }
2969 return false;
David Majnemer9d168222016-08-05 17:44:54 +00002970 },
2971 false /* skip top directive */);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002972 if (DeadLock) {
2973 SemaRef.Diag(StartLoc,
2974 diag::err_omp_prohibited_region_critical_same_name)
2975 << CurrentName.getName();
2976 if (PreviousCriticalLoc.isValid())
2977 SemaRef.Diag(PreviousCriticalLoc,
2978 diag::note_omp_previous_critical_region);
2979 return true;
2980 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002981 } else if (CurrentRegion == OMPD_barrier) {
2982 // OpenMP [2.16, Nesting of Regions]
2983 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002984 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00002985 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2986 isOpenMPTaskingDirective(ParentRegion) ||
2987 ParentRegion == OMPD_master ||
2988 ParentRegion == OMPD_critical ||
2989 ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00002990 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Kelvin Li579e41c2016-11-30 23:51:03 +00002991 !isOpenMPParallelDirective(CurrentRegion) &&
2992 !isOpenMPTeamsDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002993 // OpenMP [2.16, Nesting of Regions]
2994 // A worksharing region may not be closely nested inside a worksharing,
2995 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00002996 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
2997 isOpenMPTaskingDirective(ParentRegion) ||
2998 ParentRegion == OMPD_master ||
2999 ParentRegion == OMPD_critical ||
3000 ParentRegion == OMPD_ordered;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003001 Recommend = ShouldBeInParallelRegion;
3002 } else if (CurrentRegion == OMPD_ordered) {
3003 // OpenMP [2.16, Nesting of Regions]
3004 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00003005 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003006 // An ordered region must be closely nested inside a loop region (or
3007 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003008 // OpenMP [2.8.1,simd Construct, Restrictions]
3009 // An ordered construct with the simd clause is the only OpenMP construct
3010 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003011 NestingProhibited = ParentRegion == OMPD_critical ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003012 isOpenMPTaskingDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003013 !(isOpenMPSimdDirective(ParentRegion) ||
3014 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003015 Recommend = ShouldBeInOrderedRegion;
Kelvin Libf594a52016-12-17 05:48:59 +00003016 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00003017 // OpenMP [2.16, Nesting of Regions]
3018 // If specified, a teams construct must be contained within a target
3019 // construct.
3020 NestingProhibited = ParentRegion != OMPD_target;
Kelvin Li2b51f722016-07-26 04:32:50 +00003021 OrphanSeen = ParentRegion == OMPD_unknown;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003022 Recommend = ShouldBeInTargetRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003023 }
Kelvin Libf594a52016-12-17 05:48:59 +00003024 if (!NestingProhibited &&
3025 !isOpenMPTargetExecutionDirective(CurrentRegion) &&
3026 !isOpenMPTargetDataManagementDirective(CurrentRegion) &&
3027 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00003028 // OpenMP [2.16, Nesting of Regions]
3029 // distribute, parallel, parallel sections, parallel workshare, and the
3030 // parallel loop and parallel loop SIMD constructs are the only OpenMP
3031 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003032 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
3033 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00003034 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003035 }
David Majnemer9d168222016-08-05 17:44:54 +00003036 if (!NestingProhibited &&
Kelvin Li02532872016-08-05 14:37:37 +00003037 isOpenMPNestingDistributeDirective(CurrentRegion)) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003038 // OpenMP 4.5 [2.17 Nesting of Regions]
3039 // The region associated with the distribute construct must be strictly
3040 // nested inside a teams region
Kelvin Libf594a52016-12-17 05:48:59 +00003041 NestingProhibited =
3042 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003043 Recommend = ShouldBeInTeamsRegion;
3044 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003045 if (!NestingProhibited &&
3046 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
3047 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
3048 // OpenMP 4.5 [2.17 Nesting of Regions]
3049 // If a target, target update, target data, target enter data, or
3050 // target exit data construct is encountered during execution of a
3051 // target region, the behavior is unspecified.
3052 NestingProhibited = Stack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +00003053 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
Alexey Bataeve3727102018-04-18 15:57:46 +00003054 SourceLocation) {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003055 if (isOpenMPTargetExecutionDirective(K)) {
3056 OffendingRegion = K;
3057 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003058 }
3059 return false;
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003060 },
3061 false /* don't skip top directive */);
3062 CloseNesting = false;
3063 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003064 if (NestingProhibited) {
Kelvin Li2b51f722016-07-26 04:32:50 +00003065 if (OrphanSeen) {
3066 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
3067 << getOpenMPDirectiveName(CurrentRegion) << Recommend;
3068 } else {
3069 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
3070 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
3071 << Recommend << getOpenMPDirectiveName(CurrentRegion);
3072 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003073 return true;
3074 }
3075 }
3076 return false;
3077}
3078
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003079static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
3080 ArrayRef<OMPClause *> Clauses,
3081 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
3082 bool ErrorFound = false;
3083 unsigned NamedModifiersNumber = 0;
3084 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
3085 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00003086 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00003087 for (const OMPClause *C : Clauses) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003088 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
3089 // At most one if clause without a directive-name-modifier can appear on
3090 // the directive.
3091 OpenMPDirectiveKind CurNM = IC->getNameModifier();
3092 if (FoundNameModifiers[CurNM]) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003093 S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003094 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
3095 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
3096 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003097 } else if (CurNM != OMPD_unknown) {
3098 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003099 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003100 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003101 FoundNameModifiers[CurNM] = IC;
3102 if (CurNM == OMPD_unknown)
3103 continue;
3104 // Check if the specified name modifier is allowed for the current
3105 // directive.
3106 // At most one if clause with the particular directive-name-modifier can
3107 // appear on the directive.
3108 bool MatchFound = false;
3109 for (auto NM : AllowedNameModifiers) {
3110 if (CurNM == NM) {
3111 MatchFound = true;
3112 break;
3113 }
3114 }
3115 if (!MatchFound) {
3116 S.Diag(IC->getNameModifierLoc(),
3117 diag::err_omp_wrong_if_directive_name_modifier)
3118 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
3119 ErrorFound = true;
3120 }
3121 }
3122 }
3123 // If any if clause on the directive includes a directive-name-modifier then
3124 // all if clauses on the directive must include a directive-name-modifier.
3125 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
3126 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003127 S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003128 diag::err_omp_no_more_if_clause);
3129 } else {
3130 std::string Values;
3131 std::string Sep(", ");
3132 unsigned AllowedCnt = 0;
3133 unsigned TotalAllowedNum =
3134 AllowedNameModifiers.size() - NamedModifiersNumber;
3135 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3136 ++Cnt) {
3137 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
3138 if (!FoundNameModifiers[NM]) {
3139 Values += "'";
3140 Values += getOpenMPDirectiveName(NM);
3141 Values += "'";
3142 if (AllowedCnt + 2 == TotalAllowedNum)
3143 Values += " or ";
3144 else if (AllowedCnt + 1 != TotalAllowedNum)
3145 Values += Sep;
3146 ++AllowedCnt;
3147 }
3148 }
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003149 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003150 diag::err_omp_unnamed_if_clause)
3151 << (TotalAllowedNum > 1) << Values;
3152 }
Alexey Bataeve3727102018-04-18 15:57:46 +00003153 for (SourceLocation Loc : NameModifierLoc) {
Alexey Bataevecb156a2015-09-15 17:23:56 +00003154 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3155 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003156 ErrorFound = true;
3157 }
3158 return ErrorFound;
3159}
3160
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003161StmtResult Sema::ActOnOpenMPExecutableDirective(
3162 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
3163 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3164 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003165 StmtResult Res = StmtError();
Jonas Hahnfeld64a9e3c2017-02-22 06:49:10 +00003166 // First check CancelRegion which is then used in checkNestingOfRegions.
3167 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) ||
3168 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003169 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00003170 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003171
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003172 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataeve3727102018-04-18 15:57:46 +00003173 VarsWithInheritedDSAType VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003174 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00003175 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00003176 if (AStmt && !CurContext->isDependentContext()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00003177 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
3178
3179 // Check default data sharing attributes for referenced variables.
3180 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
Arpith Chacko Jacob1f46b702017-01-23 15:38:49 +00003181 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind);
3182 Stmt *S = AStmt;
3183 while (--ThisCaptureLevel >= 0)
3184 S = cast<CapturedStmt>(S)->getCapturedStmt();
3185 DSAChecker.Visit(S);
Alexey Bataev68446b72014-07-18 07:47:19 +00003186 if (DSAChecker.isErrorFound())
3187 return StmtError();
3188 // Generate list of implicitly defined firstprivate variables.
3189 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00003190
Alexey Bataev88202be2017-07-27 13:20:36 +00003191 SmallVector<Expr *, 4> ImplicitFirstprivates(
3192 DSAChecker.getImplicitFirstprivate().begin(),
3193 DSAChecker.getImplicitFirstprivate().end());
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003194 SmallVector<Expr *, 4> ImplicitMaps(DSAChecker.getImplicitMap().begin(),
3195 DSAChecker.getImplicitMap().end());
Alexey Bataev88202be2017-07-27 13:20:36 +00003196 // Mark taskgroup task_reduction descriptors as implicitly firstprivate.
Alexey Bataeve3727102018-04-18 15:57:46 +00003197 for (OMPClause *C : Clauses) {
Alexey Bataev88202be2017-07-27 13:20:36 +00003198 if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003199 for (Expr *E : IRC->taskgroup_descriptors())
Alexey Bataev88202be2017-07-27 13:20:36 +00003200 if (E)
3201 ImplicitFirstprivates.emplace_back(E);
3202 }
3203 }
3204 if (!ImplicitFirstprivates.empty()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00003205 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
Alexey Bataev88202be2017-07-27 13:20:36 +00003206 ImplicitFirstprivates, SourceLocation(), SourceLocation(),
3207 SourceLocation())) {
Alexey Bataev68446b72014-07-18 07:47:19 +00003208 ClausesWithImplicit.push_back(Implicit);
3209 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
Alexey Bataev88202be2017-07-27 13:20:36 +00003210 ImplicitFirstprivates.size();
Alexey Bataeve3727102018-04-18 15:57:46 +00003211 } else {
Alexey Bataev68446b72014-07-18 07:47:19 +00003212 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003213 }
Alexey Bataev68446b72014-07-18 07:47:19 +00003214 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003215 if (!ImplicitMaps.empty()) {
3216 if (OMPClause *Implicit = ActOnOpenMPMapClause(
3217 OMPC_MAP_unknown, OMPC_MAP_tofrom, /*IsMapTypeImplicit=*/true,
3218 SourceLocation(), SourceLocation(), ImplicitMaps,
3219 SourceLocation(), SourceLocation(), SourceLocation())) {
3220 ClausesWithImplicit.emplace_back(Implicit);
3221 ErrorFound |=
3222 cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMaps.size();
Alexey Bataeve3727102018-04-18 15:57:46 +00003223 } else {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003224 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003225 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003226 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003227 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003228
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003229 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003230 switch (Kind) {
3231 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00003232 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3233 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003234 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003235 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003236 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003237 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3238 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003239 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003240 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003241 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3242 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003243 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003244 case OMPD_for_simd:
3245 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3246 EndLoc, VarsWithInheritedDSA);
3247 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003248 case OMPD_sections:
3249 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3250 EndLoc);
3251 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003252 case OMPD_section:
3253 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003254 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003255 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3256 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003257 case OMPD_single:
3258 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3259 EndLoc);
3260 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003261 case OMPD_master:
3262 assert(ClausesWithImplicit.empty() &&
3263 "No clauses are allowed for 'omp master' directive");
3264 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3265 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003266 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003267 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3268 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003269 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003270 case OMPD_parallel_for:
3271 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3272 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003273 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003274 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003275 case OMPD_parallel_for_simd:
3276 Res = ActOnOpenMPParallelForSimdDirective(
3277 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003278 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003279 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003280 case OMPD_parallel_sections:
3281 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3282 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003283 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003284 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003285 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003286 Res =
3287 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003288 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003289 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003290 case OMPD_taskyield:
3291 assert(ClausesWithImplicit.empty() &&
3292 "No clauses are allowed for 'omp taskyield' directive");
3293 assert(AStmt == nullptr &&
3294 "No associated statement allowed for 'omp taskyield' directive");
3295 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3296 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003297 case OMPD_barrier:
3298 assert(ClausesWithImplicit.empty() &&
3299 "No clauses are allowed for 'omp barrier' directive");
3300 assert(AStmt == nullptr &&
3301 "No associated statement allowed for 'omp barrier' directive");
3302 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3303 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003304 case OMPD_taskwait:
3305 assert(ClausesWithImplicit.empty() &&
3306 "No clauses are allowed for 'omp taskwait' directive");
3307 assert(AStmt == nullptr &&
3308 "No associated statement allowed for 'omp taskwait' directive");
3309 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3310 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003311 case OMPD_taskgroup:
Alexey Bataev169d96a2017-07-18 20:17:46 +00003312 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc,
3313 EndLoc);
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003314 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003315 case OMPD_flush:
3316 assert(AStmt == nullptr &&
3317 "No associated statement allowed for 'omp flush' directive");
3318 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3319 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003320 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003321 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3322 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003323 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003324 case OMPD_atomic:
3325 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3326 EndLoc);
3327 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003328 case OMPD_teams:
3329 Res =
3330 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3331 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003332 case OMPD_target:
3333 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3334 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003335 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003336 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003337 case OMPD_target_parallel:
3338 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3339 StartLoc, EndLoc);
3340 AllowedNameModifiers.push_back(OMPD_target);
3341 AllowedNameModifiers.push_back(OMPD_parallel);
3342 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003343 case OMPD_target_parallel_for:
3344 Res = ActOnOpenMPTargetParallelForDirective(
3345 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3346 AllowedNameModifiers.push_back(OMPD_target);
3347 AllowedNameModifiers.push_back(OMPD_parallel);
3348 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003349 case OMPD_cancellation_point:
3350 assert(ClausesWithImplicit.empty() &&
3351 "No clauses are allowed for 'omp cancellation point' directive");
3352 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3353 "cancellation point' directive");
3354 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3355 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003356 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003357 assert(AStmt == nullptr &&
3358 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003359 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3360 CancelRegion);
3361 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003362 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003363 case OMPD_target_data:
3364 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3365 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003366 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003367 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003368 case OMPD_target_enter_data:
3369 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00003370 EndLoc, AStmt);
Samuel Antaodf67fc42016-01-19 19:15:56 +00003371 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3372 break;
Samuel Antao72590762016-01-19 20:04:50 +00003373 case OMPD_target_exit_data:
3374 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00003375 EndLoc, AStmt);
Samuel Antao72590762016-01-19 20:04:50 +00003376 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3377 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003378 case OMPD_taskloop:
3379 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3380 EndLoc, VarsWithInheritedDSA);
3381 AllowedNameModifiers.push_back(OMPD_taskloop);
3382 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003383 case OMPD_taskloop_simd:
3384 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3385 EndLoc, VarsWithInheritedDSA);
3386 AllowedNameModifiers.push_back(OMPD_taskloop);
3387 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003388 case OMPD_distribute:
3389 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3390 EndLoc, VarsWithInheritedDSA);
3391 break;
Samuel Antao686c70c2016-05-26 17:30:50 +00003392 case OMPD_target_update:
Alexey Bataev7828b252017-11-21 17:08:48 +00003393 Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc,
3394 EndLoc, AStmt);
Samuel Antao686c70c2016-05-26 17:30:50 +00003395 AllowedNameModifiers.push_back(OMPD_target_update);
3396 break;
Carlo Bertolli9925f152016-06-27 14:55:37 +00003397 case OMPD_distribute_parallel_for:
3398 Res = ActOnOpenMPDistributeParallelForDirective(
3399 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3400 AllowedNameModifiers.push_back(OMPD_parallel);
3401 break;
Kelvin Li4a39add2016-07-05 05:00:15 +00003402 case OMPD_distribute_parallel_for_simd:
3403 Res = ActOnOpenMPDistributeParallelForSimdDirective(
3404 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3405 AllowedNameModifiers.push_back(OMPD_parallel);
3406 break;
Kelvin Li787f3fc2016-07-06 04:45:38 +00003407 case OMPD_distribute_simd:
3408 Res = ActOnOpenMPDistributeSimdDirective(
3409 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3410 break;
Kelvin Lia579b912016-07-14 02:54:56 +00003411 case OMPD_target_parallel_for_simd:
3412 Res = ActOnOpenMPTargetParallelForSimdDirective(
3413 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3414 AllowedNameModifiers.push_back(OMPD_target);
3415 AllowedNameModifiers.push_back(OMPD_parallel);
3416 break;
Kelvin Li986330c2016-07-20 22:57:10 +00003417 case OMPD_target_simd:
3418 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3419 EndLoc, VarsWithInheritedDSA);
3420 AllowedNameModifiers.push_back(OMPD_target);
3421 break;
Kelvin Li02532872016-08-05 14:37:37 +00003422 case OMPD_teams_distribute:
David Majnemer9d168222016-08-05 17:44:54 +00003423 Res = ActOnOpenMPTeamsDistributeDirective(
3424 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Kelvin Li02532872016-08-05 14:37:37 +00003425 break;
Kelvin Li4e325f72016-10-25 12:50:55 +00003426 case OMPD_teams_distribute_simd:
3427 Res = ActOnOpenMPTeamsDistributeSimdDirective(
3428 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3429 break;
Kelvin Li579e41c2016-11-30 23:51:03 +00003430 case OMPD_teams_distribute_parallel_for_simd:
3431 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective(
3432 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3433 AllowedNameModifiers.push_back(OMPD_parallel);
3434 break;
Kelvin Li7ade93f2016-12-09 03:24:30 +00003435 case OMPD_teams_distribute_parallel_for:
3436 Res = ActOnOpenMPTeamsDistributeParallelForDirective(
3437 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3438 AllowedNameModifiers.push_back(OMPD_parallel);
3439 break;
Kelvin Libf594a52016-12-17 05:48:59 +00003440 case OMPD_target_teams:
3441 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc,
3442 EndLoc);
3443 AllowedNameModifiers.push_back(OMPD_target);
3444 break;
Kelvin Li83c451e2016-12-25 04:52:54 +00003445 case OMPD_target_teams_distribute:
3446 Res = ActOnOpenMPTargetTeamsDistributeDirective(
3447 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3448 AllowedNameModifiers.push_back(OMPD_target);
3449 break;
Kelvin Li80e8f562016-12-29 22:16:30 +00003450 case OMPD_target_teams_distribute_parallel_for:
3451 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective(
3452 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3453 AllowedNameModifiers.push_back(OMPD_target);
3454 AllowedNameModifiers.push_back(OMPD_parallel);
3455 break;
Kelvin Li1851df52017-01-03 05:23:48 +00003456 case OMPD_target_teams_distribute_parallel_for_simd:
3457 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
3458 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3459 AllowedNameModifiers.push_back(OMPD_target);
3460 AllowedNameModifiers.push_back(OMPD_parallel);
3461 break;
Kelvin Lida681182017-01-10 18:08:18 +00003462 case OMPD_target_teams_distribute_simd:
3463 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective(
3464 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3465 AllowedNameModifiers.push_back(OMPD_target);
3466 break;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00003467 case OMPD_declare_target:
3468 case OMPD_end_declare_target:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003469 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003470 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003471 case OMPD_declare_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00003472 case OMPD_requires:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003473 llvm_unreachable("OpenMP Directive is not allowed");
3474 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003475 llvm_unreachable("Unknown OpenMP directive");
3476 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003477
Alexey Bataeve3727102018-04-18 15:57:46 +00003478 for (const auto &P : VarsWithInheritedDSA) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003479 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3480 << P.first << P.second->getSourceRange();
3481 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003482 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3483
3484 if (!AllowedNameModifiers.empty())
3485 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3486 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003487
Alexey Bataeved09d242014-05-28 05:53:51 +00003488 if (ErrorFound)
3489 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003490 return Res;
3491}
3492
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003493Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3494 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
Alexey Bataevd93d3762016-04-12 09:35:56 +00003495 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
Alexey Bataevecba70f2016-04-12 11:02:11 +00003496 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3497 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003498 assert(Aligneds.size() == Alignments.size());
Alexey Bataevecba70f2016-04-12 11:02:11 +00003499 assert(Linears.size() == LinModifiers.size());
3500 assert(Linears.size() == Steps.size());
Alexey Bataev587e1de2016-03-30 10:43:55 +00003501 if (!DG || DG.get().isNull())
3502 return DeclGroupPtrTy();
3503
3504 if (!DG.get().isSingleDecl()) {
Alexey Bataev20dfd772016-04-04 10:12:15 +00003505 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003506 return DG;
3507 }
Alexey Bataeve3727102018-04-18 15:57:46 +00003508 Decl *ADecl = DG.get().getSingleDecl();
Alexey Bataev587e1de2016-03-30 10:43:55 +00003509 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3510 ADecl = FTD->getTemplatedDecl();
3511
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003512 auto *FD = dyn_cast<FunctionDecl>(ADecl);
3513 if (!FD) {
3514 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003515 return DeclGroupPtrTy();
3516 }
3517
Alexey Bataev2af33e32016-04-07 12:45:37 +00003518 // OpenMP [2.8.2, declare simd construct, Description]
3519 // The parameter of the simdlen clause must be a constant positive integer
3520 // expression.
3521 ExprResult SL;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003522 if (Simdlen)
Alexey Bataev2af33e32016-04-07 12:45:37 +00003523 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003524 // OpenMP [2.8.2, declare simd construct, Description]
3525 // The special this pointer can be used as if was one of the arguments to the
3526 // function in any of the linear, aligned, or uniform clauses.
3527 // The uniform clause declares one or more arguments to have an invariant
3528 // value for all concurrent invocations of the function in the execution of a
3529 // single SIMD loop.
Alexey Bataeve3727102018-04-18 15:57:46 +00003530 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs;
3531 const Expr *UniformedLinearThis = nullptr;
3532 for (const Expr *E : Uniforms) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003533 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00003534 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3535 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003536 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3537 FD->getParamDecl(PVD->getFunctionScopeIndex())
Alexey Bataevecba70f2016-04-12 11:02:11 +00003538 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
Alexey Bataev43a919f2018-04-13 17:48:43 +00003539 UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003540 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003541 }
3542 if (isa<CXXThisExpr>(E)) {
3543 UniformedLinearThis = E;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003544 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003545 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003546 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3547 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
Alexey Bataev2af33e32016-04-07 12:45:37 +00003548 }
Alexey Bataevd93d3762016-04-12 09:35:56 +00003549 // OpenMP [2.8.2, declare simd construct, Description]
3550 // The aligned clause declares that the object to which each list item points
3551 // is aligned to the number of bytes expressed in the optional parameter of
3552 // the aligned clause.
3553 // The special this pointer can be used as if was one of the arguments to the
3554 // function in any of the linear, aligned, or uniform clauses.
3555 // The type of list items appearing in the aligned clause must be array,
3556 // pointer, reference to array, or reference to pointer.
Alexey Bataeve3727102018-04-18 15:57:46 +00003557 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs;
3558 const Expr *AlignedThis = nullptr;
3559 for (const Expr *E : Aligneds) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003560 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00003561 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3562 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3563 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevd93d3762016-04-12 09:35:56 +00003564 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3565 FD->getParamDecl(PVD->getFunctionScopeIndex())
3566 ->getCanonicalDecl() == CanonPVD) {
3567 // OpenMP [2.8.1, simd construct, Restrictions]
3568 // A list-item cannot appear in more than one aligned clause.
3569 if (AlignedArgs.count(CanonPVD) > 0) {
3570 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3571 << 1 << E->getSourceRange();
3572 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3573 diag::note_omp_explicit_dsa)
3574 << getOpenMPClauseName(OMPC_aligned);
3575 continue;
3576 }
3577 AlignedArgs[CanonPVD] = E;
3578 QualType QTy = PVD->getType()
3579 .getNonReferenceType()
3580 .getUnqualifiedType()
3581 .getCanonicalType();
3582 const Type *Ty = QTy.getTypePtrOrNull();
3583 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3584 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3585 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3586 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3587 }
3588 continue;
3589 }
3590 }
3591 if (isa<CXXThisExpr>(E)) {
3592 if (AlignedThis) {
3593 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3594 << 2 << E->getSourceRange();
3595 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3596 << getOpenMPClauseName(OMPC_aligned);
3597 }
3598 AlignedThis = E;
3599 continue;
3600 }
3601 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3602 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3603 }
3604 // The optional parameter of the aligned clause, alignment, must be a constant
3605 // positive integer expression. If no optional parameter is specified,
3606 // implementation-defined default alignments for SIMD instructions on the
3607 // target platforms are assumed.
Alexey Bataeve3727102018-04-18 15:57:46 +00003608 SmallVector<const Expr *, 4> NewAligns;
3609 for (Expr *E : Alignments) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003610 ExprResult Align;
3611 if (E)
3612 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3613 NewAligns.push_back(Align.get());
3614 }
Alexey Bataevecba70f2016-04-12 11:02:11 +00003615 // OpenMP [2.8.2, declare simd construct, Description]
3616 // The linear clause declares one or more list items to be private to a SIMD
3617 // lane and to have a linear relationship with respect to the iteration space
3618 // of a loop.
3619 // The special this pointer can be used as if was one of the arguments to the
3620 // function in any of the linear, aligned, or uniform clauses.
3621 // When a linear-step expression is specified in a linear clause it must be
3622 // either a constant integer expression or an integer-typed parameter that is
3623 // specified in a uniform clause on the directive.
Alexey Bataeve3727102018-04-18 15:57:46 +00003624 llvm::DenseMap<const Decl *, const Expr *> LinearArgs;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003625 const bool IsUniformedThis = UniformedLinearThis != nullptr;
3626 auto MI = LinModifiers.begin();
Alexey Bataeve3727102018-04-18 15:57:46 +00003627 for (const Expr *E : Linears) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003628 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3629 ++MI;
3630 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00003631 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3632 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3633 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevecba70f2016-04-12 11:02:11 +00003634 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3635 FD->getParamDecl(PVD->getFunctionScopeIndex())
3636 ->getCanonicalDecl() == CanonPVD) {
3637 // OpenMP [2.15.3.7, linear Clause, Restrictions]
3638 // A list-item cannot appear in more than one linear clause.
3639 if (LinearArgs.count(CanonPVD) > 0) {
3640 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3641 << getOpenMPClauseName(OMPC_linear)
3642 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3643 Diag(LinearArgs[CanonPVD]->getExprLoc(),
3644 diag::note_omp_explicit_dsa)
3645 << getOpenMPClauseName(OMPC_linear);
3646 continue;
3647 }
3648 // Each argument can appear in at most one uniform or linear clause.
3649 if (UniformedArgs.count(CanonPVD) > 0) {
3650 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3651 << getOpenMPClauseName(OMPC_linear)
3652 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3653 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3654 diag::note_omp_explicit_dsa)
3655 << getOpenMPClauseName(OMPC_uniform);
3656 continue;
3657 }
3658 LinearArgs[CanonPVD] = E;
3659 if (E->isValueDependent() || E->isTypeDependent() ||
3660 E->isInstantiationDependent() ||
3661 E->containsUnexpandedParameterPack())
3662 continue;
3663 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3664 PVD->getOriginalType());
3665 continue;
3666 }
3667 }
3668 if (isa<CXXThisExpr>(E)) {
3669 if (UniformedLinearThis) {
3670 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3671 << getOpenMPClauseName(OMPC_linear)
3672 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3673 << E->getSourceRange();
3674 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3675 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3676 : OMPC_linear);
3677 continue;
3678 }
3679 UniformedLinearThis = E;
3680 if (E->isValueDependent() || E->isTypeDependent() ||
3681 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3682 continue;
3683 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3684 E->getType());
3685 continue;
3686 }
3687 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3688 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3689 }
3690 Expr *Step = nullptr;
3691 Expr *NewStep = nullptr;
3692 SmallVector<Expr *, 4> NewSteps;
Alexey Bataeve3727102018-04-18 15:57:46 +00003693 for (Expr *E : Steps) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003694 // Skip the same step expression, it was checked already.
3695 if (Step == E || !E) {
3696 NewSteps.push_back(E ? NewStep : nullptr);
3697 continue;
3698 }
3699 Step = E;
Alexey Bataeve3727102018-04-18 15:57:46 +00003700 if (const auto *DRE = dyn_cast<DeclRefExpr>(Step))
3701 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3702 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevecba70f2016-04-12 11:02:11 +00003703 if (UniformedArgs.count(CanonPVD) == 0) {
3704 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
3705 << Step->getSourceRange();
3706 } else if (E->isValueDependent() || E->isTypeDependent() ||
3707 E->isInstantiationDependent() ||
3708 E->containsUnexpandedParameterPack() ||
Alexey Bataeve3727102018-04-18 15:57:46 +00003709 CanonPVD->getType()->hasIntegerRepresentation()) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003710 NewSteps.push_back(Step);
Alexey Bataeve3727102018-04-18 15:57:46 +00003711 } else {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003712 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
3713 << Step->getSourceRange();
3714 }
3715 continue;
3716 }
3717 NewStep = Step;
3718 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3719 !Step->isInstantiationDependent() &&
3720 !Step->containsUnexpandedParameterPack()) {
3721 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
3722 .get();
3723 if (NewStep)
3724 NewStep = VerifyIntegerConstantExpression(NewStep).get();
3725 }
3726 NewSteps.push_back(NewStep);
3727 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003728 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3729 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
Alexey Bataevd93d3762016-04-12 09:35:56 +00003730 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
Alexey Bataevecba70f2016-04-12 11:02:11 +00003731 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
3732 const_cast<Expr **>(Linears.data()), Linears.size(),
3733 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
3734 NewSteps.data(), NewSteps.size(), SR);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003735 ADecl->addAttr(NewAttr);
3736 return ConvertDeclToDeclGroup(ADecl);
3737}
3738
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003739StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3740 Stmt *AStmt,
3741 SourceLocation StartLoc,
3742 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003743 if (!AStmt)
3744 return StmtError();
3745
Alexey Bataeve3727102018-04-18 15:57:46 +00003746 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev9959db52014-05-06 10:08:46 +00003747 // 1.2.2 OpenMP Language Terminology
3748 // Structured block - An executable statement with a single entry at the
3749 // top and a single exit at the bottom.
3750 // The point of exit cannot be a branch out of the structured block.
3751 // longjmp() and throw() must not violate the entry/exit criteria.
3752 CS->getCapturedDecl()->setNothrow();
3753
Reid Kleckner87a31802018-03-12 21:43:02 +00003754 setFunctionHasBranchProtectedScope();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003755
Alexey Bataev25e5b442015-09-15 12:52:43 +00003756 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3757 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003758}
3759
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003760namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003761/// Helper class for checking canonical form of the OpenMP loops and
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003762/// extracting iteration space of each loop in the loop nest, that will be used
3763/// for IR generation.
3764class OpenMPIterationSpaceChecker {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003765 /// Reference to Sema.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003766 Sema &SemaRef;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003767 /// A location for diagnostics (when there is no some better location).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003768 SourceLocation DefaultLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003769 /// A location for diagnostics (when increment is not compatible).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003770 SourceLocation ConditionLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003771 /// A source location for referring to loop init later.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003772 SourceRange InitSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003773 /// A source location for referring to condition later.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003774 SourceRange ConditionSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003775 /// A source location for referring to increment later.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003776 SourceRange IncrementSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003777 /// Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003778 ValueDecl *LCDecl = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003779 /// Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003780 Expr *LCRef = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003781 /// Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003782 Expr *LB = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003783 /// Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003784 Expr *UB = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003785 /// Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003786 Expr *Step = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003787 /// This flag is true when condition is one of:
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003788 /// Var < UB
3789 /// Var <= UB
3790 /// UB > Var
3791 /// UB >= Var
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003792 bool TestIsLessOp = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003793 /// This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003794 bool TestIsStrictOp = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003795 /// This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003796 bool SubtractStep = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003797
3798public:
3799 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003800 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003801 /// Check init-expr for canonical loop form and save loop counter
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003802 /// variable - #Var and its initialization value - #LB.
Alexey Bataeve3727102018-04-18 15:57:46 +00003803 bool checkAndSetInit(Stmt *S, bool EmitDiags = true);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003804 /// Check test-expr for canonical form, save upper-bound (#UB), flags
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003805 /// for less/greater and for strict/non-strict comparison.
Alexey Bataeve3727102018-04-18 15:57:46 +00003806 bool checkAndSetCond(Expr *S);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003807 /// Check incr-expr for canonical loop form and return true if it
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003808 /// does not conform, otherwise save loop step (#Step).
Alexey Bataeve3727102018-04-18 15:57:46 +00003809 bool checkAndSetInc(Expr *S);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003810 /// Return the loop counter variable.
Alexey Bataeve3727102018-04-18 15:57:46 +00003811 ValueDecl *getLoopDecl() const { return LCDecl; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003812 /// Return the reference expression to loop counter variable.
Alexey Bataeve3727102018-04-18 15:57:46 +00003813 Expr *getLoopDeclRefExpr() const { return LCRef; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003814 /// Source range of the loop init.
Alexey Bataeve3727102018-04-18 15:57:46 +00003815 SourceRange getInitSrcRange() const { return InitSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003816 /// Source range of the loop condition.
Alexey Bataeve3727102018-04-18 15:57:46 +00003817 SourceRange getConditionSrcRange() const { return ConditionSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003818 /// Source range of the loop increment.
Alexey Bataeve3727102018-04-18 15:57:46 +00003819 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003820 /// True if the step should be subtracted.
Alexey Bataeve3727102018-04-18 15:57:46 +00003821 bool shouldSubtractStep() const { return SubtractStep; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003822 /// Build the expression to calculate the number of iterations.
Alexey Bataeve3727102018-04-18 15:57:46 +00003823 Expr *buildNumIterations(
3824 Scope *S, const bool LimitedType,
3825 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003826 /// Build the precondition expression for the loops.
Alexey Bataeve3727102018-04-18 15:57:46 +00003827 Expr *
3828 buildPreCond(Scope *S, Expr *Cond,
3829 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003830 /// Build reference expression to the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00003831 DeclRefExpr *
3832 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
3833 DSAStackTy &DSA) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003834 /// Build reference expression to the private counter be used for
Alexey Bataeva8899172015-08-06 12:30:57 +00003835 /// codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00003836 Expr *buildPrivateCounterVar() const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003837 /// Build initialization of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00003838 Expr *buildCounterInit() const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003839 /// Build step of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00003840 Expr *buildCounterStep() const;
Alexey Bataevf138fda2018-08-13 19:04:24 +00003841 /// Build loop data with counter value for depend clauses in ordered
3842 /// directives.
3843 Expr *
3844 buildOrderedLoopData(Scope *S, Expr *Counter,
3845 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
3846 SourceLocation Loc, Expr *Inc = nullptr,
3847 OverloadedOperatorKind OOK = OO_Amp);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003848 /// Return true if any expression is dependent.
Alexey Bataeve3727102018-04-18 15:57:46 +00003849 bool dependent() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003850
3851private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003852 /// Check the right-hand side of an assignment in the increment
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003853 /// expression.
Alexey Bataeve3727102018-04-18 15:57:46 +00003854 bool checkAndSetIncRHS(Expr *RHS);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003855 /// Helper to set loop counter variable and its initializer.
Alexey Bataeve3727102018-04-18 15:57:46 +00003856 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003857 /// Helper to set upper bound.
Alexey Bataeve3727102018-04-18 15:57:46 +00003858 bool setUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00003859 SourceLocation SL);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003860 /// Helper to set loop increment.
Alexey Bataeve3727102018-04-18 15:57:46 +00003861 bool setStep(Expr *NewStep, bool Subtract);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003862};
3863
Alexey Bataeve3727102018-04-18 15:57:46 +00003864bool OpenMPIterationSpaceChecker::dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003865 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003866 assert(!LB && !UB && !Step);
3867 return false;
3868 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003869 return LCDecl->getType()->isDependentType() ||
3870 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
3871 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003872}
3873
Alexey Bataeve3727102018-04-18 15:57:46 +00003874bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl,
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003875 Expr *NewLCRefExpr,
3876 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003877 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003878 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003879 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003880 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003881 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003882 LCDecl = getCanonicalDecl(NewLCDecl);
3883 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003884 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
3885 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00003886 if ((Ctor->isCopyOrMoveConstructor() ||
3887 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
3888 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003889 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003890 LB = NewLB;
3891 return false;
3892}
3893
Alexey Bataeve3727102018-04-18 15:57:46 +00003894bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00003895 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003896 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003897 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
3898 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003899 if (!NewUB)
3900 return true;
3901 UB = NewUB;
3902 TestIsLessOp = LessOp;
3903 TestIsStrictOp = StrictOp;
3904 ConditionSrcRange = SR;
3905 ConditionLoc = SL;
3906 return false;
3907}
3908
Alexey Bataeve3727102018-04-18 15:57:46 +00003909bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003910 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003911 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003912 if (!NewStep)
3913 return true;
3914 if (!NewStep->isValueDependent()) {
3915 // Check that the step is integer expression.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003916 SourceLocation StepLoc = NewStep->getBeginLoc();
Alexey Bataev5372fb82017-08-31 23:06:52 +00003917 ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion(
3918 StepLoc, getExprAsWritten(NewStep));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003919 if (Val.isInvalid())
3920 return true;
3921 NewStep = Val.get();
3922
3923 // OpenMP [2.6, Canonical Loop Form, Restrictions]
3924 // If test-expr is of form var relational-op b and relational-op is < or
3925 // <= then incr-expr must cause var to increase on each iteration of the
3926 // loop. If test-expr is of form var relational-op b and relational-op is
3927 // > or >= then incr-expr must cause var to decrease on each iteration of
3928 // the loop.
3929 // If test-expr is of form b relational-op var and relational-op is < or
3930 // <= then incr-expr must cause var to decrease on each iteration of the
3931 // loop. If test-expr is of form b relational-op var and relational-op is
3932 // > or >= then incr-expr must cause var to increase on each iteration of
3933 // the loop.
3934 llvm::APSInt Result;
3935 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
3936 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
3937 bool IsConstNeg =
3938 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003939 bool IsConstPos =
3940 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003941 bool IsConstZero = IsConstant && !Result.getBoolValue();
3942 if (UB && (IsConstZero ||
3943 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00003944 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003945 SemaRef.Diag(NewStep->getExprLoc(),
3946 diag::err_omp_loop_incr_not_compatible)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003947 << LCDecl << TestIsLessOp << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003948 SemaRef.Diag(ConditionLoc,
3949 diag::note_omp_loop_cond_requres_compatible_incr)
3950 << TestIsLessOp << ConditionSrcRange;
3951 return true;
3952 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003953 if (TestIsLessOp == Subtract) {
David Majnemer9d168222016-08-05 17:44:54 +00003954 NewStep =
3955 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep)
3956 .get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003957 Subtract = !Subtract;
3958 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003959 }
3960
3961 Step = NewStep;
3962 SubtractStep = Subtract;
3963 return false;
3964}
3965
Alexey Bataeve3727102018-04-18 15:57:46 +00003966bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003967 // Check init-expr for canonical loop form and save loop counter
3968 // variable - #Var and its initialization value - #LB.
3969 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
3970 // var = lb
3971 // integer-type var = lb
3972 // random-access-iterator-type var = lb
3973 // pointer-type var = lb
3974 //
3975 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00003976 if (EmitDiags) {
3977 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
3978 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003979 return true;
3980 }
Tim Shen4a05bb82016-06-21 20:29:17 +00003981 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
3982 if (!ExprTemp->cleanupsHaveSideEffects())
3983 S = ExprTemp->getSubExpr();
3984
Alexander Musmana5f070a2014-10-01 06:03:56 +00003985 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003986 if (Expr *E = dyn_cast<Expr>(S))
3987 S = E->IgnoreParens();
David Majnemer9d168222016-08-05 17:44:54 +00003988 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003989 if (BO->getOpcode() == BO_Assign) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003990 Expr *LHS = BO->getLHS()->IgnoreParens();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003991 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
3992 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
3993 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataeve3727102018-04-18 15:57:46 +00003994 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
3995 return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003996 }
3997 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
3998 if (ME->isArrow() &&
3999 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
Alexey Bataeve3727102018-04-18 15:57:46 +00004000 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004001 }
4002 }
David Majnemer9d168222016-08-05 17:44:54 +00004003 } else if (auto *DS = dyn_cast<DeclStmt>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004004 if (DS->isSingleDecl()) {
David Majnemer9d168222016-08-05 17:44:54 +00004005 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00004006 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004007 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00004008 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004009 SemaRef.Diag(S->getBeginLoc(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004010 diag::ext_omp_loop_not_canonical_init)
4011 << S->getSourceRange();
Alexey Bataevf138fda2018-08-13 19:04:24 +00004012 return setLCDeclAndLB(
4013 Var,
4014 buildDeclRefExpr(SemaRef, Var,
4015 Var->getType().getNonReferenceType(),
4016 DS->getBeginLoc()),
4017 Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004018 }
4019 }
4020 }
David Majnemer9d168222016-08-05 17:44:54 +00004021 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004022 if (CE->getOperator() == OO_Equal) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004023 Expr *LHS = CE->getArg(0);
David Majnemer9d168222016-08-05 17:44:54 +00004024 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004025 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4026 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataeve3727102018-04-18 15:57:46 +00004027 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4028 return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004029 }
4030 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4031 if (ME->isArrow() &&
4032 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
Alexey Bataeve3727102018-04-18 15:57:46 +00004033 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004034 }
4035 }
4036 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004037
Alexey Bataeve3727102018-04-18 15:57:46 +00004038 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004039 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00004040 if (EmitDiags) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004041 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init)
Alexey Bataev9c821032015-04-30 04:23:23 +00004042 << S->getSourceRange();
4043 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004044 return true;
4045}
4046
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004047/// Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004048/// variable (which may be the loop variable) if possible.
Alexey Bataeve3727102018-04-18 15:57:46 +00004049static const ValueDecl *getInitLCDecl(const Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004050 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00004051 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004052 E = getExprAsWritten(E);
Alexey Bataeve3727102018-04-18 15:57:46 +00004053 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004054 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00004055 if ((Ctor->isCopyOrMoveConstructor() ||
4056 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4057 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004058 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00004059 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
4060 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004061 return getCanonicalDecl(VD);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004062 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004063 if (const auto *ME = dyn_cast_or_null<MemberExpr>(E))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004064 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4065 return getCanonicalDecl(ME->getMemberDecl());
4066 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004067}
4068
Alexey Bataeve3727102018-04-18 15:57:46 +00004069bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004070 // Check test-expr for canonical form, save upper-bound UB, flags for
4071 // less/greater and for strict/non-strict comparison.
4072 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4073 // var relational-op b
4074 // b relational-op var
4075 //
4076 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004077 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004078 return true;
4079 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004080 S = getExprAsWritten(S);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004081 SourceLocation CondLoc = S->getBeginLoc();
David Majnemer9d168222016-08-05 17:44:54 +00004082 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004083 if (BO->isRelationalOp()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004084 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4085 return setUB(BO->getRHS(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004086 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
4087 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4088 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +00004089 if (getInitLCDecl(BO->getRHS()) == LCDecl)
4090 return setUB(BO->getLHS(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004091 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
4092 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4093 BO->getSourceRange(), BO->getOperatorLoc());
4094 }
David Majnemer9d168222016-08-05 17:44:54 +00004095 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004096 if (CE->getNumArgs() == 2) {
4097 auto Op = CE->getOperator();
4098 switch (Op) {
4099 case OO_Greater:
4100 case OO_GreaterEqual:
4101 case OO_Less:
4102 case OO_LessEqual:
Alexey Bataeve3727102018-04-18 15:57:46 +00004103 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4104 return setUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004105 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4106 CE->getOperatorLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +00004107 if (getInitLCDecl(CE->getArg(1)) == LCDecl)
4108 return setUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004109 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4110 CE->getOperatorLoc());
4111 break;
4112 default:
4113 break;
4114 }
4115 }
4116 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004117 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004118 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004119 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004120 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004121 return true;
4122}
4123
Alexey Bataeve3727102018-04-18 15:57:46 +00004124bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004125 // RHS of canonical loop form increment can be:
4126 // var + incr
4127 // incr + var
4128 // var - incr
4129 //
4130 RHS = RHS->IgnoreParenImpCasts();
David Majnemer9d168222016-08-05 17:44:54 +00004131 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004132 if (BO->isAdditiveOp()) {
4133 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataeve3727102018-04-18 15:57:46 +00004134 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4135 return setStep(BO->getRHS(), !IsAdd);
4136 if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl)
4137 return setStep(BO->getLHS(), /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004138 }
David Majnemer9d168222016-08-05 17:44:54 +00004139 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004140 bool IsAdd = CE->getOperator() == OO_Plus;
4141 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004142 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4143 return setStep(CE->getArg(1), !IsAdd);
4144 if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl)
4145 return setStep(CE->getArg(0), /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004146 }
4147 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004148 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004149 return false;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004150 SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004151 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004152 return true;
4153}
4154
Alexey Bataeve3727102018-04-18 15:57:46 +00004155bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004156 // Check incr-expr for canonical loop form and return true if it
4157 // does not conform.
4158 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4159 // ++var
4160 // var++
4161 // --var
4162 // var--
4163 // var += incr
4164 // var -= incr
4165 // var = var + incr
4166 // var = incr + var
4167 // var = var - incr
4168 //
4169 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004170 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004171 return true;
4172 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004173 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4174 if (!ExprTemp->cleanupsHaveSideEffects())
4175 S = ExprTemp->getSubExpr();
4176
Alexander Musmana5f070a2014-10-01 06:03:56 +00004177 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004178 S = S->IgnoreParens();
David Majnemer9d168222016-08-05 17:44:54 +00004179 if (auto *UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004180 if (UO->isIncrementDecrementOp() &&
Alexey Bataeve3727102018-04-18 15:57:46 +00004181 getInitLCDecl(UO->getSubExpr()) == LCDecl)
4182 return setStep(SemaRef
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004183 .ActOnIntegerConstant(UO->getBeginLoc(),
David Majnemer9d168222016-08-05 17:44:54 +00004184 (UO->isDecrementOp() ? -1 : 1))
4185 .get(),
Alexey Bataeve3727102018-04-18 15:57:46 +00004186 /*Subtract=*/false);
David Majnemer9d168222016-08-05 17:44:54 +00004187 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004188 switch (BO->getOpcode()) {
4189 case BO_AddAssign:
4190 case BO_SubAssign:
Alexey Bataeve3727102018-04-18 15:57:46 +00004191 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4192 return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004193 break;
4194 case BO_Assign:
Alexey Bataeve3727102018-04-18 15:57:46 +00004195 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4196 return checkAndSetIncRHS(BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004197 break;
4198 default:
4199 break;
4200 }
David Majnemer9d168222016-08-05 17:44:54 +00004201 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004202 switch (CE->getOperator()) {
4203 case OO_PlusPlus:
4204 case OO_MinusMinus:
Alexey Bataeve3727102018-04-18 15:57:46 +00004205 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4206 return setStep(SemaRef
David Majnemer9d168222016-08-05 17:44:54 +00004207 .ActOnIntegerConstant(
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004208 CE->getBeginLoc(),
David Majnemer9d168222016-08-05 17:44:54 +00004209 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
4210 .get(),
Alexey Bataeve3727102018-04-18 15:57:46 +00004211 /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004212 break;
4213 case OO_PlusEqual:
4214 case OO_MinusEqual:
Alexey Bataeve3727102018-04-18 15:57:46 +00004215 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4216 return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004217 break;
4218 case OO_Equal:
Alexey Bataeve3727102018-04-18 15:57:46 +00004219 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4220 return checkAndSetIncRHS(CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004221 break;
4222 default:
4223 break;
4224 }
4225 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004226 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004227 return false;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004228 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004229 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004230 return true;
4231}
Alexander Musmana5f070a2014-10-01 06:03:56 +00004232
Alexey Bataev5a3af132016-03-29 08:58:54 +00004233static ExprResult
4234tryBuildCapture(Sema &SemaRef, Expr *Capture,
Alexey Bataeve3727102018-04-18 15:57:46 +00004235 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00004236 if (SemaRef.CurContext->isDependentContext())
4237 return ExprResult(Capture);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004238 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
4239 return SemaRef.PerformImplicitConversion(
4240 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
4241 /*AllowExplicit=*/true);
4242 auto I = Captures.find(Capture);
4243 if (I != Captures.end())
4244 return buildCapture(SemaRef, Capture, I->second);
4245 DeclRefExpr *Ref = nullptr;
4246 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
4247 Captures[Capture] = Ref;
4248 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004249}
4250
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004251/// Build the expression to calculate the number of iterations.
Alexey Bataeve3727102018-04-18 15:57:46 +00004252Expr *OpenMPIterationSpaceChecker::buildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004253 Scope *S, const bool LimitedType,
Alexey Bataeve3727102018-04-18 15:57:46 +00004254 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004255 ExprResult Diff;
Alexey Bataeve3727102018-04-18 15:57:46 +00004256 QualType VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004257 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004258 SemaRef.getLangOpts().CPlusPlus) {
4259 // Upper - Lower
Alexey Bataeve3727102018-04-18 15:57:46 +00004260 Expr *UBExpr = TestIsLessOp ? UB : LB;
4261 Expr *LBExpr = TestIsLessOp ? LB : UB;
Alexey Bataev5a3af132016-03-29 08:58:54 +00004262 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
4263 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004264 if (!Upper || !Lower)
4265 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004266
4267 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4268
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004269 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004270 // BuildBinOp already emitted error, this one is to point user to upper
4271 // and lower bound, and to tell what is passed to 'operator-'.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004272 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004273 << Upper->getSourceRange() << Lower->getSourceRange();
4274 return nullptr;
4275 }
4276 }
4277
4278 if (!Diff.isUsable())
4279 return nullptr;
4280
4281 // Upper - Lower [- 1]
4282 if (TestIsStrictOp)
4283 Diff = SemaRef.BuildBinOp(
4284 S, DefaultLoc, BO_Sub, Diff.get(),
4285 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4286 if (!Diff.isUsable())
4287 return nullptr;
4288
4289 // Upper - Lower [- 1] + Step
Alexey Bataeve3727102018-04-18 15:57:46 +00004290 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004291 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004292 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004293 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004294 if (!Diff.isUsable())
4295 return nullptr;
4296
4297 // Parentheses (for dumping/debugging purposes only).
4298 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4299 if (!Diff.isUsable())
4300 return nullptr;
4301
4302 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004303 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004304 if (!Diff.isUsable())
4305 return nullptr;
4306
Alexander Musman174b3ca2014-10-06 11:16:29 +00004307 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004308 QualType Type = Diff.get()->getType();
Alexey Bataeve3727102018-04-18 15:57:46 +00004309 ASTContext &C = SemaRef.Context;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004310 bool UseVarType = VarType->hasIntegerRepresentation() &&
4311 C.getTypeSize(Type) > C.getTypeSize(VarType);
4312 if (!Type->isIntegerType() || UseVarType) {
4313 unsigned NewSize =
4314 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4315 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4316 : Type->hasSignedIntegerRepresentation();
4317 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00004318 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4319 Diff = SemaRef.PerformImplicitConversion(
4320 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4321 if (!Diff.isUsable())
4322 return nullptr;
4323 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004324 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004325 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00004326 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4327 if (NewSize != C.getTypeSize(Type)) {
4328 if (NewSize < C.getTypeSize(Type)) {
4329 assert(NewSize == 64 && "incorrect loop var size");
4330 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4331 << InitSrcRange << ConditionSrcRange;
4332 }
4333 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004334 NewSize, Type->hasSignedIntegerRepresentation() ||
4335 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00004336 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4337 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4338 Sema::AA_Converting, true);
4339 if (!Diff.isUsable())
4340 return nullptr;
4341 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004342 }
4343 }
4344
Alexander Musmana5f070a2014-10-01 06:03:56 +00004345 return Diff.get();
4346}
4347
Alexey Bataeve3727102018-04-18 15:57:46 +00004348Expr *OpenMPIterationSpaceChecker::buildPreCond(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004349 Scope *S, Expr *Cond,
Alexey Bataeve3727102018-04-18 15:57:46 +00004350 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004351 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4352 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4353 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004354
Alexey Bataeve3727102018-04-18 15:57:46 +00004355 ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures);
4356 ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004357 if (!NewLB.isUsable() || !NewUB.isUsable())
4358 return nullptr;
4359
Alexey Bataeve3727102018-04-18 15:57:46 +00004360 ExprResult CondExpr =
4361 SemaRef.BuildBinOp(S, DefaultLoc,
4362 TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
4363 : (TestIsStrictOp ? BO_GT : BO_GE),
4364 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004365 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004366 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4367 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00004368 CondExpr = SemaRef.PerformImplicitConversion(
4369 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4370 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004371 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00004372 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4373 // Otherwise use original loop conditon and evaluate it in runtime.
4374 return CondExpr.isUsable() ? CondExpr.get() : Cond;
4375}
4376
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004377/// Build reference expression to the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004378DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar(
Alexey Bataevf138fda2018-08-13 19:04:24 +00004379 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
4380 DSAStackTy &DSA) const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004381 auto *VD = dyn_cast<VarDecl>(LCDecl);
4382 if (!VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004383 VD = SemaRef.isOpenMPCapturedDecl(LCDecl);
4384 DeclRefExpr *Ref = buildDeclRefExpr(
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004385 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
Alexey Bataeve3727102018-04-18 15:57:46 +00004386 const DSAStackTy::DSAVarData Data =
4387 DSA.getTopDSA(LCDecl, /*FromParent=*/false);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004388 // If the loop control decl is explicitly marked as private, do not mark it
4389 // as captured again.
4390 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4391 Captures.insert(std::make_pair(LCRef, Ref));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004392 return Ref;
4393 }
4394 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
Alexey Bataeva8899172015-08-06 12:30:57 +00004395 DefaultLoc);
4396}
4397
Alexey Bataeve3727102018-04-18 15:57:46 +00004398Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004399 if (LCDecl && !LCDecl->isInvalidDecl()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004400 QualType Type = LCDecl->getType().getNonReferenceType();
4401 VarDecl *PrivateVar = buildVarDecl(
Alexey Bataev63cc8e92018-03-20 14:45:59 +00004402 SemaRef, DefaultLoc, Type, LCDecl->getName(),
4403 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr,
4404 isa<VarDecl>(LCDecl)
4405 ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc)
4406 : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00004407 if (PrivateVar->isInvalidDecl())
4408 return nullptr;
4409 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4410 }
4411 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004412}
4413
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004414/// Build initialization of the counter to be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004415Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004416
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004417/// Build step of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004418Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004419
Alexey Bataevf138fda2018-08-13 19:04:24 +00004420Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData(
4421 Scope *S, Expr *Counter,
4422 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc,
4423 Expr *Inc, OverloadedOperatorKind OOK) {
4424 Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get();
4425 if (!Cnt)
4426 return nullptr;
4427 if (Inc) {
4428 assert((OOK == OO_Plus || OOK == OO_Minus) &&
4429 "Expected only + or - operations for depend clauses.");
4430 BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub;
4431 Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get();
4432 if (!Cnt)
4433 return nullptr;
4434 }
4435 ExprResult Diff;
4436 QualType VarType = LCDecl->getType().getNonReferenceType();
4437 if (VarType->isIntegerType() || VarType->isPointerType() ||
4438 SemaRef.getLangOpts().CPlusPlus) {
4439 // Upper - Lower
4440 Expr *Upper =
4441 TestIsLessOp ? Cnt : tryBuildCapture(SemaRef, UB, Captures).get();
4442 Expr *Lower =
4443 TestIsLessOp ? tryBuildCapture(SemaRef, LB, Captures).get() : Cnt;
4444 if (!Upper || !Lower)
4445 return nullptr;
4446
4447 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4448
4449 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
4450 // BuildBinOp already emitted error, this one is to point user to upper
4451 // and lower bound, and to tell what is passed to 'operator-'.
4452 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
4453 << Upper->getSourceRange() << Lower->getSourceRange();
4454 return nullptr;
4455 }
4456 }
4457
4458 if (!Diff.isUsable())
4459 return nullptr;
4460
4461 // Parentheses (for dumping/debugging purposes only).
4462 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4463 if (!Diff.isUsable())
4464 return nullptr;
4465
4466 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
4467 if (!NewStep.isUsable())
4468 return nullptr;
4469 // (Upper - Lower) / Step
4470 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
4471 if (!Diff.isUsable())
4472 return nullptr;
4473
4474 return Diff.get();
4475}
4476
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004477/// Iteration space of a single for loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004478struct LoopIterationSpace final {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004479 /// Condition of the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004480 Expr *PreCond = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004481 /// This expression calculates the number of iterations in the loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004482 /// It is always possible to calculate it before starting the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004483 Expr *NumIterations = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004484 /// The loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004485 Expr *CounterVar = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004486 /// Private loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004487 Expr *PrivateCounterVar = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004488 /// This is initializer for the initial value of #CounterVar.
Alexey Bataev8b427062016-05-25 12:36:08 +00004489 Expr *CounterInit = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004490 /// This is step for the #CounterVar used to generate its update:
Alexander Musmana5f070a2014-10-01 06:03:56 +00004491 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
Alexey Bataev8b427062016-05-25 12:36:08 +00004492 Expr *CounterStep = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004493 /// Should step be subtracted?
Alexey Bataev8b427062016-05-25 12:36:08 +00004494 bool Subtract = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004495 /// Source range of the loop init.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004496 SourceRange InitSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004497 /// Source range of the loop condition.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004498 SourceRange CondSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004499 /// Source range of the loop increment.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004500 SourceRange IncSrcRange;
4501};
4502
Alexey Bataev23b69422014-06-18 07:08:49 +00004503} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004504
Alexey Bataev9c821032015-04-30 04:23:23 +00004505void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4506 assert(getLangOpts().OpenMP && "OpenMP is not active.");
4507 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004508 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4509 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00004510 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
4511 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataeve3727102018-04-18 15:57:46 +00004512 if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) {
4513 if (ValueDecl *D = ISC.getLoopDecl()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004514 auto *VD = dyn_cast<VarDecl>(D);
4515 if (!VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004516 if (VarDecl *Private = isOpenMPCapturedDecl(D)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004517 VD = Private;
Alexey Bataeve3727102018-04-18 15:57:46 +00004518 } else {
4519 DeclRefExpr *Ref = buildCapture(*this, D, ISC.getLoopDeclRefExpr(),
4520 /*WithInit=*/false);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004521 VD = cast<VarDecl>(Ref->getDecl());
4522 }
4523 }
4524 DSAStack->addLoopControlVariable(D, VD);
4525 }
4526 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004527 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00004528 }
4529}
4530
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004531/// Called on a for stmt to check and extract its iteration space
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004532/// for further processing (such as collapsing).
Alexey Bataeve3727102018-04-18 15:57:46 +00004533static bool checkOpenMPIterationSpace(
Alexey Bataev4acb8592014-07-07 13:01:15 +00004534 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4535 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataevf138fda2018-08-13 19:04:24 +00004536 unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr,
4537 Expr *OrderedLoopCountExpr,
Alexey Bataeve3727102018-04-18 15:57:46 +00004538 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004539 LoopIterationSpace &ResultIterSpace,
Alexey Bataeve3727102018-04-18 15:57:46 +00004540 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004541 // OpenMP [2.6, Canonical Loop Form]
4542 // for (init-expr; test-expr; incr-expr) structured-block
David Majnemer9d168222016-08-05 17:44:54 +00004543 auto *For = dyn_cast_or_null<ForStmt>(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004544 if (!For) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004545 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004546 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
Alexey Bataevf138fda2018-08-13 19:04:24 +00004547 << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount
Alexey Bataev10e775f2015-07-30 11:36:16 +00004548 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
Alexey Bataevf138fda2018-08-13 19:04:24 +00004549 if (TotalNestedLoopCount > 1) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00004550 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4551 SemaRef.Diag(DSA.getConstructLoc(),
4552 diag::note_omp_collapse_ordered_expr)
4553 << 2 << CollapseLoopCountExpr->getSourceRange()
4554 << OrderedLoopCountExpr->getSourceRange();
4555 else if (CollapseLoopCountExpr)
4556 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4557 diag::note_omp_collapse_ordered_expr)
4558 << 0 << CollapseLoopCountExpr->getSourceRange();
4559 else
4560 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4561 diag::note_omp_collapse_ordered_expr)
4562 << 1 << OrderedLoopCountExpr->getSourceRange();
4563 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004564 return true;
4565 }
4566 assert(For->getBody());
4567
4568 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4569
4570 // Check init.
Alexey Bataeve3727102018-04-18 15:57:46 +00004571 Stmt *Init = For->getInit();
4572 if (ISC.checkAndSetInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004573 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004574
4575 bool HasErrors = false;
4576
4577 // Check loop variable's type.
Alexey Bataeve3727102018-04-18 15:57:46 +00004578 if (ValueDecl *LCDecl = ISC.getLoopDecl()) {
4579 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004580
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004581 // OpenMP [2.6, Canonical Loop Form]
4582 // Var is one of the following:
4583 // A variable of signed or unsigned integer type.
4584 // For C++, a variable of a random access iterator type.
4585 // For C, a variable of a pointer type.
Alexey Bataeve3727102018-04-18 15:57:46 +00004586 QualType VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004587 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4588 !VarType->isPointerType() &&
4589 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004590 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004591 << SemaRef.getLangOpts().CPlusPlus;
4592 HasErrors = true;
4593 }
4594
4595 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4596 // a Construct
4597 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4598 // parallel for construct is (are) private.
4599 // The loop iteration variable in the associated for-loop of a simd
4600 // construct with just one associated for-loop is linear with a
4601 // constant-linear-step that is the increment of the associated for-loop.
4602 // Exclude loop var from the list of variables with implicitly defined data
4603 // sharing attributes.
4604 VarsWithImplicitDSA.erase(LCDecl);
4605
4606 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4607 // in a Construct, C/C++].
4608 // The loop iteration variable in the associated for-loop of a simd
4609 // construct with just one associated for-loop may be listed in a linear
4610 // clause with a constant-linear-step that is the increment of the
4611 // associated for-loop.
4612 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4613 // parallel for construct may be listed in a private or lastprivate clause.
4614 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4615 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4616 // declared in the loop and it is predetermined as a private.
Alexey Bataeve3727102018-04-18 15:57:46 +00004617 OpenMPClauseKind PredeterminedCKind =
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004618 isOpenMPSimdDirective(DKind)
4619 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4620 : OMPC_private;
4621 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4622 DVar.CKind != PredeterminedCKind) ||
4623 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4624 isOpenMPDistributeDirective(DKind)) &&
4625 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4626 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4627 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004628 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004629 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4630 << getOpenMPClauseName(PredeterminedCKind);
4631 if (DVar.RefExpr == nullptr)
4632 DVar.CKind = PredeterminedCKind;
Alexey Bataeve3727102018-04-18 15:57:46 +00004633 reportOriginalDsa(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004634 HasErrors = true;
4635 } else if (LoopDeclRefExpr != nullptr) {
4636 // Make the loop iteration variable private (for worksharing constructs),
4637 // linear (for simd directives with the only one associated loop) or
4638 // lastprivate (for simd directives with several collapsed or ordered
4639 // loops).
4640 if (DVar.CKind == OMPC_unknown)
Alexey Bataev7ace49d2016-05-17 08:55:33 +00004641 DVar = DSA.hasDSA(LCDecl, isOpenMPPrivate,
4642 [](OpenMPDirectiveKind) -> bool { return true; },
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004643 /*FromParent=*/false);
4644 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
4645 }
4646
4647 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4648
4649 // Check test-expr.
Alexey Bataeve3727102018-04-18 15:57:46 +00004650 HasErrors |= ISC.checkAndSetCond(For->getCond());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004651
4652 // Check incr-expr.
Alexey Bataeve3727102018-04-18 15:57:46 +00004653 HasErrors |= ISC.checkAndSetInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004654 }
4655
Alexey Bataeve3727102018-04-18 15:57:46 +00004656 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004657 return HasErrors;
4658
Alexander Musmana5f070a2014-10-01 06:03:56 +00004659 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004660 ResultIterSpace.PreCond =
Alexey Bataeve3727102018-04-18 15:57:46 +00004661 ISC.buildPreCond(DSA.getCurScope(), For->getCond(), Captures);
4662 ResultIterSpace.NumIterations = ISC.buildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004663 DSA.getCurScope(),
4664 (isOpenMPWorksharingDirective(DKind) ||
4665 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4666 Captures);
Alexey Bataeve3727102018-04-18 15:57:46 +00004667 ResultIterSpace.CounterVar = ISC.buildCounterVar(Captures, DSA);
4668 ResultIterSpace.PrivateCounterVar = ISC.buildPrivateCounterVar();
4669 ResultIterSpace.CounterInit = ISC.buildCounterInit();
4670 ResultIterSpace.CounterStep = ISC.buildCounterStep();
4671 ResultIterSpace.InitSrcRange = ISC.getInitSrcRange();
4672 ResultIterSpace.CondSrcRange = ISC.getConditionSrcRange();
4673 ResultIterSpace.IncSrcRange = ISC.getIncrementSrcRange();
4674 ResultIterSpace.Subtract = ISC.shouldSubtractStep();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004675
Alexey Bataev62dbb972015-04-22 11:59:37 +00004676 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4677 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004678 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004679 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004680 ResultIterSpace.CounterInit == nullptr ||
4681 ResultIterSpace.CounterStep == nullptr);
Alexey Bataevf138fda2018-08-13 19:04:24 +00004682 if (!HasErrors && DSA.isOrderedRegion()) {
4683 if (DSA.getOrderedRegionParam().second->getNumForLoops()) {
4684 if (CurrentNestedLoopCount <
4685 DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) {
4686 DSA.getOrderedRegionParam().second->setLoopNumIterations(
4687 CurrentNestedLoopCount, ResultIterSpace.NumIterations);
4688 DSA.getOrderedRegionParam().second->setLoopCounter(
4689 CurrentNestedLoopCount, ResultIterSpace.CounterVar);
4690 }
4691 }
4692 for (auto &Pair : DSA.getDoacrossDependClauses()) {
4693 if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) {
4694 // Erroneous case - clause has some problems.
4695 continue;
4696 }
4697 if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink &&
4698 Pair.second.size() <= CurrentNestedLoopCount) {
4699 // Erroneous case - clause has some problems.
4700 Pair.first->setLoopData(CurrentNestedLoopCount, nullptr);
4701 continue;
4702 }
4703 Expr *CntValue;
4704 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
4705 CntValue = ISC.buildOrderedLoopData(
4706 DSA.getCurScope(), ResultIterSpace.CounterVar, Captures,
4707 Pair.first->getDependencyLoc());
4708 else
4709 CntValue = ISC.buildOrderedLoopData(
4710 DSA.getCurScope(), ResultIterSpace.CounterVar, Captures,
4711 Pair.first->getDependencyLoc(),
4712 Pair.second[CurrentNestedLoopCount].first,
4713 Pair.second[CurrentNestedLoopCount].second);
4714 Pair.first->setLoopData(CurrentNestedLoopCount, CntValue);
4715 }
4716 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004717
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004718 return HasErrors;
4719}
4720
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004721/// Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004722static ExprResult
Alexey Bataeve3727102018-04-18 15:57:46 +00004723buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004724 ExprResult Start,
Alexey Bataeve3727102018-04-18 15:57:46 +00004725 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004726 // Build 'VarRef = Start.
Alexey Bataeve3727102018-04-18 15:57:46 +00004727 ExprResult NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004728 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004729 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004730 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00004731 VarRef.get()->getType())) {
4732 NewStart = SemaRef.PerformImplicitConversion(
4733 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4734 /*AllowExplicit=*/true);
4735 if (!NewStart.isUsable())
4736 return ExprError();
4737 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004738
Alexey Bataeve3727102018-04-18 15:57:46 +00004739 ExprResult Init =
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004740 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4741 return Init;
4742}
4743
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004744/// Build 'VarRef = Start + Iter * Step'.
Alexey Bataeve3727102018-04-18 15:57:46 +00004745static ExprResult buildCounterUpdate(
4746 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4747 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract,
4748 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004749 // Add parentheses (for debugging purposes only).
4750 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4751 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4752 !Step.isUsable())
4753 return ExprError();
4754
Alexey Bataev5a3af132016-03-29 08:58:54 +00004755 ExprResult NewStep = Step;
4756 if (Captures)
4757 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004758 if (NewStep.isInvalid())
4759 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004760 ExprResult Update =
4761 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004762 if (!Update.isUsable())
4763 return ExprError();
4764
Alexey Bataevc0214e02016-02-16 12:13:49 +00004765 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4766 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004767 ExprResult NewStart = Start;
4768 if (Captures)
4769 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004770 if (NewStart.isInvalid())
4771 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004772
Alexey Bataevc0214e02016-02-16 12:13:49 +00004773 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
4774 ExprResult SavedUpdate = Update;
4775 ExprResult UpdateVal;
4776 if (VarRef.get()->getType()->isOverloadableType() ||
4777 NewStart.get()->getType()->isOverloadableType() ||
4778 Update.get()->getType()->isOverloadableType()) {
4779 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4780 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
4781 Update =
4782 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4783 if (Update.isUsable()) {
4784 UpdateVal =
4785 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
4786 VarRef.get(), SavedUpdate.get());
4787 if (UpdateVal.isUsable()) {
4788 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
4789 UpdateVal.get());
4790 }
4791 }
4792 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4793 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004794
Alexey Bataevc0214e02016-02-16 12:13:49 +00004795 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
4796 if (!Update.isUsable() || !UpdateVal.isUsable()) {
4797 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
4798 NewStart.get(), SavedUpdate.get());
4799 if (!Update.isUsable())
4800 return ExprError();
4801
Alexey Bataev11481f52016-02-17 10:29:05 +00004802 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
4803 VarRef.get()->getType())) {
4804 Update = SemaRef.PerformImplicitConversion(
4805 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
4806 if (!Update.isUsable())
4807 return ExprError();
4808 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00004809
4810 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
4811 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004812 return Update;
4813}
4814
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004815/// Convert integer expression \a E to make it have at least \a Bits
Alexander Musmana5f070a2014-10-01 06:03:56 +00004816/// bits.
Alexey Bataeve3727102018-04-18 15:57:46 +00004817static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004818 if (E == nullptr)
4819 return ExprError();
Alexey Bataeve3727102018-04-18 15:57:46 +00004820 ASTContext &C = SemaRef.Context;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004821 QualType OldType = E->getType();
4822 unsigned HasBits = C.getTypeSize(OldType);
4823 if (HasBits >= Bits)
4824 return ExprResult(E);
4825 // OK to convert to signed, because new type has more bits than old.
4826 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
4827 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
4828 true);
4829}
4830
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004831/// Check if the given expression \a E is a constant integer that fits
Alexander Musmana5f070a2014-10-01 06:03:56 +00004832/// into \a Bits bits.
Alexey Bataeve3727102018-04-18 15:57:46 +00004833static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004834 if (E == nullptr)
4835 return false;
4836 llvm::APSInt Result;
4837 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
4838 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
4839 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004840}
4841
Alexey Bataev5a3af132016-03-29 08:58:54 +00004842/// Build preinits statement for the given declarations.
4843static Stmt *buildPreInits(ASTContext &Context,
Alexey Bataevc5514062017-10-25 15:44:52 +00004844 MutableArrayRef<Decl *> PreInits) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004845 if (!PreInits.empty()) {
4846 return new (Context) DeclStmt(
4847 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
4848 SourceLocation(), SourceLocation());
4849 }
4850 return nullptr;
4851}
4852
4853/// Build preinits statement for the given declarations.
Alexey Bataevc5514062017-10-25 15:44:52 +00004854static Stmt *
4855buildPreInits(ASTContext &Context,
Alexey Bataeve3727102018-04-18 15:57:46 +00004856 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004857 if (!Captures.empty()) {
4858 SmallVector<Decl *, 16> PreInits;
Alexey Bataeve3727102018-04-18 15:57:46 +00004859 for (const auto &Pair : Captures)
Alexey Bataev5a3af132016-03-29 08:58:54 +00004860 PreInits.push_back(Pair.second->getDecl());
4861 return buildPreInits(Context, PreInits);
4862 }
4863 return nullptr;
4864}
4865
4866/// Build postupdate expression for the given list of postupdates expressions.
4867static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
4868 Expr *PostUpdate = nullptr;
4869 if (!PostUpdates.empty()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004870 for (Expr *E : PostUpdates) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004871 Expr *ConvE = S.BuildCStyleCastExpr(
4872 E->getExprLoc(),
4873 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
4874 E->getExprLoc(), E)
4875 .get();
4876 PostUpdate = PostUpdate
4877 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
4878 PostUpdate, ConvE)
4879 .get()
4880 : ConvE;
4881 }
4882 }
4883 return PostUpdate;
4884}
4885
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004886/// Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00004887/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
4888/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004889static unsigned
Alexey Bataeve3727102018-04-18 15:57:46 +00004890checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
Alexey Bataev10e775f2015-07-30 11:36:16 +00004891 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
4892 DSAStackTy &DSA,
Alexey Bataeve3727102018-04-18 15:57:46 +00004893 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00004894 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004895 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004896 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004897 // Found 'collapse' clause - calculate collapse number.
4898 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004899 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004900 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004901 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00004902 unsigned OrderedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004903 if (OrderedLoopCountExpr) {
4904 // Found 'ordered' clause - calculate collapse number.
4905 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004906 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
4907 if (Result.getLimitedValue() < NestedLoopCount) {
4908 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4909 diag::err_omp_wrong_ordered_loop_count)
4910 << OrderedLoopCountExpr->getSourceRange();
4911 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4912 diag::note_collapse_loop_count)
4913 << CollapseLoopCountExpr->getSourceRange();
4914 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00004915 OrderedLoopCount = Result.getLimitedValue();
Alexey Bataev7b6bc882015-11-26 07:50:39 +00004916 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004917 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004918 // This is helper routine for loop directives (e.g., 'for', 'simd',
4919 // 'for simd', etc.).
Alexey Bataeve3727102018-04-18 15:57:46 +00004920 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004921 SmallVector<LoopIterationSpace, 4> IterSpaces;
Alexey Bataevf138fda2018-08-13 19:04:24 +00004922 IterSpaces.resize(std::max(OrderedLoopCount, NestedLoopCount));
Alexander Musmana5f070a2014-10-01 06:03:56 +00004923 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004924 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataevf138fda2018-08-13 19:04:24 +00004925 if (checkOpenMPIterationSpace(
4926 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
4927 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
4928 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt],
4929 Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00004930 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004931 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004932 // OpenMP [2.8.1, simd construct, Restrictions]
4933 // All loops associated with the construct must be perfectly nested; that
4934 // is, there must be no intervening code nor any OpenMP directive between
4935 // any two loops.
4936 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004937 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00004938 for (unsigned Cnt = NestedLoopCount; Cnt < OrderedLoopCount; ++Cnt) {
4939 if (checkOpenMPIterationSpace(
4940 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
4941 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
4942 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt],
4943 Captures))
4944 return 0;
4945 if (Cnt > 0 && IterSpaces[Cnt].CounterVar) {
4946 // Handle initialization of captured loop iterator variables.
4947 auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar);
4948 if (isa<OMPCapturedExprDecl>(DRE->getDecl())) {
4949 Captures[DRE] = DRE;
4950 }
4951 }
4952 // Move on to the next nested for loop, or to the loop body.
4953 // OpenMP [2.8.1, simd construct, Restrictions]
4954 // All loops associated with the construct must be perfectly nested; that
4955 // is, there must be no intervening code nor any OpenMP directive between
4956 // any two loops.
4957 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
4958 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004959
Alexander Musmana5f070a2014-10-01 06:03:56 +00004960 Built.clear(/* size */ NestedLoopCount);
4961
4962 if (SemaRef.CurContext->isDependentContext())
4963 return NestedLoopCount;
4964
4965 // An example of what is generated for the following code:
4966 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00004967 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004968 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004969 // for (k = 0; k < NK; ++k)
4970 // for (j = J0; j < NJ; j+=2) {
4971 // <loop body>
4972 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004973 //
4974 // We generate the code below.
4975 // Note: the loop body may be outlined in CodeGen.
4976 // Note: some counters may be C++ classes, operator- is used to find number of
4977 // iterations and operator+= to calculate counter value.
4978 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
4979 // or i64 is currently supported).
4980 //
4981 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
4982 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
4983 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
4984 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
4985 // // similar updates for vars in clauses (e.g. 'linear')
4986 // <loop body (using local i and j)>
4987 // }
4988 // i = NI; // assign final values of counters
4989 // j = NJ;
4990 //
4991
4992 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
4993 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00004994 // Precondition tests if there is at least one iteration (all conditions are
4995 // true).
4996 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexey Bataeve3727102018-04-18 15:57:46 +00004997 Expr *N0 = IterSpaces[0].NumIterations;
4998 ExprResult LastIteration32 =
4999 widenIterationCount(/*Bits=*/32,
5000 SemaRef
5001 .PerformImplicitConversion(
5002 N0->IgnoreImpCasts(), N0->getType(),
5003 Sema::AA_Converting, /*AllowExplicit=*/true)
5004 .get(),
5005 SemaRef);
5006 ExprResult LastIteration64 = widenIterationCount(
5007 /*Bits=*/64,
5008 SemaRef
5009 .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(),
5010 Sema::AA_Converting,
5011 /*AllowExplicit=*/true)
5012 .get(),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005013 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005014
5015 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
5016 return NestedLoopCount;
5017
Alexey Bataeve3727102018-04-18 15:57:46 +00005018 ASTContext &C = SemaRef.Context;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005019 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
5020
5021 Scope *CurScope = DSA.getCurScope();
5022 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00005023 if (PreCond.isUsable()) {
Alexey Bataeva7206b92016-12-20 16:51:02 +00005024 PreCond =
5025 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd,
5026 PreCond.get(), IterSpaces[Cnt].PreCond);
Alexey Bataev62dbb972015-04-22 11:59:37 +00005027 }
Alexey Bataeve3727102018-04-18 15:57:46 +00005028 Expr *N = IterSpaces[Cnt].NumIterations;
Alexey Bataeva7206b92016-12-20 16:51:02 +00005029 SourceLocation Loc = N->getExprLoc();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005030 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
5031 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005032 LastIteration32 = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005033 CurScope, Loc, BO_Mul, LastIteration32.get(),
David Majnemer9d168222016-08-05 17:44:54 +00005034 SemaRef
5035 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5036 Sema::AA_Converting,
5037 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005038 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005039 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005040 LastIteration64 = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005041 CurScope, Loc, BO_Mul, LastIteration64.get(),
David Majnemer9d168222016-08-05 17:44:54 +00005042 SemaRef
5043 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5044 Sema::AA_Converting,
5045 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005046 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005047 }
5048
5049 // Choose either the 32-bit or 64-bit version.
5050 ExprResult LastIteration = LastIteration64;
5051 if (LastIteration32.isUsable() &&
5052 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
5053 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
Alexey Bataeve3727102018-04-18 15:57:46 +00005054 fitsInto(
5055 /*Bits=*/32,
Alexander Musmana5f070a2014-10-01 06:03:56 +00005056 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
5057 LastIteration64.get(), SemaRef)))
5058 LastIteration = LastIteration32;
Alexey Bataev7292c292016-04-25 12:22:29 +00005059 QualType VType = LastIteration.get()->getType();
5060 QualType RealVType = VType;
5061 QualType StrideVType = VType;
5062 if (isOpenMPTaskLoopDirective(DKind)) {
5063 VType =
5064 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
5065 StrideVType =
5066 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
5067 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005068
5069 if (!LastIteration.isUsable())
5070 return 0;
5071
5072 // Save the number of iterations.
5073 ExprResult NumIterations = LastIteration;
5074 {
5075 LastIteration = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005076 CurScope, LastIteration.get()->getExprLoc(), BO_Sub,
5077 LastIteration.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00005078 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5079 if (!LastIteration.isUsable())
5080 return 0;
5081 }
5082
5083 // Calculate the last iteration number beforehand instead of doing this on
5084 // each iteration. Do not do this if the number of iterations may be kfold-ed.
5085 llvm::APSInt Result;
5086 bool IsConstant =
5087 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
5088 ExprResult CalcLastIteration;
5089 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005090 ExprResult SaveRef =
5091 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005092 LastIteration = SaveRef;
5093
5094 // Prepare SaveRef + 1.
5095 NumIterations = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005096 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00005097 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5098 if (!NumIterations.isUsable())
5099 return 0;
5100 }
5101
5102 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
5103
David Majnemer9d168222016-08-05 17:44:54 +00005104 // Build variables passed into runtime, necessary for worksharing directives.
Carlo Bertolliffafe102017-04-20 00:39:39 +00005105 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005106 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5107 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005108 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005109 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
5110 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005111 SemaRef.AddInitializerToDecl(LBDecl,
5112 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5113 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005114
5115 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005116 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
5117 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005118 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
Richard Smith3beb7c62017-01-12 02:27:38 +00005119 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005120
5121 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
5122 // This will be used to implement clause 'lastprivate'.
5123 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005124 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
5125 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005126 SemaRef.AddInitializerToDecl(ILDecl,
5127 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5128 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005129
5130 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev7292c292016-04-25 12:22:29 +00005131 VarDecl *STDecl =
5132 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
5133 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005134 SemaRef.AddInitializerToDecl(STDecl,
5135 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
5136 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005137
5138 // Build expression: UB = min(UB, LastIteration)
David Majnemer9d168222016-08-05 17:44:54 +00005139 // It is necessary for CodeGen of directives with static scheduling.
Alexander Musmanc6388682014-12-15 07:07:06 +00005140 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
5141 UB.get(), LastIteration.get());
5142 ExprResult CondOp = SemaRef.ActOnConditionalOp(
Alexey Bataev86ec3fe2018-07-25 14:40:26 +00005143 LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(),
5144 LastIteration.get(), UB.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00005145 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
5146 CondOp.get());
5147 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
Carlo Bertolli9925f152016-06-27 14:55:37 +00005148
5149 // If we have a combined directive that combines 'distribute', 'for' or
5150 // 'simd' we need to be able to access the bounds of the schedule of the
5151 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
5152 // by scheduling 'distribute' have to be passed to the schedule of 'for'.
5153 if (isOpenMPLoopBoundSharingDirective(DKind)) {
Carlo Bertolliffafe102017-04-20 00:39:39 +00005154 // Lower bound variable, initialized with zero.
5155 VarDecl *CombLBDecl =
5156 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb");
5157 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc);
5158 SemaRef.AddInitializerToDecl(
5159 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5160 /*DirectInit*/ false);
5161
5162 // Upper bound variable, initialized with last iteration number.
5163 VarDecl *CombUBDecl =
5164 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub");
5165 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc);
5166 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(),
5167 /*DirectInit*/ false);
5168
5169 ExprResult CombIsUBGreater = SemaRef.BuildBinOp(
5170 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get());
5171 ExprResult CombCondOp =
5172 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(),
5173 LastIteration.get(), CombUB.get());
5174 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(),
5175 CombCondOp.get());
5176 CombEUB = SemaRef.ActOnFinishFullExpr(CombEUB.get());
5177
Alexey Bataeve3727102018-04-18 15:57:46 +00005178 const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005179 // We expect to have at least 2 more parameters than the 'parallel'
5180 // directive does - the lower and upper bounds of the previous schedule.
5181 assert(CD->getNumParams() >= 4 &&
5182 "Unexpected number of parameters in loop combined directive");
5183
5184 // Set the proper type for the bounds given what we learned from the
5185 // enclosed loops.
Alexey Bataeve3727102018-04-18 15:57:46 +00005186 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
5187 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
Carlo Bertolli9925f152016-06-27 14:55:37 +00005188
5189 // Previous lower and upper bounds are obtained from the region
5190 // parameters.
5191 PrevLB =
5192 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
5193 PrevUB =
5194 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
5195 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005196 }
5197
5198 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005199 ExprResult IV;
Carlo Bertolliffafe102017-04-20 00:39:39 +00005200 ExprResult Init, CombInit;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005201 {
Alexey Bataev7292c292016-04-25 12:22:29 +00005202 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
5203 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
David Majnemer9d168222016-08-05 17:44:54 +00005204 Expr *RHS =
5205 (isOpenMPWorksharingDirective(DKind) ||
5206 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
5207 ? LB.get()
5208 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005209 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
5210 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Carlo Bertolliffafe102017-04-20 00:39:39 +00005211
5212 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5213 Expr *CombRHS =
5214 (isOpenMPWorksharingDirective(DKind) ||
5215 isOpenMPTaskLoopDirective(DKind) ||
5216 isOpenMPDistributeDirective(DKind))
5217 ? CombLB.get()
5218 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
5219 CombInit =
5220 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS);
5221 CombInit = SemaRef.ActOnFinishFullExpr(CombInit.get());
5222 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005223 }
5224
Alexander Musmanc6388682014-12-15 07:07:06 +00005225 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005226 SourceLocation CondLoc = AStmt->getBeginLoc();
Alexander Musmanc6388682014-12-15 07:07:06 +00005227 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005228 (isOpenMPWorksharingDirective(DKind) ||
5229 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00005230 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
5231 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
5232 NumIterations.get());
Carlo Bertolliffafe102017-04-20 00:39:39 +00005233 ExprResult CombCond;
5234 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5235 CombCond =
5236 SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), CombUB.get());
5237 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005238 // Loop increment (IV = IV + 1)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005239 SourceLocation IncLoc = AStmt->getBeginLoc();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005240 ExprResult Inc =
5241 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
5242 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
5243 if (!Inc.isUsable())
5244 return 0;
5245 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00005246 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
5247 if (!Inc.isUsable())
5248 return 0;
5249
5250 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
5251 // Used for directives with static scheduling.
Carlo Bertolliffafe102017-04-20 00:39:39 +00005252 // In combined construct, add combined version that use CombLB and CombUB
5253 // base variables for the update
5254 ExprResult NextLB, NextUB, CombNextLB, CombNextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005255 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5256 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005257 // LB + ST
5258 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
5259 if (!NextLB.isUsable())
5260 return 0;
5261 // LB = LB + ST
5262 NextLB =
5263 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
5264 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
5265 if (!NextLB.isUsable())
5266 return 0;
5267 // UB + ST
5268 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
5269 if (!NextUB.isUsable())
5270 return 0;
5271 // UB = UB + ST
5272 NextUB =
5273 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
5274 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
5275 if (!NextUB.isUsable())
5276 return 0;
Carlo Bertolliffafe102017-04-20 00:39:39 +00005277 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5278 CombNextLB =
5279 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get());
5280 if (!NextLB.isUsable())
5281 return 0;
5282 // LB = LB + ST
5283 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(),
5284 CombNextLB.get());
5285 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get());
5286 if (!CombNextLB.isUsable())
5287 return 0;
5288 // UB + ST
5289 CombNextUB =
5290 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get());
5291 if (!CombNextUB.isUsable())
5292 return 0;
5293 // UB = UB + ST
5294 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(),
5295 CombNextUB.get());
5296 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get());
5297 if (!CombNextUB.isUsable())
5298 return 0;
5299 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005300 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005301
Carlo Bertolliffafe102017-04-20 00:39:39 +00005302 // Create increment expression for distribute loop when combined in a same
Carlo Bertolli8429d812017-02-17 21:29:13 +00005303 // directive with for as IV = IV + ST; ensure upper bound expression based
5304 // on PrevUB instead of NumIterations - used to implement 'for' when found
5305 // in combination with 'distribute', like in 'distribute parallel for'
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005306 SourceLocation DistIncLoc = AStmt->getBeginLoc();
Carlo Bertolli8429d812017-02-17 21:29:13 +00005307 ExprResult DistCond, DistInc, PrevEUB;
5308 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5309 DistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get());
5310 assert(DistCond.isUsable() && "distribute cond expr was not built");
5311
5312 DistInc =
5313 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get());
5314 assert(DistInc.isUsable() && "distribute inc expr was not built");
5315 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(),
5316 DistInc.get());
5317 DistInc = SemaRef.ActOnFinishFullExpr(DistInc.get());
5318 assert(DistInc.isUsable() && "distribute inc expr was not built");
5319
5320 // Build expression: UB = min(UB, prevUB) for #for in composite or combined
5321 // construct
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005322 SourceLocation DistEUBLoc = AStmt->getBeginLoc();
Carlo Bertolli8429d812017-02-17 21:29:13 +00005323 ExprResult IsUBGreater =
5324 SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get());
5325 ExprResult CondOp = SemaRef.ActOnConditionalOp(
5326 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get());
5327 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(),
5328 CondOp.get());
5329 PrevEUB = SemaRef.ActOnFinishFullExpr(PrevEUB.get());
5330 }
5331
Alexander Musmana5f070a2014-10-01 06:03:56 +00005332 // Build updates and final values of the loop counters.
5333 bool HasErrors = false;
5334 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005335 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005336 Built.Updates.resize(NestedLoopCount);
5337 Built.Finals.resize(NestedLoopCount);
5338 {
5339 ExprResult Div;
5340 // Go from inner nested loop to outer.
5341 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
5342 LoopIterationSpace &IS = IterSpaces[Cnt];
5343 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
5344 // Build: Iter = (IV / Div) % IS.NumIters
5345 // where Div is product of previous iterations' IS.NumIters.
5346 ExprResult Iter;
5347 if (Div.isUsable()) {
5348 Iter =
5349 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
5350 } else {
5351 Iter = IV;
5352 assert((Cnt == (int)NestedLoopCount - 1) &&
5353 "unusable div expected on first iteration only");
5354 }
5355
5356 if (Cnt != 0 && Iter.isUsable())
5357 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
5358 IS.NumIterations);
5359 if (!Iter.isUsable()) {
5360 HasErrors = true;
5361 break;
5362 }
5363
Alexey Bataev39f915b82015-05-08 10:41:21 +00005364 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005365 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00005366 DeclRefExpr *CounterVar = buildDeclRefExpr(
5367 SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
5368 /*RefersToCapture=*/true);
5369 ExprResult Init = buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005370 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005371 if (!Init.isUsable()) {
5372 HasErrors = true;
5373 break;
5374 }
Alexey Bataeve3727102018-04-18 15:57:46 +00005375 ExprResult Update = buildCounterUpdate(
Alexey Bataev5a3af132016-03-29 08:58:54 +00005376 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
5377 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005378 if (!Update.isUsable()) {
5379 HasErrors = true;
5380 break;
5381 }
5382
5383 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
Alexey Bataeve3727102018-04-18 15:57:46 +00005384 ExprResult Final = buildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00005385 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005386 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005387 if (!Final.isUsable()) {
5388 HasErrors = true;
5389 break;
5390 }
5391
5392 // Build Div for the next iteration: Div <- Div * IS.NumIters
5393 if (Cnt != 0) {
5394 if (Div.isUnset())
5395 Div = IS.NumIterations;
5396 else
5397 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
5398 IS.NumIterations);
5399
5400 // Add parentheses (for debugging purposes only).
5401 if (Div.isUsable())
Alexey Bataev8b427062016-05-25 12:36:08 +00005402 Div = tryBuildCapture(SemaRef, Div.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005403 if (!Div.isUsable()) {
5404 HasErrors = true;
5405 break;
5406 }
5407 }
5408 if (!Update.isUsable() || !Final.isUsable()) {
5409 HasErrors = true;
5410 break;
5411 }
5412 // Save results
5413 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00005414 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005415 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005416 Built.Updates[Cnt] = Update.get();
5417 Built.Finals[Cnt] = Final.get();
5418 }
5419 }
5420
5421 if (HasErrors)
5422 return 0;
5423
5424 // Save results
5425 Built.IterationVarRef = IV.get();
5426 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00005427 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00005428 Built.CalcLastIteration =
5429 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005430 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00005431 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005432 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005433 Built.Init = Init.get();
5434 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005435 Built.LB = LB.get();
5436 Built.UB = UB.get();
5437 Built.IL = IL.get();
5438 Built.ST = ST.get();
5439 Built.EUB = EUB.get();
5440 Built.NLB = NextLB.get();
5441 Built.NUB = NextUB.get();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005442 Built.PrevLB = PrevLB.get();
5443 Built.PrevUB = PrevUB.get();
Carlo Bertolli8429d812017-02-17 21:29:13 +00005444 Built.DistInc = DistInc.get();
5445 Built.PrevEUB = PrevEUB.get();
Carlo Bertolliffafe102017-04-20 00:39:39 +00005446 Built.DistCombinedFields.LB = CombLB.get();
5447 Built.DistCombinedFields.UB = CombUB.get();
5448 Built.DistCombinedFields.EUB = CombEUB.get();
5449 Built.DistCombinedFields.Init = CombInit.get();
5450 Built.DistCombinedFields.Cond = CombCond.get();
5451 Built.DistCombinedFields.NLB = CombNextLB.get();
5452 Built.DistCombinedFields.NUB = CombNextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005453
Alexey Bataevabfc0692014-06-25 06:52:00 +00005454 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005455}
5456
Alexey Bataev10e775f2015-07-30 11:36:16 +00005457static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005458 auto CollapseClauses =
5459 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
5460 if (CollapseClauses.begin() != CollapseClauses.end())
5461 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005462 return nullptr;
5463}
5464
Alexey Bataev10e775f2015-07-30 11:36:16 +00005465static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005466 auto OrderedClauses =
5467 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
5468 if (OrderedClauses.begin() != OrderedClauses.end())
5469 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005470 return nullptr;
5471}
5472
Kelvin Lic5609492016-07-15 04:39:07 +00005473static bool checkSimdlenSafelenSpecified(Sema &S,
5474 const ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00005475 const OMPSafelenClause *Safelen = nullptr;
5476 const OMPSimdlenClause *Simdlen = nullptr;
Kelvin Lic5609492016-07-15 04:39:07 +00005477
Alexey Bataeve3727102018-04-18 15:57:46 +00005478 for (const OMPClause *Clause : Clauses) {
Kelvin Lic5609492016-07-15 04:39:07 +00005479 if (Clause->getClauseKind() == OMPC_safelen)
5480 Safelen = cast<OMPSafelenClause>(Clause);
5481 else if (Clause->getClauseKind() == OMPC_simdlen)
5482 Simdlen = cast<OMPSimdlenClause>(Clause);
5483 if (Safelen && Simdlen)
5484 break;
5485 }
5486
5487 if (Simdlen && Safelen) {
5488 llvm::APSInt SimdlenRes, SafelenRes;
Alexey Bataeve3727102018-04-18 15:57:46 +00005489 const Expr *SimdlenLength = Simdlen->getSimdlen();
5490 const Expr *SafelenLength = Safelen->getSafelen();
Kelvin Lic5609492016-07-15 04:39:07 +00005491 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
5492 SimdlenLength->isInstantiationDependent() ||
5493 SimdlenLength->containsUnexpandedParameterPack())
5494 return false;
5495 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
5496 SafelenLength->isInstantiationDependent() ||
5497 SafelenLength->containsUnexpandedParameterPack())
5498 return false;
5499 SimdlenLength->EvaluateAsInt(SimdlenRes, S.Context);
5500 SafelenLength->EvaluateAsInt(SafelenRes, S.Context);
5501 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
5502 // If both simdlen and safelen clauses are specified, the value of the
5503 // simdlen parameter must be less than or equal to the value of the safelen
5504 // parameter.
5505 if (SimdlenRes > SafelenRes) {
5506 S.Diag(SimdlenLength->getExprLoc(),
5507 diag::err_omp_wrong_simdlen_safelen_values)
5508 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
5509 return true;
5510 }
Alexey Bataev66b15b52015-08-21 11:14:16 +00005511 }
5512 return false;
5513}
5514
Alexey Bataeve3727102018-04-18 15:57:46 +00005515StmtResult
5516Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
5517 SourceLocation StartLoc, SourceLocation EndLoc,
5518 VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005519 if (!AStmt)
5520 return StmtError();
5521
5522 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005523 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005524 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5525 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00005526 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataev10e775f2015-07-30 11:36:16 +00005527 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5528 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005529 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005530 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005531
Alexander Musmana5f070a2014-10-01 06:03:56 +00005532 assert((CurContext->isDependentContext() || B.builtAll()) &&
5533 "omp simd loop exprs were not built");
5534
Alexander Musman3276a272015-03-21 10:12:56 +00005535 if (!CurContext->isDependentContext()) {
5536 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005537 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005538 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexander Musman3276a272015-03-21 10:12:56 +00005539 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005540 B.NumIterations, *this, CurScope,
5541 DSAStack))
Alexander Musman3276a272015-03-21 10:12:56 +00005542 return StmtError();
5543 }
5544 }
5545
Kelvin Lic5609492016-07-15 04:39:07 +00005546 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005547 return StmtError();
5548
Reid Kleckner87a31802018-03-12 21:43:02 +00005549 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005550 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5551 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005552}
5553
Alexey Bataeve3727102018-04-18 15:57:46 +00005554StmtResult
5555Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
5556 SourceLocation StartLoc, SourceLocation EndLoc,
5557 VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005558 if (!AStmt)
5559 return StmtError();
5560
5561 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005562 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005563 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5564 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00005565 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataev10e775f2015-07-30 11:36:16 +00005566 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5567 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005568 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005569 return StmtError();
5570
Alexander Musmana5f070a2014-10-01 06:03:56 +00005571 assert((CurContext->isDependentContext() || B.builtAll()) &&
5572 "omp for loop exprs were not built");
5573
Alexey Bataev54acd402015-08-04 11:18:19 +00005574 if (!CurContext->isDependentContext()) {
5575 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005576 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005577 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00005578 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005579 B.NumIterations, *this, CurScope,
5580 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005581 return StmtError();
5582 }
5583 }
5584
Reid Kleckner87a31802018-03-12 21:43:02 +00005585 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005586 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005587 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005588}
5589
Alexander Musmanf82886e2014-09-18 05:12:34 +00005590StmtResult Sema::ActOnOpenMPForSimdDirective(
5591 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00005592 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005593 if (!AStmt)
5594 return StmtError();
5595
5596 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005597 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005598 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5599 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00005600 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00005601 checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00005602 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5603 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005604 if (NestedLoopCount == 0)
5605 return StmtError();
5606
Alexander Musmanc6388682014-12-15 07:07:06 +00005607 assert((CurContext->isDependentContext() || B.builtAll()) &&
5608 "omp for simd loop exprs were not built");
5609
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005610 if (!CurContext->isDependentContext()) {
5611 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005612 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005613 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005614 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005615 B.NumIterations, *this, CurScope,
5616 DSAStack))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005617 return StmtError();
5618 }
5619 }
5620
Kelvin Lic5609492016-07-15 04:39:07 +00005621 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005622 return StmtError();
5623
Reid Kleckner87a31802018-03-12 21:43:02 +00005624 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005625 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5626 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005627}
5628
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005629StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5630 Stmt *AStmt,
5631 SourceLocation StartLoc,
5632 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005633 if (!AStmt)
5634 return StmtError();
5635
5636 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005637 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00005638 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005639 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00005640 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005641 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005642 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005643 return StmtError();
5644 // All associated statements must be '#pragma omp section' except for
5645 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005646 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005647 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5648 if (SectionStmt)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005649 Diag(SectionStmt->getBeginLoc(),
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005650 diag::err_omp_sections_substmt_not_section);
5651 return StmtError();
5652 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005653 cast<OMPSectionDirective>(SectionStmt)
5654 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005655 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005656 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005657 Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005658 return StmtError();
5659 }
5660
Reid Kleckner87a31802018-03-12 21:43:02 +00005661 setFunctionHasBranchProtectedScope();
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005662
Alexey Bataev25e5b442015-09-15 12:52:43 +00005663 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5664 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005665}
5666
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005667StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5668 SourceLocation StartLoc,
5669 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005670 if (!AStmt)
5671 return StmtError();
5672
5673 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005674
Reid Kleckner87a31802018-03-12 21:43:02 +00005675 setFunctionHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00005676 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005677
Alexey Bataev25e5b442015-09-15 12:52:43 +00005678 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5679 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005680}
5681
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005682StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5683 Stmt *AStmt,
5684 SourceLocation StartLoc,
5685 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005686 if (!AStmt)
5687 return StmtError();
5688
5689 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00005690
Reid Kleckner87a31802018-03-12 21:43:02 +00005691 setFunctionHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00005692
Alexey Bataev3255bf32015-01-19 05:20:46 +00005693 // OpenMP [2.7.3, single Construct, Restrictions]
5694 // The copyprivate clause must not be used with the nowait clause.
Alexey Bataeve3727102018-04-18 15:57:46 +00005695 const OMPClause *Nowait = nullptr;
5696 const OMPClause *Copyprivate = nullptr;
5697 for (const OMPClause *Clause : Clauses) {
Alexey Bataev3255bf32015-01-19 05:20:46 +00005698 if (Clause->getClauseKind() == OMPC_nowait)
5699 Nowait = Clause;
5700 else if (Clause->getClauseKind() == OMPC_copyprivate)
5701 Copyprivate = Clause;
5702 if (Copyprivate && Nowait) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005703 Diag(Copyprivate->getBeginLoc(),
Alexey Bataev3255bf32015-01-19 05:20:46 +00005704 diag::err_omp_single_copyprivate_with_nowait);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005705 Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here);
Alexey Bataev3255bf32015-01-19 05:20:46 +00005706 return StmtError();
5707 }
5708 }
5709
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005710 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5711}
5712
Alexander Musman80c22892014-07-17 08:54:58 +00005713StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
5714 SourceLocation StartLoc,
5715 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005716 if (!AStmt)
5717 return StmtError();
5718
5719 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00005720
Reid Kleckner87a31802018-03-12 21:43:02 +00005721 setFunctionHasBranchProtectedScope();
Alexander Musman80c22892014-07-17 08:54:58 +00005722
5723 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
5724}
5725
Alexey Bataev28c75412015-12-15 08:19:24 +00005726StmtResult Sema::ActOnOpenMPCriticalDirective(
5727 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
5728 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005729 if (!AStmt)
5730 return StmtError();
5731
5732 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005733
Alexey Bataev28c75412015-12-15 08:19:24 +00005734 bool ErrorFound = false;
5735 llvm::APSInt Hint;
5736 SourceLocation HintLoc;
5737 bool DependentHint = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00005738 for (const OMPClause *C : Clauses) {
Alexey Bataev28c75412015-12-15 08:19:24 +00005739 if (C->getClauseKind() == OMPC_hint) {
5740 if (!DirName.getName()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005741 Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name);
Alexey Bataev28c75412015-12-15 08:19:24 +00005742 ErrorFound = true;
5743 }
5744 Expr *E = cast<OMPHintClause>(C)->getHint();
5745 if (E->isTypeDependent() || E->isValueDependent() ||
Alexey Bataeve3727102018-04-18 15:57:46 +00005746 E->isInstantiationDependent()) {
Alexey Bataev28c75412015-12-15 08:19:24 +00005747 DependentHint = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00005748 } else {
Alexey Bataev28c75412015-12-15 08:19:24 +00005749 Hint = E->EvaluateKnownConstInt(Context);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005750 HintLoc = C->getBeginLoc();
Alexey Bataev28c75412015-12-15 08:19:24 +00005751 }
5752 }
5753 }
5754 if (ErrorFound)
5755 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00005756 const auto Pair = DSAStack->getCriticalWithHint(DirName);
Alexey Bataev28c75412015-12-15 08:19:24 +00005757 if (Pair.first && DirName.getName() && !DependentHint) {
5758 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
5759 Diag(StartLoc, diag::err_omp_critical_with_hint);
Alexey Bataeve3727102018-04-18 15:57:46 +00005760 if (HintLoc.isValid())
Alexey Bataev28c75412015-12-15 08:19:24 +00005761 Diag(HintLoc, diag::note_omp_critical_hint_here)
5762 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +00005763 else
Alexey Bataev28c75412015-12-15 08:19:24 +00005764 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
Alexey Bataeve3727102018-04-18 15:57:46 +00005765 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005766 Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here)
Alexey Bataev28c75412015-12-15 08:19:24 +00005767 << 1
5768 << C->getHint()->EvaluateKnownConstInt(Context).toString(
5769 /*Radix=*/10, /*Signed=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +00005770 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005771 Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1;
Alexey Bataeve3727102018-04-18 15:57:46 +00005772 }
Alexey Bataev28c75412015-12-15 08:19:24 +00005773 }
5774 }
5775
Reid Kleckner87a31802018-03-12 21:43:02 +00005776 setFunctionHasBranchProtectedScope();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005777
Alexey Bataev28c75412015-12-15 08:19:24 +00005778 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
5779 Clauses, AStmt);
5780 if (!Pair.first && DirName.getName() && !DependentHint)
5781 DSAStack->addCriticalWithHint(Dir, Hint);
5782 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00005783}
5784
Alexey Bataev4acb8592014-07-07 13:01:15 +00005785StmtResult Sema::ActOnOpenMPParallelForDirective(
5786 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00005787 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005788 if (!AStmt)
5789 return StmtError();
5790
Alexey Bataeve3727102018-04-18 15:57:46 +00005791 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005792 // 1.2.2 OpenMP Language Terminology
5793 // Structured block - An executable statement with a single entry at the
5794 // top and a single exit at the bottom.
5795 // The point of exit cannot be a branch out of the structured block.
5796 // longjmp() and throw() must not violate the entry/exit criteria.
5797 CS->getCapturedDecl()->setNothrow();
5798
Alexander Musmanc6388682014-12-15 07:07:06 +00005799 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005800 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5801 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005802 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00005803 checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00005804 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5805 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00005806 if (NestedLoopCount == 0)
5807 return StmtError();
5808
Alexander Musmana5f070a2014-10-01 06:03:56 +00005809 assert((CurContext->isDependentContext() || B.builtAll()) &&
5810 "omp parallel for loop exprs were not built");
5811
Alexey Bataev54acd402015-08-04 11:18:19 +00005812 if (!CurContext->isDependentContext()) {
5813 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005814 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005815 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00005816 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005817 B.NumIterations, *this, CurScope,
5818 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005819 return StmtError();
5820 }
5821 }
5822
Reid Kleckner87a31802018-03-12 21:43:02 +00005823 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005824 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005825 NestedLoopCount, Clauses, AStmt, B,
5826 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00005827}
5828
Alexander Musmane4e893b2014-09-23 09:33:00 +00005829StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
5830 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00005831 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005832 if (!AStmt)
5833 return StmtError();
5834
Alexey Bataeve3727102018-04-18 15:57:46 +00005835 auto *CS = cast<CapturedStmt>(AStmt);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005836 // 1.2.2 OpenMP Language Terminology
5837 // Structured block - An executable statement with a single entry at the
5838 // top and a single exit at the bottom.
5839 // The point of exit cannot be a branch out of the structured block.
5840 // longjmp() and throw() must not violate the entry/exit criteria.
5841 CS->getCapturedDecl()->setNothrow();
5842
Alexander Musmanc6388682014-12-15 07:07:06 +00005843 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005844 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5845 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00005846 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00005847 checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00005848 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5849 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005850 if (NestedLoopCount == 0)
5851 return StmtError();
5852
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005853 if (!CurContext->isDependentContext()) {
5854 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005855 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005856 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005857 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005858 B.NumIterations, *this, CurScope,
5859 DSAStack))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00005860 return StmtError();
5861 }
5862 }
5863
Kelvin Lic5609492016-07-15 04:39:07 +00005864 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005865 return StmtError();
5866
Reid Kleckner87a31802018-03-12 21:43:02 +00005867 setFunctionHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005868 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00005869 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00005870}
5871
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005872StmtResult
5873Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
5874 Stmt *AStmt, SourceLocation StartLoc,
5875 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005876 if (!AStmt)
5877 return StmtError();
5878
5879 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005880 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00005881 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005882 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00005883 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005884 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005885 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005886 return StmtError();
5887 // All associated statements must be '#pragma omp section' except for
5888 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005889 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005890 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5891 if (SectionStmt)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005892 Diag(SectionStmt->getBeginLoc(),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005893 diag::err_omp_parallel_sections_substmt_not_section);
5894 return StmtError();
5895 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005896 cast<OMPSectionDirective>(SectionStmt)
5897 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005898 }
5899 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005900 Diag(AStmt->getBeginLoc(),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005901 diag::err_omp_parallel_sections_not_compound_stmt);
5902 return StmtError();
5903 }
5904
Reid Kleckner87a31802018-03-12 21:43:02 +00005905 setFunctionHasBranchProtectedScope();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005906
Alexey Bataev25e5b442015-09-15 12:52:43 +00005907 return OMPParallelSectionsDirective::Create(
5908 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00005909}
5910
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005911StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
5912 Stmt *AStmt, SourceLocation StartLoc,
5913 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005914 if (!AStmt)
5915 return StmtError();
5916
David Majnemer9d168222016-08-05 17:44:54 +00005917 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005918 // 1.2.2 OpenMP Language Terminology
5919 // Structured block - An executable statement with a single entry at the
5920 // top and a single exit at the bottom.
5921 // The point of exit cannot be a branch out of the structured block.
5922 // longjmp() and throw() must not violate the entry/exit criteria.
5923 CS->getCapturedDecl()->setNothrow();
5924
Reid Kleckner87a31802018-03-12 21:43:02 +00005925 setFunctionHasBranchProtectedScope();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005926
Alexey Bataev25e5b442015-09-15 12:52:43 +00005927 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5928 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005929}
5930
Alexey Bataev68446b72014-07-18 07:47:19 +00005931StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
5932 SourceLocation EndLoc) {
5933 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
5934}
5935
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00005936StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
5937 SourceLocation EndLoc) {
5938 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
5939}
5940
Alexey Bataev2df347a2014-07-18 10:17:07 +00005941StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
5942 SourceLocation EndLoc) {
5943 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
5944}
5945
Alexey Bataev169d96a2017-07-18 20:17:46 +00005946StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses,
5947 Stmt *AStmt,
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005948 SourceLocation StartLoc,
5949 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005950 if (!AStmt)
5951 return StmtError();
5952
5953 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005954
Reid Kleckner87a31802018-03-12 21:43:02 +00005955 setFunctionHasBranchProtectedScope();
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005956
Alexey Bataev169d96a2017-07-18 20:17:46 +00005957 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses,
Alexey Bataev3b1b8952017-07-25 15:53:26 +00005958 AStmt,
5959 DSAStack->getTaskgroupReductionRef());
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00005960}
5961
Alexey Bataev6125da92014-07-21 11:26:11 +00005962StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
5963 SourceLocation StartLoc,
5964 SourceLocation EndLoc) {
5965 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
5966 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
5967}
5968
Alexey Bataev346265e2015-09-25 10:37:12 +00005969StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
5970 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00005971 SourceLocation StartLoc,
5972 SourceLocation EndLoc) {
Alexey Bataeve3727102018-04-18 15:57:46 +00005973 const OMPClause *DependFound = nullptr;
5974 const OMPClause *DependSourceClause = nullptr;
5975 const OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00005976 bool ErrorFound = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00005977 const OMPThreadsClause *TC = nullptr;
5978 const OMPSIMDClause *SC = nullptr;
5979 for (const OMPClause *C : Clauses) {
Alexey Bataeveb482352015-12-18 05:05:56 +00005980 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
5981 DependFound = C;
5982 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
5983 if (DependSourceClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005984 Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
Alexey Bataeveb482352015-12-18 05:05:56 +00005985 << getOpenMPDirectiveName(OMPD_ordered)
5986 << getOpenMPClauseName(OMPC_depend) << 2;
5987 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00005988 } else {
Alexey Bataeveb482352015-12-18 05:05:56 +00005989 DependSourceClause = C;
Alexey Bataeve3727102018-04-18 15:57:46 +00005990 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005991 if (DependSinkClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005992 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005993 << 0;
5994 ErrorFound = true;
5995 }
5996 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
5997 if (DependSourceClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005998 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00005999 << 1;
6000 ErrorFound = true;
6001 }
6002 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00006003 }
Alexey Bataeve3727102018-04-18 15:57:46 +00006004 } else if (C->getClauseKind() == OMPC_threads) {
Alexey Bataev346265e2015-09-25 10:37:12 +00006005 TC = cast<OMPThreadsClause>(C);
Alexey Bataeve3727102018-04-18 15:57:46 +00006006 } else if (C->getClauseKind() == OMPC_simd) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006007 SC = cast<OMPSIMDClause>(C);
Alexey Bataeve3727102018-04-18 15:57:46 +00006008 }
Alexey Bataev346265e2015-09-25 10:37:12 +00006009 }
Alexey Bataeveb482352015-12-18 05:05:56 +00006010 if (!ErrorFound && !SC &&
6011 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006012 // OpenMP [2.8.1,simd Construct, Restrictions]
6013 // An ordered construct with the simd clause is the only OpenMP construct
6014 // that can appear in the simd region.
6015 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00006016 ErrorFound = true;
6017 } else if (DependFound && (TC || SC)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006018 Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd)
Alexey Bataeveb482352015-12-18 05:05:56 +00006019 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
6020 ErrorFound = true;
Alexey Bataevf138fda2018-08-13 19:04:24 +00006021 } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006022 Diag(DependFound->getBeginLoc(),
Alexey Bataeveb482352015-12-18 05:05:56 +00006023 diag::err_omp_ordered_directive_without_param);
6024 ErrorFound = true;
6025 } else if (TC || Clauses.empty()) {
Alexey Bataevf138fda2018-08-13 19:04:24 +00006026 if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006027 SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc;
Alexey Bataeveb482352015-12-18 05:05:56 +00006028 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
6029 << (TC != nullptr);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006030 Diag(Param->getBeginLoc(), diag::note_omp_ordered_param);
Alexey Bataeveb482352015-12-18 05:05:56 +00006031 ErrorFound = true;
6032 }
6033 }
6034 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006035 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00006036
6037 if (AStmt) {
6038 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6039
Reid Kleckner87a31802018-03-12 21:43:02 +00006040 setFunctionHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006041 }
Alexey Bataev346265e2015-09-25 10:37:12 +00006042
6043 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00006044}
6045
Alexey Bataev1d160b12015-03-13 12:27:31 +00006046namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006047/// Helper class for checking expression in 'omp atomic [update]'
Alexey Bataev1d160b12015-03-13 12:27:31 +00006048/// construct.
6049class OpenMPAtomicUpdateChecker {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006050 /// Error results for atomic update expressions.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006051 enum ExprAnalysisErrorCode {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006052 /// A statement is not an expression statement.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006053 NotAnExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006054 /// Expression is not builtin binary or unary operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006055 NotABinaryOrUnaryExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006056 /// Unary operation is not post-/pre- increment/decrement operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006057 NotAnUnaryIncDecExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006058 /// An expression is not of scalar type.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006059 NotAScalarType,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006060 /// A binary operation is not an assignment operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006061 NotAnAssignmentOp,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006062 /// RHS part of the binary operation is not a binary expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006063 NotABinaryExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006064 /// RHS part is not additive/multiplicative/shift/biwise binary
Alexey Bataev1d160b12015-03-13 12:27:31 +00006065 /// expression.
6066 NotABinaryOperator,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006067 /// RHS binary operation does not have reference to the updated LHS
Alexey Bataev1d160b12015-03-13 12:27:31 +00006068 /// part.
6069 NotAnUpdateExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006070 /// No errors is found.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006071 NoError
6072 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006073 /// Reference to Sema.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006074 Sema &SemaRef;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006075 /// A location for note diagnostics (when error is found).
Alexey Bataev1d160b12015-03-13 12:27:31 +00006076 SourceLocation NoteLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006077 /// 'x' lvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006078 Expr *X;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006079 /// 'expr' rvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006080 Expr *E;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006081 /// Helper expression of the form
Alexey Bataevb4505a72015-03-30 05:20:59 +00006082 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6083 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6084 Expr *UpdateExpr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006085 /// Is 'x' a LHS in a RHS part of full update expression. It is
Alexey Bataevb4505a72015-03-30 05:20:59 +00006086 /// important for non-associative operations.
6087 bool IsXLHSInRHSPart;
6088 BinaryOperatorKind Op;
6089 SourceLocation OpLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006090 /// true if the source expression is a postfix unary operation, false
Alexey Bataevb78ca832015-04-01 03:33:17 +00006091 /// if it is a prefix unary operation.
6092 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006093
6094public:
6095 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00006096 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00006097 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006098 /// Check specified statement that it is suitable for 'atomic update'
Alexey Bataev1d160b12015-03-13 12:27:31 +00006099 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00006100 /// expression. If DiagId and NoteId == 0, then only check is performed
6101 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006102 /// \param DiagId Diagnostic which should be emitted if error is found.
6103 /// \param NoteId Diagnostic note for the main error message.
6104 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00006105 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006106 /// Return the 'x' lvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006107 Expr *getX() const { return X; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006108 /// Return the 'expr' rvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006109 Expr *getExpr() const { return E; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006110 /// Return the update expression used in calculation of the updated
Alexey Bataevb4505a72015-03-30 05:20:59 +00006111 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6112 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6113 Expr *getUpdateExpr() const { return UpdateExpr; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006114 /// Return true if 'x' is LHS in RHS part of full update expression,
Alexey Bataevb4505a72015-03-30 05:20:59 +00006115 /// false otherwise.
6116 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
6117
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006118 /// true if the source expression is a postfix unary operation, false
Alexey Bataevb78ca832015-04-01 03:33:17 +00006119 /// if it is a prefix unary operation.
6120 bool isPostfixUpdate() const { return IsPostfixUpdate; }
6121
Alexey Bataev1d160b12015-03-13 12:27:31 +00006122private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00006123 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
6124 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00006125};
6126} // namespace
6127
6128bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
6129 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
6130 ExprAnalysisErrorCode ErrorFound = NoError;
6131 SourceLocation ErrorLoc, NoteLoc;
6132 SourceRange ErrorRange, NoteRange;
6133 // Allowed constructs are:
6134 // x = x binop expr;
6135 // x = expr binop x;
6136 if (AtomicBinOp->getOpcode() == BO_Assign) {
6137 X = AtomicBinOp->getLHS();
Alexey Bataeve3727102018-04-18 15:57:46 +00006138 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006139 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
6140 if (AtomicInnerBinOp->isMultiplicativeOp() ||
6141 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
6142 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006143 Op = AtomicInnerBinOp->getOpcode();
6144 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataeve3727102018-04-18 15:57:46 +00006145 Expr *LHS = AtomicInnerBinOp->getLHS();
6146 Expr *RHS = AtomicInnerBinOp->getRHS();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006147 llvm::FoldingSetNodeID XId, LHSId, RHSId;
6148 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
6149 /*Canonical=*/true);
6150 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
6151 /*Canonical=*/true);
6152 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
6153 /*Canonical=*/true);
6154 if (XId == LHSId) {
6155 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006156 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006157 } else if (XId == RHSId) {
6158 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006159 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006160 } else {
6161 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6162 ErrorRange = AtomicInnerBinOp->getSourceRange();
6163 NoteLoc = X->getExprLoc();
6164 NoteRange = X->getSourceRange();
6165 ErrorFound = NotAnUpdateExpression;
6166 }
6167 } else {
6168 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6169 ErrorRange = AtomicInnerBinOp->getSourceRange();
6170 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
6171 NoteRange = SourceRange(NoteLoc, NoteLoc);
6172 ErrorFound = NotABinaryOperator;
6173 }
6174 } else {
6175 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
6176 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
6177 ErrorFound = NotABinaryExpression;
6178 }
6179 } else {
6180 ErrorLoc = AtomicBinOp->getExprLoc();
6181 ErrorRange = AtomicBinOp->getSourceRange();
6182 NoteLoc = AtomicBinOp->getOperatorLoc();
6183 NoteRange = SourceRange(NoteLoc, NoteLoc);
6184 ErrorFound = NotAnAssignmentOp;
6185 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006186 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006187 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6188 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6189 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006190 }
6191 if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006192 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006193 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006194}
6195
6196bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
6197 unsigned NoteId) {
6198 ExprAnalysisErrorCode ErrorFound = NoError;
6199 SourceLocation ErrorLoc, NoteLoc;
6200 SourceRange ErrorRange, NoteRange;
6201 // Allowed constructs are:
6202 // x++;
6203 // x--;
6204 // ++x;
6205 // --x;
6206 // x binop= expr;
6207 // x = x binop expr;
6208 // x = expr binop x;
6209 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
6210 AtomicBody = AtomicBody->IgnoreParenImpCasts();
6211 if (AtomicBody->getType()->isScalarType() ||
6212 AtomicBody->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006213 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006214 AtomicBody->IgnoreParenImpCasts())) {
6215 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00006216 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006217 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00006218 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006219 E = AtomicCompAssignOp->getRHS();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006220 X = AtomicCompAssignOp->getLHS()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006221 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006222 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
6223 AtomicBody->IgnoreParenImpCasts())) {
6224 // Check for Binary Operation
David Majnemer9d168222016-08-05 17:44:54 +00006225 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
Alexey Bataevb4505a72015-03-30 05:20:59 +00006226 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006227 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
David Majnemer9d168222016-08-05 17:44:54 +00006228 AtomicBody->IgnoreParenImpCasts())) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006229 // Check for Unary Operation
6230 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006231 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006232 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
6233 OpLoc = AtomicUnaryOp->getOperatorLoc();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006234 X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006235 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
6236 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006237 } else {
6238 ErrorFound = NotAnUnaryIncDecExpression;
6239 ErrorLoc = AtomicUnaryOp->getExprLoc();
6240 ErrorRange = AtomicUnaryOp->getSourceRange();
6241 NoteLoc = AtomicUnaryOp->getOperatorLoc();
6242 NoteRange = SourceRange(NoteLoc, NoteLoc);
6243 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006244 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006245 ErrorFound = NotABinaryOrUnaryExpression;
6246 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
6247 NoteRange = ErrorRange = AtomicBody->getSourceRange();
6248 }
6249 } else {
6250 ErrorFound = NotAScalarType;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006251 NoteLoc = ErrorLoc = AtomicBody->getBeginLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006252 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6253 }
6254 } else {
6255 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006256 NoteLoc = ErrorLoc = S->getBeginLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006257 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6258 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006259 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006260 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6261 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6262 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006263 }
6264 if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006265 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006266 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006267 // Build an update expression of form 'OpaqueValueExpr(x) binop
6268 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
6269 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
6270 auto *OVEX = new (SemaRef.getASTContext())
6271 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
6272 auto *OVEExpr = new (SemaRef.getASTContext())
6273 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
Alexey Bataeve3727102018-04-18 15:57:46 +00006274 ExprResult Update =
Alexey Bataevb4505a72015-03-30 05:20:59 +00006275 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
6276 IsXLHSInRHSPart ? OVEExpr : OVEX);
6277 if (Update.isInvalid())
6278 return true;
6279 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
6280 Sema::AA_Casting);
6281 if (Update.isInvalid())
6282 return true;
6283 UpdateExpr = Update.get();
6284 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00006285 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006286}
6287
Alexey Bataev0162e452014-07-22 10:10:35 +00006288StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
6289 Stmt *AStmt,
6290 SourceLocation StartLoc,
6291 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006292 if (!AStmt)
6293 return StmtError();
6294
David Majnemer9d168222016-08-05 17:44:54 +00006295 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00006296 // 1.2.2 OpenMP Language Terminology
6297 // Structured block - An executable statement with a single entry at the
6298 // top and a single exit at the bottom.
6299 // The point of exit cannot be a branch out of the structured block.
6300 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00006301 OpenMPClauseKind AtomicKind = OMPC_unknown;
6302 SourceLocation AtomicKindLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00006303 for (const OMPClause *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00006304 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00006305 C->getClauseKind() == OMPC_update ||
6306 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00006307 if (AtomicKind != OMPC_unknown) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006308 Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00006309 << SourceRange(C->getBeginLoc(), C->getEndLoc());
Alexey Bataevdea47612014-07-23 07:46:59 +00006310 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
6311 << getOpenMPClauseName(AtomicKind);
6312 } else {
6313 AtomicKind = C->getClauseKind();
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006314 AtomicKindLoc = C->getBeginLoc();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006315 }
6316 }
6317 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006318
Alexey Bataeve3727102018-04-18 15:57:46 +00006319 Stmt *Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00006320 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
6321 Body = EWC->getSubExpr();
6322
Alexey Bataev62cec442014-11-18 10:14:22 +00006323 Expr *X = nullptr;
6324 Expr *V = nullptr;
6325 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006326 Expr *UE = nullptr;
6327 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006328 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00006329 // OpenMP [2.12.6, atomic Construct]
6330 // In the next expressions:
6331 // * x and v (as applicable) are both l-value expressions with scalar type.
6332 // * During the execution of an atomic region, multiple syntactic
6333 // occurrences of x must designate the same storage location.
6334 // * Neither of v and expr (as applicable) may access the storage location
6335 // designated by x.
6336 // * Neither of x and expr (as applicable) may access the storage location
6337 // designated by v.
6338 // * expr is an expression with scalar type.
6339 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
6340 // * binop, binop=, ++, and -- are not overloaded operators.
6341 // * The expression x binop expr must be numerically equivalent to x binop
6342 // (expr). This requirement is satisfied if the operators in expr have
6343 // precedence greater than binop, or by using parentheses around expr or
6344 // subexpressions of expr.
6345 // * The expression expr binop x must be numerically equivalent to (expr)
6346 // binop x. This requirement is satisfied if the operators in expr have
6347 // precedence equal to or greater than binop, or by using parentheses around
6348 // expr or subexpressions of expr.
6349 // * For forms that allow multiple occurrences of x, the number of times
6350 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00006351 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006352 enum {
6353 NotAnExpression,
6354 NotAnAssignmentOp,
6355 NotAScalarType,
6356 NotAnLValue,
6357 NoError
6358 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00006359 SourceLocation ErrorLoc, NoteLoc;
6360 SourceRange ErrorRange, NoteRange;
6361 // If clause is read:
6362 // v = x;
Alexey Bataeve3727102018-04-18 15:57:46 +00006363 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
6364 const auto *AtomicBinOp =
Alexey Bataev62cec442014-11-18 10:14:22 +00006365 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6366 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6367 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6368 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
6369 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6370 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
6371 if (!X->isLValue() || !V->isLValue()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006372 const Expr *NotLValueExpr = X->isLValue() ? V : X;
Alexey Bataev62cec442014-11-18 10:14:22 +00006373 ErrorFound = NotAnLValue;
6374 ErrorLoc = AtomicBinOp->getExprLoc();
6375 ErrorRange = AtomicBinOp->getSourceRange();
6376 NoteLoc = NotLValueExpr->getExprLoc();
6377 NoteRange = NotLValueExpr->getSourceRange();
6378 }
6379 } else if (!X->isInstantiationDependent() ||
6380 !V->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006381 const Expr *NotScalarExpr =
Alexey Bataev62cec442014-11-18 10:14:22 +00006382 (X->isInstantiationDependent() || X->getType()->isScalarType())
6383 ? V
6384 : X;
6385 ErrorFound = NotAScalarType;
6386 ErrorLoc = AtomicBinOp->getExprLoc();
6387 ErrorRange = AtomicBinOp->getSourceRange();
6388 NoteLoc = NotScalarExpr->getExprLoc();
6389 NoteRange = NotScalarExpr->getSourceRange();
6390 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006391 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00006392 ErrorFound = NotAnAssignmentOp;
6393 ErrorLoc = AtomicBody->getExprLoc();
6394 ErrorRange = AtomicBody->getSourceRange();
6395 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6396 : AtomicBody->getExprLoc();
6397 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6398 : AtomicBody->getSourceRange();
6399 }
6400 } else {
6401 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006402 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataev62cec442014-11-18 10:14:22 +00006403 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006404 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006405 if (ErrorFound != NoError) {
6406 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
6407 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006408 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6409 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00006410 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00006411 }
6412 if (CurContext->isDependentContext())
Alexey Bataev62cec442014-11-18 10:14:22 +00006413 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00006414 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006415 enum {
6416 NotAnExpression,
6417 NotAnAssignmentOp,
6418 NotAScalarType,
6419 NotAnLValue,
6420 NoError
6421 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006422 SourceLocation ErrorLoc, NoteLoc;
6423 SourceRange ErrorRange, NoteRange;
6424 // If clause is write:
6425 // x = expr;
Alexey Bataeve3727102018-04-18 15:57:46 +00006426 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
6427 const auto *AtomicBinOp =
Alexey Bataevf33eba62014-11-28 07:21:40 +00006428 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6429 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00006430 X = AtomicBinOp->getLHS();
6431 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006432 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6433 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
6434 if (!X->isLValue()) {
6435 ErrorFound = NotAnLValue;
6436 ErrorLoc = AtomicBinOp->getExprLoc();
6437 ErrorRange = AtomicBinOp->getSourceRange();
6438 NoteLoc = X->getExprLoc();
6439 NoteRange = X->getSourceRange();
6440 }
6441 } else if (!X->isInstantiationDependent() ||
6442 !E->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006443 const Expr *NotScalarExpr =
Alexey Bataevf33eba62014-11-28 07:21:40 +00006444 (X->isInstantiationDependent() || X->getType()->isScalarType())
6445 ? E
6446 : X;
6447 ErrorFound = NotAScalarType;
6448 ErrorLoc = AtomicBinOp->getExprLoc();
6449 ErrorRange = AtomicBinOp->getSourceRange();
6450 NoteLoc = NotScalarExpr->getExprLoc();
6451 NoteRange = NotScalarExpr->getSourceRange();
6452 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006453 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00006454 ErrorFound = NotAnAssignmentOp;
6455 ErrorLoc = AtomicBody->getExprLoc();
6456 ErrorRange = AtomicBody->getSourceRange();
6457 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6458 : AtomicBody->getExprLoc();
6459 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6460 : AtomicBody->getSourceRange();
6461 }
6462 } else {
6463 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006464 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006465 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006466 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00006467 if (ErrorFound != NoError) {
6468 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
6469 << ErrorRange;
6470 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6471 << NoteRange;
6472 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00006473 }
6474 if (CurContext->isDependentContext())
Alexey Bataevf33eba62014-11-28 07:21:40 +00006475 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006476 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006477 // If clause is update:
6478 // x++;
6479 // x--;
6480 // ++x;
6481 // --x;
6482 // x binop= expr;
6483 // x = x binop expr;
6484 // x = expr binop x;
6485 OpenMPAtomicUpdateChecker Checker(*this);
6486 if (Checker.checkStatement(
6487 Body, (AtomicKind == OMPC_update)
6488 ? diag::err_omp_atomic_update_not_expression_statement
6489 : diag::err_omp_atomic_not_expression_statement,
6490 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00006491 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006492 if (!CurContext->isDependentContext()) {
6493 E = Checker.getExpr();
6494 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006495 UE = Checker.getUpdateExpr();
6496 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00006497 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006498 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006499 enum {
6500 NotAnAssignmentOp,
6501 NotACompoundStatement,
6502 NotTwoSubstatements,
6503 NotASpecificExpression,
6504 NoError
6505 } ErrorFound = NoError;
6506 SourceLocation ErrorLoc, NoteLoc;
6507 SourceRange ErrorRange, NoteRange;
Alexey Bataeve3727102018-04-18 15:57:46 +00006508 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006509 // If clause is a capture:
6510 // v = x++;
6511 // v = x--;
6512 // v = ++x;
6513 // v = --x;
6514 // v = x binop= expr;
6515 // v = x = x binop expr;
6516 // v = x = expr binop x;
Alexey Bataeve3727102018-04-18 15:57:46 +00006517 const auto *AtomicBinOp =
Alexey Bataevb78ca832015-04-01 03:33:17 +00006518 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6519 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6520 V = AtomicBinOp->getLHS();
6521 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6522 OpenMPAtomicUpdateChecker Checker(*this);
6523 if (Checker.checkStatement(
6524 Body, diag::err_omp_atomic_capture_not_expression_statement,
6525 diag::note_omp_atomic_update))
6526 return StmtError();
6527 E = Checker.getExpr();
6528 X = Checker.getX();
6529 UE = Checker.getUpdateExpr();
6530 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6531 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00006532 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006533 ErrorLoc = AtomicBody->getExprLoc();
6534 ErrorRange = AtomicBody->getSourceRange();
6535 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6536 : AtomicBody->getExprLoc();
6537 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6538 : AtomicBody->getSourceRange();
6539 ErrorFound = NotAnAssignmentOp;
6540 }
6541 if (ErrorFound != NoError) {
6542 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6543 << ErrorRange;
6544 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6545 return StmtError();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006546 }
Alexey Bataeve3727102018-04-18 15:57:46 +00006547 if (CurContext->isDependentContext())
6548 UE = V = E = X = nullptr;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006549 } else {
6550 // If clause is a capture:
6551 // { v = x; x = expr; }
6552 // { v = x; x++; }
6553 // { v = x; x--; }
6554 // { v = x; ++x; }
6555 // { v = x; --x; }
6556 // { v = x; x binop= expr; }
6557 // { v = x; x = x binop expr; }
6558 // { v = x; x = expr binop x; }
6559 // { x++; v = x; }
6560 // { x--; v = x; }
6561 // { ++x; v = x; }
6562 // { --x; v = x; }
6563 // { x binop= expr; v = x; }
6564 // { x = x binop expr; v = x; }
6565 // { x = expr binop x; v = x; }
6566 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6567 // Check that this is { expr1; expr2; }
6568 if (CS->size() == 2) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006569 Stmt *First = CS->body_front();
6570 Stmt *Second = CS->body_back();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006571 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6572 First = EWC->getSubExpr()->IgnoreParenImpCasts();
6573 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6574 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6575 // Need to find what subexpression is 'v' and what is 'x'.
6576 OpenMPAtomicUpdateChecker Checker(*this);
6577 bool IsUpdateExprFound = !Checker.checkStatement(Second);
6578 BinaryOperator *BinOp = nullptr;
6579 if (IsUpdateExprFound) {
6580 BinOp = dyn_cast<BinaryOperator>(First);
6581 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6582 }
6583 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6584 // { v = x; x++; }
6585 // { v = x; x--; }
6586 // { v = x; ++x; }
6587 // { v = x; --x; }
6588 // { v = x; x binop= expr; }
6589 // { v = x; x = x binop expr; }
6590 // { v = x; x = expr binop x; }
6591 // Check that the first expression has form v = x.
Alexey Bataeve3727102018-04-18 15:57:46 +00006592 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006593 llvm::FoldingSetNodeID XId, PossibleXId;
6594 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6595 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6596 IsUpdateExprFound = XId == PossibleXId;
6597 if (IsUpdateExprFound) {
6598 V = BinOp->getLHS();
6599 X = Checker.getX();
6600 E = Checker.getExpr();
6601 UE = Checker.getUpdateExpr();
6602 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006603 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006604 }
6605 }
6606 if (!IsUpdateExprFound) {
6607 IsUpdateExprFound = !Checker.checkStatement(First);
6608 BinOp = nullptr;
6609 if (IsUpdateExprFound) {
6610 BinOp = dyn_cast<BinaryOperator>(Second);
6611 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6612 }
6613 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6614 // { x++; v = x; }
6615 // { x--; v = x; }
6616 // { ++x; v = x; }
6617 // { --x; v = x; }
6618 // { x binop= expr; v = x; }
6619 // { x = x binop expr; v = x; }
6620 // { x = expr binop x; v = x; }
6621 // Check that the second expression has form v = x.
Alexey Bataeve3727102018-04-18 15:57:46 +00006622 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006623 llvm::FoldingSetNodeID XId, PossibleXId;
6624 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6625 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6626 IsUpdateExprFound = XId == PossibleXId;
6627 if (IsUpdateExprFound) {
6628 V = BinOp->getLHS();
6629 X = Checker.getX();
6630 E = Checker.getExpr();
6631 UE = Checker.getUpdateExpr();
6632 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006633 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006634 }
6635 }
6636 }
6637 if (!IsUpdateExprFound) {
6638 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00006639 auto *FirstExpr = dyn_cast<Expr>(First);
6640 auto *SecondExpr = dyn_cast<Expr>(Second);
6641 if (!FirstExpr || !SecondExpr ||
6642 !(FirstExpr->isInstantiationDependent() ||
6643 SecondExpr->isInstantiationDependent())) {
6644 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6645 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006646 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00006647 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006648 : First->getBeginLoc();
Alexey Bataev5a195472015-09-04 12:55:50 +00006649 NoteRange = ErrorRange = FirstBinOp
6650 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00006651 : SourceRange(ErrorLoc, ErrorLoc);
6652 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006653 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6654 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6655 ErrorFound = NotAnAssignmentOp;
6656 NoteLoc = ErrorLoc = SecondBinOp
6657 ? SecondBinOp->getOperatorLoc()
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006658 : Second->getBeginLoc();
Alexey Bataev5a195472015-09-04 12:55:50 +00006659 NoteRange = ErrorRange =
6660 SecondBinOp ? SecondBinOp->getSourceRange()
6661 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00006662 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +00006663 Expr *PossibleXRHSInFirst =
Alexey Bataev5a195472015-09-04 12:55:50 +00006664 FirstBinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00006665 Expr *PossibleXLHSInSecond =
Alexey Bataev5a195472015-09-04 12:55:50 +00006666 SecondBinOp->getLHS()->IgnoreParenImpCasts();
6667 llvm::FoldingSetNodeID X1Id, X2Id;
6668 PossibleXRHSInFirst->Profile(X1Id, Context,
6669 /*Canonical=*/true);
6670 PossibleXLHSInSecond->Profile(X2Id, Context,
6671 /*Canonical=*/true);
6672 IsUpdateExprFound = X1Id == X2Id;
6673 if (IsUpdateExprFound) {
6674 V = FirstBinOp->getLHS();
6675 X = SecondBinOp->getLHS();
6676 E = SecondBinOp->getRHS();
6677 UE = nullptr;
6678 IsXLHSInRHSPart = false;
6679 IsPostfixUpdate = true;
6680 } else {
6681 ErrorFound = NotASpecificExpression;
6682 ErrorLoc = FirstBinOp->getExprLoc();
6683 ErrorRange = FirstBinOp->getSourceRange();
6684 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6685 NoteRange = SecondBinOp->getRHS()->getSourceRange();
6686 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006687 }
6688 }
6689 }
6690 }
6691 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006692 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006693 NoteRange = ErrorRange =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006694 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
Alexey Bataevb78ca832015-04-01 03:33:17 +00006695 ErrorFound = NotTwoSubstatements;
6696 }
6697 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006698 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006699 NoteRange = ErrorRange =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006700 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
Alexey Bataevb78ca832015-04-01 03:33:17 +00006701 ErrorFound = NotACompoundStatement;
6702 }
6703 if (ErrorFound != NoError) {
6704 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
6705 << ErrorRange;
6706 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6707 return StmtError();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006708 }
Alexey Bataeve3727102018-04-18 15:57:46 +00006709 if (CurContext->isDependentContext())
6710 UE = V = E = X = nullptr;
Alexey Bataev459dec02014-07-24 06:46:57 +00006711 }
Alexey Bataevdea47612014-07-23 07:46:59 +00006712 }
Alexey Bataev0162e452014-07-22 10:10:35 +00006713
Reid Kleckner87a31802018-03-12 21:43:02 +00006714 setFunctionHasBranchProtectedScope();
Alexey Bataev0162e452014-07-22 10:10:35 +00006715
Alexey Bataev62cec442014-11-18 10:14:22 +00006716 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00006717 X, V, E, UE, IsXLHSInRHSPart,
6718 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00006719}
6720
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006721StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
6722 Stmt *AStmt,
6723 SourceLocation StartLoc,
6724 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006725 if (!AStmt)
6726 return StmtError();
6727
Alexey Bataeve3727102018-04-18 15:57:46 +00006728 auto *CS = cast<CapturedStmt>(AStmt);
Samuel Antao4af1b7b2015-12-02 17:44:43 +00006729 // 1.2.2 OpenMP Language Terminology
6730 // Structured block - An executable statement with a single entry at the
6731 // top and a single exit at the bottom.
6732 // The point of exit cannot be a branch out of the structured block.
6733 // longjmp() and throw() must not violate the entry/exit criteria.
6734 CS->getCapturedDecl()->setNothrow();
Alexey Bataev8451efa2018-01-15 19:06:12 +00006735 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target);
6736 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6737 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6738 // 1.2.2 OpenMP Language Terminology
6739 // Structured block - An executable statement with a single entry at the
6740 // top and a single exit at the bottom.
6741 // The point of exit cannot be a branch out of the structured block.
6742 // longjmp() and throw() must not violate the entry/exit criteria.
6743 CS->getCapturedDecl()->setNothrow();
6744 }
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006745
Alexey Bataev13314bf2014-10-09 04:18:56 +00006746 // OpenMP [2.16, Nesting of Regions]
6747 // If specified, a teams construct must be contained within a target
6748 // construct. That target construct must contain no statements or directives
6749 // outside of the teams construct.
6750 if (DSAStack->hasInnerTeamsRegion()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006751 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true);
Alexey Bataev13314bf2014-10-09 04:18:56 +00006752 bool OMPTeamsFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006753 if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00006754 auto I = CS->body_begin();
6755 while (I != CS->body_end()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006756 const auto *OED = dyn_cast<OMPExecutableDirective>(*I);
Alexey Bataev13314bf2014-10-09 04:18:56 +00006757 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
6758 OMPTeamsFound = false;
6759 break;
6760 }
6761 ++I;
6762 }
6763 assert(I != CS->body_end() && "Not found statement");
6764 S = *I;
Kelvin Li3834dce2016-06-27 19:15:43 +00006765 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +00006766 const auto *OED = dyn_cast<OMPExecutableDirective>(S);
Kelvin Li3834dce2016-06-27 19:15:43 +00006767 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
Alexey Bataev13314bf2014-10-09 04:18:56 +00006768 }
6769 if (!OMPTeamsFound) {
6770 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
6771 Diag(DSAStack->getInnerTeamsRegionLoc(),
6772 diag::note_omp_nested_teams_construct_here);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006773 Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here)
Alexey Bataev13314bf2014-10-09 04:18:56 +00006774 << isa<OMPExecutableDirective>(S);
6775 return StmtError();
6776 }
6777 }
6778
Reid Kleckner87a31802018-03-12 21:43:02 +00006779 setFunctionHasBranchProtectedScope();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00006780
6781 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6782}
6783
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006784StmtResult
6785Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
6786 Stmt *AStmt, SourceLocation StartLoc,
6787 SourceLocation EndLoc) {
6788 if (!AStmt)
6789 return StmtError();
6790
Alexey Bataeve3727102018-04-18 15:57:46 +00006791 auto *CS = cast<CapturedStmt>(AStmt);
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006792 // 1.2.2 OpenMP Language Terminology
6793 // Structured block - An executable statement with a single entry at the
6794 // top and a single exit at the bottom.
6795 // The point of exit cannot be a branch out of the structured block.
6796 // longjmp() and throw() must not violate the entry/exit criteria.
6797 CS->getCapturedDecl()->setNothrow();
Alexey Bataev8451efa2018-01-15 19:06:12 +00006798 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel);
6799 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6800 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6801 // 1.2.2 OpenMP Language Terminology
6802 // Structured block - An executable statement with a single entry at the
6803 // top and a single exit at the bottom.
6804 // The point of exit cannot be a branch out of the structured block.
6805 // longjmp() and throw() must not violate the entry/exit criteria.
6806 CS->getCapturedDecl()->setNothrow();
6807 }
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006808
Reid Kleckner87a31802018-03-12 21:43:02 +00006809 setFunctionHasBranchProtectedScope();
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00006810
6811 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
6812 AStmt);
6813}
6814
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006815StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
6816 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00006817 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006818 if (!AStmt)
6819 return StmtError();
6820
Alexey Bataeve3727102018-04-18 15:57:46 +00006821 auto *CS = cast<CapturedStmt>(AStmt);
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006822 // 1.2.2 OpenMP Language Terminology
6823 // Structured block - An executable statement with a single entry at the
6824 // top and a single exit at the bottom.
6825 // The point of exit cannot be a branch out of the structured block.
6826 // longjmp() and throw() must not violate the entry/exit criteria.
6827 CS->getCapturedDecl()->setNothrow();
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00006828 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
6829 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6830 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6831 // 1.2.2 OpenMP Language Terminology
6832 // Structured block - An executable statement with a single entry at the
6833 // top and a single exit at the bottom.
6834 // The point of exit cannot be a branch out of the structured block.
6835 // longjmp() and throw() must not violate the entry/exit criteria.
6836 CS->getCapturedDecl()->setNothrow();
6837 }
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006838
6839 OMPLoopDirective::HelperExprs B;
6840 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6841 // define the nested loops number.
6842 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00006843 checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00006844 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006845 VarsWithImplicitDSA, B);
6846 if (NestedLoopCount == 0)
6847 return StmtError();
6848
6849 assert((CurContext->isDependentContext() || B.builtAll()) &&
6850 "omp target parallel for loop exprs were not built");
6851
6852 if (!CurContext->isDependentContext()) {
6853 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00006854 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00006855 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006856 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006857 B.NumIterations, *this, CurScope,
6858 DSAStack))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006859 return StmtError();
6860 }
6861 }
6862
Reid Kleckner87a31802018-03-12 21:43:02 +00006863 setFunctionHasBranchProtectedScope();
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00006864 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
6865 NestedLoopCount, Clauses, AStmt,
6866 B, DSAStack->isCancelRegion());
6867}
6868
Alexey Bataev95b64a92017-05-30 16:00:04 +00006869/// Check for existence of a map clause in the list of clauses.
6870static bool hasClauses(ArrayRef<OMPClause *> Clauses,
6871 const OpenMPClauseKind K) {
6872 return llvm::any_of(
6873 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; });
6874}
Samuel Antaodf67fc42016-01-19 19:15:56 +00006875
Alexey Bataev95b64a92017-05-30 16:00:04 +00006876template <typename... Params>
6877static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K,
6878 const Params... ClauseTypes) {
6879 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...);
Samuel Antaodf67fc42016-01-19 19:15:56 +00006880}
6881
Michael Wong65f367f2015-07-21 13:44:28 +00006882StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
6883 Stmt *AStmt,
6884 SourceLocation StartLoc,
6885 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006886 if (!AStmt)
6887 return StmtError();
6888
6889 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6890
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006891 // OpenMP [2.10.1, Restrictions, p. 97]
6892 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00006893 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr)) {
6894 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
6895 << "'map' or 'use_device_ptr'"
David Majnemer9d168222016-08-05 17:44:54 +00006896 << getOpenMPDirectiveName(OMPD_target_data);
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00006897 return StmtError();
6898 }
6899
Reid Kleckner87a31802018-03-12 21:43:02 +00006900 setFunctionHasBranchProtectedScope();
Michael Wong65f367f2015-07-21 13:44:28 +00006901
6902 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6903 AStmt);
6904}
6905
Samuel Antaodf67fc42016-01-19 19:15:56 +00006906StmtResult
6907Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
6908 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00006909 SourceLocation EndLoc, Stmt *AStmt) {
6910 if (!AStmt)
6911 return StmtError();
6912
Alexey Bataeve3727102018-04-18 15:57:46 +00006913 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00006914 // 1.2.2 OpenMP Language Terminology
6915 // Structured block - An executable statement with a single entry at the
6916 // top and a single exit at the bottom.
6917 // The point of exit cannot be a branch out of the structured block.
6918 // longjmp() and throw() must not violate the entry/exit criteria.
6919 CS->getCapturedDecl()->setNothrow();
6920 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data);
6921 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6922 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6923 // 1.2.2 OpenMP Language Terminology
6924 // Structured block - An executable statement with a single entry at the
6925 // top and a single exit at the bottom.
6926 // The point of exit cannot be a branch out of the structured block.
6927 // longjmp() and throw() must not violate the entry/exit criteria.
6928 CS->getCapturedDecl()->setNothrow();
6929 }
6930
Samuel Antaodf67fc42016-01-19 19:15:56 +00006931 // OpenMP [2.10.2, Restrictions, p. 99]
6932 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00006933 if (!hasClauses(Clauses, OMPC_map)) {
6934 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
6935 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data);
Samuel Antaodf67fc42016-01-19 19:15:56 +00006936 return StmtError();
6937 }
6938
Alexey Bataev7828b252017-11-21 17:08:48 +00006939 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6940 AStmt);
Samuel Antaodf67fc42016-01-19 19:15:56 +00006941}
6942
Samuel Antao72590762016-01-19 20:04:50 +00006943StmtResult
6944Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
6945 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00006946 SourceLocation EndLoc, Stmt *AStmt) {
6947 if (!AStmt)
6948 return StmtError();
6949
Alexey Bataeve3727102018-04-18 15:57:46 +00006950 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00006951 // 1.2.2 OpenMP Language Terminology
6952 // Structured block - An executable statement with a single entry at the
6953 // top and a single exit at the bottom.
6954 // The point of exit cannot be a branch out of the structured block.
6955 // longjmp() and throw() must not violate the entry/exit criteria.
6956 CS->getCapturedDecl()->setNothrow();
6957 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data);
6958 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6959 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6960 // 1.2.2 OpenMP Language Terminology
6961 // Structured block - An executable statement with a single entry at the
6962 // top and a single exit at the bottom.
6963 // The point of exit cannot be a branch out of the structured block.
6964 // longjmp() and throw() must not violate the entry/exit criteria.
6965 CS->getCapturedDecl()->setNothrow();
6966 }
6967
Samuel Antao72590762016-01-19 20:04:50 +00006968 // OpenMP [2.10.3, Restrictions, p. 102]
6969 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00006970 if (!hasClauses(Clauses, OMPC_map)) {
6971 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
6972 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data);
Samuel Antao72590762016-01-19 20:04:50 +00006973 return StmtError();
6974 }
6975
Alexey Bataev7828b252017-11-21 17:08:48 +00006976 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
6977 AStmt);
Samuel Antao72590762016-01-19 20:04:50 +00006978}
6979
Samuel Antao686c70c2016-05-26 17:30:50 +00006980StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
6981 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00006982 SourceLocation EndLoc,
6983 Stmt *AStmt) {
6984 if (!AStmt)
6985 return StmtError();
6986
Alexey Bataeve3727102018-04-18 15:57:46 +00006987 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00006988 // 1.2.2 OpenMP Language Terminology
6989 // Structured block - An executable statement with a single entry at the
6990 // top and a single exit at the bottom.
6991 // The point of exit cannot be a branch out of the structured block.
6992 // longjmp() and throw() must not violate the entry/exit criteria.
6993 CS->getCapturedDecl()->setNothrow();
6994 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update);
6995 ThisCaptureLevel > 1; --ThisCaptureLevel) {
6996 CS = cast<CapturedStmt>(CS->getCapturedStmt());
6997 // 1.2.2 OpenMP Language Terminology
6998 // Structured block - An executable statement with a single entry at the
6999 // top and a single exit at the bottom.
7000 // The point of exit cannot be a branch out of the structured block.
7001 // longjmp() and throw() must not violate the entry/exit criteria.
7002 CS->getCapturedDecl()->setNothrow();
7003 }
7004
Alexey Bataev95b64a92017-05-30 16:00:04 +00007005 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) {
Samuel Antao686c70c2016-05-26 17:30:50 +00007006 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
7007 return StmtError();
7008 }
Alexey Bataev7828b252017-11-21 17:08:48 +00007009 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses,
7010 AStmt);
Samuel Antao686c70c2016-05-26 17:30:50 +00007011}
7012
Alexey Bataev13314bf2014-10-09 04:18:56 +00007013StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
7014 Stmt *AStmt, SourceLocation StartLoc,
7015 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007016 if (!AStmt)
7017 return StmtError();
7018
Alexey Bataeve3727102018-04-18 15:57:46 +00007019 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev13314bf2014-10-09 04:18:56 +00007020 // 1.2.2 OpenMP Language Terminology
7021 // Structured block - An executable statement with a single entry at the
7022 // top and a single exit at the bottom.
7023 // The point of exit cannot be a branch out of the structured block.
7024 // longjmp() and throw() must not violate the entry/exit criteria.
7025 CS->getCapturedDecl()->setNothrow();
7026
Reid Kleckner87a31802018-03-12 21:43:02 +00007027 setFunctionHasBranchProtectedScope();
Alexey Bataev13314bf2014-10-09 04:18:56 +00007028
Alexey Bataevceabd412017-11-30 18:01:54 +00007029 DSAStack->setParentTeamsRegionLoc(StartLoc);
7030
Alexey Bataev13314bf2014-10-09 04:18:56 +00007031 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
7032}
7033
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007034StmtResult
7035Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
7036 SourceLocation EndLoc,
7037 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007038 if (DSAStack->isParentNowaitRegion()) {
7039 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
7040 return StmtError();
7041 }
7042 if (DSAStack->isParentOrderedRegion()) {
7043 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
7044 return StmtError();
7045 }
7046 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
7047 CancelRegion);
7048}
7049
Alexey Bataev87933c72015-09-18 08:07:34 +00007050StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
7051 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00007052 SourceLocation EndLoc,
7053 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev80909872015-07-02 11:25:17 +00007054 if (DSAStack->isParentNowaitRegion()) {
7055 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
7056 return StmtError();
7057 }
7058 if (DSAStack->isParentOrderedRegion()) {
7059 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
7060 return StmtError();
7061 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00007062 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00007063 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
7064 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00007065}
7066
Alexey Bataev382967a2015-12-08 12:06:20 +00007067static bool checkGrainsizeNumTasksClauses(Sema &S,
7068 ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007069 const OMPClause *PrevClause = nullptr;
Alexey Bataev382967a2015-12-08 12:06:20 +00007070 bool ErrorFound = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00007071 for (const OMPClause *C : Clauses) {
Alexey Bataev382967a2015-12-08 12:06:20 +00007072 if (C->getClauseKind() == OMPC_grainsize ||
7073 C->getClauseKind() == OMPC_num_tasks) {
7074 if (!PrevClause)
7075 PrevClause = C;
7076 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007077 S.Diag(C->getBeginLoc(),
Alexey Bataev382967a2015-12-08 12:06:20 +00007078 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
7079 << getOpenMPClauseName(C->getClauseKind())
7080 << getOpenMPClauseName(PrevClause->getClauseKind());
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007081 S.Diag(PrevClause->getBeginLoc(),
Alexey Bataev382967a2015-12-08 12:06:20 +00007082 diag::note_omp_previous_grainsize_num_tasks)
7083 << getOpenMPClauseName(PrevClause->getClauseKind());
7084 ErrorFound = true;
7085 }
7086 }
7087 }
7088 return ErrorFound;
7089}
7090
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007091static bool checkReductionClauseWithNogroup(Sema &S,
7092 ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007093 const OMPClause *ReductionClause = nullptr;
7094 const OMPClause *NogroupClause = nullptr;
7095 for (const OMPClause *C : Clauses) {
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007096 if (C->getClauseKind() == OMPC_reduction) {
7097 ReductionClause = C;
7098 if (NogroupClause)
7099 break;
7100 continue;
7101 }
7102 if (C->getClauseKind() == OMPC_nogroup) {
7103 NogroupClause = C;
7104 if (ReductionClause)
7105 break;
7106 continue;
7107 }
7108 }
7109 if (ReductionClause && NogroupClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007110 S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup)
7111 << SourceRange(NogroupClause->getBeginLoc(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007112 NogroupClause->getEndLoc());
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007113 return true;
7114 }
7115 return false;
7116}
7117
Alexey Bataev49f6e782015-12-01 04:18:41 +00007118StmtResult Sema::ActOnOpenMPTaskLoopDirective(
7119 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007120 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00007121 if (!AStmt)
7122 return StmtError();
7123
7124 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7125 OMPLoopDirective::HelperExprs B;
7126 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7127 // define the nested loops number.
7128 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007129 checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007130 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00007131 VarsWithImplicitDSA, B);
7132 if (NestedLoopCount == 0)
7133 return StmtError();
7134
7135 assert((CurContext->isDependentContext() || B.builtAll()) &&
7136 "omp for loop exprs were not built");
7137
Alexey Bataev382967a2015-12-08 12:06:20 +00007138 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7139 // The grainsize clause and num_tasks clause are mutually exclusive and may
7140 // not appear on the same taskloop directive.
7141 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7142 return StmtError();
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007143 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7144 // If a reduction clause is present on the taskloop directive, the nogroup
7145 // clause must not be specified.
7146 if (checkReductionClauseWithNogroup(*this, Clauses))
7147 return StmtError();
Alexey Bataev382967a2015-12-08 12:06:20 +00007148
Reid Kleckner87a31802018-03-12 21:43:02 +00007149 setFunctionHasBranchProtectedScope();
Alexey Bataev49f6e782015-12-01 04:18:41 +00007150 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
7151 NestedLoopCount, Clauses, AStmt, B);
7152}
7153
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007154StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
7155 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007156 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007157 if (!AStmt)
7158 return StmtError();
7159
7160 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7161 OMPLoopDirective::HelperExprs B;
7162 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7163 // define the nested loops number.
7164 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007165 checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007166 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
7167 VarsWithImplicitDSA, B);
7168 if (NestedLoopCount == 0)
7169 return StmtError();
7170
7171 assert((CurContext->isDependentContext() || B.builtAll()) &&
7172 "omp for loop exprs were not built");
7173
Alexey Bataev5a3af132016-03-29 08:58:54 +00007174 if (!CurContext->isDependentContext()) {
7175 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007176 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007177 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007178 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007179 B.NumIterations, *this, CurScope,
7180 DSAStack))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007181 return StmtError();
7182 }
7183 }
7184
Alexey Bataev382967a2015-12-08 12:06:20 +00007185 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7186 // The grainsize clause and num_tasks clause are mutually exclusive and may
7187 // not appear on the same taskloop directive.
7188 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7189 return StmtError();
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007190 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7191 // If a reduction clause is present on the taskloop directive, the nogroup
7192 // clause must not be specified.
7193 if (checkReductionClauseWithNogroup(*this, Clauses))
7194 return StmtError();
Alexey Bataev438388c2017-11-22 18:34:02 +00007195 if (checkSimdlenSafelenSpecified(*this, Clauses))
7196 return StmtError();
Alexey Bataev382967a2015-12-08 12:06:20 +00007197
Reid Kleckner87a31802018-03-12 21:43:02 +00007198 setFunctionHasBranchProtectedScope();
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007199 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
7200 NestedLoopCount, Clauses, AStmt, B);
7201}
7202
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007203StmtResult Sema::ActOnOpenMPDistributeDirective(
7204 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007205 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007206 if (!AStmt)
7207 return StmtError();
7208
7209 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7210 OMPLoopDirective::HelperExprs B;
7211 // In presence of clause 'collapse' with number of loops, it will
7212 // define the nested loops number.
7213 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007214 checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007215 nullptr /*ordered not a clause on distribute*/, AStmt,
7216 *this, *DSAStack, VarsWithImplicitDSA, B);
7217 if (NestedLoopCount == 0)
7218 return StmtError();
7219
7220 assert((CurContext->isDependentContext() || B.builtAll()) &&
7221 "omp for loop exprs were not built");
7222
Reid Kleckner87a31802018-03-12 21:43:02 +00007223 setFunctionHasBranchProtectedScope();
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007224 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
7225 NestedLoopCount, Clauses, AStmt, B);
7226}
7227
Carlo Bertolli9925f152016-06-27 14:55:37 +00007228StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
7229 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007230 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Carlo Bertolli9925f152016-06-27 14:55:37 +00007231 if (!AStmt)
7232 return StmtError();
7233
Alexey Bataeve3727102018-04-18 15:57:46 +00007234 auto *CS = cast<CapturedStmt>(AStmt);
Carlo Bertolli9925f152016-06-27 14:55:37 +00007235 // 1.2.2 OpenMP Language Terminology
7236 // Structured block - An executable statement with a single entry at the
7237 // top and a single exit at the bottom.
7238 // The point of exit cannot be a branch out of the structured block.
7239 // longjmp() and throw() must not violate the entry/exit criteria.
7240 CS->getCapturedDecl()->setNothrow();
Alexey Bataev7f96c372017-11-22 17:19:31 +00007241 for (int ThisCaptureLevel =
7242 getOpenMPCaptureLevels(OMPD_distribute_parallel_for);
7243 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7244 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7245 // 1.2.2 OpenMP Language Terminology
7246 // Structured block - An executable statement with a single entry at the
7247 // top and a single exit at the bottom.
7248 // The point of exit cannot be a branch out of the structured block.
7249 // longjmp() and throw() must not violate the entry/exit criteria.
7250 CS->getCapturedDecl()->setNothrow();
7251 }
Carlo Bertolli9925f152016-06-27 14:55:37 +00007252
7253 OMPLoopDirective::HelperExprs B;
7254 // In presence of clause 'collapse' with number of loops, it will
7255 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007256 unsigned NestedLoopCount = checkOpenMPLoop(
Carlo Bertolli9925f152016-06-27 14:55:37 +00007257 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataev7f96c372017-11-22 17:19:31 +00007258 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Carlo Bertolli9925f152016-06-27 14:55:37 +00007259 VarsWithImplicitDSA, B);
7260 if (NestedLoopCount == 0)
7261 return StmtError();
7262
7263 assert((CurContext->isDependentContext() || B.builtAll()) &&
7264 "omp for loop exprs were not built");
7265
Reid Kleckner87a31802018-03-12 21:43:02 +00007266 setFunctionHasBranchProtectedScope();
Carlo Bertolli9925f152016-06-27 14:55:37 +00007267 return OMPDistributeParallelForDirective::Create(
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00007268 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7269 DSAStack->isCancelRegion());
Carlo Bertolli9925f152016-06-27 14:55:37 +00007270}
7271
Kelvin Li4a39add2016-07-05 05:00:15 +00007272StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
7273 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007274 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li4a39add2016-07-05 05:00:15 +00007275 if (!AStmt)
7276 return StmtError();
7277
Alexey Bataeve3727102018-04-18 15:57:46 +00007278 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li4a39add2016-07-05 05:00:15 +00007279 // 1.2.2 OpenMP Language Terminology
7280 // Structured block - An executable statement with a single entry at the
7281 // top and a single exit at the bottom.
7282 // The point of exit cannot be a branch out of the structured block.
7283 // longjmp() and throw() must not violate the entry/exit criteria.
7284 CS->getCapturedDecl()->setNothrow();
Alexey Bataev974acd62017-11-27 19:38:52 +00007285 for (int ThisCaptureLevel =
7286 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd);
7287 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7288 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7289 // 1.2.2 OpenMP Language Terminology
7290 // Structured block - An executable statement with a single entry at the
7291 // top and a single exit at the bottom.
7292 // The point of exit cannot be a branch out of the structured block.
7293 // longjmp() and throw() must not violate the entry/exit criteria.
7294 CS->getCapturedDecl()->setNothrow();
7295 }
Kelvin Li4a39add2016-07-05 05:00:15 +00007296
7297 OMPLoopDirective::HelperExprs B;
7298 // In presence of clause 'collapse' with number of loops, it will
7299 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007300 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li4a39add2016-07-05 05:00:15 +00007301 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev974acd62017-11-27 19:38:52 +00007302 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li4a39add2016-07-05 05:00:15 +00007303 VarsWithImplicitDSA, B);
7304 if (NestedLoopCount == 0)
7305 return StmtError();
7306
7307 assert((CurContext->isDependentContext() || B.builtAll()) &&
7308 "omp for loop exprs were not built");
7309
Alexey Bataev438388c2017-11-22 18:34:02 +00007310 if (!CurContext->isDependentContext()) {
7311 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007312 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00007313 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7314 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7315 B.NumIterations, *this, CurScope,
7316 DSAStack))
7317 return StmtError();
7318 }
7319 }
7320
Kelvin Lic5609492016-07-15 04:39:07 +00007321 if (checkSimdlenSafelenSpecified(*this, Clauses))
7322 return StmtError();
7323
Reid Kleckner87a31802018-03-12 21:43:02 +00007324 setFunctionHasBranchProtectedScope();
Kelvin Li4a39add2016-07-05 05:00:15 +00007325 return OMPDistributeParallelForSimdDirective::Create(
7326 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7327}
7328
Kelvin Li787f3fc2016-07-06 04:45:38 +00007329StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
7330 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007331 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li787f3fc2016-07-06 04:45:38 +00007332 if (!AStmt)
7333 return StmtError();
7334
Alexey Bataeve3727102018-04-18 15:57:46 +00007335 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li787f3fc2016-07-06 04:45:38 +00007336 // 1.2.2 OpenMP Language Terminology
7337 // Structured block - An executable statement with a single entry at the
7338 // top and a single exit at the bottom.
7339 // The point of exit cannot be a branch out of the structured block.
7340 // longjmp() and throw() must not violate the entry/exit criteria.
7341 CS->getCapturedDecl()->setNothrow();
Alexey Bataev617db5f2017-12-04 15:38:33 +00007342 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd);
7343 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7344 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7345 // 1.2.2 OpenMP Language Terminology
7346 // Structured block - An executable statement with a single entry at the
7347 // top and a single exit at the bottom.
7348 // The point of exit cannot be a branch out of the structured block.
7349 // longjmp() and throw() must not violate the entry/exit criteria.
7350 CS->getCapturedDecl()->setNothrow();
7351 }
Kelvin Li787f3fc2016-07-06 04:45:38 +00007352
7353 OMPLoopDirective::HelperExprs B;
7354 // In presence of clause 'collapse' with number of loops, it will
7355 // define the nested loops number.
7356 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007357 checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev617db5f2017-12-04 15:38:33 +00007358 nullptr /*ordered not a clause on distribute*/, CS, *this,
7359 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li787f3fc2016-07-06 04:45:38 +00007360 if (NestedLoopCount == 0)
7361 return StmtError();
7362
7363 assert((CurContext->isDependentContext() || B.builtAll()) &&
7364 "omp for loop exprs were not built");
7365
Alexey Bataev438388c2017-11-22 18:34:02 +00007366 if (!CurContext->isDependentContext()) {
7367 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007368 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00007369 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7370 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7371 B.NumIterations, *this, CurScope,
7372 DSAStack))
7373 return StmtError();
7374 }
7375 }
7376
Kelvin Lic5609492016-07-15 04:39:07 +00007377 if (checkSimdlenSafelenSpecified(*this, Clauses))
7378 return StmtError();
7379
Reid Kleckner87a31802018-03-12 21:43:02 +00007380 setFunctionHasBranchProtectedScope();
Kelvin Li787f3fc2016-07-06 04:45:38 +00007381 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
7382 NestedLoopCount, Clauses, AStmt, B);
7383}
7384
Kelvin Lia579b912016-07-14 02:54:56 +00007385StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
7386 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007387 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Lia579b912016-07-14 02:54:56 +00007388 if (!AStmt)
7389 return StmtError();
7390
Alexey Bataeve3727102018-04-18 15:57:46 +00007391 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Lia579b912016-07-14 02:54:56 +00007392 // 1.2.2 OpenMP Language Terminology
7393 // Structured block - An executable statement with a single entry at the
7394 // top and a single exit at the bottom.
7395 // The point of exit cannot be a branch out of the structured block.
7396 // longjmp() and throw() must not violate the entry/exit criteria.
7397 CS->getCapturedDecl()->setNothrow();
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007398 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
7399 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7400 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7401 // 1.2.2 OpenMP Language Terminology
7402 // Structured block - An executable statement with a single entry at the
7403 // top and a single exit at the bottom.
7404 // The point of exit cannot be a branch out of the structured block.
7405 // longjmp() and throw() must not violate the entry/exit criteria.
7406 CS->getCapturedDecl()->setNothrow();
7407 }
Kelvin Lia579b912016-07-14 02:54:56 +00007408
7409 OMPLoopDirective::HelperExprs B;
7410 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7411 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007412 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Lia579b912016-07-14 02:54:56 +00007413 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007414 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Kelvin Lia579b912016-07-14 02:54:56 +00007415 VarsWithImplicitDSA, B);
7416 if (NestedLoopCount == 0)
7417 return StmtError();
7418
7419 assert((CurContext->isDependentContext() || B.builtAll()) &&
7420 "omp target parallel for simd loop exprs were not built");
7421
7422 if (!CurContext->isDependentContext()) {
7423 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007424 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007425 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Lia579b912016-07-14 02:54:56 +00007426 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7427 B.NumIterations, *this, CurScope,
7428 DSAStack))
7429 return StmtError();
7430 }
7431 }
Kelvin Lic5609492016-07-15 04:39:07 +00007432 if (checkSimdlenSafelenSpecified(*this, Clauses))
Kelvin Lia579b912016-07-14 02:54:56 +00007433 return StmtError();
7434
Reid Kleckner87a31802018-03-12 21:43:02 +00007435 setFunctionHasBranchProtectedScope();
Kelvin Lia579b912016-07-14 02:54:56 +00007436 return OMPTargetParallelForSimdDirective::Create(
7437 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7438}
7439
Kelvin Li986330c2016-07-20 22:57:10 +00007440StmtResult Sema::ActOnOpenMPTargetSimdDirective(
7441 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007442 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li986330c2016-07-20 22:57:10 +00007443 if (!AStmt)
7444 return StmtError();
7445
Alexey Bataeve3727102018-04-18 15:57:46 +00007446 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li986330c2016-07-20 22:57:10 +00007447 // 1.2.2 OpenMP Language Terminology
7448 // Structured block - An executable statement with a single entry at the
7449 // top and a single exit at the bottom.
7450 // The point of exit cannot be a branch out of the structured block.
7451 // longjmp() and throw() must not violate the entry/exit criteria.
7452 CS->getCapturedDecl()->setNothrow();
Alexey Bataevf8365372017-11-17 17:57:25 +00007453 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd);
7454 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7455 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7456 // 1.2.2 OpenMP Language Terminology
7457 // Structured block - An executable statement with a single entry at the
7458 // top and a single exit at the bottom.
7459 // The point of exit cannot be a branch out of the structured block.
7460 // longjmp() and throw() must not violate the entry/exit criteria.
7461 CS->getCapturedDecl()->setNothrow();
7462 }
7463
Kelvin Li986330c2016-07-20 22:57:10 +00007464 OMPLoopDirective::HelperExprs B;
7465 // In presence of clause 'collapse' with number of loops, it will define the
7466 // nested loops number.
David Majnemer9d168222016-08-05 17:44:54 +00007467 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007468 checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
Alexey Bataevf8365372017-11-17 17:57:25 +00007469 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Kelvin Li986330c2016-07-20 22:57:10 +00007470 VarsWithImplicitDSA, B);
7471 if (NestedLoopCount == 0)
7472 return StmtError();
7473
7474 assert((CurContext->isDependentContext() || B.builtAll()) &&
7475 "omp target simd loop exprs were not built");
7476
7477 if (!CurContext->isDependentContext()) {
7478 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007479 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007480 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Li986330c2016-07-20 22:57:10 +00007481 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7482 B.NumIterations, *this, CurScope,
7483 DSAStack))
7484 return StmtError();
7485 }
7486 }
7487
7488 if (checkSimdlenSafelenSpecified(*this, Clauses))
7489 return StmtError();
7490
Reid Kleckner87a31802018-03-12 21:43:02 +00007491 setFunctionHasBranchProtectedScope();
Kelvin Li986330c2016-07-20 22:57:10 +00007492 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
7493 NestedLoopCount, Clauses, AStmt, B);
7494}
7495
Kelvin Li02532872016-08-05 14:37:37 +00007496StmtResult Sema::ActOnOpenMPTeamsDistributeDirective(
7497 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007498 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li02532872016-08-05 14:37:37 +00007499 if (!AStmt)
7500 return StmtError();
7501
Alexey Bataeve3727102018-04-18 15:57:46 +00007502 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li02532872016-08-05 14:37:37 +00007503 // 1.2.2 OpenMP Language Terminology
7504 // Structured block - An executable statement with a single entry at the
7505 // top and a single exit at the bottom.
7506 // The point of exit cannot be a branch out of the structured block.
7507 // longjmp() and throw() must not violate the entry/exit criteria.
7508 CS->getCapturedDecl()->setNothrow();
Alexey Bataev95c6dd42017-11-29 15:14:16 +00007509 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute);
7510 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7511 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7512 // 1.2.2 OpenMP Language Terminology
7513 // Structured block - An executable statement with a single entry at the
7514 // top and a single exit at the bottom.
7515 // The point of exit cannot be a branch out of the structured block.
7516 // longjmp() and throw() must not violate the entry/exit criteria.
7517 CS->getCapturedDecl()->setNothrow();
7518 }
Kelvin Li02532872016-08-05 14:37:37 +00007519
7520 OMPLoopDirective::HelperExprs B;
7521 // In presence of clause 'collapse' with number of loops, it will
7522 // define the nested loops number.
7523 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007524 checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses),
Alexey Bataev95c6dd42017-11-29 15:14:16 +00007525 nullptr /*ordered not a clause on distribute*/, CS, *this,
7526 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li02532872016-08-05 14:37:37 +00007527 if (NestedLoopCount == 0)
7528 return StmtError();
7529
7530 assert((CurContext->isDependentContext() || B.builtAll()) &&
7531 "omp teams distribute loop exprs were not built");
7532
Reid Kleckner87a31802018-03-12 21:43:02 +00007533 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007534
7535 DSAStack->setParentTeamsRegionLoc(StartLoc);
7536
David Majnemer9d168222016-08-05 17:44:54 +00007537 return OMPTeamsDistributeDirective::Create(
7538 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Kelvin Li02532872016-08-05 14:37:37 +00007539}
7540
Kelvin Li4e325f72016-10-25 12:50:55 +00007541StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective(
7542 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007543 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li4e325f72016-10-25 12:50:55 +00007544 if (!AStmt)
7545 return StmtError();
7546
Alexey Bataeve3727102018-04-18 15:57:46 +00007547 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li4e325f72016-10-25 12:50:55 +00007548 // 1.2.2 OpenMP Language Terminology
7549 // Structured block - An executable statement with a single entry at the
7550 // top and a single exit at the bottom.
7551 // The point of exit cannot be a branch out of the structured block.
7552 // longjmp() and throw() must not violate the entry/exit criteria.
7553 CS->getCapturedDecl()->setNothrow();
Alexey Bataev999277a2017-12-06 14:31:09 +00007554 for (int ThisCaptureLevel =
7555 getOpenMPCaptureLevels(OMPD_teams_distribute_simd);
7556 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7557 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7558 // 1.2.2 OpenMP Language Terminology
7559 // Structured block - An executable statement with a single entry at the
7560 // top and a single exit at the bottom.
7561 // The point of exit cannot be a branch out of the structured block.
7562 // longjmp() and throw() must not violate the entry/exit criteria.
7563 CS->getCapturedDecl()->setNothrow();
7564 }
7565
Kelvin Li4e325f72016-10-25 12:50:55 +00007566
7567 OMPLoopDirective::HelperExprs B;
7568 // In presence of clause 'collapse' with number of loops, it will
7569 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007570 unsigned NestedLoopCount = checkOpenMPLoop(
Samuel Antao4c8035b2016-12-12 18:00:20 +00007571 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev999277a2017-12-06 14:31:09 +00007572 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Samuel Antao4c8035b2016-12-12 18:00:20 +00007573 VarsWithImplicitDSA, B);
Kelvin Li4e325f72016-10-25 12:50:55 +00007574
7575 if (NestedLoopCount == 0)
7576 return StmtError();
7577
7578 assert((CurContext->isDependentContext() || B.builtAll()) &&
7579 "omp teams distribute simd loop exprs were not built");
7580
7581 if (!CurContext->isDependentContext()) {
7582 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007583 for (OMPClause *C : Clauses) {
Kelvin Li4e325f72016-10-25 12:50:55 +00007584 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7585 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7586 B.NumIterations, *this, CurScope,
7587 DSAStack))
7588 return StmtError();
7589 }
7590 }
7591
7592 if (checkSimdlenSafelenSpecified(*this, Clauses))
7593 return StmtError();
7594
Reid Kleckner87a31802018-03-12 21:43:02 +00007595 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007596
7597 DSAStack->setParentTeamsRegionLoc(StartLoc);
7598
Kelvin Li4e325f72016-10-25 12:50:55 +00007599 return OMPTeamsDistributeSimdDirective::Create(
7600 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7601}
7602
Kelvin Li579e41c2016-11-30 23:51:03 +00007603StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective(
7604 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007605 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li579e41c2016-11-30 23:51:03 +00007606 if (!AStmt)
7607 return StmtError();
7608
Alexey Bataeve3727102018-04-18 15:57:46 +00007609 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li579e41c2016-11-30 23:51:03 +00007610 // 1.2.2 OpenMP Language Terminology
7611 // Structured block - An executable statement with a single entry at the
7612 // top and a single exit at the bottom.
7613 // The point of exit cannot be a branch out of the structured block.
7614 // longjmp() and throw() must not violate the entry/exit criteria.
7615 CS->getCapturedDecl()->setNothrow();
7616
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00007617 for (int ThisCaptureLevel =
7618 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd);
7619 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7620 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7621 // 1.2.2 OpenMP Language Terminology
7622 // Structured block - An executable statement with a single entry at the
7623 // top and a single exit at the bottom.
7624 // The point of exit cannot be a branch out of the structured block.
7625 // longjmp() and throw() must not violate the entry/exit criteria.
7626 CS->getCapturedDecl()->setNothrow();
7627 }
7628
Kelvin Li579e41c2016-11-30 23:51:03 +00007629 OMPLoopDirective::HelperExprs B;
7630 // In presence of clause 'collapse' with number of loops, it will
7631 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007632 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li579e41c2016-11-30 23:51:03 +00007633 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00007634 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li579e41c2016-11-30 23:51:03 +00007635 VarsWithImplicitDSA, B);
7636
7637 if (NestedLoopCount == 0)
7638 return StmtError();
7639
7640 assert((CurContext->isDependentContext() || B.builtAll()) &&
7641 "omp for loop exprs were not built");
7642
7643 if (!CurContext->isDependentContext()) {
7644 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007645 for (OMPClause *C : Clauses) {
Kelvin Li579e41c2016-11-30 23:51:03 +00007646 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7647 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7648 B.NumIterations, *this, CurScope,
7649 DSAStack))
7650 return StmtError();
7651 }
7652 }
7653
7654 if (checkSimdlenSafelenSpecified(*this, Clauses))
7655 return StmtError();
7656
Reid Kleckner87a31802018-03-12 21:43:02 +00007657 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007658
7659 DSAStack->setParentTeamsRegionLoc(StartLoc);
7660
Kelvin Li579e41c2016-11-30 23:51:03 +00007661 return OMPTeamsDistributeParallelForSimdDirective::Create(
7662 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7663}
7664
Kelvin Li7ade93f2016-12-09 03:24:30 +00007665StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective(
7666 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007667 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li7ade93f2016-12-09 03:24:30 +00007668 if (!AStmt)
7669 return StmtError();
7670
Alexey Bataeve3727102018-04-18 15:57:46 +00007671 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li7ade93f2016-12-09 03:24:30 +00007672 // 1.2.2 OpenMP Language Terminology
7673 // Structured block - An executable statement with a single entry at the
7674 // top and a single exit at the bottom.
7675 // The point of exit cannot be a branch out of the structured block.
7676 // longjmp() and throw() must not violate the entry/exit criteria.
7677 CS->getCapturedDecl()->setNothrow();
7678
Carlo Bertolli62fae152017-11-20 20:46:39 +00007679 for (int ThisCaptureLevel =
7680 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for);
7681 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7682 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7683 // 1.2.2 OpenMP Language Terminology
7684 // Structured block - An executable statement with a single entry at the
7685 // top and a single exit at the bottom.
7686 // The point of exit cannot be a branch out of the structured block.
7687 // longjmp() and throw() must not violate the entry/exit criteria.
7688 CS->getCapturedDecl()->setNothrow();
7689 }
7690
Kelvin Li7ade93f2016-12-09 03:24:30 +00007691 OMPLoopDirective::HelperExprs B;
7692 // In presence of clause 'collapse' with number of loops, it will
7693 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007694 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li7ade93f2016-12-09 03:24:30 +00007695 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
Carlo Bertolli62fae152017-11-20 20:46:39 +00007696 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li7ade93f2016-12-09 03:24:30 +00007697 VarsWithImplicitDSA, B);
7698
7699 if (NestedLoopCount == 0)
7700 return StmtError();
7701
7702 assert((CurContext->isDependentContext() || B.builtAll()) &&
7703 "omp for loop exprs were not built");
7704
Reid Kleckner87a31802018-03-12 21:43:02 +00007705 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007706
7707 DSAStack->setParentTeamsRegionLoc(StartLoc);
7708
Kelvin Li7ade93f2016-12-09 03:24:30 +00007709 return OMPTeamsDistributeParallelForDirective::Create(
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00007710 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7711 DSAStack->isCancelRegion());
Kelvin Li7ade93f2016-12-09 03:24:30 +00007712}
7713
Kelvin Libf594a52016-12-17 05:48:59 +00007714StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses,
7715 Stmt *AStmt,
7716 SourceLocation StartLoc,
7717 SourceLocation EndLoc) {
7718 if (!AStmt)
7719 return StmtError();
7720
Alexey Bataeve3727102018-04-18 15:57:46 +00007721 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Libf594a52016-12-17 05:48:59 +00007722 // 1.2.2 OpenMP Language Terminology
7723 // Structured block - An executable statement with a single entry at the
7724 // top and a single exit at the bottom.
7725 // The point of exit cannot be a branch out of the structured block.
7726 // longjmp() and throw() must not violate the entry/exit criteria.
7727 CS->getCapturedDecl()->setNothrow();
7728
Alexey Bataevf9fc42e2017-11-22 14:25:55 +00007729 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams);
7730 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7731 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7732 // 1.2.2 OpenMP Language Terminology
7733 // Structured block - An executable statement with a single entry at the
7734 // top and a single exit at the bottom.
7735 // The point of exit cannot be a branch out of the structured block.
7736 // longjmp() and throw() must not violate the entry/exit criteria.
7737 CS->getCapturedDecl()->setNothrow();
7738 }
Reid Kleckner87a31802018-03-12 21:43:02 +00007739 setFunctionHasBranchProtectedScope();
Kelvin Libf594a52016-12-17 05:48:59 +00007740
7741 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses,
7742 AStmt);
7743}
7744
Kelvin Li83c451e2016-12-25 04:52:54 +00007745StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective(
7746 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007747 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li83c451e2016-12-25 04:52:54 +00007748 if (!AStmt)
7749 return StmtError();
7750
Alexey Bataeve3727102018-04-18 15:57:46 +00007751 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li83c451e2016-12-25 04:52:54 +00007752 // 1.2.2 OpenMP Language Terminology
7753 // Structured block - An executable statement with a single entry at the
7754 // top and a single exit at the bottom.
7755 // The point of exit cannot be a branch out of the structured block.
7756 // longjmp() and throw() must not violate the entry/exit criteria.
7757 CS->getCapturedDecl()->setNothrow();
Alexey Bataevdfa430f2017-12-08 15:03:50 +00007758 for (int ThisCaptureLevel =
7759 getOpenMPCaptureLevels(OMPD_target_teams_distribute);
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 }
Kelvin Li83c451e2016-12-25 04:52:54 +00007769
7770 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(
Alexey Bataevdfa430f2017-12-08 15:03:50 +00007774 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses),
7775 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li83c451e2016-12-25 04:52:54 +00007776 VarsWithImplicitDSA, B);
7777 if (NestedLoopCount == 0)
7778 return StmtError();
7779
7780 assert((CurContext->isDependentContext() || B.builtAll()) &&
7781 "omp target teams distribute loop exprs were not built");
7782
Reid Kleckner87a31802018-03-12 21:43:02 +00007783 setFunctionHasBranchProtectedScope();
Kelvin Li83c451e2016-12-25 04:52:54 +00007784 return OMPTargetTeamsDistributeDirective::Create(
7785 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7786}
7787
Kelvin Li80e8f562016-12-29 22:16:30 +00007788StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective(
7789 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007790 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li80e8f562016-12-29 22:16:30 +00007791 if (!AStmt)
7792 return StmtError();
7793
Alexey Bataeve3727102018-04-18 15:57:46 +00007794 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li80e8f562016-12-29 22:16:30 +00007795 // 1.2.2 OpenMP Language Terminology
7796 // Structured block - An executable statement with a single entry at the
7797 // top and a single exit at the bottom.
7798 // The point of exit cannot be a branch out of the structured block.
7799 // longjmp() and throw() must not violate the entry/exit criteria.
7800 CS->getCapturedDecl()->setNothrow();
Carlo Bertolli52978c32018-01-03 21:12:44 +00007801 for (int ThisCaptureLevel =
7802 getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for);
7803 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7804 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7805 // 1.2.2 OpenMP Language Terminology
7806 // Structured block - An executable statement with a single entry at the
7807 // top and a single exit at the bottom.
7808 // The point of exit cannot be a branch out of the structured block.
7809 // longjmp() and throw() must not violate the entry/exit criteria.
7810 CS->getCapturedDecl()->setNothrow();
7811 }
7812
Kelvin Li80e8f562016-12-29 22:16:30 +00007813 OMPLoopDirective::HelperExprs B;
7814 // In presence of clause 'collapse' with number of loops, it will
7815 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007816 unsigned NestedLoopCount = checkOpenMPLoop(
Carlo Bertolli52978c32018-01-03 21:12:44 +00007817 OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
7818 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li80e8f562016-12-29 22:16:30 +00007819 VarsWithImplicitDSA, B);
7820 if (NestedLoopCount == 0)
7821 return StmtError();
7822
7823 assert((CurContext->isDependentContext() || B.builtAll()) &&
7824 "omp target teams distribute parallel for loop exprs were not built");
7825
Alexey Bataev647dd842018-01-15 20:59:40 +00007826 if (!CurContext->isDependentContext()) {
7827 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007828 for (OMPClause *C : Clauses) {
Alexey Bataev647dd842018-01-15 20:59:40 +00007829 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7830 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7831 B.NumIterations, *this, CurScope,
7832 DSAStack))
7833 return StmtError();
7834 }
7835 }
7836
Reid Kleckner87a31802018-03-12 21:43:02 +00007837 setFunctionHasBranchProtectedScope();
Kelvin Li80e8f562016-12-29 22:16:30 +00007838 return OMPTargetTeamsDistributeParallelForDirective::Create(
Alexey Bataev16e79882017-11-22 21:12:03 +00007839 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7840 DSAStack->isCancelRegion());
Kelvin Li80e8f562016-12-29 22:16:30 +00007841}
7842
Kelvin Li1851df52017-01-03 05:23:48 +00007843StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
7844 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007845 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li1851df52017-01-03 05:23:48 +00007846 if (!AStmt)
7847 return StmtError();
7848
Alexey Bataeve3727102018-04-18 15:57:46 +00007849 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li1851df52017-01-03 05:23:48 +00007850 // 1.2.2 OpenMP Language Terminology
7851 // Structured block - An executable statement with a single entry at the
7852 // top and a single exit at the bottom.
7853 // The point of exit cannot be a branch out of the structured block.
7854 // longjmp() and throw() must not violate the entry/exit criteria.
7855 CS->getCapturedDecl()->setNothrow();
Alexey Bataev647dd842018-01-15 20:59:40 +00007856 for (int ThisCaptureLevel = getOpenMPCaptureLevels(
7857 OMPD_target_teams_distribute_parallel_for_simd);
7858 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7859 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7860 // 1.2.2 OpenMP Language Terminology
7861 // Structured block - An executable statement with a single entry at the
7862 // top and a single exit at the bottom.
7863 // The point of exit cannot be a branch out of the structured block.
7864 // longjmp() and throw() must not violate the entry/exit criteria.
7865 CS->getCapturedDecl()->setNothrow();
7866 }
Kelvin Li1851df52017-01-03 05:23:48 +00007867
7868 OMPLoopDirective::HelperExprs B;
7869 // In presence of clause 'collapse' with number of loops, it will
7870 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007871 unsigned NestedLoopCount =
7872 checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd,
Alexey Bataev647dd842018-01-15 20:59:40 +00007873 getCollapseNumberExpr(Clauses),
7874 nullptr /*ordered not a clause on distribute*/, CS, *this,
7875 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li1851df52017-01-03 05:23:48 +00007876 if (NestedLoopCount == 0)
7877 return StmtError();
7878
7879 assert((CurContext->isDependentContext() || B.builtAll()) &&
7880 "omp target teams distribute parallel for simd loop exprs were not "
7881 "built");
7882
7883 if (!CurContext->isDependentContext()) {
7884 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007885 for (OMPClause *C : Clauses) {
Kelvin Li1851df52017-01-03 05:23:48 +00007886 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7887 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7888 B.NumIterations, *this, CurScope,
7889 DSAStack))
7890 return StmtError();
7891 }
7892 }
7893
Alexey Bataev438388c2017-11-22 18:34:02 +00007894 if (checkSimdlenSafelenSpecified(*this, Clauses))
7895 return StmtError();
7896
Reid Kleckner87a31802018-03-12 21:43:02 +00007897 setFunctionHasBranchProtectedScope();
Kelvin Li1851df52017-01-03 05:23:48 +00007898 return OMPTargetTeamsDistributeParallelForSimdDirective::Create(
7899 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7900}
7901
Kelvin Lida681182017-01-10 18:08:18 +00007902StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective(
7903 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007904 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Lida681182017-01-10 18:08:18 +00007905 if (!AStmt)
7906 return StmtError();
7907
7908 auto *CS = cast<CapturedStmt>(AStmt);
7909 // 1.2.2 OpenMP Language Terminology
7910 // Structured block - An executable statement with a single entry at the
7911 // top and a single exit at the bottom.
7912 // The point of exit cannot be a branch out of the structured block.
7913 // longjmp() and throw() must not violate the entry/exit criteria.
7914 CS->getCapturedDecl()->setNothrow();
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00007915 for (int ThisCaptureLevel =
7916 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd);
7917 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7918 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7919 // 1.2.2 OpenMP Language Terminology
7920 // Structured block - An executable statement with a single entry at the
7921 // top and a single exit at the bottom.
7922 // The point of exit cannot be a branch out of the structured block.
7923 // longjmp() and throw() must not violate the entry/exit criteria.
7924 CS->getCapturedDecl()->setNothrow();
7925 }
Kelvin Lida681182017-01-10 18:08:18 +00007926
7927 OMPLoopDirective::HelperExprs B;
7928 // In presence of clause 'collapse' with number of loops, it will
7929 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007930 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Lida681182017-01-10 18:08:18 +00007931 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00007932 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Lida681182017-01-10 18:08:18 +00007933 VarsWithImplicitDSA, B);
7934 if (NestedLoopCount == 0)
7935 return StmtError();
7936
7937 assert((CurContext->isDependentContext() || B.builtAll()) &&
7938 "omp target teams distribute simd loop exprs were not built");
7939
Alexey Bataev438388c2017-11-22 18:34:02 +00007940 if (!CurContext->isDependentContext()) {
7941 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007942 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00007943 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7944 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7945 B.NumIterations, *this, CurScope,
7946 DSAStack))
7947 return StmtError();
7948 }
7949 }
7950
7951 if (checkSimdlenSafelenSpecified(*this, Clauses))
7952 return StmtError();
7953
Reid Kleckner87a31802018-03-12 21:43:02 +00007954 setFunctionHasBranchProtectedScope();
Kelvin Lida681182017-01-10 18:08:18 +00007955 return OMPTargetTeamsDistributeSimdDirective::Create(
7956 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7957}
7958
Alexey Bataeved09d242014-05-28 05:53:51 +00007959OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007960 SourceLocation StartLoc,
7961 SourceLocation LParenLoc,
7962 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007963 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00007964 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00007965 case OMPC_final:
7966 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
7967 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00007968 case OMPC_num_threads:
7969 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
7970 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00007971 case OMPC_safelen:
7972 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
7973 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00007974 case OMPC_simdlen:
7975 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
7976 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00007977 case OMPC_collapse:
7978 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
7979 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00007980 case OMPC_ordered:
7981 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
7982 break;
Michael Wonge710d542015-08-07 16:16:36 +00007983 case OMPC_device:
7984 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
7985 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00007986 case OMPC_num_teams:
7987 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
7988 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007989 case OMPC_thread_limit:
7990 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
7991 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00007992 case OMPC_priority:
7993 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
7994 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00007995 case OMPC_grainsize:
7996 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
7997 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00007998 case OMPC_num_tasks:
7999 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
8000 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00008001 case OMPC_hint:
8002 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
8003 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008004 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008005 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008006 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008007 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008008 case OMPC_private:
8009 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00008010 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008011 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00008012 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008013 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008014 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00008015 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008016 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008017 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008018 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00008019 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008020 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008021 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008022 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00008023 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008024 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008025 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008026 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008027 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008028 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008029 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00008030 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008031 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008032 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00008033 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008034 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008035 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008036 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008037 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00008038 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00008039 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00008040 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00008041 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00008042 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00008043 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00008044 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00008045 case OMPC_dynamic_allocators:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008046 llvm_unreachable("Clause is not allowed.");
8047 }
8048 return Res;
8049}
8050
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008051// An OpenMP directive such as 'target parallel' has two captured regions:
8052// for the 'target' and 'parallel' respectively. This function returns
8053// the region in which to capture expressions associated with a clause.
8054// A return value of OMPD_unknown signifies that the expression should not
8055// be captured.
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008056static OpenMPDirectiveKind getOpenMPCaptureRegionForClause(
8057 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
8058 OpenMPDirectiveKind NameModifier = OMPD_unknown) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008059 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008060 switch (CKind) {
8061 case OMPC_if:
8062 switch (DKind) {
8063 case OMPD_target_parallel:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008064 case OMPD_target_parallel_for:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00008065 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008066 // If this clause applies to the nested 'parallel' region, capture within
8067 // the 'target' region, otherwise do not capture.
8068 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
8069 CaptureRegion = OMPD_target;
8070 break;
Carlo Bertolli52978c32018-01-03 21:12:44 +00008071 case OMPD_target_teams_distribute_parallel_for:
8072 case OMPD_target_teams_distribute_parallel_for_simd:
8073 // If this clause applies to the nested 'parallel' region, capture within
8074 // the 'teams' region, otherwise do not capture.
8075 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
8076 CaptureRegion = OMPD_teams;
8077 break;
Carlo Bertolli62fae152017-11-20 20:46:39 +00008078 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008079 case OMPD_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008080 CaptureRegion = OMPD_teams;
8081 break;
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008082 case OMPD_target_update:
Alexey Bataevfab20e42017-12-27 18:49:38 +00008083 case OMPD_target_enter_data:
8084 case OMPD_target_exit_data:
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008085 CaptureRegion = OMPD_task;
8086 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008087 case OMPD_cancel:
8088 case OMPD_parallel:
8089 case OMPD_parallel_sections:
8090 case OMPD_parallel_for:
8091 case OMPD_parallel_for_simd:
8092 case OMPD_target:
8093 case OMPD_target_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008094 case OMPD_target_teams:
8095 case OMPD_target_teams_distribute:
8096 case OMPD_target_teams_distribute_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008097 case OMPD_distribute_parallel_for:
8098 case OMPD_distribute_parallel_for_simd:
8099 case OMPD_task:
8100 case OMPD_taskloop:
8101 case OMPD_taskloop_simd:
8102 case OMPD_target_data:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008103 // Do not capture if-clause expressions.
8104 break;
8105 case OMPD_threadprivate:
8106 case OMPD_taskyield:
8107 case OMPD_barrier:
8108 case OMPD_taskwait:
8109 case OMPD_cancellation_point:
8110 case OMPD_flush:
8111 case OMPD_declare_reduction:
8112 case OMPD_declare_simd:
8113 case OMPD_declare_target:
8114 case OMPD_end_declare_target:
8115 case OMPD_teams:
8116 case OMPD_simd:
8117 case OMPD_for:
8118 case OMPD_for_simd:
8119 case OMPD_sections:
8120 case OMPD_section:
8121 case OMPD_single:
8122 case OMPD_master:
8123 case OMPD_critical:
8124 case OMPD_taskgroup:
8125 case OMPD_distribute:
8126 case OMPD_ordered:
8127 case OMPD_atomic:
8128 case OMPD_distribute_simd:
8129 case OMPD_teams_distribute:
8130 case OMPD_teams_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008131 case OMPD_requires:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008132 llvm_unreachable("Unexpected OpenMP directive with if-clause");
8133 case OMPD_unknown:
8134 llvm_unreachable("Unknown OpenMP directive");
8135 }
8136 break;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008137 case OMPC_num_threads:
8138 switch (DKind) {
8139 case OMPD_target_parallel:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008140 case OMPD_target_parallel_for:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00008141 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008142 CaptureRegion = OMPD_target;
8143 break;
Carlo Bertolli62fae152017-11-20 20:46:39 +00008144 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008145 case OMPD_teams_distribute_parallel_for_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008146 case OMPD_target_teams_distribute_parallel_for:
8147 case OMPD_target_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008148 CaptureRegion = OMPD_teams;
8149 break;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008150 case OMPD_parallel:
8151 case OMPD_parallel_sections:
8152 case OMPD_parallel_for:
8153 case OMPD_parallel_for_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008154 case OMPD_distribute_parallel_for:
8155 case OMPD_distribute_parallel_for_simd:
8156 // Do not capture num_threads-clause expressions.
8157 break;
8158 case OMPD_target_data:
8159 case OMPD_target_enter_data:
8160 case OMPD_target_exit_data:
8161 case OMPD_target_update:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008162 case OMPD_target:
8163 case OMPD_target_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008164 case OMPD_target_teams:
8165 case OMPD_target_teams_distribute:
8166 case OMPD_target_teams_distribute_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008167 case OMPD_cancel:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008168 case OMPD_task:
8169 case OMPD_taskloop:
8170 case OMPD_taskloop_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008171 case OMPD_threadprivate:
8172 case OMPD_taskyield:
8173 case OMPD_barrier:
8174 case OMPD_taskwait:
8175 case OMPD_cancellation_point:
8176 case OMPD_flush:
8177 case OMPD_declare_reduction:
8178 case OMPD_declare_simd:
8179 case OMPD_declare_target:
8180 case OMPD_end_declare_target:
8181 case OMPD_teams:
8182 case OMPD_simd:
8183 case OMPD_for:
8184 case OMPD_for_simd:
8185 case OMPD_sections:
8186 case OMPD_section:
8187 case OMPD_single:
8188 case OMPD_master:
8189 case OMPD_critical:
8190 case OMPD_taskgroup:
8191 case OMPD_distribute:
8192 case OMPD_ordered:
8193 case OMPD_atomic:
8194 case OMPD_distribute_simd:
8195 case OMPD_teams_distribute:
8196 case OMPD_teams_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008197 case OMPD_requires:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008198 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause");
8199 case OMPD_unknown:
8200 llvm_unreachable("Unknown OpenMP directive");
8201 }
8202 break;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008203 case OMPC_num_teams:
8204 switch (DKind) {
8205 case OMPD_target_teams:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008206 case OMPD_target_teams_distribute:
8207 case OMPD_target_teams_distribute_simd:
8208 case OMPD_target_teams_distribute_parallel_for:
8209 case OMPD_target_teams_distribute_parallel_for_simd:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008210 CaptureRegion = OMPD_target;
8211 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008212 case OMPD_teams_distribute_parallel_for:
8213 case OMPD_teams_distribute_parallel_for_simd:
8214 case OMPD_teams:
8215 case OMPD_teams_distribute:
8216 case OMPD_teams_distribute_simd:
8217 // Do not capture num_teams-clause expressions.
8218 break;
8219 case OMPD_distribute_parallel_for:
8220 case OMPD_distribute_parallel_for_simd:
8221 case OMPD_task:
8222 case OMPD_taskloop:
8223 case OMPD_taskloop_simd:
8224 case OMPD_target_data:
8225 case OMPD_target_enter_data:
8226 case OMPD_target_exit_data:
8227 case OMPD_target_update:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008228 case OMPD_cancel:
8229 case OMPD_parallel:
8230 case OMPD_parallel_sections:
8231 case OMPD_parallel_for:
8232 case OMPD_parallel_for_simd:
8233 case OMPD_target:
8234 case OMPD_target_simd:
8235 case OMPD_target_parallel:
8236 case OMPD_target_parallel_for:
8237 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008238 case OMPD_threadprivate:
8239 case OMPD_taskyield:
8240 case OMPD_barrier:
8241 case OMPD_taskwait:
8242 case OMPD_cancellation_point:
8243 case OMPD_flush:
8244 case OMPD_declare_reduction:
8245 case OMPD_declare_simd:
8246 case OMPD_declare_target:
8247 case OMPD_end_declare_target:
8248 case OMPD_simd:
8249 case OMPD_for:
8250 case OMPD_for_simd:
8251 case OMPD_sections:
8252 case OMPD_section:
8253 case OMPD_single:
8254 case OMPD_master:
8255 case OMPD_critical:
8256 case OMPD_taskgroup:
8257 case OMPD_distribute:
8258 case OMPD_ordered:
8259 case OMPD_atomic:
8260 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008261 case OMPD_requires:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008262 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
8263 case OMPD_unknown:
8264 llvm_unreachable("Unknown OpenMP directive");
8265 }
8266 break;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008267 case OMPC_thread_limit:
8268 switch (DKind) {
8269 case OMPD_target_teams:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008270 case OMPD_target_teams_distribute:
8271 case OMPD_target_teams_distribute_simd:
8272 case OMPD_target_teams_distribute_parallel_for:
8273 case OMPD_target_teams_distribute_parallel_for_simd:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008274 CaptureRegion = OMPD_target;
8275 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008276 case OMPD_teams_distribute_parallel_for:
8277 case OMPD_teams_distribute_parallel_for_simd:
8278 case OMPD_teams:
8279 case OMPD_teams_distribute:
8280 case OMPD_teams_distribute_simd:
8281 // Do not capture thread_limit-clause expressions.
8282 break;
8283 case OMPD_distribute_parallel_for:
8284 case OMPD_distribute_parallel_for_simd:
8285 case OMPD_task:
8286 case OMPD_taskloop:
8287 case OMPD_taskloop_simd:
8288 case OMPD_target_data:
8289 case OMPD_target_enter_data:
8290 case OMPD_target_exit_data:
8291 case OMPD_target_update:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008292 case OMPD_cancel:
8293 case OMPD_parallel:
8294 case OMPD_parallel_sections:
8295 case OMPD_parallel_for:
8296 case OMPD_parallel_for_simd:
8297 case OMPD_target:
8298 case OMPD_target_simd:
8299 case OMPD_target_parallel:
8300 case OMPD_target_parallel_for:
8301 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008302 case OMPD_threadprivate:
8303 case OMPD_taskyield:
8304 case OMPD_barrier:
8305 case OMPD_taskwait:
8306 case OMPD_cancellation_point:
8307 case OMPD_flush:
8308 case OMPD_declare_reduction:
8309 case OMPD_declare_simd:
8310 case OMPD_declare_target:
8311 case OMPD_end_declare_target:
8312 case OMPD_simd:
8313 case OMPD_for:
8314 case OMPD_for_simd:
8315 case OMPD_sections:
8316 case OMPD_section:
8317 case OMPD_single:
8318 case OMPD_master:
8319 case OMPD_critical:
8320 case OMPD_taskgroup:
8321 case OMPD_distribute:
8322 case OMPD_ordered:
8323 case OMPD_atomic:
8324 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008325 case OMPD_requires:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008326 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause");
8327 case OMPD_unknown:
8328 llvm_unreachable("Unknown OpenMP directive");
8329 }
8330 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008331 case OMPC_schedule:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008332 switch (DKind) {
Alexey Bataev2ba67042017-11-28 21:11:44 +00008333 case OMPD_parallel_for:
8334 case OMPD_parallel_for_simd:
Alexey Bataev7f96c372017-11-22 17:19:31 +00008335 case OMPD_distribute_parallel_for:
Alexey Bataev974acd62017-11-27 19:38:52 +00008336 case OMPD_distribute_parallel_for_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008337 case OMPD_teams_distribute_parallel_for:
8338 case OMPD_teams_distribute_parallel_for_simd:
8339 case OMPD_target_parallel_for:
8340 case OMPD_target_parallel_for_simd:
8341 case OMPD_target_teams_distribute_parallel_for:
8342 case OMPD_target_teams_distribute_parallel_for_simd:
Alexey Bataev7f96c372017-11-22 17:19:31 +00008343 CaptureRegion = OMPD_parallel;
8344 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008345 case OMPD_for:
8346 case OMPD_for_simd:
8347 // Do not capture schedule-clause expressions.
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008348 break;
8349 case OMPD_task:
8350 case OMPD_taskloop:
8351 case OMPD_taskloop_simd:
8352 case OMPD_target_data:
8353 case OMPD_target_enter_data:
8354 case OMPD_target_exit_data:
8355 case OMPD_target_update:
8356 case OMPD_teams:
8357 case OMPD_teams_distribute:
8358 case OMPD_teams_distribute_simd:
8359 case OMPD_target_teams_distribute:
8360 case OMPD_target_teams_distribute_simd:
8361 case OMPD_target:
8362 case OMPD_target_simd:
8363 case OMPD_target_parallel:
8364 case OMPD_cancel:
8365 case OMPD_parallel:
8366 case OMPD_parallel_sections:
8367 case OMPD_threadprivate:
8368 case OMPD_taskyield:
8369 case OMPD_barrier:
8370 case OMPD_taskwait:
8371 case OMPD_cancellation_point:
8372 case OMPD_flush:
8373 case OMPD_declare_reduction:
8374 case OMPD_declare_simd:
8375 case OMPD_declare_target:
8376 case OMPD_end_declare_target:
8377 case OMPD_simd:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008378 case OMPD_sections:
8379 case OMPD_section:
8380 case OMPD_single:
8381 case OMPD_master:
8382 case OMPD_critical:
8383 case OMPD_taskgroup:
8384 case OMPD_distribute:
8385 case OMPD_ordered:
8386 case OMPD_atomic:
8387 case OMPD_distribute_simd:
8388 case OMPD_target_teams:
Kelvin Li1408f912018-09-26 04:28:39 +00008389 case OMPD_requires:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008390 llvm_unreachable("Unexpected OpenMP directive with schedule clause");
8391 case OMPD_unknown:
8392 llvm_unreachable("Unknown OpenMP directive");
8393 }
8394 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008395 case OMPC_dist_schedule:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008396 switch (DKind) {
8397 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008398 case OMPD_teams_distribute_parallel_for_simd:
8399 case OMPD_teams_distribute:
8400 case OMPD_teams_distribute_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008401 case OMPD_target_teams_distribute_parallel_for:
8402 case OMPD_target_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008403 case OMPD_target_teams_distribute:
8404 case OMPD_target_teams_distribute_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008405 CaptureRegion = OMPD_teams;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008406 break;
8407 case OMPD_distribute_parallel_for:
8408 case OMPD_distribute_parallel_for_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008409 case OMPD_distribute:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008410 case OMPD_distribute_simd:
8411 // Do not capture thread_limit-clause expressions.
8412 break;
8413 case OMPD_parallel_for:
8414 case OMPD_parallel_for_simd:
8415 case OMPD_target_parallel_for_simd:
8416 case OMPD_target_parallel_for:
8417 case OMPD_task:
8418 case OMPD_taskloop:
8419 case OMPD_taskloop_simd:
8420 case OMPD_target_data:
8421 case OMPD_target_enter_data:
8422 case OMPD_target_exit_data:
8423 case OMPD_target_update:
8424 case OMPD_teams:
8425 case OMPD_target:
8426 case OMPD_target_simd:
8427 case OMPD_target_parallel:
8428 case OMPD_cancel:
8429 case OMPD_parallel:
8430 case OMPD_parallel_sections:
8431 case OMPD_threadprivate:
8432 case OMPD_taskyield:
8433 case OMPD_barrier:
8434 case OMPD_taskwait:
8435 case OMPD_cancellation_point:
8436 case OMPD_flush:
8437 case OMPD_declare_reduction:
8438 case OMPD_declare_simd:
8439 case OMPD_declare_target:
8440 case OMPD_end_declare_target:
8441 case OMPD_simd:
8442 case OMPD_for:
8443 case OMPD_for_simd:
8444 case OMPD_sections:
8445 case OMPD_section:
8446 case OMPD_single:
8447 case OMPD_master:
8448 case OMPD_critical:
8449 case OMPD_taskgroup:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008450 case OMPD_ordered:
8451 case OMPD_atomic:
8452 case OMPD_target_teams:
Kelvin Li1408f912018-09-26 04:28:39 +00008453 case OMPD_requires:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008454 llvm_unreachable("Unexpected OpenMP directive with schedule clause");
8455 case OMPD_unknown:
8456 llvm_unreachable("Unknown OpenMP directive");
8457 }
8458 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008459 case OMPC_device:
8460 switch (DKind) {
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008461 case OMPD_target_update:
Alexey Bataevfab20e42017-12-27 18:49:38 +00008462 case OMPD_target_enter_data:
8463 case OMPD_target_exit_data:
Alexey Bataev8451efa2018-01-15 19:06:12 +00008464 case OMPD_target:
Alexey Bataevf41c88f2018-01-16 15:05:16 +00008465 case OMPD_target_simd:
Alexey Bataev0c869ef2018-01-16 15:57:07 +00008466 case OMPD_target_teams:
Alexey Bataev54d5c7d2018-01-16 16:27:49 +00008467 case OMPD_target_parallel:
Alexey Bataev79df7562018-01-16 16:46:46 +00008468 case OMPD_target_teams_distribute:
Alexey Bataev8d16a432018-01-16 17:22:50 +00008469 case OMPD_target_teams_distribute_simd:
Alexey Bataev8ed895512018-01-16 17:41:04 +00008470 case OMPD_target_parallel_for:
Alexey Bataevd60d1ba2018-01-16 17:55:15 +00008471 case OMPD_target_parallel_for_simd:
Alexey Bataev9f9fb0b2018-01-16 19:02:33 +00008472 case OMPD_target_teams_distribute_parallel_for:
Alexey Bataev9350fc32018-01-16 19:18:24 +00008473 case OMPD_target_teams_distribute_parallel_for_simd:
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008474 CaptureRegion = OMPD_task;
8475 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008476 case OMPD_target_data:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008477 // Do not capture device-clause expressions.
8478 break;
8479 case OMPD_teams_distribute_parallel_for:
8480 case OMPD_teams_distribute_parallel_for_simd:
8481 case OMPD_teams:
8482 case OMPD_teams_distribute:
8483 case OMPD_teams_distribute_simd:
8484 case OMPD_distribute_parallel_for:
8485 case OMPD_distribute_parallel_for_simd:
8486 case OMPD_task:
8487 case OMPD_taskloop:
8488 case OMPD_taskloop_simd:
8489 case OMPD_cancel:
8490 case OMPD_parallel:
8491 case OMPD_parallel_sections:
8492 case OMPD_parallel_for:
8493 case OMPD_parallel_for_simd:
8494 case OMPD_threadprivate:
8495 case OMPD_taskyield:
8496 case OMPD_barrier:
8497 case OMPD_taskwait:
8498 case OMPD_cancellation_point:
8499 case OMPD_flush:
8500 case OMPD_declare_reduction:
8501 case OMPD_declare_simd:
8502 case OMPD_declare_target:
8503 case OMPD_end_declare_target:
8504 case OMPD_simd:
8505 case OMPD_for:
8506 case OMPD_for_simd:
8507 case OMPD_sections:
8508 case OMPD_section:
8509 case OMPD_single:
8510 case OMPD_master:
8511 case OMPD_critical:
8512 case OMPD_taskgroup:
8513 case OMPD_distribute:
8514 case OMPD_ordered:
8515 case OMPD_atomic:
8516 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008517 case OMPD_requires:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008518 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
8519 case OMPD_unknown:
8520 llvm_unreachable("Unknown OpenMP directive");
8521 }
8522 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008523 case OMPC_firstprivate:
8524 case OMPC_lastprivate:
8525 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008526 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008527 case OMPC_in_reduction:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008528 case OMPC_linear:
8529 case OMPC_default:
8530 case OMPC_proc_bind:
8531 case OMPC_final:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008532 case OMPC_safelen:
8533 case OMPC_simdlen:
8534 case OMPC_collapse:
8535 case OMPC_private:
8536 case OMPC_shared:
8537 case OMPC_aligned:
8538 case OMPC_copyin:
8539 case OMPC_copyprivate:
8540 case OMPC_ordered:
8541 case OMPC_nowait:
8542 case OMPC_untied:
8543 case OMPC_mergeable:
8544 case OMPC_threadprivate:
8545 case OMPC_flush:
8546 case OMPC_read:
8547 case OMPC_write:
8548 case OMPC_update:
8549 case OMPC_capture:
8550 case OMPC_seq_cst:
8551 case OMPC_depend:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008552 case OMPC_threads:
8553 case OMPC_simd:
8554 case OMPC_map:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008555 case OMPC_priority:
8556 case OMPC_grainsize:
8557 case OMPC_nogroup:
8558 case OMPC_num_tasks:
8559 case OMPC_hint:
8560 case OMPC_defaultmap:
8561 case OMPC_unknown:
8562 case OMPC_uniform:
8563 case OMPC_to:
8564 case OMPC_from:
8565 case OMPC_use_device_ptr:
8566 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00008567 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00008568 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00008569 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00008570 case OMPC_dynamic_allocators:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008571 llvm_unreachable("Unexpected OpenMP clause.");
8572 }
8573 return CaptureRegion;
8574}
8575
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008576OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
8577 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008578 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008579 SourceLocation NameModifierLoc,
8580 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008581 SourceLocation EndLoc) {
8582 Expr *ValExpr = Condition;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008583 Stmt *HelperValStmt = nullptr;
8584 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008585 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
8586 !Condition->isInstantiationDependent() &&
8587 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00008588 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008589 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008590 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008591
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008592 ValExpr = Val.get();
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008593
8594 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
8595 CaptureRegion =
8596 getOpenMPCaptureRegionForClause(DKind, OMPC_if, NameModifier);
Alexey Bataev2ba67042017-11-28 21:11:44 +00008597 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008598 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00008599 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008600 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8601 HelperValStmt = buildPreInits(Context, Captures);
8602 }
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008603 }
8604
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008605 return new (Context)
8606 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
8607 LParenLoc, NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008608}
8609
Alexey Bataev3778b602014-07-17 07:32:53 +00008610OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
8611 SourceLocation StartLoc,
8612 SourceLocation LParenLoc,
8613 SourceLocation EndLoc) {
8614 Expr *ValExpr = Condition;
8615 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
8616 !Condition->isInstantiationDependent() &&
8617 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00008618 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataev3778b602014-07-17 07:32:53 +00008619 if (Val.isInvalid())
8620 return nullptr;
8621
Richard Smith03a4aa32016-06-23 19:02:52 +00008622 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataev3778b602014-07-17 07:32:53 +00008623 }
8624
8625 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8626}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00008627ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
8628 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00008629 if (!Op)
8630 return ExprError();
8631
8632 class IntConvertDiagnoser : public ICEConvertDiagnoser {
8633 public:
8634 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00008635 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00008636 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
8637 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008638 return S.Diag(Loc, diag::err_omp_not_integral) << T;
8639 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008640 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
8641 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008642 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
8643 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008644 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
8645 QualType T,
8646 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008647 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
8648 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008649 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
8650 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008651 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00008652 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00008653 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008654 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
8655 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008656 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
8657 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008658 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
8659 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008660 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00008661 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00008662 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008663 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
8664 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008665 llvm_unreachable("conversion functions are permitted");
8666 }
8667 } ConvertDiagnoser;
8668 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
8669}
8670
Alexey Bataeve3727102018-04-18 15:57:46 +00008671static bool isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00008672 OpenMPClauseKind CKind,
8673 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008674 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
8675 !ValExpr->isInstantiationDependent()) {
8676 SourceLocation Loc = ValExpr->getExprLoc();
8677 ExprResult Value =
8678 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
8679 if (Value.isInvalid())
8680 return false;
8681
8682 ValExpr = Value.get();
8683 // The expression must evaluate to a non-negative integer value.
8684 llvm::APSInt Result;
8685 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00008686 Result.isSigned() &&
8687 !((!StrictlyPositive && Result.isNonNegative()) ||
8688 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008689 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00008690 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
8691 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008692 return false;
8693 }
8694 }
8695 return true;
8696}
8697
Alexey Bataev568a8332014-03-06 06:15:19 +00008698OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
8699 SourceLocation StartLoc,
8700 SourceLocation LParenLoc,
8701 SourceLocation EndLoc) {
8702 Expr *ValExpr = NumThreads;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008703 Stmt *HelperValStmt = nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00008704
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008705 // OpenMP [2.5, Restrictions]
8706 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +00008707 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
Alexey Bataeva0569352015-12-01 10:17:31 +00008708 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008709 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00008710
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008711 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +00008712 OpenMPDirectiveKind CaptureRegion =
8713 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads);
8714 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008715 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00008716 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008717 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8718 HelperValStmt = buildPreInits(Context, Captures);
8719 }
8720
8721 return new (Context) OMPNumThreadsClause(
8722 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00008723}
8724
Alexey Bataev62c87d22014-03-21 04:51:18 +00008725ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008726 OpenMPClauseKind CKind,
8727 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00008728 if (!E)
8729 return ExprError();
8730 if (E->isValueDependent() || E->isTypeDependent() ||
8731 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00008732 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00008733 llvm::APSInt Result;
8734 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
8735 if (ICE.isInvalid())
8736 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008737 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
8738 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00008739 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008740 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
8741 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00008742 return ExprError();
8743 }
Alexander Musman09184fe2014-09-30 05:29:28 +00008744 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
8745 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
8746 << E->getSourceRange();
8747 return ExprError();
8748 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008749 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
8750 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00008751 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00008752 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00008753 return ICE;
8754}
8755
8756OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
8757 SourceLocation LParenLoc,
8758 SourceLocation EndLoc) {
8759 // OpenMP [2.8.1, simd construct, Description]
8760 // The parameter of the safelen clause must be a constant
8761 // positive integer expression.
8762 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
8763 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008764 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00008765 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00008766 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00008767}
8768
Alexey Bataev66b15b52015-08-21 11:14:16 +00008769OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
8770 SourceLocation LParenLoc,
8771 SourceLocation EndLoc) {
8772 // OpenMP [2.8.1, simd construct, Description]
8773 // The parameter of the simdlen clause must be a constant
8774 // positive integer expression.
8775 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
8776 if (Simdlen.isInvalid())
8777 return nullptr;
8778 return new (Context)
8779 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
8780}
8781
Alexander Musman64d33f12014-06-04 07:53:32 +00008782OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
8783 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00008784 SourceLocation LParenLoc,
8785 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00008786 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00008787 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00008788 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00008789 // The parameter of the collapse clause must be a constant
8790 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00008791 ExprResult NumForLoopsResult =
8792 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
8793 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00008794 return nullptr;
8795 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00008796 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00008797}
8798
Alexey Bataev10e775f2015-07-30 11:36:16 +00008799OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
8800 SourceLocation EndLoc,
8801 SourceLocation LParenLoc,
8802 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00008803 // OpenMP [2.7.1, loop construct, Description]
8804 // OpenMP [2.8.1, simd construct, Description]
8805 // OpenMP [2.9.6, distribute construct, Description]
8806 // The parameter of the ordered clause must be a constant
8807 // positive integer expression if any.
8808 if (NumForLoops && LParenLoc.isValid()) {
8809 ExprResult NumForLoopsResult =
8810 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
8811 if (NumForLoopsResult.isInvalid())
8812 return nullptr;
8813 NumForLoops = NumForLoopsResult.get();
Alexey Bataeve3727102018-04-18 15:57:46 +00008814 } else {
Alexey Bataev346265e2015-09-25 10:37:12 +00008815 NumForLoops = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +00008816 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00008817 auto *Clause = OMPOrderedClause::Create(
8818 Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0,
8819 StartLoc, LParenLoc, EndLoc);
8820 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause);
8821 return Clause;
Alexey Bataev10e775f2015-07-30 11:36:16 +00008822}
8823
Alexey Bataeved09d242014-05-28 05:53:51 +00008824OMPClause *Sema::ActOnOpenMPSimpleClause(
8825 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
8826 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008827 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008828 switch (Kind) {
8829 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00008830 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00008831 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
8832 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008833 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008834 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00008835 Res = ActOnOpenMPProcBindClause(
8836 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
8837 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008838 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008839 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00008840 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00008841 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00008842 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00008843 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00008844 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008845 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008846 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00008847 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00008848 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00008849 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00008850 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008851 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008852 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00008853 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008854 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008855 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008856 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00008857 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00008858 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008859 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008860 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008861 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00008862 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008863 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008864 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008865 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008866 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008867 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008868 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00008869 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00008870 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008871 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008872 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00008873 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008874 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00008875 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008876 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00008877 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00008878 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00008879 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008880 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008881 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008882 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008883 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00008884 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00008885 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00008886 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00008887 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00008888 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00008889 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00008890 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00008891 case OMPC_dynamic_allocators:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008892 llvm_unreachable("Clause is not allowed.");
8893 }
8894 return Res;
8895}
8896
Alexey Bataev6402bca2015-12-28 07:25:51 +00008897static std::string
8898getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
8899 ArrayRef<unsigned> Exclude = llvm::None) {
Alexey Bataeve3727102018-04-18 15:57:46 +00008900 SmallString<256> Buffer;
8901 llvm::raw_svector_ostream Out(Buffer);
Alexey Bataev6402bca2015-12-28 07:25:51 +00008902 unsigned Bound = Last >= 2 ? Last - 2 : 0;
8903 unsigned Skipped = Exclude.size();
8904 auto S = Exclude.begin(), E = Exclude.end();
Alexey Bataeve3727102018-04-18 15:57:46 +00008905 for (unsigned I = First; I < Last; ++I) {
8906 if (std::find(S, E, I) != E) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00008907 --Skipped;
8908 continue;
8909 }
Alexey Bataeve3727102018-04-18 15:57:46 +00008910 Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'";
8911 if (I == Bound - Skipped)
8912 Out << " or ";
8913 else if (I != Bound + 1 - Skipped)
8914 Out << ", ";
Alexey Bataev6402bca2015-12-28 07:25:51 +00008915 }
Alexey Bataeve3727102018-04-18 15:57:46 +00008916 return Out.str();
Alexey Bataev6402bca2015-12-28 07:25:51 +00008917}
8918
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008919OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
8920 SourceLocation KindKwLoc,
8921 SourceLocation StartLoc,
8922 SourceLocation LParenLoc,
8923 SourceLocation EndLoc) {
8924 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00008925 static_assert(OMPC_DEFAULT_unknown > 0,
8926 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008927 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008928 << getListOfPossibleValues(OMPC_default, /*First=*/0,
8929 /*Last=*/OMPC_DEFAULT_unknown)
8930 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008931 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008932 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00008933 switch (Kind) {
8934 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008935 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008936 break;
8937 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008938 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00008939 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008940 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008941 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00008942 break;
8943 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008944 return new (Context)
8945 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00008946}
8947
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008948OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
8949 SourceLocation KindKwLoc,
8950 SourceLocation StartLoc,
8951 SourceLocation LParenLoc,
8952 SourceLocation EndLoc) {
8953 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008954 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00008955 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
8956 /*Last=*/OMPC_PROC_BIND_unknown)
8957 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008958 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008959 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008960 return new (Context)
8961 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008962}
8963
Alexey Bataev56dafe82014-06-20 07:16:17 +00008964OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00008965 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00008966 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00008967 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00008968 SourceLocation EndLoc) {
8969 OMPClause *Res = nullptr;
8970 switch (Kind) {
8971 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00008972 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
8973 assert(Argument.size() == NumberOfElements &&
8974 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00008975 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00008976 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
8977 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
8978 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
8979 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
8980 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00008981 break;
8982 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00008983 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
8984 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
8985 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
8986 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008987 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00008988 case OMPC_dist_schedule:
8989 Res = ActOnOpenMPDistScheduleClause(
8990 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
8991 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
8992 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008993 case OMPC_defaultmap:
8994 enum { Modifier, DefaultmapKind };
8995 Res = ActOnOpenMPDefaultmapClause(
8996 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
8997 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
David Majnemer9d168222016-08-05 17:44:54 +00008998 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind],
8999 EndLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009000 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00009001 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009002 case OMPC_num_threads:
9003 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009004 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009005 case OMPC_collapse:
9006 case OMPC_default:
9007 case OMPC_proc_bind:
9008 case OMPC_private:
9009 case OMPC_firstprivate:
9010 case OMPC_lastprivate:
9011 case OMPC_shared:
9012 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00009013 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00009014 case OMPC_in_reduction:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009015 case OMPC_linear:
9016 case OMPC_aligned:
9017 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009018 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009019 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00009020 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009021 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009022 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009023 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00009024 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009025 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00009026 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00009027 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00009028 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009029 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009030 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00009031 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00009032 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009033 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00009034 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009035 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009036 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009037 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009038 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00009039 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00009040 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009041 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009042 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009043 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00009044 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00009045 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00009046 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00009047 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00009048 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00009049 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009050 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009051 case OMPC_dynamic_allocators:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009052 llvm_unreachable("Clause is not allowed.");
9053 }
9054 return Res;
9055}
9056
Alexey Bataev6402bca2015-12-28 07:25:51 +00009057static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
9058 OpenMPScheduleClauseModifier M2,
9059 SourceLocation M1Loc, SourceLocation M2Loc) {
9060 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
9061 SmallVector<unsigned, 2> Excluded;
9062 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
9063 Excluded.push_back(M2);
9064 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
9065 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
9066 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
9067 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
9068 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
9069 << getListOfPossibleValues(OMPC_schedule,
9070 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
9071 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
9072 Excluded)
9073 << getOpenMPClauseName(OMPC_schedule);
9074 return true;
9075 }
9076 return false;
9077}
9078
Alexey Bataev56dafe82014-06-20 07:16:17 +00009079OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00009080 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00009081 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00009082 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
9083 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
9084 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
9085 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
9086 return nullptr;
9087 // OpenMP, 2.7.1, Loop Construct, Restrictions
9088 // Either the monotonic modifier or the nonmonotonic modifier can be specified
9089 // but not both.
9090 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
9091 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
9092 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
9093 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
9094 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
9095 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
9096 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
9097 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
9098 return nullptr;
9099 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00009100 if (Kind == OMPC_SCHEDULE_unknown) {
9101 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00009102 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
9103 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
9104 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
9105 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
9106 Exclude);
9107 } else {
9108 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
9109 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009110 }
9111 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
9112 << Values << getOpenMPClauseName(OMPC_schedule);
9113 return nullptr;
9114 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00009115 // OpenMP, 2.7.1, Loop Construct, Restrictions
9116 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
9117 // schedule(guided).
9118 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
9119 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
9120 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
9121 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
9122 diag::err_omp_schedule_nonmonotonic_static);
9123 return nullptr;
9124 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00009125 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00009126 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00009127 if (ChunkSize) {
9128 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
9129 !ChunkSize->isInstantiationDependent() &&
9130 !ChunkSize->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00009131 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
Alexey Bataev56dafe82014-06-20 07:16:17 +00009132 ExprResult Val =
9133 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
9134 if (Val.isInvalid())
9135 return nullptr;
9136
9137 ValExpr = Val.get();
9138
9139 // OpenMP [2.7.1, Restrictions]
9140 // chunk_size must be a loop invariant integer expression with a positive
9141 // value.
9142 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00009143 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
9144 if (Result.isSigned() && !Result.isStrictlyPositive()) {
9145 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00009146 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00009147 return nullptr;
9148 }
Alexey Bataev2ba67042017-11-28 21:11:44 +00009149 } else if (getOpenMPCaptureRegionForClause(
9150 DSAStack->getCurrentDirective(), OMPC_schedule) !=
9151 OMPD_unknown &&
Alexey Bataevb46cdea2016-06-15 11:20:48 +00009152 !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00009153 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00009154 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev5a3af132016-03-29 08:58:54 +00009155 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
9156 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009157 }
9158 }
9159 }
9160
Alexey Bataev6402bca2015-12-28 07:25:51 +00009161 return new (Context)
9162 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00009163 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009164}
9165
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009166OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
9167 SourceLocation StartLoc,
9168 SourceLocation EndLoc) {
9169 OMPClause *Res = nullptr;
9170 switch (Kind) {
9171 case OMPC_ordered:
9172 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
9173 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00009174 case OMPC_nowait:
9175 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
9176 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009177 case OMPC_untied:
9178 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
9179 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009180 case OMPC_mergeable:
9181 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
9182 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009183 case OMPC_read:
9184 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
9185 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00009186 case OMPC_write:
9187 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
9188 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00009189 case OMPC_update:
9190 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
9191 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00009192 case OMPC_capture:
9193 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
9194 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009195 case OMPC_seq_cst:
9196 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
9197 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00009198 case OMPC_threads:
9199 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
9200 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009201 case OMPC_simd:
9202 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
9203 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00009204 case OMPC_nogroup:
9205 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
9206 break;
Kelvin Li1408f912018-09-26 04:28:39 +00009207 case OMPC_unified_address:
9208 Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc);
9209 break;
Patrick Lyster4a370b92018-10-01 13:47:43 +00009210 case OMPC_unified_shared_memory:
9211 Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
9212 break;
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009213 case OMPC_reverse_offload:
9214 Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc);
9215 break;
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009216 case OMPC_dynamic_allocators:
9217 Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc);
9218 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009219 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00009220 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009221 case OMPC_num_threads:
9222 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009223 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009224 case OMPC_collapse:
9225 case OMPC_schedule:
9226 case OMPC_private:
9227 case OMPC_firstprivate:
9228 case OMPC_lastprivate:
9229 case OMPC_shared:
9230 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00009231 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00009232 case OMPC_in_reduction:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009233 case OMPC_linear:
9234 case OMPC_aligned:
9235 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009236 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009237 case OMPC_default:
9238 case OMPC_proc_bind:
9239 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00009240 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009241 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00009242 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00009243 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009244 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009245 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009246 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009247 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00009248 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009249 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00009250 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009251 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009252 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009253 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00009254 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00009255 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00009256 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00009257 case OMPC_is_device_ptr:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009258 llvm_unreachable("Clause is not allowed.");
9259 }
9260 return Res;
9261}
9262
Alexey Bataev236070f2014-06-20 11:19:47 +00009263OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
9264 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00009265 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00009266 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
9267}
9268
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009269OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
9270 SourceLocation EndLoc) {
9271 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
9272}
9273
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009274OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
9275 SourceLocation EndLoc) {
9276 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
9277}
9278
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009279OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
9280 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009281 return new (Context) OMPReadClause(StartLoc, EndLoc);
9282}
9283
Alexey Bataevdea47612014-07-23 07:46:59 +00009284OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
9285 SourceLocation EndLoc) {
9286 return new (Context) OMPWriteClause(StartLoc, EndLoc);
9287}
9288
Alexey Bataev67a4f222014-07-23 10:25:33 +00009289OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
9290 SourceLocation EndLoc) {
9291 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
9292}
9293
Alexey Bataev459dec02014-07-24 06:46:57 +00009294OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
9295 SourceLocation EndLoc) {
9296 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
9297}
9298
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009299OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
9300 SourceLocation EndLoc) {
9301 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
9302}
9303
Alexey Bataev346265e2015-09-25 10:37:12 +00009304OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
9305 SourceLocation EndLoc) {
9306 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
9307}
9308
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009309OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
9310 SourceLocation EndLoc) {
9311 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
9312}
9313
Alexey Bataevb825de12015-12-07 10:51:44 +00009314OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
9315 SourceLocation EndLoc) {
9316 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
9317}
9318
Kelvin Li1408f912018-09-26 04:28:39 +00009319OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc,
9320 SourceLocation EndLoc) {
9321 return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc);
9322}
9323
Patrick Lyster4a370b92018-10-01 13:47:43 +00009324OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc,
9325 SourceLocation EndLoc) {
9326 return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
9327}
9328
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009329OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc,
9330 SourceLocation EndLoc) {
9331 return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc);
9332}
9333
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009334OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc,
9335 SourceLocation EndLoc) {
9336 return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc);
9337}
9338
Alexey Bataevc5e02582014-06-16 07:08:35 +00009339OMPClause *Sema::ActOnOpenMPVarListClause(
9340 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
9341 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
9342 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009343 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Samuel Antao23abd722016-01-19 20:40:49 +00009344 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
9345 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
9346 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009347 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009348 switch (Kind) {
9349 case OMPC_private:
9350 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9351 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009352 case OMPC_firstprivate:
9353 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9354 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00009355 case OMPC_lastprivate:
9356 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9357 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00009358 case OMPC_shared:
9359 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
9360 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009361 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00009362 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9363 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00009364 break;
Alexey Bataev169d96a2017-07-18 20:17:46 +00009365 case OMPC_task_reduction:
9366 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9367 EndLoc, ReductionIdScopeSpec,
9368 ReductionId);
9369 break;
Alexey Bataevfa312f32017-07-21 18:48:21 +00009370 case OMPC_in_reduction:
9371 Res =
9372 ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9373 EndLoc, ReductionIdScopeSpec, ReductionId);
9374 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00009375 case OMPC_linear:
9376 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00009377 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00009378 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009379 case OMPC_aligned:
9380 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
9381 ColonLoc, EndLoc);
9382 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009383 case OMPC_copyin:
9384 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
9385 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009386 case OMPC_copyprivate:
9387 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9388 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00009389 case OMPC_flush:
9390 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
9391 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009392 case OMPC_depend:
David Majnemer9d168222016-08-05 17:44:54 +00009393 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
Kelvin Li0bff7af2015-11-23 05:32:03 +00009394 StartLoc, LParenLoc, EndLoc);
9395 break;
9396 case OMPC_map:
Samuel Antao23abd722016-01-19 20:40:49 +00009397 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, IsMapTypeImplicit,
9398 DepLinMapLoc, ColonLoc, VarList, StartLoc,
9399 LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009400 break;
Samuel Antao661c0902016-05-26 17:39:58 +00009401 case OMPC_to:
9402 Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc);
9403 break;
Samuel Antaoec172c62016-05-26 17:49:04 +00009404 case OMPC_from:
9405 Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc);
9406 break;
Carlo Bertolli2404b172016-07-13 15:37:16 +00009407 case OMPC_use_device_ptr:
9408 Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
9409 break;
Carlo Bertolli70594e92016-07-13 17:16:49 +00009410 case OMPC_is_device_ptr:
9411 Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
9412 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009413 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00009414 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00009415 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00009416 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009417 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00009418 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009419 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009420 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009421 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009422 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00009423 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009424 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009425 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009426 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009427 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00009428 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00009429 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00009430 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009431 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00009432 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00009433 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009434 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009435 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009436 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009437 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009438 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00009439 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00009440 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009441 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00009442 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009443 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009444 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009445 case OMPC_uniform:
Kelvin Li1408f912018-09-26 04:28:39 +00009446 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00009447 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009448 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009449 case OMPC_dynamic_allocators:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009450 llvm_unreachable("Clause is not allowed.");
9451 }
9452 return Res;
9453}
9454
Alexey Bataev90c228f2016-02-08 09:29:13 +00009455ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00009456 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00009457 ExprResult Res = BuildDeclRefExpr(
9458 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
9459 if (!Res.isUsable())
9460 return ExprError();
9461 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
9462 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
9463 if (!Res.isUsable())
9464 return ExprError();
9465 }
9466 if (VK != VK_LValue && Res.get()->isGLValue()) {
9467 Res = DefaultLvalueConversion(Res.get());
9468 if (!Res.isUsable())
9469 return ExprError();
9470 }
9471 return Res;
9472}
9473
Alexey Bataev60da77e2016-02-29 05:54:20 +00009474static std::pair<ValueDecl *, bool>
9475getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
9476 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00009477 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
9478 RefExpr->containsUnexpandedParameterPack())
9479 return std::make_pair(nullptr, true);
9480
Alexey Bataevd985eda2016-02-10 11:29:16 +00009481 // OpenMP [3.1, C/C++]
9482 // A list item is a variable name.
9483 // OpenMP [2.9.3.3, Restrictions, p.1]
9484 // A variable that is part of another variable (as an array or
9485 // structure element) cannot appear in a private clause.
9486 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009487 enum {
9488 NoArrayExpr = -1,
9489 ArraySubscript = 0,
9490 OMPArraySection = 1
9491 } IsArrayExpr = NoArrayExpr;
9492 if (AllowArraySection) {
9493 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009494 Expr *Base = ASE->getBase()->IgnoreParenImpCasts();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009495 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
9496 Base = TempASE->getBase()->IgnoreParenImpCasts();
9497 RefExpr = Base;
9498 IsArrayExpr = ArraySubscript;
9499 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009500 Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009501 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
9502 Base = TempOASE->getBase()->IgnoreParenImpCasts();
9503 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
9504 Base = TempASE->getBase()->IgnoreParenImpCasts();
9505 RefExpr = Base;
9506 IsArrayExpr = OMPArraySection;
9507 }
9508 }
9509 ELoc = RefExpr->getExprLoc();
9510 ERange = RefExpr->getSourceRange();
9511 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00009512 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
9513 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
9514 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
9515 (S.getCurrentThisType().isNull() || !ME ||
9516 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
9517 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009518 if (IsArrayExpr != NoArrayExpr) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009519 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
9520 << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +00009521 } else {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009522 S.Diag(ELoc,
9523 AllowArraySection
9524 ? diag::err_omp_expected_var_name_member_expr_or_array_item
9525 : diag::err_omp_expected_var_name_member_expr)
9526 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
9527 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009528 return std::make_pair(nullptr, false);
9529 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +00009530 return std::make_pair(
9531 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009532}
9533
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009534OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
9535 SourceLocation StartLoc,
9536 SourceLocation LParenLoc,
9537 SourceLocation EndLoc) {
9538 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00009539 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeve3727102018-04-18 15:57:46 +00009540 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +00009541 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009542 SourceLocation ELoc;
9543 SourceRange ERange;
9544 Expr *SimpleRefExpr = RefExpr;
9545 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009546 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009547 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009548 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009549 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009550 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009551 ValueDecl *D = Res.first;
9552 if (!D)
9553 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009554
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009555 QualType Type = D->getType();
9556 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009557
9558 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9559 // A variable that appears in a private clause must not have an incomplete
9560 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009561 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009562 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009563 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009564
Alexey Bataev758e55e2013-09-06 18:03:48 +00009565 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9566 // in a Construct]
9567 // Variables with the predetermined data-sharing attributes may not be
9568 // listed in data-sharing attributes clauses, except for the cases
9569 // listed below. For these exceptions only, listing a predetermined
9570 // variable in a data-sharing attribute clause is allowed and overrides
9571 // the variable's predetermined data-sharing attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +00009572 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009573 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00009574 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9575 << getOpenMPClauseName(OMPC_private);
Alexey Bataeve3727102018-04-18 15:57:46 +00009576 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009577 continue;
9578 }
9579
Alexey Bataeve3727102018-04-18 15:57:46 +00009580 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009581 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00009582 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Kelvin Libf594a52016-12-17 05:48:59 +00009583 isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009584 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
9585 << getOpenMPClauseName(OMPC_private) << Type
Kelvin Libf594a52016-12-17 05:48:59 +00009586 << getOpenMPDirectiveName(CurrDir);
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009587 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009588 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009589 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009590 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009591 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009592 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009593 continue;
9594 }
9595
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009596 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
9597 // A list item cannot appear in both a map clause and a data-sharing
9598 // attribute clause on the same construct
Alexey Bataeve3727102018-04-18 15:57:46 +00009599 if (isOpenMPTargetExecutionDirective(CurrDir)) {
Samuel Antao6890b092016-07-28 14:25:09 +00009600 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +00009601 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +00009602 VD, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +00009603 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
9604 OpenMPClauseKind WhereFoundClauseKind) -> bool {
9605 ConflictKind = WhereFoundClauseKind;
9606 return true;
9607 })) {
9608 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009609 << getOpenMPClauseName(OMPC_private)
Samuel Antao6890b092016-07-28 14:25:09 +00009610 << getOpenMPClauseName(ConflictKind)
Kelvin Libf594a52016-12-17 05:48:59 +00009611 << getOpenMPDirectiveName(CurrDir);
Alexey Bataeve3727102018-04-18 15:57:46 +00009612 reportOriginalDsa(*this, DSAStack, D, DVar);
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009613 continue;
9614 }
9615 }
9616
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009617 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
9618 // A variable of class type (or array thereof) that appears in a private
9619 // clause requires an accessible, unambiguous default constructor for the
9620 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00009621 // Generate helper private variable and initialize it with the default
9622 // value. The address of the original variable is replaced by the address of
9623 // the new private variable in CodeGen. This new variable is not added to
9624 // IdResolver, so the code in the OpenMP region uses original variable for
9625 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009626 Type = Type.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +00009627 VarDecl *VDPrivate =
Alexey Bataev63cc8e92018-03-20 14:45:59 +00009628 buildVarDecl(*this, ELoc, Type, D->getName(),
9629 D->hasAttrs() ? &D->getAttrs() : nullptr,
9630 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Richard Smith3beb7c62017-01-12 02:27:38 +00009631 ActOnUninitializedDecl(VDPrivate);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009632 if (VDPrivate->isInvalidDecl())
9633 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00009634 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009635 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009636
Alexey Bataev90c228f2016-02-08 09:29:13 +00009637 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009638 if (!VD && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00009639 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00009640 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009641 Vars.push_back((VD || CurContext->isDependentContext())
9642 ? RefExpr->IgnoreParens()
9643 : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009644 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009645 }
9646
Alexey Bataeved09d242014-05-28 05:53:51 +00009647 if (Vars.empty())
9648 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009649
Alexey Bataev03b340a2014-10-21 03:16:40 +00009650 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
9651 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009652}
9653
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009654namespace {
9655class DiagsUninitializedSeveretyRAII {
9656private:
9657 DiagnosticsEngine &Diags;
9658 SourceLocation SavedLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00009659 bool IsIgnored = false;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009660
9661public:
9662 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
9663 bool IsIgnored)
9664 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
9665 if (!IsIgnored) {
9666 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
9667 /*Map*/ diag::Severity::Ignored, Loc);
9668 }
9669 }
9670 ~DiagsUninitializedSeveretyRAII() {
9671 if (!IsIgnored)
9672 Diags.popMappings(SavedLoc);
9673 }
9674};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00009675}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009676
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009677OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
9678 SourceLocation StartLoc,
9679 SourceLocation LParenLoc,
9680 SourceLocation EndLoc) {
9681 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009682 SmallVector<Expr *, 8> PrivateCopies;
9683 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +00009684 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009685 bool IsImplicitClause =
9686 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
Alexey Bataeve3727102018-04-18 15:57:46 +00009687 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009688
Alexey Bataeve3727102018-04-18 15:57:46 +00009689 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +00009690 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009691 SourceLocation ELoc;
9692 SourceRange ERange;
9693 Expr *SimpleRefExpr = RefExpr;
9694 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009695 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009696 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009697 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009698 PrivateCopies.push_back(nullptr);
9699 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009700 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009701 ValueDecl *D = Res.first;
9702 if (!D)
9703 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009704
Alexey Bataev60da77e2016-02-29 05:54:20 +00009705 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +00009706 QualType Type = D->getType();
9707 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009708
9709 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9710 // A variable that appears in a private clause must not have an incomplete
9711 // type or a reference type.
9712 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +00009713 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009714 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009715 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009716
9717 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
9718 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00009719 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009720 // class type.
Alexey Bataeve3727102018-04-18 15:57:46 +00009721 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009722
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009723 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +00009724 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009725 if (!IsImplicitClause) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009726 DSAStackTy::DSAVarData DVar =
9727 DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +00009728 TopDVar = DVar;
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009729 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009730 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009731 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
9732 // A list item that specifies a given variable may not appear in more
9733 // than one clause on the same directive, except that a variable may be
9734 // specified in both firstprivate and lastprivate clauses.
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009735 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
9736 // A list item may appear in a firstprivate or lastprivate clause but not
9737 // both.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009738 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevb358f992017-12-01 17:40:15 +00009739 (isOpenMPDistributeDirective(CurrDir) ||
9740 DVar.CKind != OMPC_lastprivate) &&
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009741 DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009742 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00009743 << getOpenMPClauseName(DVar.CKind)
9744 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +00009745 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009746 continue;
9747 }
9748
9749 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9750 // in a Construct]
9751 // Variables with the predetermined data-sharing attributes may not be
9752 // listed in data-sharing attributes clauses, except for the cases
9753 // listed below. For these exceptions only, listing a predetermined
9754 // variable in a data-sharing attribute clause is allowed and overrides
9755 // the variable's predetermined data-sharing attributes.
9756 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9757 // in a Construct, C/C++, p.2]
9758 // Variables with const-qualified type having no mutable member may be
9759 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +00009760 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009761 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
9762 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00009763 << getOpenMPClauseName(DVar.CKind)
9764 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +00009765 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009766 continue;
9767 }
9768
9769 // OpenMP [2.9.3.4, Restrictions, p.2]
9770 // A list item that is private within a parallel region must not appear
9771 // in a firstprivate clause on a worksharing construct if any of the
9772 // worksharing regions arising from the worksharing construct ever bind
9773 // to any of the parallel regions arising from the parallel construct.
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009774 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
9775 // A list item that is private within a teams region must not appear in a
9776 // firstprivate clause on a distribute construct if any of the distribute
9777 // regions arising from the distribute construct ever bind to any of the
9778 // teams regions arising from the teams construct.
9779 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
9780 // A list item that appears in a reduction clause of a teams construct
9781 // must not appear in a firstprivate clause on a distribute construct if
9782 // any of the distribute regions arising from the distribute construct
9783 // ever bind to any of the teams regions arising from the teams construct.
9784 if ((isOpenMPWorksharingDirective(CurrDir) ||
9785 isOpenMPDistributeDirective(CurrDir)) &&
Kelvin Li579e41c2016-11-30 23:51:03 +00009786 !isOpenMPParallelDirective(CurrDir) &&
9787 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00009788 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009789 if (DVar.CKind != OMPC_shared &&
9790 (isOpenMPParallelDirective(DVar.DKind) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009791 isOpenMPTeamsDirective(DVar.DKind) ||
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009792 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00009793 Diag(ELoc, diag::err_omp_required_access)
9794 << getOpenMPClauseName(OMPC_firstprivate)
9795 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +00009796 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00009797 continue;
9798 }
9799 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009800 // OpenMP [2.9.3.4, Restrictions, p.3]
9801 // A list item that appears in a reduction clause of a parallel construct
9802 // must not appear in a firstprivate clause on a worksharing or task
9803 // construct if any of the worksharing or task regions arising from the
9804 // worksharing or task construct ever bind to any of the parallel regions
9805 // arising from the parallel construct.
9806 // OpenMP [2.9.3.4, Restrictions, p.4]
9807 // A list item that appears in a reduction clause in worksharing
9808 // construct must not appear in a firstprivate clause in a task construct
9809 // encountered during execution of any of the worksharing regions arising
9810 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +00009811 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00009812 DVar = DSAStack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00009813 D, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
9814 [](OpenMPDirectiveKind K) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00009815 return isOpenMPParallelDirective(K) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009816 isOpenMPWorksharingDirective(K) ||
9817 isOpenMPTeamsDirective(K);
Alexey Bataev7ace49d2016-05-17 08:55:33 +00009818 },
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009819 /*FromParent=*/true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009820 if (DVar.CKind == OMPC_reduction &&
9821 (isOpenMPParallelDirective(DVar.DKind) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009822 isOpenMPWorksharingDirective(DVar.DKind) ||
9823 isOpenMPTeamsDirective(DVar.DKind))) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009824 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
9825 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataeve3727102018-04-18 15:57:46 +00009826 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00009827 continue;
9828 }
9829 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00009830
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009831 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
9832 // A list item cannot appear in both a map clause and a data-sharing
9833 // attribute clause on the same construct
Alexey Bataevb358f992017-12-01 17:40:15 +00009834 if (isOpenMPTargetExecutionDirective(CurrDir)) {
Samuel Antao6890b092016-07-28 14:25:09 +00009835 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +00009836 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +00009837 VD, /*CurrentRegionOnly=*/true,
Alexey Bataeve3727102018-04-18 15:57:46 +00009838 [&ConflictKind](
9839 OMPClauseMappableExprCommon::MappableExprComponentListRef,
9840 OpenMPClauseKind WhereFoundClauseKind) {
Samuel Antao6890b092016-07-28 14:25:09 +00009841 ConflictKind = WhereFoundClauseKind;
9842 return true;
9843 })) {
9844 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009845 << getOpenMPClauseName(OMPC_firstprivate)
Samuel Antao6890b092016-07-28 14:25:09 +00009846 << getOpenMPClauseName(ConflictKind)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009847 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +00009848 reportOriginalDsa(*this, DSAStack, D, DVar);
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009849 continue;
9850 }
9851 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009852 }
9853
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009854 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00009855 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +00009856 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009857 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
9858 << getOpenMPClauseName(OMPC_firstprivate) << Type
9859 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
9860 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +00009861 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009862 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +00009863 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009864 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +00009865 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009866 continue;
9867 }
9868
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009869 Type = Type.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +00009870 VarDecl *VDPrivate =
Alexey Bataev63cc8e92018-03-20 14:45:59 +00009871 buildVarDecl(*this, ELoc, Type, D->getName(),
9872 D->hasAttrs() ? &D->getAttrs() : nullptr,
9873 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009874 // Generate helper private variable and initialize it with the value of the
9875 // original variable. The address of the original variable is replaced by
9876 // the address of the new private variable in the CodeGen. This new variable
9877 // is not added to IdResolver, so the code in the OpenMP region uses
9878 // original variable for proper diagnostics and variable capturing.
9879 Expr *VDInitRefExpr = nullptr;
9880 // For arrays generate initializer for single element and replace it by the
9881 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009882 if (Type->isArrayType()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009883 VarDecl *VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +00009884 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009885 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataeve3727102018-04-18 15:57:46 +00009886 Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009887 ElemType = ElemType.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +00009888 VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
9889 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00009890 InitializedEntity Entity =
9891 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009892 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
9893
9894 InitializationSequence InitSeq(*this, Entity, Kind, Init);
9895 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
9896 if (Result.isInvalid())
9897 VDPrivate->setInvalidDecl();
9898 else
9899 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00009900 // Remove temp variable declaration.
9901 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009902 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +00009903 VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
9904 ".firstprivate.temp");
Alexey Bataevd985eda2016-02-10 11:29:16 +00009905 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
9906 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00009907 AddInitializerToDecl(VDPrivate,
9908 DefaultLvalueConversion(VDInitRefExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +00009909 /*DirectInit=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009910 }
9911 if (VDPrivate->isInvalidDecl()) {
9912 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00009913 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009914 diag::note_omp_task_predetermined_firstprivate_here);
9915 }
9916 continue;
9917 }
9918 CurContext->addDecl(VDPrivate);
Alexey Bataeve3727102018-04-18 15:57:46 +00009919 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +00009920 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
9921 RefExpr->getExprLoc());
9922 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009923 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009924 if (TopDVar.CKind == OMPC_lastprivate) {
Alexey Bataev005248a2016-02-25 05:25:57 +00009925 Ref = TopDVar.PrivateCopy;
Alexey Bataeve3727102018-04-18 15:57:46 +00009926 } else {
Alexey Bataev61205072016-03-02 04:57:40 +00009927 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataeve3727102018-04-18 15:57:46 +00009928 if (!isOpenMPCapturedDecl(D))
Alexey Bataev005248a2016-02-25 05:25:57 +00009929 ExprCaptures.push_back(Ref->getDecl());
9930 }
Alexey Bataev417089f2016-02-17 13:19:37 +00009931 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009932 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009933 Vars.push_back((VD || CurContext->isDependentContext())
9934 ? RefExpr->IgnoreParens()
9935 : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00009936 PrivateCopies.push_back(VDPrivateRefExpr);
9937 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009938 }
9939
Alexey Bataeved09d242014-05-28 05:53:51 +00009940 if (Vars.empty())
9941 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009942
9943 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +00009944 Vars, PrivateCopies, Inits,
9945 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009946}
9947
Alexander Musman1bb328c2014-06-04 13:06:39 +00009948OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
9949 SourceLocation StartLoc,
9950 SourceLocation LParenLoc,
9951 SourceLocation EndLoc) {
9952 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00009953 SmallVector<Expr *, 8> SrcExprs;
9954 SmallVector<Expr *, 8> DstExprs;
9955 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +00009956 SmallVector<Decl *, 4> ExprCaptures;
9957 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeve3727102018-04-18 15:57:46 +00009958 for (Expr *RefExpr : VarList) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00009959 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009960 SourceLocation ELoc;
9961 SourceRange ERange;
9962 Expr *SimpleRefExpr = RefExpr;
9963 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +00009964 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00009965 // It will be analyzed later.
9966 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00009967 SrcExprs.push_back(nullptr);
9968 DstExprs.push_back(nullptr);
9969 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00009970 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00009971 ValueDecl *D = Res.first;
9972 if (!D)
9973 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00009974
Alexey Bataev74caaf22016-02-20 04:09:36 +00009975 QualType Type = D->getType();
9976 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +00009977
9978 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
9979 // A variable that appears in a lastprivate clause must not have an
9980 // incomplete type or a reference type.
9981 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +00009982 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +00009983 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009984 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00009985
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009986 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexander Musman1bb328c2014-06-04 13:06:39 +00009987 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
9988 // in a Construct]
9989 // Variables with the predetermined data-sharing attributes may not be
9990 // listed in data-sharing attributes clauses, except for the cases
9991 // listed below.
Alexey Bataeveffbdf12017-07-21 17:24:30 +00009992 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
9993 // A list item may appear in a firstprivate or lastprivate clause but not
9994 // both.
Alexey Bataeve3727102018-04-18 15:57:46 +00009995 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00009996 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
Alexey Bataevb358f992017-12-01 17:40:15 +00009997 (isOpenMPDistributeDirective(CurrDir) ||
9998 DVar.CKind != OMPC_firstprivate) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +00009999 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
10000 Diag(ELoc, diag::err_omp_wrong_dsa)
10001 << getOpenMPClauseName(DVar.CKind)
10002 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000010003 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010004 continue;
10005 }
10006
Alexey Bataevf29276e2014-06-18 04:14:57 +000010007 // OpenMP [2.14.3.5, Restrictions, p.2]
10008 // A list item that is private within a parallel region, or that appears in
10009 // the reduction clause of a parallel construct, must not appear in a
10010 // lastprivate clause on a worksharing construct if any of the corresponding
10011 // worksharing regions ever binds to any of the corresponding parallel
10012 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +000010013 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +000010014 if (isOpenMPWorksharingDirective(CurrDir) &&
Kelvin Li579e41c2016-11-30 23:51:03 +000010015 !isOpenMPParallelDirective(CurrDir) &&
10016 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +000010017 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +000010018 if (DVar.CKind != OMPC_shared) {
10019 Diag(ELoc, diag::err_omp_required_access)
10020 << getOpenMPClauseName(OMPC_lastprivate)
10021 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +000010022 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +000010023 continue;
10024 }
10025 }
Alexey Bataev74caaf22016-02-20 04:09:36 +000010026
Alexander Musman1bb328c2014-06-04 13:06:39 +000010027 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +000010028 // A variable of class type (or array thereof) that appears in a
10029 // lastprivate clause requires an accessible, unambiguous default
10030 // constructor for the class type, unless the list item is also specified
10031 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +000010032 // A variable of class type (or array thereof) that appears in a
10033 // lastprivate clause requires an accessible, unambiguous copy assignment
10034 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +000010035 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010036 VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(),
10037 Type.getUnqualifiedType(), ".lastprivate.src",
10038 D->hasAttrs() ? &D->getAttrs() : nullptr);
10039 DeclRefExpr *PseudoSrcExpr =
Alexey Bataev74caaf22016-02-20 04:09:36 +000010040 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataeve3727102018-04-18 15:57:46 +000010041 VarDecl *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +000010042 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +000010043 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000010044 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +000010045 // For arrays generate assignment operation for single element and replace
10046 // it by the original array element in CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +000010047 ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
10048 PseudoDstExpr, PseudoSrcExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +000010049 if (AssignmentOp.isInvalid())
10050 continue;
Alexey Bataev74caaf22016-02-20 04:09:36 +000010051 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataev38e89532015-04-16 04:54:05 +000010052 /*DiscardedValue=*/true);
10053 if (AssignmentOp.isInvalid())
10054 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +000010055
Alexey Bataev74caaf22016-02-20 04:09:36 +000010056 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010057 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010058 if (TopDVar.CKind == OMPC_firstprivate) {
Alexey Bataev005248a2016-02-25 05:25:57 +000010059 Ref = TopDVar.PrivateCopy;
Alexey Bataeve3727102018-04-18 15:57:46 +000010060 } else {
Alexey Bataev61205072016-03-02 04:57:40 +000010061 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000010062 if (!isOpenMPCapturedDecl(D))
Alexey Bataev005248a2016-02-25 05:25:57 +000010063 ExprCaptures.push_back(Ref->getDecl());
10064 }
10065 if (TopDVar.CKind == OMPC_firstprivate ||
Alexey Bataeve3727102018-04-18 15:57:46 +000010066 (!isOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010067 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +000010068 ExprResult RefRes = DefaultLvalueConversion(Ref);
10069 if (!RefRes.isUsable())
10070 continue;
10071 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +000010072 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
10073 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +000010074 if (!PostUpdateRes.isUsable())
10075 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +000010076 ExprPostUpdates.push_back(
10077 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +000010078 }
10079 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010080 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010081 Vars.push_back((VD || CurContext->isDependentContext())
10082 ? RefExpr->IgnoreParens()
10083 : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +000010084 SrcExprs.push_back(PseudoSrcExpr);
10085 DstExprs.push_back(PseudoDstExpr);
10086 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +000010087 }
10088
10089 if (Vars.empty())
10090 return nullptr;
10091
10092 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +000010093 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +000010094 buildPreInits(Context, ExprCaptures),
10095 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +000010096}
10097
Alexey Bataev758e55e2013-09-06 18:03:48 +000010098OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
10099 SourceLocation StartLoc,
10100 SourceLocation LParenLoc,
10101 SourceLocation EndLoc) {
10102 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +000010103 for (Expr *RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010104 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +000010105 SourceLocation ELoc;
10106 SourceRange ERange;
10107 Expr *SimpleRefExpr = RefExpr;
10108 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010109 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +000010110 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000010111 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010112 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010113 ValueDecl *D = Res.first;
10114 if (!D)
10115 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +000010116
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010117 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010118 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
10119 // in a Construct]
10120 // Variables with the predetermined data-sharing attributes may not be
10121 // listed in data-sharing attributes clauses, except for the cases
10122 // listed below. For these exceptions only, listing a predetermined
10123 // variable in a data-sharing attribute clause is allowed and overrides
10124 // the variable's predetermined data-sharing attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +000010125 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataeved09d242014-05-28 05:53:51 +000010126 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
10127 DVar.RefExpr) {
10128 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
10129 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +000010130 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010131 continue;
10132 }
10133
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010134 DeclRefExpr *Ref = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000010135 if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +000010136 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010137 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010138 Vars.push_back((VD || !Ref || CurContext->isDependentContext())
10139 ? RefExpr->IgnoreParens()
10140 : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010141 }
10142
Alexey Bataeved09d242014-05-28 05:53:51 +000010143 if (Vars.empty())
10144 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +000010145
10146 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
10147}
10148
Alexey Bataevc5e02582014-06-16 07:08:35 +000010149namespace {
10150class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
10151 DSAStackTy *Stack;
10152
10153public:
10154 bool VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010155 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
10156 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataevc5e02582014-06-16 07:08:35 +000010157 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
10158 return false;
10159 if (DVar.CKind != OMPC_unknown)
10160 return true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010161 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +000010162 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) { return true; },
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010163 /*FromParent=*/true);
Alexey Bataeve3727102018-04-18 15:57:46 +000010164 return DVarPrivate.CKind != OMPC_unknown;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010165 }
10166 return false;
10167 }
10168 bool VisitStmt(Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010169 for (Stmt *Child : S->children()) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010170 if (Child && Visit(Child))
10171 return true;
10172 }
10173 return false;
10174 }
Alexey Bataev23b69422014-06-18 07:08:49 +000010175 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +000010176};
Alexey Bataev23b69422014-06-18 07:08:49 +000010177} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +000010178
Alexey Bataev60da77e2016-02-29 05:54:20 +000010179namespace {
10180// Transform MemberExpression for specified FieldDecl of current class to
10181// DeclRefExpr to specified OMPCapturedExprDecl.
10182class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
10183 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
Alexey Bataeve3727102018-04-18 15:57:46 +000010184 ValueDecl *Field = nullptr;
10185 DeclRefExpr *CapturedExpr = nullptr;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010186
10187public:
10188 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
10189 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
10190
10191 ExprResult TransformMemberExpr(MemberExpr *E) {
10192 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
10193 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +000010194 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +000010195 return CapturedExpr;
10196 }
10197 return BaseTransform::TransformMemberExpr(E);
10198 }
10199 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
10200};
10201} // namespace
10202
Alexey Bataev97d18bf2018-04-11 19:21:00 +000010203template <typename T, typename U>
10204static T filterLookupForUDR(SmallVectorImpl<U> &Lookups,
10205 const llvm::function_ref<T(ValueDecl *)> Gen) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010206 for (U &Set : Lookups) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010207 for (auto *D : Set) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010208 if (T Res = Gen(cast<ValueDecl>(D)))
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010209 return Res;
10210 }
10211 }
10212 return T();
10213}
10214
Alexey Bataev43b90b72018-09-12 16:31:59 +000010215static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) {
10216 assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case");
10217
10218 for (auto RD : D->redecls()) {
10219 // Don't bother with extra checks if we already know this one isn't visible.
10220 if (RD == D)
10221 continue;
10222
10223 auto ND = cast<NamedDecl>(RD);
10224 if (LookupResult::isVisible(SemaRef, ND))
10225 return ND;
10226 }
10227
10228 return nullptr;
10229}
10230
10231static void
10232argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &ReductionId,
10233 SourceLocation Loc, QualType Ty,
10234 SmallVectorImpl<UnresolvedSet<8>> &Lookups) {
10235 // Find all of the associated namespaces and classes based on the
10236 // arguments we have.
10237 Sema::AssociatedNamespaceSet AssociatedNamespaces;
10238 Sema::AssociatedClassSet AssociatedClasses;
10239 OpaqueValueExpr OVE(Loc, Ty, VK_LValue);
10240 SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces,
10241 AssociatedClasses);
10242
10243 // C++ [basic.lookup.argdep]p3:
10244 // Let X be the lookup set produced by unqualified lookup (3.4.1)
10245 // and let Y be the lookup set produced by argument dependent
10246 // lookup (defined as follows). If X contains [...] then Y is
10247 // empty. Otherwise Y is the set of declarations found in the
10248 // namespaces associated with the argument types as described
10249 // below. The set of declarations found by the lookup of the name
10250 // is the union of X and Y.
10251 //
10252 // Here, we compute Y and add its members to the overloaded
10253 // candidate set.
10254 for (auto *NS : AssociatedNamespaces) {
10255 // When considering an associated namespace, the lookup is the
10256 // same as the lookup performed when the associated namespace is
10257 // used as a qualifier (3.4.3.2) except that:
10258 //
10259 // -- Any using-directives in the associated namespace are
10260 // ignored.
10261 //
10262 // -- Any namespace-scope friend functions declared in
10263 // associated classes are visible within their respective
10264 // namespaces even if they are not visible during an ordinary
10265 // lookup (11.4).
10266 DeclContext::lookup_result R = NS->lookup(ReductionId.getName());
10267 for (auto *D : R) {
10268 auto *Underlying = D;
10269 if (auto *USD = dyn_cast<UsingShadowDecl>(D))
10270 Underlying = USD->getTargetDecl();
10271
10272 if (!isa<OMPDeclareReductionDecl>(Underlying))
10273 continue;
10274
10275 if (!SemaRef.isVisible(D)) {
10276 D = findAcceptableDecl(SemaRef, D);
10277 if (!D)
10278 continue;
10279 if (auto *USD = dyn_cast<UsingShadowDecl>(D))
10280 Underlying = USD->getTargetDecl();
10281 }
10282 Lookups.emplace_back();
10283 Lookups.back().addDecl(Underlying);
10284 }
10285 }
10286}
10287
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010288static ExprResult
10289buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
10290 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
10291 const DeclarationNameInfo &ReductionId, QualType Ty,
10292 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
10293 if (ReductionIdScopeSpec.isInvalid())
10294 return ExprError();
10295 SmallVector<UnresolvedSet<8>, 4> Lookups;
10296 if (S) {
10297 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
10298 Lookup.suppressDiagnostics();
10299 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010300 NamedDecl *D = Lookup.getRepresentativeDecl();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010301 do {
10302 S = S->getParent();
10303 } while (S && !S->isDeclScope(D));
10304 if (S)
10305 S = S->getParent();
Alexey Bataev43b90b72018-09-12 16:31:59 +000010306 Lookups.emplace_back();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010307 Lookups.back().append(Lookup.begin(), Lookup.end());
10308 Lookup.clear();
10309 }
10310 } else if (auto *ULE =
10311 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
10312 Lookups.push_back(UnresolvedSet<8>());
10313 Decl *PrevD = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000010314 for (NamedDecl *D : ULE->decls()) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010315 if (D == PrevD)
10316 Lookups.push_back(UnresolvedSet<8>());
10317 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
10318 Lookups.back().addDecl(DRD);
10319 PrevD = D;
10320 }
10321 }
Alexey Bataevfdc20352017-08-25 15:43:55 +000010322 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() ||
10323 Ty->isInstantiationDependentType() ||
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010324 Ty->containsUnexpandedParameterPack() ||
Alexey Bataeve3727102018-04-18 15:57:46 +000010325 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010326 return !D->isInvalidDecl() &&
10327 (D->getType()->isDependentType() ||
10328 D->getType()->isInstantiationDependentType() ||
10329 D->getType()->containsUnexpandedParameterPack());
10330 })) {
10331 UnresolvedSet<8> ResSet;
Alexey Bataeve3727102018-04-18 15:57:46 +000010332 for (const UnresolvedSet<8> &Set : Lookups) {
Alexey Bataev43b90b72018-09-12 16:31:59 +000010333 if (Set.empty())
10334 continue;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010335 ResSet.append(Set.begin(), Set.end());
10336 // The last item marks the end of all declarations at the specified scope.
10337 ResSet.addDecl(Set[Set.size() - 1]);
10338 }
10339 return UnresolvedLookupExpr::Create(
10340 SemaRef.Context, /*NamingClass=*/nullptr,
10341 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
10342 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
10343 }
Alexey Bataev43b90b72018-09-12 16:31:59 +000010344 // Lookup inside the classes.
10345 // C++ [over.match.oper]p3:
10346 // For a unary operator @ with an operand of a type whose
10347 // cv-unqualified version is T1, and for a binary operator @ with
10348 // a left operand of a type whose cv-unqualified version is T1 and
10349 // a right operand of a type whose cv-unqualified version is T2,
10350 // three sets of candidate functions, designated member
10351 // candidates, non-member candidates and built-in candidates, are
10352 // constructed as follows:
10353 // -- If T1 is a complete class type or a class currently being
10354 // defined, the set of member candidates is the result of the
10355 // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise,
10356 // the set of member candidates is empty.
10357 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
10358 Lookup.suppressDiagnostics();
10359 if (const auto *TyRec = Ty->getAs<RecordType>()) {
10360 // Complete the type if it can be completed.
10361 // If the type is neither complete nor being defined, bail out now.
10362 if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() ||
10363 TyRec->getDecl()->getDefinition()) {
10364 Lookup.clear();
10365 SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl());
10366 if (Lookup.empty()) {
10367 Lookups.emplace_back();
10368 Lookups.back().append(Lookup.begin(), Lookup.end());
10369 }
10370 }
10371 }
10372 // Perform ADL.
10373 argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010374 if (auto *VD = filterLookupForUDR<ValueDecl *>(
10375 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
10376 if (!D->isInvalidDecl() &&
10377 SemaRef.Context.hasSameType(D->getType(), Ty))
10378 return D;
10379 return nullptr;
10380 }))
10381 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
10382 if (auto *VD = filterLookupForUDR<ValueDecl *>(
10383 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
10384 if (!D->isInvalidDecl() &&
10385 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
10386 !Ty.isMoreQualifiedThan(D->getType()))
10387 return D;
10388 return nullptr;
10389 })) {
10390 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
10391 /*DetectVirtual=*/false);
10392 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
10393 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
10394 VD->getType().getUnqualifiedType()))) {
10395 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
10396 /*DiagID=*/0) !=
10397 Sema::AR_inaccessible) {
10398 SemaRef.BuildBasePathArray(Paths, BasePath);
10399 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
10400 }
10401 }
10402 }
10403 }
10404 if (ReductionIdScopeSpec.isSet()) {
10405 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
10406 return ExprError();
10407 }
10408 return ExprEmpty();
10409}
10410
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010411namespace {
10412/// Data for the reduction-based clauses.
10413struct ReductionData {
10414 /// List of original reduction items.
10415 SmallVector<Expr *, 8> Vars;
10416 /// List of private copies of the reduction items.
10417 SmallVector<Expr *, 8> Privates;
10418 /// LHS expressions for the reduction_op expressions.
10419 SmallVector<Expr *, 8> LHSs;
10420 /// RHS expressions for the reduction_op expressions.
10421 SmallVector<Expr *, 8> RHSs;
10422 /// Reduction operation expression.
10423 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev88202be2017-07-27 13:20:36 +000010424 /// Taskgroup descriptors for the corresponding reduction items in
10425 /// in_reduction clauses.
10426 SmallVector<Expr *, 8> TaskgroupDescriptors;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010427 /// List of captures for clause.
10428 SmallVector<Decl *, 4> ExprCaptures;
10429 /// List of postupdate expressions.
10430 SmallVector<Expr *, 4> ExprPostUpdates;
10431 ReductionData() = delete;
10432 /// Reserves required memory for the reduction data.
10433 ReductionData(unsigned Size) {
10434 Vars.reserve(Size);
10435 Privates.reserve(Size);
10436 LHSs.reserve(Size);
10437 RHSs.reserve(Size);
10438 ReductionOps.reserve(Size);
Alexey Bataev88202be2017-07-27 13:20:36 +000010439 TaskgroupDescriptors.reserve(Size);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010440 ExprCaptures.reserve(Size);
10441 ExprPostUpdates.reserve(Size);
10442 }
10443 /// Stores reduction item and reduction operation only (required for dependent
10444 /// reduction item).
10445 void push(Expr *Item, Expr *ReductionOp) {
10446 Vars.emplace_back(Item);
10447 Privates.emplace_back(nullptr);
10448 LHSs.emplace_back(nullptr);
10449 RHSs.emplace_back(nullptr);
10450 ReductionOps.emplace_back(ReductionOp);
Alexey Bataev88202be2017-07-27 13:20:36 +000010451 TaskgroupDescriptors.emplace_back(nullptr);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010452 }
10453 /// Stores reduction data.
Alexey Bataev88202be2017-07-27 13:20:36 +000010454 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp,
10455 Expr *TaskgroupDescriptor) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010456 Vars.emplace_back(Item);
10457 Privates.emplace_back(Private);
10458 LHSs.emplace_back(LHS);
10459 RHSs.emplace_back(RHS);
10460 ReductionOps.emplace_back(ReductionOp);
Alexey Bataev88202be2017-07-27 13:20:36 +000010461 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010462 }
10463};
10464} // namespace
10465
Alexey Bataeve3727102018-04-18 15:57:46 +000010466static bool checkOMPArraySectionConstantForReduction(
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010467 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement,
10468 SmallVectorImpl<llvm::APSInt> &ArraySizes) {
10469 const Expr *Length = OASE->getLength();
10470 if (Length == nullptr) {
10471 // For array sections of the form [1:] or [:], we would need to analyze
10472 // the lower bound...
10473 if (OASE->getColonLoc().isValid())
10474 return false;
10475
10476 // This is an array subscript which has implicit length 1!
10477 SingleElement = true;
10478 ArraySizes.push_back(llvm::APSInt::get(1));
10479 } else {
10480 llvm::APSInt ConstantLengthValue;
10481 if (!Length->EvaluateAsInt(ConstantLengthValue, Context))
10482 return false;
10483
10484 SingleElement = (ConstantLengthValue.getSExtValue() == 1);
10485 ArraySizes.push_back(ConstantLengthValue);
10486 }
10487
10488 // Get the base of this array section and walk up from there.
10489 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
10490
10491 // We require length = 1 for all array sections except the right-most to
10492 // guarantee that the memory region is contiguous and has no holes in it.
10493 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) {
10494 Length = TempOASE->getLength();
10495 if (Length == nullptr) {
10496 // For array sections of the form [1:] or [:], we would need to analyze
10497 // the lower bound...
10498 if (OASE->getColonLoc().isValid())
10499 return false;
10500
10501 // This is an array subscript which has implicit length 1!
10502 ArraySizes.push_back(llvm::APSInt::get(1));
10503 } else {
10504 llvm::APSInt ConstantLengthValue;
10505 if (!Length->EvaluateAsInt(ConstantLengthValue, Context) ||
10506 ConstantLengthValue.getSExtValue() != 1)
10507 return false;
10508
10509 ArraySizes.push_back(ConstantLengthValue);
10510 }
10511 Base = TempOASE->getBase()->IgnoreParenImpCasts();
10512 }
10513
10514 // If we have a single element, we don't need to add the implicit lengths.
10515 if (!SingleElement) {
10516 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) {
10517 // Has implicit length 1!
10518 ArraySizes.push_back(llvm::APSInt::get(1));
10519 Base = TempASE->getBase()->IgnoreParenImpCasts();
10520 }
10521 }
10522
10523 // This array section can be privatized as a single value or as a constant
10524 // sized array.
10525 return true;
10526}
10527
Alexey Bataeve3727102018-04-18 15:57:46 +000010528static bool actOnOMPReductionKindClause(
Alexey Bataev169d96a2017-07-18 20:17:46 +000010529 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind,
10530 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
10531 SourceLocation ColonLoc, SourceLocation EndLoc,
10532 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010533 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010534 DeclarationName DN = ReductionId.getName();
10535 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator();
Alexey Bataevc5e02582014-06-16 07:08:35 +000010536 BinaryOperatorKind BOK = BO_Comma;
10537
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010538 ASTContext &Context = S.Context;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010539 // OpenMP [2.14.3.6, reduction clause]
10540 // C
10541 // reduction-identifier is either an identifier or one of the following
10542 // operators: +, -, *, &, |, ^, && and ||
10543 // C++
10544 // reduction-identifier is either an id-expression or one of the following
10545 // operators: +, -, *, &, |, ^, && and ||
Alexey Bataevc5e02582014-06-16 07:08:35 +000010546 switch (OOK) {
10547 case OO_Plus:
10548 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010549 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010550 break;
10551 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010552 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010553 break;
10554 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010555 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010556 break;
10557 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010558 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010559 break;
10560 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010561 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010562 break;
10563 case OO_AmpAmp:
10564 BOK = BO_LAnd;
10565 break;
10566 case OO_PipePipe:
10567 BOK = BO_LOr;
10568 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010569 case OO_New:
10570 case OO_Delete:
10571 case OO_Array_New:
10572 case OO_Array_Delete:
10573 case OO_Slash:
10574 case OO_Percent:
10575 case OO_Tilde:
10576 case OO_Exclaim:
10577 case OO_Equal:
10578 case OO_Less:
10579 case OO_Greater:
10580 case OO_LessEqual:
10581 case OO_GreaterEqual:
10582 case OO_PlusEqual:
10583 case OO_MinusEqual:
10584 case OO_StarEqual:
10585 case OO_SlashEqual:
10586 case OO_PercentEqual:
10587 case OO_CaretEqual:
10588 case OO_AmpEqual:
10589 case OO_PipeEqual:
10590 case OO_LessLess:
10591 case OO_GreaterGreater:
10592 case OO_LessLessEqual:
10593 case OO_GreaterGreaterEqual:
10594 case OO_EqualEqual:
10595 case OO_ExclaimEqual:
Richard Smithd30b23d2017-12-01 02:13:10 +000010596 case OO_Spaceship:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010597 case OO_PlusPlus:
10598 case OO_MinusMinus:
10599 case OO_Comma:
10600 case OO_ArrowStar:
10601 case OO_Arrow:
10602 case OO_Call:
10603 case OO_Subscript:
10604 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +000010605 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010606 case NUM_OVERLOADED_OPERATORS:
10607 llvm_unreachable("Unexpected reduction identifier");
10608 case OO_None:
Alexey Bataeve3727102018-04-18 15:57:46 +000010609 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010610 if (II->isStr("max"))
10611 BOK = BO_GT;
10612 else if (II->isStr("min"))
10613 BOK = BO_LT;
10614 }
10615 break;
10616 }
10617 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010618 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +000010619 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataev4d4624c2017-07-20 16:47:47 +000010620 else
10621 ReductionIdRange.setBegin(ReductionId.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +000010622 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +000010623
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010624 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
10625 bool FirstIter = true;
Alexey Bataeve3727102018-04-18 15:57:46 +000010626 for (Expr *RefExpr : VarList) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010627 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +000010628 // OpenMP [2.1, C/C++]
10629 // A list item is a variable or array section, subject to the restrictions
10630 // specified in Section 2.4 on page 42 and in each of the sections
10631 // describing clauses and directives for which a list appears.
10632 // OpenMP [2.14.3.3, Restrictions, p.1]
10633 // A variable that is part of another variable (as an array or
10634 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010635 if (!FirstIter && IR != ER)
10636 ++IR;
10637 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010638 SourceLocation ELoc;
10639 SourceRange ERange;
10640 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010641 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
Alexey Bataev60da77e2016-02-29 05:54:20 +000010642 /*AllowArraySection=*/true);
10643 if (Res.second) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010644 // Try to find 'declare reduction' corresponding construct before using
10645 // builtin/overloaded operators.
10646 QualType Type = Context.DependentTy;
10647 CXXCastPath BasePath;
10648 ExprResult DeclareReductionRef = buildDeclareReductionRef(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010649 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010650 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010651 Expr *ReductionOp = nullptr;
10652 if (S.CurContext->isDependentContext() &&
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010653 (DeclareReductionRef.isUnset() ||
10654 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010655 ReductionOp = DeclareReductionRef.get();
10656 // It will be analyzed later.
10657 RD.push(RefExpr, ReductionOp);
Alexey Bataevc5e02582014-06-16 07:08:35 +000010658 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000010659 ValueDecl *D = Res.first;
10660 if (!D)
10661 continue;
10662
Alexey Bataev88202be2017-07-27 13:20:36 +000010663 Expr *TaskgroupDescriptor = nullptr;
Alexey Bataeva1764212015-09-30 09:22:36 +000010664 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010665 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
10666 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
Alexey Bataeve3727102018-04-18 15:57:46 +000010667 if (ASE) {
Alexey Bataev31300ed2016-02-04 11:27:03 +000010668 Type = ASE->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010669 } else if (OASE) {
10670 QualType BaseType =
10671 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
10672 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
Alexey Bataeva1764212015-09-30 09:22:36 +000010673 Type = ATy->getElementType();
10674 else
10675 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +000010676 Type = Type.getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010677 } else {
Alexey Bataev60da77e2016-02-29 05:54:20 +000010678 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
Alexey Bataeve3727102018-04-18 15:57:46 +000010679 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000010680 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +000010681
Alexey Bataevc5e02582014-06-16 07:08:35 +000010682 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
10683 // A variable that appears in a private clause must not have an incomplete
10684 // type or a reference type.
Joel E. Denny3cabf732018-06-28 19:54:49 +000010685 if (S.RequireCompleteType(ELoc, D->getType(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010686 diag::err_omp_reduction_incomplete_type))
Alexey Bataevc5e02582014-06-16 07:08:35 +000010687 continue;
10688 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +000010689 // A list item that appears in a reduction clause must not be
10690 // const-qualified.
10691 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataev169d96a2017-07-18 20:17:46 +000010692 S.Diag(ELoc, diag::err_omp_const_reduction_list_item) << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010693 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010694 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10695 VarDecl::DeclarationOnly;
10696 S.Diag(D->getLocation(),
10697 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +000010698 << D;
Alexey Bataeva1764212015-09-30 09:22:36 +000010699 }
Alexey Bataevc5e02582014-06-16 07:08:35 +000010700 continue;
10701 }
Alexey Bataevbc529672018-09-28 19:33:14 +000010702
10703 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective();
Alexey Bataevc5e02582014-06-16 07:08:35 +000010704 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
10705 // If a list-item is a reference type then it must bind to the same object
10706 // for all threads of the team.
Alexey Bataevbc529672018-09-28 19:33:14 +000010707 if (!ASE && !OASE) {
10708 if (VD) {
10709 VarDecl *VDDef = VD->getDefinition();
10710 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) {
10711 DSARefChecker Check(Stack);
10712 if (Check.Visit(VDDef->getInit())) {
10713 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg)
10714 << getOpenMPClauseName(ClauseKind) << ERange;
10715 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
10716 continue;
10717 }
Alexey Bataeva1764212015-09-30 09:22:36 +000010718 }
Alexey Bataevc5e02582014-06-16 07:08:35 +000010719 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010720
Alexey Bataevbc529672018-09-28 19:33:14 +000010721 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
10722 // in a Construct]
10723 // Variables with the predetermined data-sharing attributes may not be
10724 // listed in data-sharing attributes clauses, except for the cases
10725 // listed below. For these exceptions only, listing a predetermined
10726 // variable in a data-sharing attribute clause is allowed and overrides
10727 // the variable's predetermined data-sharing attributes.
10728 // OpenMP [2.14.3.6, Restrictions, p.3]
10729 // Any number of reduction clauses can be specified on the directive,
10730 // but a list item can appear only once in the reduction clauses for that
10731 // directive.
10732 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false);
10733 if (DVar.CKind == OMPC_reduction) {
10734 S.Diag(ELoc, diag::err_omp_once_referenced)
10735 << getOpenMPClauseName(ClauseKind);
10736 if (DVar.RefExpr)
10737 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
10738 continue;
10739 }
10740 if (DVar.CKind != OMPC_unknown) {
10741 S.Diag(ELoc, diag::err_omp_wrong_dsa)
10742 << getOpenMPClauseName(DVar.CKind)
10743 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataeve3727102018-04-18 15:57:46 +000010744 reportOriginalDsa(S, Stack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010745 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +000010746 }
Alexey Bataevbc529672018-09-28 19:33:14 +000010747
10748 // OpenMP [2.14.3.6, Restrictions, p.1]
10749 // A list item that appears in a reduction clause of a worksharing
10750 // construct must be shared in the parallel regions to which any of the
10751 // worksharing regions arising from the worksharing construct bind.
10752 if (isOpenMPWorksharingDirective(CurrDir) &&
10753 !isOpenMPParallelDirective(CurrDir) &&
10754 !isOpenMPTeamsDirective(CurrDir)) {
10755 DVar = Stack->getImplicitDSA(D, true);
10756 if (DVar.CKind != OMPC_shared) {
10757 S.Diag(ELoc, diag::err_omp_required_access)
10758 << getOpenMPClauseName(OMPC_reduction)
10759 << getOpenMPClauseName(OMPC_shared);
10760 reportOriginalDsa(S, Stack, D, DVar);
10761 continue;
10762 }
10763 }
Alexey Bataevf29276e2014-06-18 04:14:57 +000010764 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010765
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010766 // Try to find 'declare reduction' corresponding construct before using
10767 // builtin/overloaded operators.
10768 CXXCastPath BasePath;
10769 ExprResult DeclareReductionRef = buildDeclareReductionRef(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010770 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010771 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
10772 if (DeclareReductionRef.isInvalid())
10773 continue;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010774 if (S.CurContext->isDependentContext() &&
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010775 (DeclareReductionRef.isUnset() ||
10776 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010777 RD.push(RefExpr, DeclareReductionRef.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010778 continue;
10779 }
10780 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
10781 // Not allowed reduction identifier is found.
Stephen Kellyf2ceec42018-08-09 21:08:08 +000010782 S.Diag(ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010783 diag::err_omp_unknown_reduction_identifier)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010784 << Type << ReductionIdRange;
10785 continue;
10786 }
10787
10788 // OpenMP [2.14.3.6, reduction clause, Restrictions]
10789 // The type of a list item that appears in a reduction clause must be valid
10790 // for the reduction-identifier. For a max or min reduction in C, the type
10791 // of the list item must be an allowed arithmetic data type: char, int,
10792 // float, double, or _Bool, possibly modified with long, short, signed, or
10793 // unsigned. For a max or min reduction in C++, the type of the list item
10794 // must be an allowed arithmetic data type: char, wchar_t, int, float,
10795 // double, or bool, possibly modified with long, short, signed, or unsigned.
10796 if (DeclareReductionRef.isUnset()) {
10797 if ((BOK == BO_GT || BOK == BO_LT) &&
10798 !(Type->isScalarType() ||
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010799 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
10800 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
Alexey Bataev169d96a2017-07-18 20:17:46 +000010801 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010802 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010803 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10804 VarDecl::DeclarationOnly;
10805 S.Diag(D->getLocation(),
10806 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010807 << D;
10808 }
10809 continue;
10810 }
10811 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010812 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) {
Alexey Bataev169d96a2017-07-18 20:17:46 +000010813 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg)
10814 << getOpenMPClauseName(ClauseKind);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010815 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010816 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
10817 VarDecl::DeclarationOnly;
10818 S.Diag(D->getLocation(),
10819 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010820 << D;
10821 }
10822 continue;
10823 }
10824 }
10825
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010826 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010827 VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs",
10828 D->hasAttrs() ? &D->getAttrs() : nullptr);
10829 VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(),
10830 D->hasAttrs() ? &D->getAttrs() : nullptr);
10831 QualType PrivateTy = Type;
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010832
10833 // Try if we can determine constant lengths for all array sections and avoid
10834 // the VLA.
10835 bool ConstantLengthOASE = false;
10836 if (OASE) {
10837 bool SingleElement;
10838 llvm::SmallVector<llvm::APSInt, 4> ArraySizes;
Alexey Bataeve3727102018-04-18 15:57:46 +000010839 ConstantLengthOASE = checkOMPArraySectionConstantForReduction(
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010840 Context, OASE, SingleElement, ArraySizes);
10841
10842 // If we don't have a single element, we must emit a constant array type.
10843 if (ConstantLengthOASE && !SingleElement) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010844 for (llvm::APSInt &Size : ArraySizes)
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010845 PrivateTy = Context.getConstantArrayType(
10846 PrivateTy, Size, ArrayType::Normal, /*IndexTypeQuals=*/0);
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010847 }
10848 }
10849
10850 if ((OASE && !ConstantLengthOASE) ||
Jonas Hahnfeld96087f32017-11-02 13:30:42 +000010851 (!OASE && !ASE &&
Alexey Bataev60da77e2016-02-29 05:54:20 +000010852 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Jonas Hahnfeld87d44262017-11-18 21:00:46 +000010853 if (!Context.getTargetInfo().isVLASupported() &&
10854 S.shouldDiagnoseTargetSupportFromOpenMP()) {
10855 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE;
10856 S.Diag(ELoc, diag::note_vla_unsupported);
10857 continue;
10858 }
David Majnemer9d168222016-08-05 17:44:54 +000010859 // For arrays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010860 // Create pseudo array type for private copy. The size for this array will
10861 // be generated during codegen.
10862 // For array subscripts or single variables Private Ty is the same as Type
10863 // (type of the variable or single array element).
10864 PrivateTy = Context.getVariableArrayType(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010865 Type,
Alexey Bataevd070a582017-10-25 15:54:04 +000010866 new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue),
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010867 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +000010868 } else if (!ASE && !OASE &&
Alexey Bataeve3727102018-04-18 15:57:46 +000010869 Context.getAsArrayType(D->getType().getNonReferenceType())) {
Alexey Bataev60da77e2016-02-29 05:54:20 +000010870 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010871 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010872 // Private copy.
Alexey Bataeve3727102018-04-18 15:57:46 +000010873 VarDecl *PrivateVD =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000010874 buildVarDecl(S, ELoc, PrivateTy, D->getName(),
10875 D->hasAttrs() ? &D->getAttrs() : nullptr,
10876 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010877 // Add initializer for private variable.
10878 Expr *Init = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000010879 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc);
10880 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010881 if (DeclareReductionRef.isUsable()) {
10882 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
10883 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
10884 if (DRD->getInitializer()) {
10885 Init = DRDRef;
10886 RHSVD->setInit(DRDRef);
10887 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010888 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010889 } else {
10890 switch (BOK) {
10891 case BO_Add:
10892 case BO_Xor:
10893 case BO_Or:
10894 case BO_LOr:
10895 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
10896 if (Type->isScalarType() || Type->isAnyComplexType())
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010897 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010898 break;
10899 case BO_Mul:
10900 case BO_LAnd:
10901 if (Type->isScalarType() || Type->isAnyComplexType()) {
10902 // '*' and '&&' reduction ops - initializer is '1'.
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010903 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +000010904 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010905 break;
10906 case BO_And: {
10907 // '&' reduction op - initializer is '~0'.
10908 QualType OrigType = Type;
10909 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
10910 Type = ComplexTy->getElementType();
10911 if (Type->isRealFloatingType()) {
10912 llvm::APFloat InitValue =
10913 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
10914 /*isIEEE=*/true);
10915 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
10916 Type, ELoc);
10917 } else if (Type->isScalarType()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010918 uint64_t Size = Context.getTypeSize(Type);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010919 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
10920 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
10921 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
10922 }
10923 if (Init && OrigType->isAnyComplexType()) {
10924 // Init = 0xFFFF + 0xFFFFi;
10925 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010926 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010927 }
10928 Type = OrigType;
10929 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010930 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010931 case BO_LT:
10932 case BO_GT: {
10933 // 'min' reduction op - initializer is 'Largest representable number in
10934 // the reduction list item type'.
10935 // 'max' reduction op - initializer is 'Least representable number in
10936 // the reduction list item type'.
10937 if (Type->isIntegerType() || Type->isPointerType()) {
10938 bool IsSigned = Type->hasSignedIntegerRepresentation();
Alexey Bataeve3727102018-04-18 15:57:46 +000010939 uint64_t Size = Context.getTypeSize(Type);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010940 QualType IntTy =
10941 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
10942 llvm::APInt InitValue =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010943 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
10944 : llvm::APInt::getMinValue(Size)
10945 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
10946 : llvm::APInt::getMaxValue(Size);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010947 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
10948 if (Type->isPointerType()) {
10949 // Cast to pointer type.
Alexey Bataeve3727102018-04-18 15:57:46 +000010950 ExprResult CastExpr = S.BuildCStyleCastExpr(
Alexey Bataevd070a582017-10-25 15:54:04 +000010951 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010952 if (CastExpr.isInvalid())
10953 continue;
10954 Init = CastExpr.get();
10955 }
10956 } else if (Type->isRealFloatingType()) {
10957 llvm::APFloat InitValue = llvm::APFloat::getLargest(
10958 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
10959 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
10960 Type, ELoc);
10961 }
10962 break;
10963 }
10964 case BO_PtrMemD:
10965 case BO_PtrMemI:
10966 case BO_MulAssign:
10967 case BO_Div:
10968 case BO_Rem:
10969 case BO_Sub:
10970 case BO_Shl:
10971 case BO_Shr:
10972 case BO_LE:
10973 case BO_GE:
10974 case BO_EQ:
10975 case BO_NE:
Richard Smithc70f1d62017-12-14 15:16:18 +000010976 case BO_Cmp:
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010977 case BO_AndAssign:
10978 case BO_XorAssign:
10979 case BO_OrAssign:
10980 case BO_Assign:
10981 case BO_AddAssign:
10982 case BO_SubAssign:
10983 case BO_DivAssign:
10984 case BO_RemAssign:
10985 case BO_ShlAssign:
10986 case BO_ShrAssign:
10987 case BO_Comma:
10988 llvm_unreachable("Unexpected reduction operation");
10989 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010990 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010991 if (Init && DeclareReductionRef.isUnset())
10992 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false);
10993 else if (!Init)
10994 S.ActOnUninitializedDecl(RHSVD);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010995 if (RHSVD->isInvalidDecl())
10996 continue;
10997 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010998 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible)
10999 << Type << ReductionIdRange;
11000 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
11001 VarDecl::DeclarationOnly;
11002 S.Diag(D->getLocation(),
11003 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +000011004 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011005 continue;
11006 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011007 // Store initializer for single element in private copy. Will be used during
11008 // codegen.
11009 PrivateVD->setInit(RHSVD->getInit());
11010 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataeve3727102018-04-18 15:57:46 +000011011 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011012 ExprResult ReductionOp;
11013 if (DeclareReductionRef.isUsable()) {
11014 QualType RedTy = DeclareReductionRef.get()->getType();
11015 QualType PtrRedTy = Context.getPointerType(RedTy);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011016 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
11017 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011018 if (!BasePath.empty()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011019 LHS = S.DefaultLvalueConversion(LHS.get());
11020 RHS = S.DefaultLvalueConversion(RHS.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011021 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
11022 CK_UncheckedDerivedToBase, LHS.get(),
11023 &BasePath, LHS.get()->getValueKind());
11024 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
11025 CK_UncheckedDerivedToBase, RHS.get(),
11026 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011027 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011028 FunctionProtoType::ExtProtoInfo EPI;
11029 QualType Params[] = {PtrRedTy, PtrRedTy};
11030 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
11031 auto *OVE = new (Context) OpaqueValueExpr(
11032 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011033 S.DefaultLvalueConversion(DeclareReductionRef.get()).get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011034 Expr *Args[] = {LHS.get(), RHS.get()};
11035 ReductionOp = new (Context)
11036 CallExpr(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
11037 } else {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011038 ReductionOp = S.BuildBinOp(
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011039 Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, LHSDRE, RHSDRE);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011040 if (ReductionOp.isUsable()) {
11041 if (BOK != BO_LT && BOK != BO_GT) {
11042 ReductionOp =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011043 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011044 BO_Assign, LHSDRE, ReductionOp.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011045 } else {
Alexey Bataevd070a582017-10-25 15:54:04 +000011046 auto *ConditionalOp = new (Context)
11047 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE,
11048 Type, VK_LValue, OK_Ordinary);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011049 ReductionOp =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011050 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011051 BO_Assign, LHSDRE, ConditionalOp);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011052 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +000011053 if (ReductionOp.isUsable())
11054 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011055 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +000011056 if (!ReductionOp.isUsable())
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011057 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +000011058 }
11059
Alexey Bataevfa312f32017-07-21 18:48:21 +000011060 // OpenMP [2.15.4.6, Restrictions, p.2]
11061 // A list item that appears in an in_reduction clause of a task construct
11062 // must appear in a task_reduction clause of a construct associated with a
11063 // taskgroup region that includes the participating task in its taskgroup
11064 // set. The construct associated with the innermost region that meets this
11065 // condition must specify the same reduction-identifier as the in_reduction
11066 // clause.
11067 if (ClauseKind == OMPC_in_reduction) {
Alexey Bataevfa312f32017-07-21 18:48:21 +000011068 SourceRange ParentSR;
11069 BinaryOperatorKind ParentBOK;
11070 const Expr *ParentReductionOp;
Alexey Bataev88202be2017-07-27 13:20:36 +000011071 Expr *ParentBOKTD, *ParentReductionOpTD;
Alexey Bataevf189cb72017-07-24 14:52:13 +000011072 DSAStackTy::DSAVarData ParentBOKDSA =
Alexey Bataev88202be2017-07-27 13:20:36 +000011073 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK,
11074 ParentBOKTD);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011075 DSAStackTy::DSAVarData ParentReductionOpDSA =
Alexey Bataev88202be2017-07-27 13:20:36 +000011076 Stack->getTopMostTaskgroupReductionData(
11077 D, ParentSR, ParentReductionOp, ParentReductionOpTD);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011078 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown;
11079 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown;
11080 if (!IsParentBOK && !IsParentReductionOp) {
11081 S.Diag(ELoc, diag::err_omp_in_reduction_not_task_reduction);
11082 continue;
11083 }
Alexey Bataevfa312f32017-07-21 18:48:21 +000011084 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) ||
11085 (DeclareReductionRef.isUsable() && IsParentBOK) || BOK != ParentBOK ||
11086 IsParentReductionOp) {
11087 bool EmitError = true;
11088 if (IsParentReductionOp && DeclareReductionRef.isUsable()) {
11089 llvm::FoldingSetNodeID RedId, ParentRedId;
11090 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true);
11091 DeclareReductionRef.get()->Profile(RedId, Context,
11092 /*Canonical=*/true);
11093 EmitError = RedId != ParentRedId;
11094 }
11095 if (EmitError) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011096 S.Diag(ReductionId.getBeginLoc(),
Alexey Bataevfa312f32017-07-21 18:48:21 +000011097 diag::err_omp_reduction_identifier_mismatch)
11098 << ReductionIdRange << RefExpr->getSourceRange();
11099 S.Diag(ParentSR.getBegin(),
11100 diag::note_omp_previous_reduction_identifier)
Alexey Bataevf189cb72017-07-24 14:52:13 +000011101 << ParentSR
11102 << (IsParentBOK ? ParentBOKDSA.RefExpr
11103 : ParentReductionOpDSA.RefExpr)
11104 ->getSourceRange();
Alexey Bataevfa312f32017-07-21 18:48:21 +000011105 continue;
11106 }
11107 }
Alexey Bataev88202be2017-07-27 13:20:36 +000011108 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD;
11109 assert(TaskgroupDescriptor && "Taskgroup descriptor must be defined.");
Alexey Bataevfa312f32017-07-21 18:48:21 +000011110 }
11111
Alexey Bataev60da77e2016-02-29 05:54:20 +000011112 DeclRefExpr *Ref = nullptr;
11113 Expr *VarsExpr = RefExpr->IgnoreParens();
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011114 if (!VD && !S.CurContext->isDependentContext()) {
Alexey Bataev60da77e2016-02-29 05:54:20 +000011115 if (ASE || OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011116 TransformExprToCaptures RebuildToCapture(S, D);
Alexey Bataev60da77e2016-02-29 05:54:20 +000011117 VarsExpr =
11118 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
11119 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +000011120 } else {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011121 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +000011122 }
Alexey Bataeve3727102018-04-18 15:57:46 +000011123 if (!S.isOpenMPCapturedDecl(D)) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011124 RD.ExprCaptures.emplace_back(Ref->getDecl());
Alexey Bataev5a3af132016-03-29 08:58:54 +000011125 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011126 ExprResult RefRes = S.DefaultLvalueConversion(Ref);
Alexey Bataev5a3af132016-03-29 08:58:54 +000011127 if (!RefRes.isUsable())
11128 continue;
11129 ExprResult PostUpdateRes =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011130 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
11131 RefRes.get());
Alexey Bataev5a3af132016-03-29 08:58:54 +000011132 if (!PostUpdateRes.isUsable())
11133 continue;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011134 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
11135 Stack->getCurrentDirective() == OMPD_taskgroup) {
11136 S.Diag(RefExpr->getExprLoc(),
11137 diag::err_omp_reduction_non_addressable_expression)
Alexey Bataevbcd0ae02017-07-11 19:16:44 +000011138 << RefExpr->getSourceRange();
11139 continue;
11140 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011141 RD.ExprPostUpdates.emplace_back(
11142 S.IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +000011143 }
11144 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000011145 }
Alexey Bataev169d96a2017-07-18 20:17:46 +000011146 // All reduction items are still marked as reduction (to do not increase
11147 // code base size).
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011148 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011149 if (CurrDir == OMPD_taskgroup) {
11150 if (DeclareReductionRef.isUsable())
Alexey Bataev3b1b8952017-07-25 15:53:26 +000011151 Stack->addTaskgroupReductionData(D, ReductionIdRange,
11152 DeclareReductionRef.get());
Alexey Bataevf189cb72017-07-24 14:52:13 +000011153 else
Alexey Bataev3b1b8952017-07-25 15:53:26 +000011154 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011155 }
Alexey Bataev88202be2017-07-27 13:20:36 +000011156 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(),
11157 TaskgroupDescriptor);
Alexey Bataevc5e02582014-06-16 07:08:35 +000011158 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011159 return RD.Vars.empty();
11160}
Alexey Bataevc5e02582014-06-16 07:08:35 +000011161
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011162OMPClause *Sema::ActOnOpenMPReductionClause(
11163 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11164 SourceLocation ColonLoc, SourceLocation EndLoc,
11165 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11166 ArrayRef<Expr *> UnresolvedReductions) {
11167 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000011168 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList,
Alexey Bataev169d96a2017-07-18 20:17:46 +000011169 StartLoc, LParenLoc, ColonLoc, EndLoc,
11170 ReductionIdScopeSpec, ReductionId,
11171 UnresolvedReductions, RD))
Alexey Bataevc5e02582014-06-16 07:08:35 +000011172 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +000011173
Alexey Bataevc5e02582014-06-16 07:08:35 +000011174 return OMPReductionClause::Create(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011175 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11176 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
11177 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
11178 buildPreInits(Context, RD.ExprCaptures),
11179 buildPostUpdate(*this, RD.ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +000011180}
11181
Alexey Bataev169d96a2017-07-18 20:17:46 +000011182OMPClause *Sema::ActOnOpenMPTaskReductionClause(
11183 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11184 SourceLocation ColonLoc, SourceLocation EndLoc,
11185 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11186 ArrayRef<Expr *> UnresolvedReductions) {
11187 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000011188 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList,
11189 StartLoc, LParenLoc, ColonLoc, EndLoc,
11190 ReductionIdScopeSpec, ReductionId,
Alexey Bataev169d96a2017-07-18 20:17:46 +000011191 UnresolvedReductions, RD))
11192 return nullptr;
11193
11194 return OMPTaskReductionClause::Create(
11195 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11196 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
11197 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
11198 buildPreInits(Context, RD.ExprCaptures),
11199 buildPostUpdate(*this, RD.ExprPostUpdates));
11200}
11201
Alexey Bataevfa312f32017-07-21 18:48:21 +000011202OMPClause *Sema::ActOnOpenMPInReductionClause(
11203 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11204 SourceLocation ColonLoc, SourceLocation EndLoc,
11205 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11206 ArrayRef<Expr *> UnresolvedReductions) {
11207 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000011208 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList,
Alexey Bataevfa312f32017-07-21 18:48:21 +000011209 StartLoc, LParenLoc, ColonLoc, EndLoc,
11210 ReductionIdScopeSpec, ReductionId,
11211 UnresolvedReductions, RD))
11212 return nullptr;
11213
11214 return OMPInReductionClause::Create(
11215 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11216 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
Alexey Bataev88202be2017-07-27 13:20:36 +000011217 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors,
Alexey Bataevfa312f32017-07-21 18:48:21 +000011218 buildPreInits(Context, RD.ExprCaptures),
11219 buildPostUpdate(*this, RD.ExprPostUpdates));
11220}
11221
Alexey Bataevecba70f2016-04-12 11:02:11 +000011222bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
11223 SourceLocation LinLoc) {
11224 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
11225 LinKind == OMPC_LINEAR_unknown) {
11226 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
11227 return true;
11228 }
11229 return false;
11230}
11231
Alexey Bataeve3727102018-04-18 15:57:46 +000011232bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc,
Alexey Bataevecba70f2016-04-12 11:02:11 +000011233 OpenMPLinearClauseKind LinKind,
11234 QualType Type) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011235 const auto *VD = dyn_cast_or_null<VarDecl>(D);
Alexey Bataevecba70f2016-04-12 11:02:11 +000011236 // A variable must not have an incomplete type or a reference type.
11237 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
11238 return true;
11239 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
11240 !Type->isReferenceType()) {
11241 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
11242 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
11243 return true;
11244 }
11245 Type = Type.getNonReferenceType();
11246
11247 // A list item must not be const-qualified.
11248 if (Type.isConstant(Context)) {
11249 Diag(ELoc, diag::err_omp_const_variable)
11250 << getOpenMPClauseName(OMPC_linear);
11251 if (D) {
11252 bool IsDecl =
11253 !VD ||
11254 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
11255 Diag(D->getLocation(),
11256 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
11257 << D;
11258 }
11259 return true;
11260 }
11261
11262 // A list item must be of integral or pointer type.
11263 Type = Type.getUnqualifiedType().getCanonicalType();
11264 const auto *Ty = Type.getTypePtrOrNull();
11265 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
11266 !Ty->isPointerType())) {
11267 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
11268 if (D) {
11269 bool IsDecl =
11270 !VD ||
11271 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
11272 Diag(D->getLocation(),
11273 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
11274 << D;
11275 }
11276 return true;
11277 }
11278 return false;
11279}
11280
Alexey Bataev182227b2015-08-20 10:54:39 +000011281OMPClause *Sema::ActOnOpenMPLinearClause(
11282 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
11283 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
11284 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +000011285 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011286 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +000011287 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +000011288 SmallVector<Decl *, 4> ExprCaptures;
11289 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +000011290 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +000011291 LinKind = OMPC_LINEAR_val;
Alexey Bataeve3727102018-04-18 15:57:46 +000011292 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000011293 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011294 SourceLocation ELoc;
11295 SourceRange ERange;
11296 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011297 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011298 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +000011299 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000011300 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011301 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +000011302 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +000011303 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011304 ValueDecl *D = Res.first;
11305 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +000011306 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +000011307
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011308 QualType Type = D->getType();
11309 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +000011310
11311 // OpenMP [2.14.3.7, linear clause]
11312 // A list-item cannot appear in more than one linear clause.
11313 // A list-item that appears in a linear clause cannot appear in any
11314 // other data-sharing attribute clause.
Alexey Bataeve3727102018-04-18 15:57:46 +000011315 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexander Musman8dba6642014-04-22 13:09:42 +000011316 if (DVar.RefExpr) {
11317 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
11318 << getOpenMPClauseName(OMPC_linear);
Alexey Bataeve3727102018-04-18 15:57:46 +000011319 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +000011320 continue;
11321 }
11322
Alexey Bataevecba70f2016-04-12 11:02:11 +000011323 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +000011324 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +000011325 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +000011326
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011327 // Build private copy of original var.
Alexey Bataeve3727102018-04-18 15:57:46 +000011328 VarDecl *Private =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000011329 buildVarDecl(*this, ELoc, Type, D->getName(),
11330 D->hasAttrs() ? &D->getAttrs() : nullptr,
11331 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011332 DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +000011333 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011334 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011335 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011336 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000011337 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev78849fb2016-03-09 09:49:00 +000011338 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011339 if (!isOpenMPCapturedDecl(D)) {
Alexey Bataev78849fb2016-03-09 09:49:00 +000011340 ExprCaptures.push_back(Ref->getDecl());
11341 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
11342 ExprResult RefRes = DefaultLvalueConversion(Ref);
11343 if (!RefRes.isUsable())
11344 continue;
11345 ExprResult PostUpdateRes =
11346 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
11347 SimpleRefExpr, RefRes.get());
11348 if (!PostUpdateRes.isUsable())
11349 continue;
11350 ExprPostUpdates.push_back(
11351 IgnoredValueConversions(PostUpdateRes.get()).get());
11352 }
11353 }
11354 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011355 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011356 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011357 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011358 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011359 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000011360 /*DirectInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011361 DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011362
11363 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000011364 Vars.push_back((VD || CurContext->isDependentContext())
11365 ? RefExpr->IgnoreParens()
11366 : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011367 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +000011368 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +000011369 }
11370
11371 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000011372 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +000011373
11374 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +000011375 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +000011376 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
11377 !Step->isInstantiationDependent() &&
11378 !Step->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011379 SourceLocation StepLoc = Step->getBeginLoc();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +000011380 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +000011381 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000011382 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +000011383 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +000011384
Alexander Musman3276a272015-03-21 10:12:56 +000011385 // Build var to save the step value.
11386 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +000011387 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +000011388 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +000011389 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +000011390 ExprResult CalcStep =
11391 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +000011392 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +000011393
Alexander Musman8dba6642014-04-22 13:09:42 +000011394 // Warn about zero linear step (it would be probably better specified as
11395 // making corresponding variables 'const').
11396 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +000011397 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
11398 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +000011399 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
11400 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +000011401 if (!IsConstant && CalcStep.isUsable()) {
11402 // Calculate the step beforehand instead of doing this on each iteration.
11403 // (This is not used if the number of iterations may be kfold-ed).
11404 CalcStepExpr = CalcStep.get();
11405 }
Alexander Musman8dba6642014-04-22 13:09:42 +000011406 }
11407
Alexey Bataev182227b2015-08-20 10:54:39 +000011408 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
11409 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +000011410 StepExpr, CalcStepExpr,
11411 buildPreInits(Context, ExprCaptures),
11412 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +000011413}
11414
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011415static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
11416 Expr *NumIterations, Sema &SemaRef,
11417 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +000011418 // Walk the vars and build update/final expressions for the CodeGen.
11419 SmallVector<Expr *, 8> Updates;
11420 SmallVector<Expr *, 8> Finals;
11421 Expr *Step = Clause.getStep();
11422 Expr *CalcStep = Clause.getCalcStep();
11423 // OpenMP [2.14.3.7, linear clause]
11424 // If linear-step is not specified it is assumed to be 1.
Alexey Bataeve3727102018-04-18 15:57:46 +000011425 if (!Step)
Alexander Musman3276a272015-03-21 10:12:56 +000011426 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000011427 else if (CalcStep)
Alexander Musman3276a272015-03-21 10:12:56 +000011428 Step = cast<BinaryOperator>(CalcStep)->getLHS();
11429 bool HasErrors = false;
11430 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011431 auto CurPrivate = Clause.privates().begin();
Alexey Bataeve3727102018-04-18 15:57:46 +000011432 OpenMPLinearClauseKind LinKind = Clause.getModifier();
11433 for (Expr *RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011434 SourceLocation ELoc;
11435 SourceRange ERange;
11436 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011437 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange);
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011438 ValueDecl *D = Res.first;
11439 if (Res.second || !D) {
11440 Updates.push_back(nullptr);
11441 Finals.push_back(nullptr);
11442 HasErrors = true;
11443 continue;
11444 }
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011445 auto &&Info = Stack->isLoopControlVariable(D);
Alexey Bataev2b86f212017-11-29 21:31:48 +000011446 // OpenMP [2.15.11, distribute simd Construct]
11447 // A list item may not appear in a linear clause, unless it is the loop
11448 // iteration variable.
11449 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) &&
11450 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) {
11451 SemaRef.Diag(ELoc,
11452 diag::err_omp_linear_distribute_var_non_loop_iteration);
11453 Updates.push_back(nullptr);
11454 Finals.push_back(nullptr);
11455 HasErrors = true;
11456 continue;
11457 }
Alexander Musman3276a272015-03-21 10:12:56 +000011458 Expr *InitExpr = *CurInit;
11459
11460 // Build privatized reference to the current linear var.
David Majnemer9d168222016-08-05 17:44:54 +000011461 auto *DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011462 Expr *CapturedRef;
11463 if (LinKind == OMPC_LINEAR_uval)
11464 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
11465 else
11466 CapturedRef =
11467 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
11468 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
11469 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +000011470
11471 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011472 ExprResult Update;
Alexey Bataeve3727102018-04-18 15:57:46 +000011473 if (!Info.first)
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011474 Update =
Alexey Bataeve3727102018-04-18 15:57:46 +000011475 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011476 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011477 else
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011478 Update = *CurPrivate;
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011479 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +000011480 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +000011481
11482 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011483 ExprResult Final;
Alexey Bataeve3727102018-04-18 15:57:46 +000011484 if (!Info.first)
11485 Final =
11486 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
11487 InitExpr, NumIterations, Step, /*Subtract=*/false);
11488 else
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011489 Final = *CurPrivate;
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011490 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +000011491 /*DiscardedValue=*/true);
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011492
Alexander Musman3276a272015-03-21 10:12:56 +000011493 if (!Update.isUsable() || !Final.isUsable()) {
11494 Updates.push_back(nullptr);
11495 Finals.push_back(nullptr);
11496 HasErrors = true;
11497 } else {
11498 Updates.push_back(Update.get());
11499 Finals.push_back(Final.get());
11500 }
Richard Trieucc3949d2016-02-18 22:34:54 +000011501 ++CurInit;
11502 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +000011503 }
11504 Clause.setUpdates(Updates);
11505 Clause.setFinals(Finals);
11506 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +000011507}
11508
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011509OMPClause *Sema::ActOnOpenMPAlignedClause(
11510 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
11511 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011512 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +000011513 for (Expr *RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +000011514 assert(RefExpr && "NULL expr in OpenMP linear clause.");
11515 SourceLocation ELoc;
11516 SourceRange ERange;
11517 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011518 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev1efd1662016-03-29 10:59:56 +000011519 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011520 // It will be analyzed later.
11521 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011522 }
Alexey Bataev1efd1662016-03-29 10:59:56 +000011523 ValueDecl *D = Res.first;
11524 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011525 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011526
Alexey Bataev1efd1662016-03-29 10:59:56 +000011527 QualType QType = D->getType();
11528 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011529
11530 // OpenMP [2.8.1, simd construct, Restrictions]
11531 // The type of list items appearing in the aligned clause must be
11532 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011533 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011534 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +000011535 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011536 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +000011537 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011538 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +000011539 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011540 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +000011541 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011542 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +000011543 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011544 continue;
11545 }
11546
11547 // OpenMP [2.8.1, simd construct, Restrictions]
11548 // A list-item cannot appear in more than one aligned clause.
Alexey Bataeve3727102018-04-18 15:57:46 +000011549 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +000011550 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011551 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
11552 << getOpenMPClauseName(OMPC_aligned);
11553 continue;
11554 }
11555
Alexey Bataev1efd1662016-03-29 10:59:56 +000011556 DeclRefExpr *Ref = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000011557 if (!VD && isOpenMPCapturedDecl(D))
Alexey Bataev1efd1662016-03-29 10:59:56 +000011558 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
11559 Vars.push_back(DefaultFunctionArrayConversion(
11560 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
11561 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011562 }
11563
11564 // OpenMP [2.8.1, simd construct, Description]
11565 // The parameter of the aligned clause, alignment, must be a constant
11566 // positive integer expression.
11567 // If no optional parameter is specified, implementation-defined default
11568 // alignments for SIMD instructions on the target platforms are assumed.
11569 if (Alignment != nullptr) {
11570 ExprResult AlignResult =
11571 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
11572 if (AlignResult.isInvalid())
11573 return nullptr;
11574 Alignment = AlignResult.get();
11575 }
11576 if (Vars.empty())
11577 return nullptr;
11578
11579 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
11580 EndLoc, Vars, Alignment);
11581}
11582
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011583OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
11584 SourceLocation StartLoc,
11585 SourceLocation LParenLoc,
11586 SourceLocation EndLoc) {
11587 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011588 SmallVector<Expr *, 8> SrcExprs;
11589 SmallVector<Expr *, 8> DstExprs;
11590 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeve3727102018-04-18 15:57:46 +000011591 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000011592 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
11593 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011594 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000011595 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011596 SrcExprs.push_back(nullptr);
11597 DstExprs.push_back(nullptr);
11598 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011599 continue;
11600 }
11601
Alexey Bataeved09d242014-05-28 05:53:51 +000011602 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011603 // OpenMP [2.1, C/C++]
11604 // A list item is a variable name.
11605 // OpenMP [2.14.4.1, Restrictions, p.1]
11606 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeve3727102018-04-18 15:57:46 +000011607 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011608 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000011609 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
11610 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011611 continue;
11612 }
11613
11614 Decl *D = DE->getDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000011615 auto *VD = cast<VarDecl>(D);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011616
11617 QualType Type = VD->getType();
11618 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
11619 // It will be analyzed later.
11620 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011621 SrcExprs.push_back(nullptr);
11622 DstExprs.push_back(nullptr);
11623 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011624 continue;
11625 }
11626
11627 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
11628 // A list item that appears in a copyin clause must be threadprivate.
11629 if (!DSAStack->isThreadPrivate(VD)) {
11630 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +000011631 << getOpenMPClauseName(OMPC_copyin)
11632 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011633 continue;
11634 }
11635
11636 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
11637 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +000011638 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011639 // operator for the class type.
Alexey Bataeve3727102018-04-18 15:57:46 +000011640 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
11641 VarDecl *SrcVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011642 buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000011643 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011644 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011645 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +000011646 VarDecl *DstVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011647 buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst",
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000011648 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011649 DeclRefExpr *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011650 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011651 // For arrays generate assignment operation for single element and replace
11652 // it by the original array element in CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +000011653 ExprResult AssignmentOp =
11654 BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr,
11655 PseudoSrcExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011656 if (AssignmentOp.isInvalid())
11657 continue;
11658 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
11659 /*DiscardedValue=*/true);
11660 if (AssignmentOp.isInvalid())
11661 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011662
11663 DSAStack->addDSA(VD, DE, OMPC_copyin);
11664 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011665 SrcExprs.push_back(PseudoSrcExpr);
11666 DstExprs.push_back(PseudoDstExpr);
11667 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011668 }
11669
Alexey Bataeved09d242014-05-28 05:53:51 +000011670 if (Vars.empty())
11671 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011672
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011673 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
11674 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011675}
11676
Alexey Bataevbae9a792014-06-27 10:37:06 +000011677OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
11678 SourceLocation StartLoc,
11679 SourceLocation LParenLoc,
11680 SourceLocation EndLoc) {
11681 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +000011682 SmallVector<Expr *, 8> SrcExprs;
11683 SmallVector<Expr *, 8> DstExprs;
11684 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeve3727102018-04-18 15:57:46 +000011685 for (Expr *RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +000011686 assert(RefExpr && "NULL expr in OpenMP linear clause.");
11687 SourceLocation ELoc;
11688 SourceRange ERange;
11689 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011690 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataeve122da12016-03-17 10:50:17 +000011691 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000011692 // It will be analyzed later.
11693 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000011694 SrcExprs.push_back(nullptr);
11695 DstExprs.push_back(nullptr);
11696 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011697 }
Alexey Bataeve122da12016-03-17 10:50:17 +000011698 ValueDecl *D = Res.first;
11699 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +000011700 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000011701
Alexey Bataeve122da12016-03-17 10:50:17 +000011702 QualType Type = D->getType();
11703 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011704
11705 // OpenMP [2.14.4.2, Restrictions, p.2]
11706 // A list item that appears in a copyprivate clause may not appear in a
11707 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +000011708 if (!VD || !DSAStack->isThreadPrivate(VD)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011709 DSAStackTy::DSAVarData DVar =
11710 DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataeva63048e2015-03-23 06:18:07 +000011711 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
11712 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000011713 Diag(ELoc, diag::err_omp_wrong_dsa)
11714 << getOpenMPClauseName(DVar.CKind)
11715 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000011716 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011717 continue;
11718 }
11719
11720 // OpenMP [2.11.4.2, Restrictions, p.1]
11721 // All list items that appear in a copyprivate clause must be either
11722 // threadprivate or private in the enclosing context.
11723 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +000011724 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011725 if (DVar.CKind == OMPC_shared) {
11726 Diag(ELoc, diag::err_omp_required_access)
11727 << getOpenMPClauseName(OMPC_copyprivate)
11728 << "threadprivate or private in the enclosing context";
Alexey Bataeve3727102018-04-18 15:57:46 +000011729 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011730 continue;
11731 }
11732 }
11733 }
11734
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011735 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000011736 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011737 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +000011738 << getOpenMPClauseName(OMPC_copyprivate) << Type
11739 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011740 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +000011741 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011742 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +000011743 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011744 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +000011745 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +000011746 continue;
11747 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +000011748
Alexey Bataevbae9a792014-06-27 10:37:06 +000011749 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
11750 // A variable of class type (or array thereof) that appears in a
11751 // copyin clause requires an accessible, unambiguous copy assignment
11752 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011753 Type = Context.getBaseElementType(Type.getNonReferenceType())
11754 .getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000011755 VarDecl *SrcVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011756 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src",
Alexey Bataeve122da12016-03-17 10:50:17 +000011757 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011758 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
11759 VarDecl *DstVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011760 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst",
Alexey Bataeve122da12016-03-17 10:50:17 +000011761 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011762 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
11763 ExprResult AssignmentOp = BuildBinOp(
11764 DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000011765 if (AssignmentOp.isInvalid())
11766 continue;
Alexey Bataeve122da12016-03-17 10:50:17 +000011767 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), ELoc,
Alexey Bataeva63048e2015-03-23 06:18:07 +000011768 /*DiscardedValue=*/true);
11769 if (AssignmentOp.isInvalid())
11770 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000011771
11772 // No need to mark vars as copyprivate, they are already threadprivate or
11773 // implicitly private.
Alexey Bataeve3727102018-04-18 15:57:46 +000011774 assert(VD || isOpenMPCapturedDecl(D));
Alexey Bataeve122da12016-03-17 10:50:17 +000011775 Vars.push_back(
11776 VD ? RefExpr->IgnoreParens()
11777 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +000011778 SrcExprs.push_back(PseudoSrcExpr);
11779 DstExprs.push_back(PseudoDstExpr);
11780 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +000011781 }
11782
11783 if (Vars.empty())
11784 return nullptr;
11785
Alexey Bataeva63048e2015-03-23 06:18:07 +000011786 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
11787 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +000011788}
11789
Alexey Bataev6125da92014-07-21 11:26:11 +000011790OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
11791 SourceLocation StartLoc,
11792 SourceLocation LParenLoc,
11793 SourceLocation EndLoc) {
11794 if (VarList.empty())
11795 return nullptr;
11796
11797 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
11798}
Alexey Bataevdea47612014-07-23 07:46:59 +000011799
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011800OMPClause *
11801Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
11802 SourceLocation DepLoc, SourceLocation ColonLoc,
11803 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
11804 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +000011805 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011806 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +000011807 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000011808 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +000011809 return nullptr;
11810 }
11811 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011812 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
11813 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +000011814 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011815 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000011816 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
11817 /*Last=*/OMPC_DEPEND_unknown, Except)
11818 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011819 return nullptr;
11820 }
11821 SmallVector<Expr *, 8> Vars;
Alexey Bataev8b427062016-05-25 12:36:08 +000011822 DSAStackTy::OperatorOffsetTy OpsOffs;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011823 llvm::APSInt DepCounter(/*BitWidth=*/32);
11824 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
Alexey Bataevf138fda2018-08-13 19:04:24 +000011825 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) {
11826 if (const Expr *OrderedCountExpr =
11827 DSAStack->getParentOrderedRegionParam().first) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011828 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
11829 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011830 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011831 }
Alexey Bataeve3727102018-04-18 15:57:46 +000011832 for (Expr *RefExpr : VarList) {
Alexey Bataev17daedf2018-02-15 22:42:57 +000011833 assert(RefExpr && "NULL expr in OpenMP shared clause.");
11834 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
11835 // It will be analyzed later.
11836 Vars.push_back(RefExpr);
11837 continue;
11838 }
11839
11840 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataeve3727102018-04-18 15:57:46 +000011841 Expr *SimpleExpr = RefExpr->IgnoreParenCasts();
Alexey Bataev17daedf2018-02-15 22:42:57 +000011842 if (DepKind == OMPC_DEPEND_sink) {
Alexey Bataevf138fda2018-08-13 19:04:24 +000011843 if (DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000011844 DepCounter >= TotalDepCount) {
11845 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
11846 continue;
11847 }
11848 ++DepCounter;
11849 // OpenMP [2.13.9, Summary]
11850 // depend(dependence-type : vec), where dependence-type is:
11851 // 'sink' and where vec is the iteration vector, which has the form:
11852 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
11853 // where n is the value specified by the ordered clause in the loop
11854 // directive, xi denotes the loop iteration variable of the i-th nested
11855 // loop associated with the loop directive, and di is a constant
11856 // non-negative integer.
11857 if (CurContext->isDependentContext()) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011858 // It will be analyzed later.
11859 Vars.push_back(RefExpr);
11860 continue;
11861 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000011862 SimpleExpr = SimpleExpr->IgnoreImplicit();
11863 OverloadedOperatorKind OOK = OO_None;
11864 SourceLocation OOLoc;
11865 Expr *LHS = SimpleExpr;
11866 Expr *RHS = nullptr;
11867 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
11868 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
11869 OOLoc = BO->getOperatorLoc();
11870 LHS = BO->getLHS()->IgnoreParenImpCasts();
11871 RHS = BO->getRHS()->IgnoreParenImpCasts();
11872 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
11873 OOK = OCE->getOperator();
11874 OOLoc = OCE->getOperatorLoc();
11875 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
11876 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
11877 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
11878 OOK = MCE->getMethodDecl()
11879 ->getNameInfo()
11880 .getName()
11881 .getCXXOverloadedOperator();
11882 OOLoc = MCE->getCallee()->getExprLoc();
11883 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
11884 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011885 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000011886 SourceLocation ELoc;
11887 SourceRange ERange;
Alexey Bataevbc529672018-09-28 19:33:14 +000011888 auto Res = getPrivateItem(*this, LHS, ELoc, ERange);
Alexey Bataev17daedf2018-02-15 22:42:57 +000011889 if (Res.second) {
11890 // It will be analyzed later.
11891 Vars.push_back(RefExpr);
11892 }
11893 ValueDecl *D = Res.first;
11894 if (!D)
11895 continue;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011896
Alexey Bataev17daedf2018-02-15 22:42:57 +000011897 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
11898 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
11899 continue;
11900 }
11901 if (RHS) {
11902 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
11903 RHS, OMPC_depend, /*StrictlyPositive=*/false);
11904 if (RHSRes.isInvalid())
11905 continue;
11906 }
11907 if (!CurContext->isDependentContext() &&
Alexey Bataevf138fda2018-08-13 19:04:24 +000011908 DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000011909 DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011910 const ValueDecl *VD =
Alexey Bataev17daedf2018-02-15 22:42:57 +000011911 DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue());
Alexey Bataeve3727102018-04-18 15:57:46 +000011912 if (VD)
Alexey Bataev17daedf2018-02-15 22:42:57 +000011913 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
11914 << 1 << VD;
Alexey Bataeve3727102018-04-18 15:57:46 +000011915 else
Alexey Bataev17daedf2018-02-15 22:42:57 +000011916 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0;
Alexey Bataev17daedf2018-02-15 22:42:57 +000011917 continue;
11918 }
Alexey Bataeve3727102018-04-18 15:57:46 +000011919 OpsOffs.emplace_back(RHS, OOK);
Alexey Bataev17daedf2018-02-15 22:42:57 +000011920 } else {
11921 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
11922 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
11923 (ASE &&
11924 !ASE->getBase()->getType().getNonReferenceType()->isPointerType() &&
11925 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
11926 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
11927 << RefExpr->getSourceRange();
11928 continue;
11929 }
11930 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
11931 getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
11932 ExprResult Res =
11933 CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RefExpr->IgnoreParenImpCasts());
11934 getDiagnostics().setSuppressAllDiagnostics(Suppress);
11935 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr)) {
11936 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
11937 << RefExpr->getSourceRange();
11938 continue;
11939 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011940 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000011941 Vars.push_back(RefExpr->IgnoreParenImpCasts());
Alexey Bataeva636c7f2015-12-23 10:27:45 +000011942 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000011943
11944 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
11945 TotalDepCount > VarList.size() &&
Alexey Bataevf138fda2018-08-13 19:04:24 +000011946 DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000011947 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) {
11948 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
11949 << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
11950 }
11951 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
11952 Vars.empty())
11953 return nullptr;
11954
Alexey Bataev8b427062016-05-25 12:36:08 +000011955 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataevf138fda2018-08-13 19:04:24 +000011956 DepKind, DepLoc, ColonLoc, Vars,
11957 TotalDepCount.getZExtValue());
Alexey Bataev17daedf2018-02-15 22:42:57 +000011958 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) &&
11959 DSAStack->isParentOrderedRegion())
Alexey Bataev8b427062016-05-25 12:36:08 +000011960 DSAStack->addDoacrossDependClause(C, OpsOffs);
11961 return C;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000011962}
Michael Wonge710d542015-08-07 16:16:36 +000011963
11964OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
11965 SourceLocation LParenLoc,
11966 SourceLocation EndLoc) {
11967 Expr *ValExpr = Device;
Alexey Bataev931e19b2017-10-02 16:32:39 +000011968 Stmt *HelperValStmt = nullptr;
Michael Wonge710d542015-08-07 16:16:36 +000011969
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011970 // OpenMP [2.9.1, Restrictions]
11971 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000011972 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
Alexey Bataeva0569352015-12-01 10:17:31 +000011973 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000011974 return nullptr;
11975
Alexey Bataev931e19b2017-10-02 16:32:39 +000011976 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000011977 OpenMPDirectiveKind CaptureRegion =
11978 getOpenMPCaptureRegionForClause(DKind, OMPC_device);
11979 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000011980 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000011981 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev931e19b2017-10-02 16:32:39 +000011982 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
11983 HelperValStmt = buildPreInits(Context, Captures);
11984 }
11985
Alexey Bataev8451efa2018-01-15 19:06:12 +000011986 return new (Context) OMPDeviceClause(ValExpr, HelperValStmt, CaptureRegion,
11987 StartLoc, LParenLoc, EndLoc);
Michael Wonge710d542015-08-07 16:16:36 +000011988}
Kelvin Li0bff7af2015-11-23 05:32:03 +000011989
Alexey Bataeve3727102018-04-18 15:57:46 +000011990static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
Alexey Bataev95c23e72018-02-27 21:31:11 +000011991 DSAStackTy *Stack, QualType QTy,
11992 bool FullCheck = true) {
Kelvin Li0bff7af2015-11-23 05:32:03 +000011993 NamedDecl *ND;
11994 if (QTy->isIncompleteType(&ND)) {
11995 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
11996 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +000011997 }
Alexey Bataev95c23e72018-02-27 21:31:11 +000011998 if (FullCheck && !SemaRef.CurContext->isDependentContext() &&
11999 !QTy.isTrivialType(SemaRef.Context))
12000 SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR;
Kelvin Li0bff7af2015-11-23 05:32:03 +000012001 return true;
12002}
12003
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000012004/// Return true if it can be proven that the provided array expression
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012005/// (array section or array subscript) does NOT specify the whole size of the
12006/// array whose base type is \a BaseQTy.
Alexey Bataeve3727102018-04-18 15:57:46 +000012007static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012008 const Expr *E,
12009 QualType BaseQTy) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012010 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012011
12012 // If this is an array subscript, it refers to the whole size if the size of
12013 // the dimension is constant and equals 1. Also, an array section assumes the
12014 // format of an array subscript if no colon is used.
12015 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012016 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012017 return ATy->getSize().getSExtValue() != 1;
12018 // Size can't be evaluated statically.
12019 return false;
12020 }
12021
12022 assert(OASE && "Expecting array section if not an array subscript.");
Alexey Bataeve3727102018-04-18 15:57:46 +000012023 const Expr *LowerBound = OASE->getLowerBound();
12024 const Expr *Length = OASE->getLength();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012025
12026 // If there is a lower bound that does not evaluates to zero, we are not
David Majnemer9d168222016-08-05 17:44:54 +000012027 // covering the whole dimension.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012028 if (LowerBound) {
12029 llvm::APSInt ConstLowerBound;
12030 if (!LowerBound->EvaluateAsInt(ConstLowerBound, SemaRef.getASTContext()))
12031 return false; // Can't get the integer value as a constant.
12032 if (ConstLowerBound.getSExtValue())
12033 return true;
12034 }
12035
12036 // If we don't have a length we covering the whole dimension.
12037 if (!Length)
12038 return false;
12039
12040 // If the base is a pointer, we don't have a way to get the size of the
12041 // pointee.
12042 if (BaseQTy->isPointerType())
12043 return false;
12044
12045 // We can only check if the length is the same as the size of the dimension
12046 // if we have a constant array.
Alexey Bataeve3727102018-04-18 15:57:46 +000012047 const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012048 if (!CATy)
12049 return false;
12050
12051 llvm::APSInt ConstLength;
12052 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
12053 return false; // Can't get the integer value as a constant.
12054
12055 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
12056}
12057
12058// Return true if it can be proven that the provided array expression (array
12059// section or array subscript) does NOT specify a single element of the array
12060// whose base type is \a BaseQTy.
Alexey Bataeve3727102018-04-18 15:57:46 +000012061static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
David Majnemer9d168222016-08-05 17:44:54 +000012062 const Expr *E,
12063 QualType BaseQTy) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012064 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012065
12066 // An array subscript always refer to a single element. Also, an array section
12067 // assumes the format of an array subscript if no colon is used.
12068 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
12069 return false;
12070
12071 assert(OASE && "Expecting array section if not an array subscript.");
Alexey Bataeve3727102018-04-18 15:57:46 +000012072 const Expr *Length = OASE->getLength();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012073
12074 // If we don't have a length we have to check if the array has unitary size
12075 // for this dimension. Also, we should always expect a length if the base type
12076 // is pointer.
12077 if (!Length) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012078 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012079 return ATy->getSize().getSExtValue() != 1;
12080 // We cannot assume anything.
12081 return false;
12082 }
12083
12084 // Check if the length evaluates to 1.
12085 llvm::APSInt ConstLength;
12086 if (!Length->EvaluateAsInt(ConstLength, SemaRef.getASTContext()))
12087 return false; // Can't get the integer value as a constant.
12088
12089 return ConstLength.getSExtValue() != 1;
12090}
12091
Samuel Antao661c0902016-05-26 17:39:58 +000012092// Return the expression of the base of the mappable expression or null if it
12093// cannot be determined and do all the necessary checks to see if the expression
12094// is valid as a standalone mappable expression. In the process, record all the
Samuel Antao90927002016-04-26 14:54:23 +000012095// components of the expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000012096static const Expr *checkMapClauseExpressionBase(
Samuel Antao90927002016-04-26 14:54:23 +000012097 Sema &SemaRef, Expr *E,
Samuel Antao661c0902016-05-26 17:39:58 +000012098 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012099 OpenMPClauseKind CKind, bool NoDiagnose) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012100 SourceLocation ELoc = E->getExprLoc();
12101 SourceRange ERange = E->getSourceRange();
12102
12103 // The base of elements of list in a map clause have to be either:
12104 // - a reference to variable or field.
12105 // - a member expression.
12106 // - an array expression.
12107 //
12108 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
12109 // reference to 'r'.
12110 //
12111 // If we have:
12112 //
12113 // struct SS {
12114 // Bla S;
12115 // foo() {
12116 // #pragma omp target map (S.Arr[:12]);
12117 // }
12118 // }
12119 //
12120 // We want to retrieve the member expression 'this->S';
12121
Alexey Bataeve3727102018-04-18 15:57:46 +000012122 const Expr *RelevantExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012123
Samuel Antao5de996e2016-01-22 20:21:36 +000012124 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
12125 // If a list item is an array section, it must specify contiguous storage.
12126 //
12127 // For this restriction it is sufficient that we make sure only references
12128 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012129 // exist except in the rightmost expression (unless they cover the whole
12130 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +000012131 //
12132 // r.ArrS[3:5].Arr[6:7]
12133 //
12134 // r.ArrS[3:5].x
12135 //
12136 // but these would be valid:
12137 // r.ArrS[3].Arr[6:7]
12138 //
12139 // r.ArrS[3].x
12140
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012141 bool AllowUnitySizeArraySection = true;
12142 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +000012143
Dmitry Polukhin644a9252016-03-11 07:58:34 +000012144 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012145 E = E->IgnoreParenImpCasts();
12146
12147 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
12148 if (!isa<VarDecl>(CurE->getDecl()))
Alexey Bataev27041fa2017-12-05 15:22:49 +000012149 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012150
12151 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012152
12153 // If we got a reference to a declaration, we should not expect any array
12154 // section before that.
12155 AllowUnitySizeArraySection = false;
12156 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000012157
12158 // Record the component.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012159 CurComponents.emplace_back(CurE, CurE->getDecl());
12160 } else if (auto *CurE = dyn_cast<MemberExpr>(E)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012161 Expr *BaseE = CurE->getBase()->IgnoreParenImpCasts();
Samuel Antao5de996e2016-01-22 20:21:36 +000012162
12163 if (isa<CXXThisExpr>(BaseE))
12164 // We found a base expression: this->Val.
12165 RelevantExpr = CurE;
12166 else
12167 E = BaseE;
12168
12169 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012170 if (!NoDiagnose) {
12171 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
12172 << CurE->getSourceRange();
12173 return nullptr;
12174 }
12175 if (RelevantExpr)
12176 return nullptr;
12177 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000012178 }
12179
12180 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
12181
12182 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
12183 // A bit-field cannot appear in a map clause.
12184 //
12185 if (FD->isBitField()) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012186 if (!NoDiagnose) {
12187 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
12188 << CurE->getSourceRange() << getOpenMPClauseName(CKind);
12189 return nullptr;
12190 }
12191 if (RelevantExpr)
12192 return nullptr;
12193 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000012194 }
12195
12196 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12197 // If the type of a list item is a reference to a type T then the type
12198 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012199 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000012200
12201 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
12202 // A list item cannot be a variable that is a member of a structure with
12203 // a union type.
12204 //
Alexey Bataeve3727102018-04-18 15:57:46 +000012205 if (CurType->isUnionType()) {
12206 if (!NoDiagnose) {
12207 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
12208 << CurE->getSourceRange();
12209 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012210 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012211 continue;
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012212 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012213
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012214 // If we got a member expression, we should not expect any array section
12215 // before that:
12216 //
12217 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
12218 // If a list item is an element of a structure, only the rightmost symbol
12219 // of the variable reference can be an array section.
12220 //
12221 AllowUnitySizeArraySection = false;
12222 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000012223
12224 // Record the component.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012225 CurComponents.emplace_back(CurE, FD);
12226 } else if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012227 E = CurE->getBase()->IgnoreParenImpCasts();
12228
12229 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012230 if (!NoDiagnose) {
12231 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
12232 << 0 << CurE->getSourceRange();
12233 return nullptr;
12234 }
12235 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000012236 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012237
12238 // If we got an array subscript that express the whole dimension we
12239 // can have any array expressions before. If it only expressing part of
12240 // the dimension, we can only have unitary-size array expressions.
Alexey Bataeve3727102018-04-18 15:57:46 +000012241 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012242 E->getType()))
12243 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000012244
12245 // Record the component - we don't have any declaration associated.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012246 CurComponents.emplace_back(CurE, nullptr);
12247 } else if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012248 assert(!NoDiagnose && "Array sections cannot be implicitly mapped.");
Samuel Antao5de996e2016-01-22 20:21:36 +000012249 E = CurE->getBase()->IgnoreParenImpCasts();
12250
Alexey Bataev27041fa2017-12-05 15:22:49 +000012251 QualType CurType =
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012252 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
12253
Samuel Antao5de996e2016-01-22 20:21:36 +000012254 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12255 // If the type of a list item is a reference to a type T then the type
12256 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +000012257 if (CurType->isReferenceType())
12258 CurType = CurType->getPointeeType();
12259
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012260 bool IsPointer = CurType->isAnyPointerType();
12261
12262 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012263 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
12264 << 0 << CurE->getSourceRange();
Alexey Bataev27041fa2017-12-05 15:22:49 +000012265 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012266 }
12267
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012268 bool NotWhole =
Alexey Bataeve3727102018-04-18 15:57:46 +000012269 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012270 bool NotUnity =
Alexey Bataeve3727102018-04-18 15:57:46 +000012271 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012272
Samuel Antaodab51bb2016-07-18 23:22:11 +000012273 if (AllowWholeSizeArraySection) {
12274 // Any array section is currently allowed. Allowing a whole size array
12275 // section implies allowing a unity array section as well.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012276 //
12277 // If this array section refers to the whole dimension we can still
12278 // accept other array sections before this one, except if the base is a
12279 // pointer. Otherwise, only unitary sections are accepted.
12280 if (NotWhole || IsPointer)
12281 AllowWholeSizeArraySection = false;
Samuel Antaodab51bb2016-07-18 23:22:11 +000012282 } else if (AllowUnitySizeArraySection && NotUnity) {
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012283 // A unity or whole array section is not allowed and that is not
12284 // compatible with the properties of the current array section.
12285 SemaRef.Diag(
12286 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
12287 << CurE->getSourceRange();
Alexey Bataev27041fa2017-12-05 15:22:49 +000012288 return nullptr;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012289 }
Samuel Antao90927002016-04-26 14:54:23 +000012290
12291 // Record the component - we don't have any declaration associated.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012292 CurComponents.emplace_back(CurE, nullptr);
12293 } else {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012294 if (!NoDiagnose) {
12295 // If nothing else worked, this is not a valid map clause expression.
12296 SemaRef.Diag(
12297 ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
12298 << ERange;
12299 }
Alexey Bataev27041fa2017-12-05 15:22:49 +000012300 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012301 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012302 }
12303
12304 return RelevantExpr;
12305}
12306
12307// Return true if expression E associated with value VD has conflicts with other
12308// map information.
Alexey Bataeve3727102018-04-18 15:57:46 +000012309static bool checkMapConflicts(
12310 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E,
Samuel Antao90927002016-04-26 14:54:23 +000012311 bool CurrentRegionOnly,
Samuel Antao661c0902016-05-26 17:39:58 +000012312 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
12313 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012314 assert(VD && E);
Samuel Antao5de996e2016-01-22 20:21:36 +000012315 SourceLocation ELoc = E->getExprLoc();
12316 SourceRange ERange = E->getSourceRange();
12317
12318 // In order to easily check the conflicts we need to match each component of
12319 // the expression under test with the components of the expressions that are
12320 // already in the stack.
12321
Samuel Antao5de996e2016-01-22 20:21:36 +000012322 assert(!CurComponents.empty() && "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000012323 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000012324 "Map clause expression with unexpected base!");
12325
12326 // Variables to help detecting enclosing problems in data environment nests.
12327 bool IsEnclosedByDataEnvironmentExpr = false;
Samuel Antao90927002016-04-26 14:54:23 +000012328 const Expr *EnclosingExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012329
Samuel Antao90927002016-04-26 14:54:23 +000012330 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
12331 VD, CurrentRegionOnly,
Alexey Bataeve3727102018-04-18 15:57:46 +000012332 [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc,
12333 ERange, CKind, &EnclosingExpr,
12334 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef
12335 StackComponents,
12336 OpenMPClauseKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012337 assert(!StackComponents.empty() &&
12338 "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000012339 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000012340 "Map clause expression with unexpected base!");
Fangrui Song16fe49a2018-04-18 19:32:01 +000012341 (void)VD;
Samuel Antao5de996e2016-01-22 20:21:36 +000012342
Samuel Antao90927002016-04-26 14:54:23 +000012343 // The whole expression in the stack.
Alexey Bataeve3727102018-04-18 15:57:46 +000012344 const Expr *RE = StackComponents.front().getAssociatedExpression();
Samuel Antao90927002016-04-26 14:54:23 +000012345
Samuel Antao5de996e2016-01-22 20:21:36 +000012346 // Expressions must start from the same base. Here we detect at which
12347 // point both expressions diverge from each other and see if we can
12348 // detect if the memory referred to both expressions is contiguous and
12349 // do not overlap.
12350 auto CI = CurComponents.rbegin();
12351 auto CE = CurComponents.rend();
12352 auto SI = StackComponents.rbegin();
12353 auto SE = StackComponents.rend();
12354 for (; CI != CE && SI != SE; ++CI, ++SI) {
12355
12356 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
12357 // At most one list item can be an array item derived from a given
12358 // variable in map clauses of the same construct.
Samuel Antao90927002016-04-26 14:54:23 +000012359 if (CurrentRegionOnly &&
12360 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
12361 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
12362 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
12363 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
12364 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
Samuel Antao5de996e2016-01-22 20:21:36 +000012365 diag::err_omp_multiple_array_items_in_map_clause)
Samuel Antao90927002016-04-26 14:54:23 +000012366 << CI->getAssociatedExpression()->getSourceRange();
12367 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
12368 diag::note_used_here)
12369 << SI->getAssociatedExpression()->getSourceRange();
Samuel Antao5de996e2016-01-22 20:21:36 +000012370 return true;
12371 }
12372
12373 // Do both expressions have the same kind?
Samuel Antao90927002016-04-26 14:54:23 +000012374 if (CI->getAssociatedExpression()->getStmtClass() !=
12375 SI->getAssociatedExpression()->getStmtClass())
Samuel Antao5de996e2016-01-22 20:21:36 +000012376 break;
12377
12378 // Are we dealing with different variables/fields?
Samuel Antao90927002016-04-26 14:54:23 +000012379 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
Samuel Antao5de996e2016-01-22 20:21:36 +000012380 break;
12381 }
Kelvin Li9f645ae2016-07-18 22:49:16 +000012382 // Check if the extra components of the expressions in the enclosing
12383 // data environment are redundant for the current base declaration.
12384 // If they are, the maps completely overlap, which is legal.
12385 for (; SI != SE; ++SI) {
12386 QualType Type;
Alexey Bataeve3727102018-04-18 15:57:46 +000012387 if (const auto *ASE =
David Majnemer9d168222016-08-05 17:44:54 +000012388 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
Kelvin Li9f645ae2016-07-18 22:49:16 +000012389 Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
Alexey Bataeve3727102018-04-18 15:57:46 +000012390 } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(
David Majnemer9d168222016-08-05 17:44:54 +000012391 SI->getAssociatedExpression())) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012392 const Expr *E = OASE->getBase()->IgnoreParenImpCasts();
Kelvin Li9f645ae2016-07-18 22:49:16 +000012393 Type =
12394 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
12395 }
12396 if (Type.isNull() || Type->isAnyPointerType() ||
Alexey Bataeve3727102018-04-18 15:57:46 +000012397 checkArrayExpressionDoesNotReferToWholeSize(
Kelvin Li9f645ae2016-07-18 22:49:16 +000012398 SemaRef, SI->getAssociatedExpression(), Type))
12399 break;
12400 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012401
12402 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
12403 // List items of map clauses in the same construct must not share
12404 // original storage.
12405 //
12406 // If the expressions are exactly the same or one is a subset of the
12407 // other, it means they are sharing storage.
12408 if (CI == CE && SI == SE) {
12409 if (CurrentRegionOnly) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012410 if (CKind == OMPC_map) {
Samuel Antao661c0902016-05-26 17:39:58 +000012411 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +000012412 } else {
Samuel Antaoec172c62016-05-26 17:49:04 +000012413 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000012414 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
12415 << ERange;
12416 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012417 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12418 << RE->getSourceRange();
12419 return true;
Samuel Antao5de996e2016-01-22 20:21:36 +000012420 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012421 // If we find the same expression in the enclosing data environment,
12422 // that is legal.
12423 IsEnclosedByDataEnvironmentExpr = true;
12424 return false;
Samuel Antao5de996e2016-01-22 20:21:36 +000012425 }
12426
Samuel Antao90927002016-04-26 14:54:23 +000012427 QualType DerivedType =
12428 std::prev(CI)->getAssociatedDeclaration()->getType();
12429 SourceLocation DerivedLoc =
12430 std::prev(CI)->getAssociatedExpression()->getExprLoc();
Samuel Antao5de996e2016-01-22 20:21:36 +000012431
12432 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12433 // If the type of a list item is a reference to a type T then the type
12434 // will be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000012435 DerivedType = DerivedType.getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000012436
12437 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
12438 // A variable for which the type is pointer and an array section
12439 // derived from that variable must not appear as list items of map
12440 // clauses of the same construct.
12441 //
12442 // Also, cover one of the cases in:
12443 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
12444 // If any part of the original storage of a list item has corresponding
12445 // storage in the device data environment, all of the original storage
12446 // must have corresponding storage in the device data environment.
12447 //
12448 if (DerivedType->isAnyPointerType()) {
12449 if (CI == CE || SI == SE) {
12450 SemaRef.Diag(
12451 DerivedLoc,
12452 diag::err_omp_pointer_mapped_along_with_derived_section)
12453 << DerivedLoc;
Alexey Bataev2819260b2018-02-27 17:42:00 +000012454 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12455 << RE->getSourceRange();
12456 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +000012457 }
12458 if (CI->getAssociatedExpression()->getStmtClass() !=
Alexey Bataev2819260b2018-02-27 17:42:00 +000012459 SI->getAssociatedExpression()->getStmtClass() ||
12460 CI->getAssociatedDeclaration()->getCanonicalDecl() ==
12461 SI->getAssociatedDeclaration()->getCanonicalDecl()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012462 assert(CI != CE && SI != SE);
Alexey Bataev2819260b2018-02-27 17:42:00 +000012463 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced)
Samuel Antao5de996e2016-01-22 20:21:36 +000012464 << DerivedLoc;
Alexey Bataev2819260b2018-02-27 17:42:00 +000012465 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12466 << RE->getSourceRange();
12467 return true;
Samuel Antao5de996e2016-01-22 20:21:36 +000012468 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012469 }
12470
12471 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
12472 // List items of map clauses in the same construct must not share
12473 // original storage.
12474 //
12475 // An expression is a subset of the other.
12476 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012477 if (CKind == OMPC_map) {
Alexey Bataeve82445f2018-09-20 13:54:02 +000012478 if (CI != CE || SI != SE) {
12479 // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is
12480 // a pointer.
12481 auto Begin =
12482 CI != CE ? CurComponents.begin() : StackComponents.begin();
12483 auto End = CI != CE ? CurComponents.end() : StackComponents.end();
12484 auto It = Begin;
12485 while (It != End && !It->getAssociatedDeclaration())
12486 std::advance(It, 1);
12487 assert(It != End &&
12488 "Expected at least one component with the declaration.");
12489 if (It != Begin && It->getAssociatedDeclaration()
12490 ->getType()
12491 .getCanonicalType()
12492 ->isAnyPointerType()) {
12493 IsEnclosedByDataEnvironmentExpr = false;
12494 EnclosingExpr = nullptr;
12495 return false;
12496 }
12497 }
Samuel Antao661c0902016-05-26 17:39:58 +000012498 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +000012499 } else {
Samuel Antaoec172c62016-05-26 17:49:04 +000012500 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000012501 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
12502 << ERange;
12503 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012504 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12505 << RE->getSourceRange();
12506 return true;
12507 }
12508
12509 // The current expression uses the same base as other expression in the
Samuel Antao90927002016-04-26 14:54:23 +000012510 // data environment but does not contain it completely.
Samuel Antao5de996e2016-01-22 20:21:36 +000012511 if (!CurrentRegionOnly && SI != SE)
12512 EnclosingExpr = RE;
12513
12514 // The current expression is a subset of the expression in the data
12515 // environment.
12516 IsEnclosedByDataEnvironmentExpr |=
12517 (!CurrentRegionOnly && CI != CE && SI == SE);
12518
12519 return false;
12520 });
12521
12522 if (CurrentRegionOnly)
12523 return FoundError;
12524
12525 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
12526 // If any part of the original storage of a list item has corresponding
12527 // storage in the device data environment, all of the original storage must
12528 // have corresponding storage in the device data environment.
12529 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
12530 // If a list item is an element of a structure, and a different element of
12531 // the structure has a corresponding list item in the device data environment
12532 // prior to a task encountering the construct associated with the map clause,
Samuel Antao90927002016-04-26 14:54:23 +000012533 // then the list item must also have a corresponding list item in the device
Samuel Antao5de996e2016-01-22 20:21:36 +000012534 // data environment prior to the task encountering the construct.
12535 //
12536 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
12537 SemaRef.Diag(ELoc,
12538 diag::err_omp_original_storage_is_shared_and_does_not_contain)
12539 << ERange;
12540 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
12541 << EnclosingExpr->getSourceRange();
12542 return true;
12543 }
12544
12545 return FoundError;
12546}
12547
Samuel Antao661c0902016-05-26 17:39:58 +000012548namespace {
12549// Utility struct that gathers all the related lists associated with a mappable
12550// expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000012551struct MappableVarListInfo {
Samuel Antao661c0902016-05-26 17:39:58 +000012552 // The list of expressions.
12553 ArrayRef<Expr *> VarList;
12554 // The list of processed expressions.
12555 SmallVector<Expr *, 16> ProcessedVarList;
12556 // The mappble components for each expression.
12557 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
12558 // The base declaration of the variable.
12559 SmallVector<ValueDecl *, 16> VarBaseDeclarations;
12560
12561 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
12562 // We have a list of components and base declarations for each entry in the
12563 // variable list.
12564 VarComponents.reserve(VarList.size());
12565 VarBaseDeclarations.reserve(VarList.size());
12566 }
12567};
12568}
12569
12570// Check the validity of the provided variable list for the provided clause kind
12571// \a CKind. In the check process the valid expressions, and mappable expression
12572// components and variables are extracted and used to fill \a Vars,
12573// \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and
12574// \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'.
12575static void
12576checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS,
12577 OpenMPClauseKind CKind, MappableVarListInfo &MVLI,
12578 SourceLocation StartLoc,
12579 OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
12580 bool IsMapTypeImplicit = false) {
Samuel Antaoec172c62016-05-26 17:49:04 +000012581 // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
12582 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
Samuel Antao661c0902016-05-26 17:39:58 +000012583 "Unexpected clause kind with mappable expressions!");
Kelvin Li0bff7af2015-11-23 05:32:03 +000012584
Samuel Antao90927002016-04-26 14:54:23 +000012585 // Keep track of the mappable components and base declarations in this clause.
12586 // Each entry in the list is going to have a list of components associated. We
12587 // record each set of the components so that we can build the clause later on.
12588 // In the end we should have the same amount of declarations and component
12589 // lists.
Samuel Antao90927002016-04-26 14:54:23 +000012590
Alexey Bataeve3727102018-04-18 15:57:46 +000012591 for (Expr *RE : MVLI.VarList) {
Samuel Antaoec172c62016-05-26 17:49:04 +000012592 assert(RE && "Null expr in omp to/from/map clause");
Kelvin Li0bff7af2015-11-23 05:32:03 +000012593 SourceLocation ELoc = RE->getExprLoc();
12594
Alexey Bataeve3727102018-04-18 15:57:46 +000012595 const Expr *VE = RE->IgnoreParenLValueCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000012596
12597 if (VE->isValueDependent() || VE->isTypeDependent() ||
12598 VE->isInstantiationDependent() ||
12599 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012600 // We can only analyze this information once the missing information is
12601 // resolved.
Samuel Antao661c0902016-05-26 17:39:58 +000012602 MVLI.ProcessedVarList.push_back(RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012603 continue;
12604 }
12605
Alexey Bataeve3727102018-04-18 15:57:46 +000012606 Expr *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000012607
Samuel Antao5de996e2016-01-22 20:21:36 +000012608 if (!RE->IgnoreParenImpCasts()->isLValue()) {
Samuel Antao661c0902016-05-26 17:39:58 +000012609 SemaRef.Diag(ELoc,
12610 diag::err_omp_expected_named_var_member_or_array_expression)
Samuel Antao5de996e2016-01-22 20:21:36 +000012611 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000012612 continue;
12613 }
12614
Samuel Antao90927002016-04-26 14:54:23 +000012615 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
12616 ValueDecl *CurDeclaration = nullptr;
12617
12618 // Obtain the array or member expression bases if required. Also, fill the
12619 // components array with all the components identified in the process.
Alexey Bataeve3727102018-04-18 15:57:46 +000012620 const Expr *BE = checkMapClauseExpressionBase(
12621 SemaRef, SimpleExpr, CurComponents, CKind, /*NoDiagnose=*/false);
Samuel Antao5de996e2016-01-22 20:21:36 +000012622 if (!BE)
12623 continue;
12624
Samuel Antao90927002016-04-26 14:54:23 +000012625 assert(!CurComponents.empty() &&
12626 "Invalid mappable expression information.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000012627
Samuel Antao90927002016-04-26 14:54:23 +000012628 // For the following checks, we rely on the base declaration which is
12629 // expected to be associated with the last component. The declaration is
12630 // expected to be a variable or a field (if 'this' is being mapped).
12631 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
12632 assert(CurDeclaration && "Null decl on map clause.");
12633 assert(
12634 CurDeclaration->isCanonicalDecl() &&
12635 "Expecting components to have associated only canonical declarations.");
12636
12637 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
Alexey Bataeve3727102018-04-18 15:57:46 +000012638 const auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
Samuel Antao5de996e2016-01-22 20:21:36 +000012639
12640 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000012641 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000012642
12643 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
Samuel Antao661c0902016-05-26 17:39:58 +000012644 // threadprivate variables cannot appear in a map clause.
12645 // OpenMP 4.5 [2.10.5, target update Construct]
12646 // threadprivate variables cannot appear in a from clause.
12647 if (VD && DSAS->isThreadPrivate(VD)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012648 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
Samuel Antao661c0902016-05-26 17:39:58 +000012649 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
12650 << getOpenMPClauseName(CKind);
Alexey Bataeve3727102018-04-18 15:57:46 +000012651 reportOriginalDsa(SemaRef, DSAS, VD, DVar);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012652 continue;
12653 }
12654
Samuel Antao5de996e2016-01-22 20:21:36 +000012655 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
12656 // A list item cannot appear in both a map clause and a data-sharing
12657 // attribute clause on the same construct.
Kelvin Li0bff7af2015-11-23 05:32:03 +000012658
Samuel Antao5de996e2016-01-22 20:21:36 +000012659 // Check conflicts with other map clause expressions. We check the conflicts
12660 // with the current construct separately from the enclosing data
Samuel Antao661c0902016-05-26 17:39:58 +000012661 // environment, because the restrictions are different. We only have to
12662 // check conflicts across regions for the map clauses.
Alexey Bataeve3727102018-04-18 15:57:46 +000012663 if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
Samuel Antao661c0902016-05-26 17:39:58 +000012664 /*CurrentRegionOnly=*/true, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000012665 break;
Samuel Antao661c0902016-05-26 17:39:58 +000012666 if (CKind == OMPC_map &&
Alexey Bataeve3727102018-04-18 15:57:46 +000012667 checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
Samuel Antao661c0902016-05-26 17:39:58 +000012668 /*CurrentRegionOnly=*/false, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000012669 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000012670
Samuel Antao661c0902016-05-26 17:39:58 +000012671 // OpenMP 4.5 [2.10.5, target update Construct]
Samuel Antao5de996e2016-01-22 20:21:36 +000012672 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12673 // If the type of a list item is a reference to a type T then the type will
12674 // be considered to be T for all purposes of this clause.
Alexey Bataev354df2e2018-05-02 18:44:10 +000012675 auto I = llvm::find_if(
12676 CurComponents,
12677 [](const OMPClauseMappableExprCommon::MappableComponent &MC) {
12678 return MC.getAssociatedDeclaration();
12679 });
12680 assert(I != CurComponents.end() && "Null decl on map clause.");
12681 QualType Type =
12682 I->getAssociatedDeclaration()->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000012683
Samuel Antao661c0902016-05-26 17:39:58 +000012684 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
12685 // A list item in a to or from clause must have a mappable type.
Samuel Antao5de996e2016-01-22 20:21:36 +000012686 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000012687 // A list item must have a mappable type.
Alexey Bataeve3727102018-04-18 15:57:46 +000012688 if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
Samuel Antao661c0902016-05-26 17:39:58 +000012689 DSAS, Type))
Kelvin Li0bff7af2015-11-23 05:32:03 +000012690 continue;
12691
Samuel Antao661c0902016-05-26 17:39:58 +000012692 if (CKind == OMPC_map) {
12693 // target enter data
12694 // OpenMP [2.10.2, Restrictions, p. 99]
12695 // A map-type must be specified in all map clauses and must be either
12696 // to or alloc.
12697 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
12698 if (DKind == OMPD_target_enter_data &&
12699 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
12700 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
12701 << (IsMapTypeImplicit ? 1 : 0)
12702 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
12703 << getOpenMPDirectiveName(DKind);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000012704 continue;
12705 }
Samuel Antao661c0902016-05-26 17:39:58 +000012706
12707 // target exit_data
12708 // OpenMP [2.10.3, Restrictions, p. 102]
12709 // A map-type must be specified in all map clauses and must be either
12710 // from, release, or delete.
12711 if (DKind == OMPD_target_exit_data &&
12712 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
12713 MapType == OMPC_MAP_delete)) {
12714 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
12715 << (IsMapTypeImplicit ? 1 : 0)
12716 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
12717 << getOpenMPDirectiveName(DKind);
12718 continue;
12719 }
12720
12721 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
12722 // A list item cannot appear in both a map clause and a data-sharing
12723 // attribute clause on the same construct
Alexey Bataeve3727102018-04-18 15:57:46 +000012724 if (VD && isOpenMPTargetExecutionDirective(DKind)) {
12725 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
Samuel Antao661c0902016-05-26 17:39:58 +000012726 if (isOpenMPPrivate(DVar.CKind)) {
Samuel Antao6890b092016-07-28 14:25:09 +000012727 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Samuel Antao661c0902016-05-26 17:39:58 +000012728 << getOpenMPClauseName(DVar.CKind)
Samuel Antao6890b092016-07-28 14:25:09 +000012729 << getOpenMPClauseName(OMPC_map)
Samuel Antao661c0902016-05-26 17:39:58 +000012730 << getOpenMPDirectiveName(DSAS->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000012731 reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar);
Samuel Antao661c0902016-05-26 17:39:58 +000012732 continue;
12733 }
12734 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +000012735 }
12736
Samuel Antao90927002016-04-26 14:54:23 +000012737 // Save the current expression.
Samuel Antao661c0902016-05-26 17:39:58 +000012738 MVLI.ProcessedVarList.push_back(RE);
Samuel Antao90927002016-04-26 14:54:23 +000012739
12740 // Store the components in the stack so that they can be used to check
12741 // against other clauses later on.
Samuel Antao6890b092016-07-28 14:25:09 +000012742 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents,
12743 /*WhereFoundClauseKind=*/OMPC_map);
Samuel Antao90927002016-04-26 14:54:23 +000012744
12745 // Save the components and declaration to create the clause. For purposes of
12746 // the clause creation, any component list that has has base 'this' uses
Samuel Antao686c70c2016-05-26 17:30:50 +000012747 // null as base declaration.
Samuel Antao661c0902016-05-26 17:39:58 +000012748 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
12749 MVLI.VarComponents.back().append(CurComponents.begin(),
12750 CurComponents.end());
12751 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
12752 : CurDeclaration);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012753 }
Samuel Antao661c0902016-05-26 17:39:58 +000012754}
12755
12756OMPClause *
12757Sema::ActOnOpenMPMapClause(OpenMPMapClauseKind MapTypeModifier,
12758 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
12759 SourceLocation MapLoc, SourceLocation ColonLoc,
12760 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
12761 SourceLocation LParenLoc, SourceLocation EndLoc) {
12762 MappableVarListInfo MVLI(VarList);
12763 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc,
12764 MapType, IsMapTypeImplicit);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012765
Samuel Antao5de996e2016-01-22 20:21:36 +000012766 // We need to produce a map clause even if we don't have variables so that
12767 // other diagnostics related with non-existing map clauses are accurate.
Samuel Antao661c0902016-05-26 17:39:58 +000012768 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc,
12769 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
12770 MVLI.VarComponents, MapTypeModifier, MapType,
12771 IsMapTypeImplicit, MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012772}
Kelvin Li099bb8c2015-11-24 20:50:12 +000012773
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012774QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
12775 TypeResult ParsedType) {
12776 assert(ParsedType.isUsable());
12777
12778 QualType ReductionType = GetTypeFromParser(ParsedType.get());
12779 if (ReductionType.isNull())
12780 return QualType();
12781
12782 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
12783 // A type name in a declare reduction directive cannot be a function type, an
12784 // array type, a reference type, or a type qualified with const, volatile or
12785 // restrict.
12786 if (ReductionType.hasQualifiers()) {
12787 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
12788 return QualType();
12789 }
12790
12791 if (ReductionType->isFunctionType()) {
12792 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
12793 return QualType();
12794 }
12795 if (ReductionType->isReferenceType()) {
12796 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
12797 return QualType();
12798 }
12799 if (ReductionType->isArrayType()) {
12800 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
12801 return QualType();
12802 }
12803 return ReductionType;
12804}
12805
12806Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
12807 Scope *S, DeclContext *DC, DeclarationName Name,
12808 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
12809 AccessSpecifier AS, Decl *PrevDeclInScope) {
12810 SmallVector<Decl *, 8> Decls;
12811 Decls.reserve(ReductionTypes.size());
12812
12813 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
Richard Smithbecb92d2017-10-10 22:33:17 +000012814 forRedeclarationInCurContext());
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012815 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
12816 // A reduction-identifier may not be re-declared in the current scope for the
12817 // same type or for a type that is compatible according to the base language
12818 // rules.
12819 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
12820 OMPDeclareReductionDecl *PrevDRD = nullptr;
12821 bool InCompoundScope = true;
12822 if (S != nullptr) {
12823 // Find previous declaration with the same name not referenced in other
12824 // declarations.
12825 FunctionScopeInfo *ParentFn = getEnclosingFunction();
12826 InCompoundScope =
12827 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
12828 LookupName(Lookup, S);
12829 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
12830 /*AllowInlineNamespace=*/false);
12831 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
Alexey Bataeve3727102018-04-18 15:57:46 +000012832 LookupResult::Filter Filter = Lookup.makeFilter();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012833 while (Filter.hasNext()) {
12834 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
12835 if (InCompoundScope) {
12836 auto I = UsedAsPrevious.find(PrevDecl);
12837 if (I == UsedAsPrevious.end())
12838 UsedAsPrevious[PrevDecl] = false;
Alexey Bataeve3727102018-04-18 15:57:46 +000012839 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope())
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012840 UsedAsPrevious[D] = true;
12841 }
12842 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
12843 PrevDecl->getLocation();
12844 }
12845 Filter.done();
12846 if (InCompoundScope) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012847 for (const auto &PrevData : UsedAsPrevious) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012848 if (!PrevData.second) {
12849 PrevDRD = PrevData.first;
12850 break;
12851 }
12852 }
12853 }
12854 } else if (PrevDeclInScope != nullptr) {
12855 auto *PrevDRDInScope = PrevDRD =
12856 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
12857 do {
12858 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
12859 PrevDRDInScope->getLocation();
12860 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
12861 } while (PrevDRDInScope != nullptr);
12862 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012863 for (const auto &TyData : ReductionTypes) {
12864 const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012865 bool Invalid = false;
12866 if (I != PreviousRedeclTypes.end()) {
12867 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
12868 << TyData.first;
12869 Diag(I->second, diag::note_previous_definition);
12870 Invalid = true;
12871 }
12872 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
12873 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
12874 Name, TyData.first, PrevDRD);
12875 DC->addDecl(DRD);
12876 DRD->setAccess(AS);
12877 Decls.push_back(DRD);
12878 if (Invalid)
12879 DRD->setInvalidDecl();
12880 else
12881 PrevDRD = DRD;
12882 }
12883
12884 return DeclGroupPtrTy::make(
12885 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
12886}
12887
12888void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
12889 auto *DRD = cast<OMPDeclareReductionDecl>(D);
12890
12891 // Enter new function scope.
12892 PushFunctionScope();
Reid Kleckner87a31802018-03-12 21:43:02 +000012893 setFunctionHasBranchProtectedScope();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012894 getCurFunction()->setHasOMPDeclareReductionCombiner();
12895
12896 if (S != nullptr)
12897 PushDeclContext(S, DRD);
12898 else
12899 CurContext = DRD;
12900
Faisal Valid143a0c2017-04-01 21:30:49 +000012901 PushExpressionEvaluationContext(
12902 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012903
12904 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012905 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
12906 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
12907 // uses semantics of argument handles by value, but it should be passed by
12908 // reference. C lang does not support references, so pass all parameters as
12909 // pointers.
12910 // Create 'T omp_in;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000012911 VarDecl *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012912 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012913 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
12914 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
12915 // uses semantics of argument handles by value, but it should be passed by
12916 // reference. C lang does not support references, so pass all parameters as
12917 // pointers.
12918 // Create 'T omp_out;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000012919 VarDecl *OmpOutParm =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012920 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
12921 if (S != nullptr) {
12922 PushOnScopeChains(OmpInParm, S);
12923 PushOnScopeChains(OmpOutParm, S);
12924 } else {
12925 DRD->addDecl(OmpInParm);
12926 DRD->addDecl(OmpOutParm);
12927 }
Alexey Bataeve6aa4692018-09-13 16:54:05 +000012928 Expr *InE =
12929 ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation());
12930 Expr *OutE =
12931 ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation());
12932 DRD->setCombinerData(InE, OutE);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012933}
12934
12935void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
12936 auto *DRD = cast<OMPDeclareReductionDecl>(D);
12937 DiscardCleanupsInEvaluationContext();
12938 PopExpressionEvaluationContext();
12939
12940 PopDeclContext();
12941 PopFunctionScopeInfo();
12942
12943 if (Combiner != nullptr)
12944 DRD->setCombiner(Combiner);
12945 else
12946 DRD->setInvalidDecl();
12947}
12948
Alexey Bataev070f43a2017-09-06 14:49:58 +000012949VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012950 auto *DRD = cast<OMPDeclareReductionDecl>(D);
12951
12952 // Enter new function scope.
12953 PushFunctionScope();
Reid Kleckner87a31802018-03-12 21:43:02 +000012954 setFunctionHasBranchProtectedScope();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012955
12956 if (S != nullptr)
12957 PushDeclContext(S, DRD);
12958 else
12959 CurContext = DRD;
12960
Faisal Valid143a0c2017-04-01 21:30:49 +000012961 PushExpressionEvaluationContext(
12962 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012963
12964 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012965 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
12966 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
12967 // uses semantics of argument handles by value, but it should be passed by
12968 // reference. C lang does not support references, so pass all parameters as
12969 // pointers.
12970 // Create 'T omp_priv;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000012971 VarDecl *OmpPrivParm =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012972 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012973 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
12974 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
12975 // uses semantics of argument handles by value, but it should be passed by
12976 // reference. C lang does not support references, so pass all parameters as
12977 // pointers.
12978 // Create 'T omp_orig;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000012979 VarDecl *OmpOrigParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000012980 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012981 if (S != nullptr) {
12982 PushOnScopeChains(OmpPrivParm, S);
12983 PushOnScopeChains(OmpOrigParm, S);
12984 } else {
12985 DRD->addDecl(OmpPrivParm);
12986 DRD->addDecl(OmpOrigParm);
12987 }
Alexey Bataeve6aa4692018-09-13 16:54:05 +000012988 Expr *OrigE =
12989 ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation());
12990 Expr *PrivE =
12991 ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation());
12992 DRD->setInitializerData(OrigE, PrivE);
Alexey Bataev070f43a2017-09-06 14:49:58 +000012993 return OmpPrivParm;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012994}
12995
Alexey Bataev070f43a2017-09-06 14:49:58 +000012996void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer,
12997 VarDecl *OmpPrivParm) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000012998 auto *DRD = cast<OMPDeclareReductionDecl>(D);
12999 DiscardCleanupsInEvaluationContext();
13000 PopExpressionEvaluationContext();
13001
13002 PopDeclContext();
13003 PopFunctionScopeInfo();
13004
Alexey Bataev070f43a2017-09-06 14:49:58 +000013005 if (Initializer != nullptr) {
13006 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit);
13007 } else if (OmpPrivParm->hasInit()) {
13008 DRD->setInitializer(OmpPrivParm->getInit(),
13009 OmpPrivParm->isDirectInit()
13010 ? OMPDeclareReductionDecl::DirectInit
13011 : OMPDeclareReductionDecl::CopyInit);
13012 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013013 DRD->setInvalidDecl();
Alexey Bataev070f43a2017-09-06 14:49:58 +000013014 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013015}
13016
13017Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
13018 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013019 for (Decl *D : DeclReductions.get()) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013020 if (IsValid) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013021 if (S)
13022 PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S,
13023 /*AddToContext=*/false);
13024 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013025 D->setInvalidDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000013026 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013027 }
13028 return DeclReductions;
13029}
13030
David Majnemer9d168222016-08-05 17:44:54 +000013031OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
Kelvin Li099bb8c2015-11-24 20:50:12 +000013032 SourceLocation StartLoc,
13033 SourceLocation LParenLoc,
13034 SourceLocation EndLoc) {
13035 Expr *ValExpr = NumTeams;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000013036 Stmt *HelperValStmt = nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000013037
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013038 // OpenMP [teams Constrcut, Restrictions]
13039 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000013040 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
Alexey Bataeva0569352015-12-01 10:17:31 +000013041 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013042 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000013043
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000013044 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000013045 OpenMPDirectiveKind CaptureRegion =
13046 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams);
13047 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000013048 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000013049 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000013050 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
13051 HelperValStmt = buildPreInits(Context, Captures);
13052 }
13053
13054 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion,
13055 StartLoc, LParenLoc, EndLoc);
Kelvin Li099bb8c2015-11-24 20:50:12 +000013056}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013057
13058OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
13059 SourceLocation StartLoc,
13060 SourceLocation LParenLoc,
13061 SourceLocation EndLoc) {
13062 Expr *ValExpr = ThreadLimit;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000013063 Stmt *HelperValStmt = nullptr;
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013064
13065 // OpenMP [teams Constrcut, Restrictions]
13066 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000013067 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
Alexey Bataeva0569352015-12-01 10:17:31 +000013068 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013069 return nullptr;
13070
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000013071 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000013072 OpenMPDirectiveKind CaptureRegion =
13073 getOpenMPCaptureRegionForClause(DKind, OMPC_thread_limit);
13074 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000013075 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000013076 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000013077 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
13078 HelperValStmt = buildPreInits(Context, Captures);
13079 }
13080
13081 return new (Context) OMPThreadLimitClause(
13082 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013083}
Alexey Bataeva0569352015-12-01 10:17:31 +000013084
13085OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
13086 SourceLocation StartLoc,
13087 SourceLocation LParenLoc,
13088 SourceLocation EndLoc) {
13089 Expr *ValExpr = Priority;
13090
13091 // OpenMP [2.9.1, task Constrcut]
13092 // The priority-value is a non-negative numerical scalar expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000013093 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
Alexey Bataeva0569352015-12-01 10:17:31 +000013094 /*StrictlyPositive=*/false))
13095 return nullptr;
13096
13097 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
13098}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000013099
13100OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
13101 SourceLocation StartLoc,
13102 SourceLocation LParenLoc,
13103 SourceLocation EndLoc) {
13104 Expr *ValExpr = Grainsize;
13105
13106 // OpenMP [2.9.2, taskloop Constrcut]
13107 // The parameter of the grainsize clause must be a positive integer
13108 // expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000013109 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000013110 /*StrictlyPositive=*/true))
13111 return nullptr;
13112
13113 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
13114}
Alexey Bataev382967a2015-12-08 12:06:20 +000013115
13116OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
13117 SourceLocation StartLoc,
13118 SourceLocation LParenLoc,
13119 SourceLocation EndLoc) {
13120 Expr *ValExpr = NumTasks;
13121
13122 // OpenMP [2.9.2, taskloop Constrcut]
13123 // The parameter of the num_tasks clause must be a positive integer
13124 // expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000013125 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
Alexey Bataev382967a2015-12-08 12:06:20 +000013126 /*StrictlyPositive=*/true))
13127 return nullptr;
13128
13129 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
13130}
13131
Alexey Bataev28c75412015-12-15 08:19:24 +000013132OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
13133 SourceLocation LParenLoc,
13134 SourceLocation EndLoc) {
13135 // OpenMP [2.13.2, critical construct, Description]
13136 // ... where hint-expression is an integer constant expression that evaluates
13137 // to a valid lock hint.
13138 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
13139 if (HintExpr.isInvalid())
13140 return nullptr;
13141 return new (Context)
13142 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
13143}
13144
Carlo Bertollib4adf552016-01-15 18:50:31 +000013145OMPClause *Sema::ActOnOpenMPDistScheduleClause(
13146 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
13147 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
13148 SourceLocation EndLoc) {
13149 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
13150 std::string Values;
13151 Values += "'";
13152 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
13153 Values += "'";
13154 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
13155 << Values << getOpenMPClauseName(OMPC_dist_schedule);
13156 return nullptr;
13157 }
13158 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000013159 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000013160 if (ChunkSize) {
13161 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
13162 !ChunkSize->isInstantiationDependent() &&
13163 !ChunkSize->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000013164 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
Carlo Bertollib4adf552016-01-15 18:50:31 +000013165 ExprResult Val =
13166 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
13167 if (Val.isInvalid())
13168 return nullptr;
13169
13170 ValExpr = Val.get();
13171
13172 // OpenMP [2.7.1, Restrictions]
13173 // chunk_size must be a loop invariant integer expression with a positive
13174 // value.
13175 llvm::APSInt Result;
13176 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
13177 if (Result.isSigned() && !Result.isStrictlyPositive()) {
13178 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
13179 << "dist_schedule" << ChunkSize->getSourceRange();
13180 return nullptr;
13181 }
Alexey Bataev2ba67042017-11-28 21:11:44 +000013182 } else if (getOpenMPCaptureRegionForClause(
13183 DSAStack->getCurrentDirective(), OMPC_dist_schedule) !=
13184 OMPD_unknown &&
Alexey Bataevb46cdea2016-06-15 11:20:48 +000013185 !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000013186 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000013187 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev5a3af132016-03-29 08:58:54 +000013188 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
13189 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000013190 }
13191 }
13192 }
13193
13194 return new (Context)
13195 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000013196 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000013197}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013198
13199OMPClause *Sema::ActOnOpenMPDefaultmapClause(
13200 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
13201 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
13202 SourceLocation KindLoc, SourceLocation EndLoc) {
13203 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
David Majnemer9d168222016-08-05 17:44:54 +000013204 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) {
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013205 std::string Value;
13206 SourceLocation Loc;
13207 Value += "'";
13208 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
13209 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000013210 OMPC_DEFAULTMAP_MODIFIER_tofrom);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013211 Loc = MLoc;
13212 } else {
13213 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000013214 OMPC_DEFAULTMAP_scalar);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013215 Loc = KindLoc;
13216 }
13217 Value += "'";
13218 Diag(Loc, diag::err_omp_unexpected_clause_value)
13219 << Value << getOpenMPClauseName(OMPC_defaultmap);
13220 return nullptr;
13221 }
Alexey Bataev2fd0cb22017-10-05 17:51:39 +000013222 DSAStack->setDefaultDMAToFromScalar(StartLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013223
13224 return new (Context)
13225 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
13226}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013227
13228bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
13229 DeclContext *CurLexicalContext = getCurLexicalContext();
13230 if (!CurLexicalContext->isFileContext() &&
13231 !CurLexicalContext->isExternCContext() &&
Alexey Bataev502ec492017-10-03 20:00:00 +000013232 !CurLexicalContext->isExternCXXContext() &&
13233 !isa<CXXRecordDecl>(CurLexicalContext) &&
13234 !isa<ClassTemplateDecl>(CurLexicalContext) &&
13235 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) &&
13236 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013237 Diag(Loc, diag::err_omp_region_not_file_context);
13238 return false;
13239 }
Kelvin Libc38e632018-09-10 02:07:09 +000013240 ++DeclareTargetNestingLevel;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013241 return true;
13242}
13243
13244void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
Kelvin Libc38e632018-09-10 02:07:09 +000013245 assert(DeclareTargetNestingLevel > 0 &&
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013246 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
Kelvin Libc38e632018-09-10 02:07:09 +000013247 --DeclareTargetNestingLevel;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013248}
13249
David Majnemer9d168222016-08-05 17:44:54 +000013250void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope,
13251 CXXScopeSpec &ScopeSpec,
13252 const DeclarationNameInfo &Id,
13253 OMPDeclareTargetDeclAttr::MapTypeTy MT,
13254 NamedDeclSetType &SameDirectiveDecls) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013255 LookupResult Lookup(*this, Id, LookupOrdinaryName);
13256 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
13257
13258 if (Lookup.isAmbiguous())
13259 return;
13260 Lookup.suppressDiagnostics();
13261
13262 if (!Lookup.isSingleResult()) {
13263 if (TypoCorrection Corrected =
13264 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
13265 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
13266 CTK_ErrorRecovery)) {
13267 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
13268 << Id.getName());
13269 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
13270 return;
13271 }
13272
13273 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
13274 return;
13275 }
13276
13277 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
Alexey Bataev30a78212018-09-11 13:59:10 +000013278 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND) ||
13279 isa<FunctionTemplateDecl>(ND)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013280 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
13281 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
Alexey Bataev30a78212018-09-11 13:59:10 +000013282 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
13283 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
13284 cast<ValueDecl>(ND));
13285 if (!Res) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013286 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013287 ND->addAttr(A);
13288 if (ASTMutationListener *ML = Context.getASTMutationListener())
13289 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
Kelvin Li1ce87c72017-12-12 20:08:12 +000013290 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Id.getLoc());
Alexey Bataev30a78212018-09-11 13:59:10 +000013291 } else if (*Res != MT) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013292 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
13293 << Id.getName();
13294 }
Alexey Bataeve3727102018-04-18 15:57:46 +000013295 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013296 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
Alexey Bataeve3727102018-04-18 15:57:46 +000013297 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013298}
13299
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013300static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
13301 Sema &SemaRef, Decl *D) {
Alexey Bataev30a78212018-09-11 13:59:10 +000013302 if (!D || !isa<VarDecl>(D))
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013303 return;
Alexey Bataev30a78212018-09-11 13:59:10 +000013304 auto *VD = cast<VarDecl>(D);
13305 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
13306 return;
13307 SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context);
13308 SemaRef.Diag(SL, diag::note_used_here) << SR;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013309}
13310
13311static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
13312 Sema &SemaRef, DSAStackTy *Stack,
13313 ValueDecl *VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013314 return VD->hasAttr<OMPDeclareTargetDeclAttr>() ||
13315 checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(),
13316 /*FullCheck=*/false);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013317}
13318
Kelvin Li1ce87c72017-12-12 20:08:12 +000013319void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D,
13320 SourceLocation IdLoc) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013321 if (!D || D->isInvalidDecl())
13322 return;
13323 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
Stephen Kellyf2ceec42018-08-09 21:08:08 +000013324 SourceLocation SL = E ? E->getBeginLoc() : D->getLocation();
Alexey Bataeve3727102018-04-18 15:57:46 +000013325 if (auto *VD = dyn_cast<VarDecl>(D)) {
Alexey Bataevc1943e72018-07-09 19:58:08 +000013326 // Only global variables can be marked as declare target.
Alexey Bataev30a78212018-09-11 13:59:10 +000013327 if (!VD->isFileVarDecl() && !VD->isStaticLocal() &&
13328 !VD->isStaticDataMember())
Alexey Bataevc1943e72018-07-09 19:58:08 +000013329 return;
13330 // 2.10.6: threadprivate variable cannot appear in a declare target
13331 // directive.
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013332 if (DSAStack->isThreadPrivate(VD)) {
13333 Diag(SL, diag::err_omp_threadprivate_in_target);
Alexey Bataeve3727102018-04-18 15:57:46 +000013334 reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013335 return;
13336 }
13337 }
Alexey Bataev97b72212018-08-14 18:31:20 +000013338 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
13339 D = FTD->getTemplatedDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000013340 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Alexey Bataev30a78212018-09-11 13:59:10 +000013341 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
13342 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD);
13343 if (Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) {
Kelvin Li1ce87c72017-12-12 20:08:12 +000013344 assert(IdLoc.isValid() && "Source location is expected");
13345 Diag(IdLoc, diag::err_omp_function_in_link_clause);
13346 Diag(FD->getLocation(), diag::note_defined_here) << FD;
13347 return;
13348 }
13349 }
Alexey Bataev30a78212018-09-11 13:59:10 +000013350 if (auto *VD = dyn_cast<ValueDecl>(D)) {
13351 // Problem if any with var declared with incomplete type will be reported
13352 // as normal, so no need to check it here.
13353 if ((E || !VD->getType()->isIncompleteType()) &&
13354 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD))
13355 return;
13356 if (!E && !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) {
13357 // Checking declaration inside declare target region.
13358 if (isa<VarDecl>(D) || isa<FunctionDecl>(D) ||
13359 isa<FunctionTemplateDecl>(D)) {
13360 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
13361 Context, OMPDeclareTargetDeclAttr::MT_To);
13362 D->addAttr(A);
13363 if (ASTMutationListener *ML = Context.getASTMutationListener())
13364 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
13365 }
13366 return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013367 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013368 }
Alexey Bataev30a78212018-09-11 13:59:10 +000013369 if (!E)
13370 return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013371 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
13372}
Samuel Antao661c0902016-05-26 17:39:58 +000013373
13374OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
13375 SourceLocation StartLoc,
13376 SourceLocation LParenLoc,
13377 SourceLocation EndLoc) {
13378 MappableVarListInfo MVLI(VarList);
13379 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc);
13380 if (MVLI.ProcessedVarList.empty())
13381 return nullptr;
13382
13383 return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13384 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
13385 MVLI.VarComponents);
13386}
Samuel Antaoec172c62016-05-26 17:49:04 +000013387
13388OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
13389 SourceLocation StartLoc,
13390 SourceLocation LParenLoc,
13391 SourceLocation EndLoc) {
13392 MappableVarListInfo MVLI(VarList);
13393 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc);
13394 if (MVLI.ProcessedVarList.empty())
13395 return nullptr;
13396
13397 return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13398 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
13399 MVLI.VarComponents);
13400}
Carlo Bertolli2404b172016-07-13 15:37:16 +000013401
13402OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
13403 SourceLocation StartLoc,
13404 SourceLocation LParenLoc,
13405 SourceLocation EndLoc) {
Samuel Antaocc10b852016-07-28 14:23:26 +000013406 MappableVarListInfo MVLI(VarList);
13407 SmallVector<Expr *, 8> PrivateCopies;
13408 SmallVector<Expr *, 8> Inits;
13409
Alexey Bataeve3727102018-04-18 15:57:46 +000013410 for (Expr *RefExpr : VarList) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000013411 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
13412 SourceLocation ELoc;
13413 SourceRange ERange;
13414 Expr *SimpleRefExpr = RefExpr;
13415 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
13416 if (Res.second) {
13417 // It will be analyzed later.
Samuel Antaocc10b852016-07-28 14:23:26 +000013418 MVLI.ProcessedVarList.push_back(RefExpr);
13419 PrivateCopies.push_back(nullptr);
13420 Inits.push_back(nullptr);
Carlo Bertolli2404b172016-07-13 15:37:16 +000013421 }
13422 ValueDecl *D = Res.first;
13423 if (!D)
13424 continue;
13425
13426 QualType Type = D->getType();
Samuel Antaocc10b852016-07-28 14:23:26 +000013427 Type = Type.getNonReferenceType().getUnqualifiedType();
13428
13429 auto *VD = dyn_cast<VarDecl>(D);
13430
13431 // Item should be a pointer or reference to pointer.
13432 if (!Type->isPointerType()) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000013433 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
13434 << 0 << RefExpr->getSourceRange();
13435 continue;
13436 }
Samuel Antaocc10b852016-07-28 14:23:26 +000013437
13438 // Build the private variable and the expression that refers to it.
Alexey Bataev63cc8e92018-03-20 14:45:59 +000013439 auto VDPrivate =
13440 buildVarDecl(*this, ELoc, Type, D->getName(),
13441 D->hasAttrs() ? &D->getAttrs() : nullptr,
13442 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Samuel Antaocc10b852016-07-28 14:23:26 +000013443 if (VDPrivate->isInvalidDecl())
13444 continue;
13445
13446 CurContext->addDecl(VDPrivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000013447 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Samuel Antaocc10b852016-07-28 14:23:26 +000013448 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
13449
13450 // Add temporary variable to initialize the private copy of the pointer.
Alexey Bataeve3727102018-04-18 15:57:46 +000013451 VarDecl *VDInit =
Samuel Antaocc10b852016-07-28 14:23:26 +000013452 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp");
Alexey Bataeve3727102018-04-18 15:57:46 +000013453 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr(
13454 *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc());
Samuel Antaocc10b852016-07-28 14:23:26 +000013455 AddInitializerToDecl(VDPrivate,
13456 DefaultLvalueConversion(VDInitRefExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000013457 /*DirectInit=*/false);
Samuel Antaocc10b852016-07-28 14:23:26 +000013458
13459 // If required, build a capture to implement the privatization initialized
13460 // with the current list item value.
13461 DeclRefExpr *Ref = nullptr;
13462 if (!VD)
13463 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
13464 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
13465 PrivateCopies.push_back(VDPrivateRefExpr);
13466 Inits.push_back(VDInitRefExpr);
13467
13468 // We need to add a data sharing attribute for this variable to make sure it
13469 // is correctly captured. A variable that shows up in a use_device_ptr has
13470 // similar properties of a first private variable.
13471 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
13472
13473 // Create a mappable component for the list item. List items in this clause
13474 // only need a component.
13475 MVLI.VarBaseDeclarations.push_back(D);
13476 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13477 MVLI.VarComponents.back().push_back(
13478 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D));
Carlo Bertolli2404b172016-07-13 15:37:16 +000013479 }
13480
Samuel Antaocc10b852016-07-28 14:23:26 +000013481 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli2404b172016-07-13 15:37:16 +000013482 return nullptr;
13483
Samuel Antaocc10b852016-07-28 14:23:26 +000013484 return OMPUseDevicePtrClause::Create(
13485 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
13486 PrivateCopies, Inits, MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli2404b172016-07-13 15:37:16 +000013487}
Carlo Bertolli70594e92016-07-13 17:16:49 +000013488
13489OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
13490 SourceLocation StartLoc,
13491 SourceLocation LParenLoc,
13492 SourceLocation EndLoc) {
Samuel Antao6890b092016-07-28 14:25:09 +000013493 MappableVarListInfo MVLI(VarList);
Alexey Bataeve3727102018-04-18 15:57:46 +000013494 for (Expr *RefExpr : VarList) {
Kelvin Li84376252016-12-14 15:39:58 +000013495 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause.");
Carlo Bertolli70594e92016-07-13 17:16:49 +000013496 SourceLocation ELoc;
13497 SourceRange ERange;
13498 Expr *SimpleRefExpr = RefExpr;
13499 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
13500 if (Res.second) {
13501 // It will be analyzed later.
Samuel Antao6890b092016-07-28 14:25:09 +000013502 MVLI.ProcessedVarList.push_back(RefExpr);
Carlo Bertolli70594e92016-07-13 17:16:49 +000013503 }
13504 ValueDecl *D = Res.first;
13505 if (!D)
13506 continue;
13507
13508 QualType Type = D->getType();
13509 // item should be a pointer or array or reference to pointer or array
13510 if (!Type.getNonReferenceType()->isPointerType() &&
13511 !Type.getNonReferenceType()->isArrayType()) {
13512 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
13513 << 0 << RefExpr->getSourceRange();
13514 continue;
13515 }
Samuel Antao6890b092016-07-28 14:25:09 +000013516
13517 // Check if the declaration in the clause does not show up in any data
13518 // sharing attribute.
Alexey Bataeve3727102018-04-18 15:57:46 +000013519 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Samuel Antao6890b092016-07-28 14:25:09 +000013520 if (isOpenMPPrivate(DVar.CKind)) {
13521 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
13522 << getOpenMPClauseName(DVar.CKind)
13523 << getOpenMPClauseName(OMPC_is_device_ptr)
13524 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000013525 reportOriginalDsa(*this, DSAStack, D, DVar);
Samuel Antao6890b092016-07-28 14:25:09 +000013526 continue;
13527 }
13528
Alexey Bataeve3727102018-04-18 15:57:46 +000013529 const Expr *ConflictExpr;
Samuel Antao6890b092016-07-28 14:25:09 +000013530 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +000013531 D, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +000013532 [&ConflictExpr](
13533 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
13534 OpenMPClauseKind) -> bool {
13535 ConflictExpr = R.front().getAssociatedExpression();
13536 return true;
13537 })) {
13538 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
13539 Diag(ConflictExpr->getExprLoc(), diag::note_used_here)
13540 << ConflictExpr->getSourceRange();
13541 continue;
13542 }
13543
13544 // Store the components in the stack so that they can be used to check
13545 // against other clauses later on.
13546 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D);
13547 DSAStack->addMappableExpressionComponents(
13548 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
13549
13550 // Record the expression we've just processed.
13551 MVLI.ProcessedVarList.push_back(SimpleRefExpr);
13552
13553 // Create a mappable component for the list item. List items in this clause
13554 // only need a component. We use a null declaration to signal fields in
13555 // 'this'.
13556 assert((isa<DeclRefExpr>(SimpleRefExpr) ||
13557 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
13558 "Unexpected device pointer expression!");
13559 MVLI.VarBaseDeclarations.push_back(
13560 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr);
13561 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13562 MVLI.VarComponents.back().push_back(MC);
Carlo Bertolli70594e92016-07-13 17:16:49 +000013563 }
13564
Samuel Antao6890b092016-07-28 14:25:09 +000013565 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli70594e92016-07-13 17:16:49 +000013566 return nullptr;
13567
Samuel Antao6890b092016-07-28 14:25:09 +000013568 return OMPIsDevicePtrClause::Create(
13569 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
13570 MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli70594e92016-07-13 17:16:49 +000013571}